using Data.SeaHavenIndustries; using FluentAssertions; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Implementation; using Xunit; namespace Api.SeaHavenIndustries.Tests; public class WorkOrderDataServiceBatchingTests { private static ApplicationDbContext NewContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new ApplicationDbContext(options); } private static async Task SeedAsync(ApplicationDbContext context) { context.Locations.Add(new Locations { Id = 100, Name = "Site A", Address1 = "1 Main St" }); context.workOrders.AddRange( new WorkOrder { Id = 1, InternalWONumber = "WO-1", WorkerOrderTitle = "First", LocationId = 100 }, new WorkOrder { Id = 2, InternalWONumber = "WO-2", WorkerOrderTitle = "Second", LocationId = 100 }, new WorkOrder { Id = 3, InternalWONumber = "WO-3", WorkerOrderTitle = "Third", LocationId = 100 }, new WorkOrder { Id = 4, InternalWONumber = "WO-T", WorkerOrderTitle = "Template", LocationId = 100, istemplate = true }); await context.SaveChangesAsync(); } [Fact] public async Task GetByRequestedIdsWithDetailsAsync_ReturnsResultsInInputOrder() { await using var context = NewContext(); await SeedAsync(context); var service = new WorkOrderDataService(context); var result = await service.GetByRequestedIdsWithDetailsAsync( new[] { 3, 1, 2 }, CancellationToken.None); result.Select(w => w.Id).Should().Equal(new[] { 3, 1, 2 }); } [Fact] public async Task GetByRequestedIdsWithDetailsAsync_DeduplicatesIdsAndSkipsMissing() { await using var context = NewContext(); await SeedAsync(context); var service = new WorkOrderDataService(context); var result = await service.GetByRequestedIdsWithDetailsAsync( new[] { 2, 2, 999, 1 }, CancellationToken.None); result.Select(w => w.Id).Should().Equal(new[] { 2, 1 }); } [Fact] public async Task GetByRequestedIdsWithDetailsAsync_ExcludesTemplatesAndEagerLoadsLocation() { await using var context = NewContext(); await SeedAsync(context); var service = new WorkOrderDataService(context); var result = await service.GetByRequestedIdsWithDetailsAsync( new[] { 1, 4 }, CancellationToken.None); result.Select(w => w.Id).Should().Equal(new[] { 1 }); result.Single().Locations.Should().NotBeNull(); result.Single().Locations!.Name.Should().Be("Site A"); } [Fact] public async Task AddAuditLogsAsync_PersistsAllRowsInSingleSave() { await using var context = NewContext(); await SeedAsync(context); var service = new WorkOrderDataService(context); await service.AddAuditLogsAsync(new[] { new WorkOrderAuditLog { WorkOrderId = 1, FieldName = "Dispatch", Action = "Dispatch" }, new WorkOrderAuditLog { WorkOrderId = 2, FieldName = "Dispatch", Action = "Dispatch" } }); context.WorkOrderAuditLogs.Should().HaveCount(2); } [Fact] public async Task GetByIdsOrderedAsync_PreservesInputOrderDeduplicatesAndSkipsMissing() { await using var context = NewContext(); context.Vendors.AddRange( new Vendor { Id = 7, CompanyName = "V7", Email = "v7@x.io" }, new Vendor { Id = 8, CompanyName = "V8", Email = "v8@x.io" }); await context.SaveChangesAsync(); var service = new VendorDataService(context); var result = await service.GetByIdsOrderedAsync(new[] { 8, 8, 7, 999 }, CancellationToken.None); result.Select(v => v.Id).Should().Equal(new[] { 8, 7 }); } }