252 lines
9.9 KiB
C#
252 lines
9.9 KiB
C#
using Heimdall.CameraAssignments.Domain;
|
|
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace Heimdall.Migration.Domain.Tests;
|
|
|
|
public sealed class CameraAssignmentTests
|
|
{
|
|
private static readonly Guid AssignmentId = Guid.Parse("00000000-0000-0000-0000-000000000001");
|
|
private static readonly Guid TenantId = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
|
private static readonly Guid SiteId = Guid.Parse("00000000-0000-0000-0000-000000000003");
|
|
private static readonly Guid CameraId = Guid.Parse("00000000-0000-0000-0000-000000000004");
|
|
private static readonly Guid AgentId = Guid.Parse("00000000-0000-0000-0000-000000000005");
|
|
private static readonly Guid OtherAgentId = Guid.Parse("00000000-0000-0000-0000-000000000006");
|
|
private static readonly Guid LeaseId = Guid.Parse("00000000-0000-0000-0000-000000000007");
|
|
private static readonly Guid OtherLeaseId = Guid.Parse("00000000-0000-0000-0000-000000000008");
|
|
private static readonly DateTimeOffset Now = new(2026, 10, 14, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
[Fact]
|
|
public void Create_ValidIdentities_StartsUnassigned()
|
|
{
|
|
CameraAssignment assignment = Create();
|
|
assignment.Id.ShouldBe(AssignmentId);
|
|
assignment.TenantId.ShouldBe(TenantId);
|
|
assignment.SiteId.ShouldBe(SiteId);
|
|
assignment.CameraId.ShouldBe(CameraId);
|
|
assignment.Epoch.ShouldBe(0);
|
|
assignment.Version.ShouldBe(1);
|
|
assignment.IsReleased.ShouldBeTrue();
|
|
assignment.AgentId.ShouldBeNull();
|
|
assignment.IsActiveAt(Now).ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(1)]
|
|
[InlineData(2)]
|
|
[InlineData(3)]
|
|
public void Create_MissingIdentity_RejectsAssignment(int missing)
|
|
{
|
|
Should.Throw<CameraAssignmentsDomainException>(() => CameraAssignment.Create(
|
|
missing == 0 ? Guid.Empty : AssignmentId, missing == 1 ? Guid.Empty : TenantId,
|
|
missing == 2 ? Guid.Empty : SiteId, missing == 3 ? Guid.Empty : CameraId));
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_UnassignedCamera_OpensFirstSession()
|
|
{
|
|
CameraAssignment assignment = Create();
|
|
assignment.Claim(1, AgentId, LeaseId, Now, Now.AddMinutes(1));
|
|
assignment.AgentId.ShouldBe(AgentId);
|
|
assignment.LeaseId.ShouldBe(LeaseId);
|
|
assignment.Epoch.ShouldBe(1);
|
|
assignment.Version.ShouldBe(2);
|
|
assignment.StartedAt.ShouldBe(Now);
|
|
assignment.ExpiresAt.ShouldBe(Now.AddMinutes(1));
|
|
assignment.IsReleased.ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1, false)]
|
|
[InlineData(0, true)]
|
|
[InlineData(59, true)]
|
|
[InlineData(60, false)]
|
|
[InlineData(61, false)]
|
|
public void IsActiveAt_LeaseBoundaries_UsesExclusiveExpiry(int seconds, bool expected)
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.IsActiveAt(Now.AddSeconds(seconds)).ShouldBe(expected);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(1)]
|
|
[InlineData(2)]
|
|
[InlineData(3)]
|
|
public void Claim_InvalidInput_PreservesUnassignedState(int invalid)
|
|
{
|
|
CameraAssignment assignment = Create();
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Claim(1,
|
|
invalid == 0 ? Guid.Empty : AgentId, invalid == 1 ? Guid.Empty : LeaseId,
|
|
Now, invalid == 2 ? Now : invalid == 3 ? Now.AddSeconds(-1) : Now.AddMinutes(1)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_ActiveSession_RejectsSecondAgentWithoutMutation()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Claim(2, OtherAgentId, OtherLeaseId, Now.AddSeconds(10), Now.AddMinutes(2)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_ExpiredSession_AdvancesEpochAndRejectsOldResult()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Claim(2, OtherAgentId, OtherLeaseId, Now.AddMinutes(1), Now.AddMinutes(2));
|
|
assignment.Epoch.ShouldBe(2);
|
|
assignment.Version.ShouldBe(3);
|
|
assignment.IsCurrentResult(AgentId, LeaseId, 1, Now.AddMinutes(1)).ShouldBeFalse();
|
|
assignment.IsCurrentResult(OtherAgentId, OtherLeaseId, 2, Now.AddMinutes(1)).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_StaleRevision_RejectsSequentialCompetingClaim()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Claim(1, OtherAgentId, OtherLeaseId, Now.AddMinutes(1), Now.AddMinutes(2)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_ReusedLeaseToken_RejectsNewSession()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Claim(2, AgentId, LeaseId, Now.AddMinutes(1), Now.AddMinutes(2)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(1)]
|
|
[InlineData(2)]
|
|
[InlineData(3)]
|
|
public void IsCurrentResult_WrongSession_RejectsResult(int invalid)
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.IsCurrentResult(invalid == 0 ? OtherAgentId : AgentId,
|
|
invalid == 1 ? OtherLeaseId : LeaseId, invalid == 2 ? 0 : invalid == 3 ? 2 : 1, Now).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Renew_ActiveSession_ExtendsExpiryWithoutChangingEpoch()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Renew(2, AgentId, LeaseId, 1, Now.AddSeconds(30), Now.AddMinutes(2));
|
|
assignment.Version.ShouldBe(3);
|
|
assignment.Epoch.ShouldBe(1);
|
|
assignment.ExpiresAt.ShouldBe(Now.AddMinutes(2));
|
|
assignment.IsCurrentResult(AgentId, LeaseId, 1, Now.AddSeconds(90)).ShouldBeTrue();
|
|
assignment.IsActiveAt(Now.AddSeconds(29)).ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(1)]
|
|
[InlineData(2)]
|
|
[InlineData(3)]
|
|
[InlineData(4)]
|
|
[InlineData(5)]
|
|
[InlineData(6)]
|
|
public void Renew_InvalidRequest_PreservesSession(int invalid)
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Renew(
|
|
invalid == 0 ? 1 : 2, invalid == 1 ? OtherAgentId : AgentId,
|
|
invalid == 2 ? OtherLeaseId : LeaseId, invalid == 3 ? 2 : 1,
|
|
invalid == 4 ? Now.AddMinutes(1) : invalid == 5 ? Now.AddSeconds(-1) : Now.AddSeconds(30),
|
|
invalid == 6 ? Now.AddMinutes(1) : Now.AddMinutes(2)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Release_ActiveSession_InvalidatesResultsAndAllowsNextEpoch()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Release(2, AgentId, LeaseId, 1, Now.AddSeconds(10));
|
|
assignment.Version.ShouldBe(3);
|
|
assignment.Epoch.ShouldBe(1);
|
|
assignment.IsCurrentResult(AgentId, LeaseId, 1, Now.AddSeconds(10)).ShouldBeFalse();
|
|
assignment.Claim(3, AgentId, OtherLeaseId, Now.AddSeconds(11), Now.AddMinutes(2));
|
|
assignment.Epoch.ShouldBe(2);
|
|
assignment.IsCurrentResult(AgentId, LeaseId, 1, Now.AddSeconds(11)).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Release_AlreadyReleasedCurrentRevision_DoesNotAdvanceVersion()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Release(2, AgentId, LeaseId, 1, Now.AddSeconds(10));
|
|
object before = Snapshot(assignment);
|
|
assignment.Release(3, AgentId, LeaseId, 1, Now.AddSeconds(11));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Release_ExpiredSession_ClosesLastSession()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Release(2, AgentId, LeaseId, 1, Now.AddMinutes(2));
|
|
assignment.IsReleased.ShouldBeTrue();
|
|
assignment.Epoch.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void Release_FormerAgentAfterReassignment_PreservesNewSession()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Claim(2, OtherAgentId, OtherLeaseId, Now.AddMinutes(1), Now.AddMinutes(2));
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Release(3, AgentId, LeaseId, 1, Now.AddSeconds(70)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_TimeBeforeRelease_PreservesReleasedSession()
|
|
{
|
|
CameraAssignment assignment = Claim();
|
|
assignment.Release(2, AgentId, LeaseId, 1, Now.AddSeconds(30));
|
|
object before = Snapshot(assignment);
|
|
Should.Throw<CameraAssignmentsDomainException>(() => assignment.Claim(3, OtherAgentId, OtherLeaseId, Now.AddSeconds(29), Now.AddMinutes(2)));
|
|
Snapshot(assignment).ShouldBe(before);
|
|
}
|
|
|
|
[Fact]
|
|
public void Claim_NonUtcInput_StoresSameInstantsInUtc()
|
|
{
|
|
CameraAssignment assignment = Create();
|
|
assignment.Claim(1, AgentId, LeaseId, Now.ToOffset(TimeSpan.FromHours(2)), Now.AddMinutes(1).ToOffset(TimeSpan.FromHours(2)));
|
|
assignment.StartedAt.ShouldBe(Now);
|
|
assignment.StartedAt!.Value.Offset.ShouldBe(TimeSpan.Zero);
|
|
assignment.ExpiresAt!.Value.Offset.ShouldBe(TimeSpan.Zero);
|
|
}
|
|
|
|
private static CameraAssignment Create() => CameraAssignment.Create(AssignmentId, TenantId, SiteId, CameraId);
|
|
|
|
private static CameraAssignment Claim()
|
|
{
|
|
CameraAssignment assignment = Create();
|
|
assignment.Claim(1, AgentId, LeaseId, Now, Now.AddMinutes(1));
|
|
return assignment;
|
|
}
|
|
|
|
private static object Snapshot(CameraAssignment assignment) => new
|
|
{
|
|
assignment.AgentId,
|
|
assignment.LeaseId,
|
|
assignment.Epoch,
|
|
assignment.Version,
|
|
assignment.StartedAt,
|
|
assignment.ExpiresAt,
|
|
assignment.ChangedAt,
|
|
assignment.IsReleased,
|
|
};
|
|
}
|