shoc-backend/Api.SeaHavenIndustries.Tests/VendorCompanyRosterDataServiceTests.cs
2026-07-31 21:41:59 -03:00

268 lines
10 KiB
C#

using Data.SeaHavenIndustries;
using Data.SeaHavenIndustries.Enums;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Implementation;
using SeaHaven.DataServices.Models;
using Xunit;
namespace Api.SeaHavenIndustries.Tests;
public class VendorCompanyRosterDataServiceTests
{
private static ApplicationDbContext NewContext(string dbName)
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(dbName)
.Options;
return new ApplicationDbContext(options);
}
private static async Task<(int companyId, Vendor vendor)> SeedCompanyWithTechnicianAsync(ApplicationDbContext context, string companyName, params string[] technicianNames)
{
var company = new VendorCompany { Name = companyName, NormalizedName = companyName.ToLowerInvariant() };
context.VendorCompanies.Add(company);
foreach (var name in technicianNames)
{
context.Vendors.Add(new Vendor
{
CompanyName = companyName,
ContactName = name,
IsActive = true,
IsDeleted = false,
Company = company
});
}
await context.SaveChangesAsync();
return (company.Id, context.Vendors.First(v => v.CompanyId == company.Id));
}
[Fact]
public async Task CreateRoster_PersistsCompanyAndTechniciansAtomically()
{
var dbName = Guid.NewGuid().ToString();
using var context = NewContext(dbName);
var service = new VendorCompanyRosterDataService(context);
var roster = await service.CreateRosterAsync(new VendorCompanyRosterWriteModel
{
Name = "Atomic Co",
Email = "atomic@example.com",
Notes = "shared notes",
ActorUserId = "42",
Technicians = new List<RosterTechnicianWriteModel>
{
new() { ContactName = "T1" },
new() { ContactName = "T2" }
}
}, CancellationToken.None);
roster.CompanyId.Should().BeGreaterThan(0);
roster.Technicians.Should().HaveCount(2);
roster.Notes.Should().Be("shared notes");
var company = context.VendorCompanies.Single(c => c.Id == roster.CompanyId);
company.Name.Should().Be("Atomic Co");
context.Vendors.Count(v => v.CompanyId == company.Id).Should().Be(2);
context.Vendors.Where(v => v.CompanyId == company.Id).Should().OnlyContain(v => v.CompanyName == "Atomic Co");
}
[Fact]
public async Task GetRoster_ByVendorId_ResolvesCompanyAndAllTechnicians()
{
var dbName = Guid.NewGuid().ToString();
int companyId, vendorId;
using (var seed = NewContext(dbName))
{
var (cid, _) = await SeedCompanyWithTechnicianAsync(seed, "Resolve Co", "A", "B");
companyId = cid;
vendorId = seed.Vendors.First(v => v.CompanyId == cid && v.ContactName == "A").Id;
}
using var context = NewContext(dbName);
var service = new VendorCompanyRosterDataService(context);
var roster = await service.GetRosterAsync(vendorId, null, CancellationToken.None);
roster.Should().NotBeNull();
roster!.CompanyId.Should().Be(companyId);
roster.Technicians.Select(t => t.ContactName).Should().BeEquivalentTo(new[] { "A", "B" });
}
[Fact]
public async Task GetRoster_WithMismatchedVendorAndCompanyIds_ReturnsNull()
{
var dbName = Guid.NewGuid().ToString();
int firstCompanyId, secondVendorId;
using (var seed = NewContext(dbName))
{
var (firstId, _) = await SeedCompanyWithTechnicianAsync(seed, "First Co", "A");
var (_, secondVendor) = await SeedCompanyWithTechnicianAsync(seed, "Second Co", "B");
firstCompanyId = firstId;
secondVendorId = secondVendor.Id;
}
using var context = NewContext(dbName);
var service = new VendorCompanyRosterDataService(context);
var roster = await service.GetRosterAsync(secondVendorId, firstCompanyId, CancellationToken.None);
roster.Should().BeNull();
}
[Fact]
public async Task SaveRoster_RenamesCompanyAndTechnicianAndUpdatesNotes()
{
var dbName = Guid.NewGuid().ToString();
int companyId, techId;
using (var seed = NewContext(dbName))
{
var (cid, vendor) = await SeedCompanyWithTechnicianAsync(seed, "Old Co", "Old Name");
companyId = cid;
techId = vendor.Id;
}
using var context = NewContext(dbName);
var service = new VendorCompanyRosterDataService(context);
var roster = await service.SaveRosterAsync(new VendorCompanyRosterWriteModel
{
CompanyId = companyId,
Name = "New Co",
Email = "new@example.com",
Notes = "updated notes",
ActorUserId = "42",
Technicians = new List<RosterTechnicianWriteModel>
{
new() { Id = techId, ContactName = "New Name", IsActive = true }
}
}, CancellationToken.None);
roster.Name.Should().Be("New Co");
roster.Notes.Should().Be("updated notes");
var tech = context.Vendors.Single(v => v.Id == techId);
tech.ContactName.Should().Be("New Name");
tech.CompanyName.Should().Be("New Co");
context.VendorCompanies.Single(c => c.Id == companyId).Notes.Should().Be("updated notes");
}
[Fact]
public async Task SaveRoster_SoftDeletesOmittedTechnicianWithoutOpenWorkOrders()
{
var dbName = Guid.NewGuid().ToString();
int companyId, keepId, dropId;
using (var seed = NewContext(dbName))
{
var (cid, _) = await SeedCompanyWithTechnicianAsync(seed, "Drop Co", "Keep", "Drop");
companyId = cid;
keepId = seed.Vendors.First(v => v.CompanyId == cid && v.ContactName == "Keep").Id;
dropId = seed.Vendors.First(v => v.CompanyId == cid && v.ContactName == "Drop").Id;
}
using (var act = NewContext(dbName))
{
var service = new VendorCompanyRosterDataService(act);
await service.SaveRosterAsync(new VendorCompanyRosterWriteModel
{
CompanyId = companyId,
Name = "Drop Co",
Email = "drop@example.com",
ActorUserId = "42",
Technicians = new List<RosterTechnicianWriteModel>
{
new() { Id = keepId, ContactName = "Keep", IsActive = true }
}
}, CancellationToken.None);
}
using var verify = NewContext(dbName);
var kept = verify.Vendors.Single(v => v.Id == keepId);
kept.IsDeleted.Should().NotBeTrue();
var dropped = verify.Vendors.Single(v => v.Id == dropId);
dropped.IsDeleted.Should().BeTrue();
dropped.IsActive.Should().BeFalse();
}
[Fact]
public async Task SaveRoster_RemovingAllTechniciansLeavesZeroVendorsAndCompanyRemains()
{
var dbName = Guid.NewGuid().ToString();
int companyId;
using (var seed = NewContext(dbName))
{
var (cid, _) = await SeedCompanyWithTechnicianAsync(seed, "Empty Co", "T1", "T2");
companyId = cid;
}
using (var act = NewContext(dbName))
{
var service = new VendorCompanyRosterDataService(act);
await service.SaveRosterAsync(new VendorCompanyRosterWriteModel
{
CompanyId = companyId,
Name = "Empty Co",
Email = "empty@example.com",
ActorUserId = "42",
Technicians = new List<RosterTechnicianWriteModel>()
}, CancellationToken.None);
}
using var verify = NewContext(dbName);
verify.Vendors.Count(v => v.CompanyId == companyId && (v.IsDeleted == null || v.IsDeleted == false)).Should().Be(0);
verify.VendorCompanies.Any(c => c.Id == companyId).Should().BeTrue();
}
[Fact]
public async Task SaveRoster_RemovalBlockedByOpenWorkOrdersFailsWholeSave()
{
var dbName = Guid.NewGuid().ToString();
int companyId, vendorId;
using (var seed = NewContext(dbName))
{
var (cid, vendor) = await SeedCompanyWithTechnicianAsync(seed, "Blocked Co", "Busy");
companyId = cid;
vendorId = vendor.Id;
seed.workOrders.Add(new WorkOrder { Id = 800, LifecycleStatus = LifecycleStatus.Scheduled, Status = "Scheduled" });
seed.Dispatches.Add(new Dispatch { VendorId = vendorId, WorkOrderId = 800, Vendor = vendor });
await seed.SaveChangesAsync();
}
using (var act = NewContext(dbName))
{
var service = new VendorCompanyRosterDataService(act);
var actCall = () => service.SaveRosterAsync(new VendorCompanyRosterWriteModel
{
CompanyId = companyId,
RowVersion = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 },
Name = "Blocked Co",
Email = "blocked@example.com",
ActorUserId = "42",
Technicians = new List<RosterTechnicianWriteModel>()
}, CancellationToken.None);
await actCall.Should().ThrowAsync<VendorRosterConflictException>();
}
using var verify = NewContext(dbName);
var tech = verify.Vendors.Single(v => v.Id == vendorId);
tech.IsDeleted.Should().NotBeTrue();
tech.IsActive.Should().BeTrue();
}
[Fact]
public async Task GetRoster_ForwardsCancellation()
{
var dbName = Guid.NewGuid().ToString();
using var context = NewContext(dbName);
var service = new VendorCompanyRosterDataService(context);
using var cts = new CancellationTokenSource();
cts.Cancel();
var act = () => service.GetRosterAsync(1, null, cts.Token);
await act.Should().ThrowAsync<OperationCanceledException>();
}
}