using System.Linq.Expressions; using WmsLite.Api.Common; using WmsLite.Api.Dtos.Common; using WmsLite.Api.Dtos.Inventory; using WmsLite.Api.Enums; using WmsLite.Api.Infrastructure.Auth; using WmsLite.Api.Models; using WmsLite.Api.Repositories.Interfaces; using WmsLite.Api.Services; namespace WmsLite.Api.Tests; public class InventoryAllocationServiceTests { [Fact] public async Task AllocateAsync_AllocatesAcrossFifoCandidates() { var repo = new FakeInventoryRepository([ NewInventory(1, "L-1", qty: 5, createTime: new DateTime(2026, 1, 1)), NewInventory(2, "L-2", qty: 8, createTime: new DateTime(2026, 1, 2)), ]); var service = new InventoryAllocationService(repo); var result = await service.AllocateAsync(NewRequest(10)); Assert.Equal(2, result.Count); Assert.Equal(1, result[0].Inventory.Id); Assert.Equal(5, result[0].Qty); Assert.Equal(2, result[1].Inventory.Id); Assert.Equal(5, result[1].Qty); } [Fact] public async Task AllocateAsync_RespectsLocationConstraint() { var repo = new FakeInventoryRepository([ NewInventory(1, "L-1", qty: 5), NewInventory(2, "L-2", qty: 8), ]); var service = new InventoryAllocationService(repo); var result = await service.AllocateAsync(NewRequest(6, locationCode: "L-2")); var item = Assert.Single(result); Assert.Equal(2, item.Inventory.Id); Assert.Equal(6, item.Qty); } [Fact] public async Task AllocateAsync_ThrowsWhenAvailableQtyIsNotEnough() { var repo = new FakeInventoryRepository([ NewInventory(1, "L-1", qty: 5, pendingOutboundQty: 3), ]); var service = new InventoryAllocationService(repo); await Assert.ThrowsAsync(() => service.AllocateAsync(NewRequest(3))); } private static InventoryAllocationRequest NewRequest(decimal qty, string? locationCode = null) { return new InventoryAllocationRequest { TenantId = "default", OwnerCode = "default", InventoryKind = InventoryKind.Material, MaterialCode = "MAT-1", WarehouseCode = "WH-1", AreaCode = "A-1", LocationCode = locationCode, Qty = qty }; } private static Inventory NewInventory( long id, string locationCode, decimal qty, decimal pendingOutboundQty = 0, DateTime? createTime = null) { return new Inventory { Id = id, TenantId = "default", OwnerCode = "default", InventoryKind = InventoryKind.Material, MaterialCode = "MAT-1", WarehouseCode = "WH-1", AreaCode = "A-1", ZoneCode = "Z-1", LocationCode = locationCode, Qty = qty, PendingOutboundQty = pendingOutboundQty, Unit = "pcs", CreateTime = createTime }; } private sealed class FakeInventoryRepository : IInventoryRepository { private readonly List _items; public FakeInventoryRepository(IEnumerable items) { _items = items.ToList(); } public Task> GetAllocatableAsync( string tenantId, string ownerCode, string warehouseCode, string areaCode, InventoryKind inventoryKind, string? materialCode, string? containerCode, string? locationCode = null) { var items = _items .Where(x => !x.IsDeleted && x.TenantId == tenantId && x.OwnerCode == ownerCode && x.WarehouseCode == warehouseCode && x.AreaCode == areaCode && x.InventoryKind == inventoryKind && x.MaterialCode == materialCode && x.Qty - x.PendingOutboundQty - x.FrozenQty > 0) .Where(x => string.IsNullOrWhiteSpace(locationCode) || x.LocationCode == locationCode) .OrderBy(x => x.CreateTime) .ThenBy(x => x.Id) .ToList(); return Task.FromResult(items); } public Task GetByStockKeyAsync(string tenantId, string ownerCode, string warehouseCode, string areaCode, string zoneCode, string locationCode, InventoryKind inventoryKind, string? materialCode, string? containerCode) => Task.FromResult(null); public Task GetByIdAsync(long id) => Task.FromResult(null); public Task GetByIdAsync(long id, string tenantId, string ownerCode) => Task.FromResult(null); public Task> GetListAsync(Expression> predicate) => Task.FromResult(new List()); public Task> GetPagedListAsync(BasePagedQuery query) => Task.FromResult(new PagedResult()); public Task> GetPagedListAsync(InventoryPagedQuery query) => Task.FromResult(new PagedResult()); public Task InsertAsync(Inventory entity) => Task.FromResult(entity.Id); public Task UpdateAsync(Inventory entity) => Task.FromResult(1); public Task SoftDeleteAsync(long id) => Task.FromResult(0); public Task SoftDeleteAsync(long id, string tenantId, string ownerCode) => Task.FromResult(0); } }