using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Implementation; using SeaHaven.Services.Constants; using SeaHaven.Services.Implementation; namespace SeaHavenIndustries.Tests; public class WorkOrderFieldLockServiceTests { private static ApplicationDbContext CreateContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new ApplicationDbContext(options); } [Fact] public async Task LockFieldAsync_DuplicateCalls_EnqueuesSingleLock() { await using var context = CreateContext(); var lockData = new WorkOrderFieldLockDataService(context); var locks = new WorkOrderFieldLockService(lockData); await locks.LockFieldAsync(1, WorkOrderFieldNames.Description, "user-1"); await locks.LockFieldAsync(1, WorkOrderFieldNames.Description, "user-2"); await context.SaveChangesAsync(); var fieldLocks = await context.WorkOrderFieldLocks.ToListAsync(); Assert.Single(fieldLocks); Assert.Equal(WorkOrderFieldNames.Description, fieldLocks[0].FieldName); } [Fact] public async Task LockFieldAsync_WhenAlreadyPersisted_IsIdempotent() { await using var context = CreateContext(); context.WorkOrderFieldLocks.Add(new WorkOrderFieldLock { WorkOrderId = 2, FieldName = WorkOrderFieldNames.AssignTo, LockedAt = DateTime.UtcNow, LockedByUserId = "user-1" }); await context.SaveChangesAsync(); var lockData = new WorkOrderFieldLockDataService(context); var locks = new WorkOrderFieldLockService(lockData); await locks.LockFieldAsync(2, WorkOrderFieldNames.AssignTo, "user-2"); await context.SaveChangesAsync(); var fieldLocks = await context.WorkOrderFieldLocks.ToListAsync(); Assert.Single(fieldLocks); } }