mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
test(work-orders): add Phase 0 domain unit tests
This commit is contained in:
parent
49e5de5152
commit
7a81b6d279
6 changed files with 249 additions and 0 deletions
39
SeaHavenIndustries.Tests/LifecycleStatusMapperTests.cs
Normal file
39
SeaHavenIndustries.Tests/LifecycleStatusMapperTests.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
using Data.SeaHavenIndustries.Enums;
|
||||||
|
using SeaHaven.Services.Helpers;
|
||||||
|
|
||||||
|
namespace SeaHavenIndustries.Tests;
|
||||||
|
|
||||||
|
public class LifecycleStatusMapperTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Open", LifecycleStatus.Incomplete)]
|
||||||
|
[InlineData("Incomplete", LifecycleStatus.Incomplete)]
|
||||||
|
[InlineData("InProgress", LifecycleStatus.InProgress)]
|
||||||
|
[InlineData("In Progress", LifecycleStatus.InProgress)]
|
||||||
|
[InlineData("Completed", LifecycleStatus.Completed)]
|
||||||
|
[InlineData("Cancelled", LifecycleStatus.Canceled)]
|
||||||
|
[InlineData("On Hold", LifecycleStatus.Pending)]
|
||||||
|
[InlineData("Scheduled", LifecycleStatus.Scheduled)]
|
||||||
|
[InlineData("En Route", LifecycleStatus.EnRoute)]
|
||||||
|
[InlineData("Pending Quote", LifecycleStatus.PendingQuote)]
|
||||||
|
public void FromLegacyStatus_MapsKnownValues(string input, LifecycleStatus expected)
|
||||||
|
{
|
||||||
|
var result = LifecycleStatusMapper.FromLegacyStatus(input);
|
||||||
|
Assert.Equal(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FromLegacyStatus_Unknown_ReturnsIncomplete()
|
||||||
|
{
|
||||||
|
var result = LifecycleStatusMapper.FromLegacyStatus("MysteryStatus");
|
||||||
|
Assert.Equal(LifecycleStatus.Incomplete, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(LifecycleStatus.EnRoute, "En Route")]
|
||||||
|
[InlineData(LifecycleStatus.PendingQuote, "Pending Quote")]
|
||||||
|
public void ToFeLabel_ReturnsBoardLabel(LifecycleStatus status, string expected)
|
||||||
|
{
|
||||||
|
Assert.Equal(expected, LifecycleStatusMapper.ToFeLabel(status));
|
||||||
|
}
|
||||||
|
}
|
||||||
28
SeaHavenIndustries.Tests/SeaHavenIndustries.Tests.csproj
Normal file
28
SeaHavenIndustries.Tests/SeaHavenIndustries.Tests.csproj
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Xunit" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Api.SeaHavenIndustries\Api.SeaHavenIndustries.csproj" />
|
||||||
|
<ProjectReference Include="..\SeaHaven.Services\SeaHaven.Services.csproj" />
|
||||||
|
<ProjectReference Include="..\Data.SeaHavenIndustries\Data.SeaHavenIndustries.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
78
SeaHavenIndustries.Tests/SyncFieldMergePolicyTests.cs
Normal file
78
SeaHavenIndustries.Tests/SyncFieldMergePolicyTests.cs
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
using Data.SeaHavenIndustries;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SeaHaven.Services.Implementation;
|
||||||
|
using SeaHaven.Services.Interfaces;
|
||||||
|
|
||||||
|
namespace SeaHavenIndustries.Tests;
|
||||||
|
|
||||||
|
public class SyncFieldMergePolicyTests
|
||||||
|
{
|
||||||
|
private static ApplicationDbContext CreateContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||||
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||||
|
.Options;
|
||||||
|
return new ApplicationDbContext(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TryApply_WhenFieldLocked_RejectsAndAudits()
|
||||||
|
{
|
||||||
|
await using var context = CreateContext();
|
||||||
|
var wo = new WorkOrder { Id = 10, Description = "SHOC edited", Status = "Open" };
|
||||||
|
context.workOrders.Add(wo);
|
||||||
|
context.WorkOrderFieldLocks.Add(new WorkOrderFieldLock
|
||||||
|
{
|
||||||
|
WorkOrderId = 10,
|
||||||
|
FieldName = "Description",
|
||||||
|
LockedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var locks = new WorkOrderFieldLockService(context);
|
||||||
|
var audit = new WorkOrderAuditService(context, locks);
|
||||||
|
var policy = new SyncFieldMergePolicy(locks, audit);
|
||||||
|
|
||||||
|
var syncContext = new WorkOrderSyncContext
|
||||||
|
{
|
||||||
|
WorkOrder = wo,
|
||||||
|
FieldLocks = locks,
|
||||||
|
Audit = audit
|
||||||
|
};
|
||||||
|
|
||||||
|
var applied = await policy.TryApplyAsync(syncContext, "Description", "Sync value");
|
||||||
|
|
||||||
|
Assert.False(applied);
|
||||||
|
Assert.Equal("SHOC edited", wo.Description);
|
||||||
|
|
||||||
|
var logs = await context.WorkOrderAuditLogs.ToListAsync();
|
||||||
|
Assert.Single(logs);
|
||||||
|
Assert.Equal("SyncRejected", logs[0].Action);
|
||||||
|
Assert.Equal("Sync", logs[0].EventType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TryApply_WhenFieldNotLocked_AppliesValue()
|
||||||
|
{
|
||||||
|
await using var context = CreateContext();
|
||||||
|
var wo = new WorkOrder { Id = 11, Description = "Original" };
|
||||||
|
context.workOrders.Add(wo);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var locks = new WorkOrderFieldLockService(context);
|
||||||
|
var audit = new WorkOrderAuditService(context, locks);
|
||||||
|
var policy = new SyncFieldMergePolicy(locks, audit);
|
||||||
|
|
||||||
|
var syncContext = new WorkOrderSyncContext
|
||||||
|
{
|
||||||
|
WorkOrder = wo,
|
||||||
|
FieldLocks = locks,
|
||||||
|
Audit = audit
|
||||||
|
};
|
||||||
|
|
||||||
|
var applied = await policy.TryApplyAsync(syncContext, "Description", "From sync");
|
||||||
|
|
||||||
|
Assert.True(applied);
|
||||||
|
Assert.Equal("From sync", wo.Description);
|
||||||
|
}
|
||||||
|
}
|
||||||
55
SeaHavenIndustries.Tests/WorkOrderAuditServiceTests.cs
Normal file
55
SeaHavenIndustries.Tests/WorkOrderAuditServiceTests.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
using Data.SeaHavenIndustries;
|
||||||
|
using Data.SeaHavenIndustries.Enums;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SeaHaven.Services.Implementation;
|
||||||
|
using SeaHaven.Services.Interfaces;
|
||||||
|
|
||||||
|
namespace SeaHavenIndustries.Tests;
|
||||||
|
|
||||||
|
public class WorkOrderAuditServiceTests
|
||||||
|
{
|
||||||
|
private static ApplicationDbContext CreateContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||||
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||||
|
.Options;
|
||||||
|
return new ApplicationDbContext(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LogStatusChanged_CreatesSingleAuditWithManualEventType()
|
||||||
|
{
|
||||||
|
await using var context = CreateContext();
|
||||||
|
context.workOrders.Add(new WorkOrder { Id = 1, Status = "Open" });
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var locks = new WorkOrderFieldLockService(context);
|
||||||
|
var audit = new WorkOrderAuditService(context, locks);
|
||||||
|
|
||||||
|
await audit.LogStatusChangedAsync(1, "Open", "In Progress", "user-1");
|
||||||
|
|
||||||
|
var logs = await context.WorkOrderAuditLogs.ToListAsync();
|
||||||
|
Assert.Single(logs);
|
||||||
|
Assert.Equal("StatusChanged", logs[0].Action);
|
||||||
|
Assert.Equal("Manual", logs[0].EventType);
|
||||||
|
Assert.Equal("Dispatcher", logs[0].ActorType);
|
||||||
|
Assert.Equal("LifecycleStatus", logs[0].FieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LogStatusChanged_CreatesFieldLock()
|
||||||
|
{
|
||||||
|
await using var context = CreateContext();
|
||||||
|
context.workOrders.Add(new WorkOrder { Id = 2, Status = "Open" });
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var locks = new WorkOrderFieldLockService(context);
|
||||||
|
var audit = new WorkOrderAuditService(context, locks);
|
||||||
|
|
||||||
|
await audit.LogStatusChangedAsync(2, "Open", "Scheduled", "user-1");
|
||||||
|
|
||||||
|
var fieldLocks = await context.WorkOrderFieldLocks.ToListAsync();
|
||||||
|
Assert.Single(fieldLocks);
|
||||||
|
Assert.Equal("LifecycleStatus", fieldLocks[0].FieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
SeaHavenIndustries.Tests/WorkOrderNumberNormalizerTests.cs
Normal file
43
SeaHavenIndustries.Tests/WorkOrderNumberNormalizerTests.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
using SeaHaven.Services.Helpers;
|
||||||
|
|
||||||
|
namespace SeaHavenIndustries.Tests;
|
||||||
|
|
||||||
|
public class WorkOrderNumberNormalizerTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void TryNormalize_PadsToElevenDigits()
|
||||||
|
{
|
||||||
|
var ok = WorkOrderNumberNormalizer.TryNormalize("12345", out var normalized, out var error);
|
||||||
|
|
||||||
|
Assert.True(ok);
|
||||||
|
Assert.Null(error);
|
||||||
|
Assert.Equal("00000012345", normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TryNormalize_AcceptsElevenDigits()
|
||||||
|
{
|
||||||
|
var ok = WorkOrderNumberNormalizer.TryNormalize("10000000001", out var normalized, out _);
|
||||||
|
|
||||||
|
Assert.True(ok);
|
||||||
|
Assert.Equal("10000000001", normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TryNormalize_RejectsEmpty()
|
||||||
|
{
|
||||||
|
var ok = WorkOrderNumberNormalizer.TryNormalize("", out _, out var error);
|
||||||
|
|
||||||
|
Assert.False(ok);
|
||||||
|
Assert.NotNull(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TryNormalize_RejectsTooLong()
|
||||||
|
{
|
||||||
|
var ok = WorkOrderNumberNormalizer.TryNormalize("123456789012", out _, out var error);
|
||||||
|
|
||||||
|
Assert.False(ok);
|
||||||
|
Assert.Contains("11", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.SeaHavenIndustries.Test
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeaHaven.Services.Tests", "SeaHaven.Services.Tests\SeaHaven.Services.Tests.csproj", "{97455198-C479-47F2-8BC8-601B335DFFAB}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeaHaven.Services.Tests", "SeaHaven.Services.Tests\SeaHaven.Services.Tests.csproj", "{97455198-C479-47F2-8BC8-601B335DFFAB}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeaHavenIndustries.Tests", "SeaHavenIndustries.Tests\SeaHavenIndustries.Tests.csproj", "{0093B3F5-0180-4C02-A088-6A564E06CEBF}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
@ -51,6 +53,10 @@ Global
|
||||||
{97455198-C479-47F2-8BC8-601B335DFFAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{97455198-C479-47F2-8BC8-601B335DFFAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{97455198-C479-47F2-8BC8-601B335DFFAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{97455198-C479-47F2-8BC8-601B335DFFAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{97455198-C479-47F2-8BC8-601B335DFFAB}.Release|Any CPU.Build.0 = Release|Any CPU
|
{97455198-C479-47F2-8BC8-601B335DFFAB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{0093B3F5-0180-4C02-A088-6A564E06CEBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{0093B3F5-0180-4C02-A088-6A564E06CEBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{0093B3F5-0180-4C02-A088-6A564E06CEBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{0093B3F5-0180-4C02-A088-6A564E06CEBF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue