fix(sitecalls): Parked is re-openable end to end — operator Retry/Discard reach the site tracking row and central SiteCalls (SCA-1, WP4 task 4.9)

This commit is contained in:
Joseph Doherty
2026-12-03 10:18:43 -05:00
parent 4070f7efca
commit 5c67559a25
12 changed files with 578 additions and 15 deletions
@@ -372,33 +372,111 @@ public class SiteCallAuditRepositoryTests : IClassFixture<MsSqlMigrationFixture>
Assert.Equal(0, row.RetryCount);
}
/// <summary>Verifies that one terminal status cannot overwrite another terminal status.</summary>
/// <summary>
/// Verifies that one terminal status cannot overwrite another terminal status — except
/// <c>Parked</c>, which is re-openable (SCA-1; see the Parked tests below).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task UpsertAsync_TerminalOverTerminal_IsNoOp()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
// Terminal statuses share rank 3 and are mutually
// exclusive — Delivered cannot overwrite Parked. Note that the
// Delivered packet carries a STRICTLY-NEWER UpdatedAtUtc, yet the row
// stays Parked — the same-rank freshness tiebreaker is scoped to
// NON-terminal ranks (< 3), so terminal immutability is preserved.
// Delivered/Failed/Discarded share rank 3 and are mutually exclusive: a later
// Failed (STRICTLY-NEWER UpdatedAtUtc) cannot overwrite Delivered. The same-rank
// freshness tiebreaker is scoped to NON-terminal ranks (< 3).
var id = TrackedOperationId.New();
var t0 = new DateTime(2026, 6, 1, 13, 0, 0, DateTimeKind.Utc);
await using var context = CreateContext();
var repo = new SiteCallAuditRepository(context);
await repo.UpsertAsync(NewRow(id, status: "Delivered", retryCount: 3, lastError: null, updatedAtUtc: t0, terminal: true, terminalAtUtc: t0));
var afterDelivered = await repo.GetAsync(id);
await repo.UpsertAsync(NewRow(id, status: "Failed", retryCount: 4, lastError: "late", updatedAtUtc: t0.AddMinutes(5), terminal: true, terminalAtUtc: t0.AddMinutes(5)));
await repo.UpsertAsync(NewRow(id, status: "Parked", retryCount: 4, lastError: "late", updatedAtUtc: t0.AddMinutes(6), terminal: true, terminalAtUtc: t0.AddMinutes(6)));
var after = await repo.GetAsync(id);
Assert.NotNull(after);
Assert.Equal("Delivered", after!.Status);
Assert.Equal(afterDelivered!.UpdatedAtUtc, after.UpdatedAtUtc);
}
/// <summary>
/// SCA-1 regression: an operator Retry of a Parked call re-opens it at the site, so a
/// NEWER Attempted and then Delivered must replace the Parked row (it used to stay Parked
/// until the 365-day purge).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task UpsertAsync_ParkedThenNewerAttemptedThenDelivered_Reopens()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var id = TrackedOperationId.New();
var t0 = new DateTime(2026, 6, 1, 13, 0, 0, DateTimeKind.Utc);
await using var context = CreateContext();
var repo = new SiteCallAuditRepository(context);
await repo.UpsertAsync(NewRow(id, status: "Parked", retryCount: 3, lastError: "parked-reason", updatedAtUtc: t0, terminal: true, terminalAtUtc: t0));
var afterPark = await repo.GetAsync(id);
await repo.UpsertAsync(NewRow(id, status: "Delivered", retryCount: 4, lastError: null, updatedAtUtc: t0.AddMinutes(5), terminal: true, terminalAtUtc: t0.AddMinutes(5)));
var afterDeliveredAttempt = await repo.GetAsync(id);
await repo.UpsertAsync(NewRow(id, status: "Attempted", retryCount: 0, lastError: null, updatedAtUtc: t0.AddMinutes(5)));
var reopened = await repo.GetAsync(id);
Assert.Equal("Attempted", reopened!.Status);
Assert.Null(reopened.TerminalAtUtc);
Assert.Null(reopened.LastError);
Assert.NotNull(afterDeliveredAttempt);
Assert.Equal("Parked", afterDeliveredAttempt!.Status);
Assert.Equal("parked-reason", afterDeliveredAttempt.LastError);
Assert.Equal(afterPark!.UpdatedAtUtc, afterDeliveredAttempt.UpdatedAtUtc);
await repo.UpsertAsync(NewRow(id, status: "Delivered", retryCount: 1, lastError: null, updatedAtUtc: t0.AddMinutes(6), terminal: true, terminalAtUtc: t0.AddMinutes(6)));
// Fresh context: the first GetAsync left the row tracked, and a raw-SQL upsert does
// not refresh a tracked entity.
await using var readContext = CreateContext();
var delivered = await new SiteCallAuditRepository(readContext).GetAsync(id);
Assert.Equal("Delivered", delivered!.Status);
Assert.NotNull(delivered.TerminalAtUtc);
}
/// <summary>SCA-1: an operator Discard of a Parked call (newer) closes it as Discarded.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task UpsertAsync_ParkedThenNewerDiscarded_IsDiscarded()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var id = TrackedOperationId.New();
var t0 = new DateTime(2026, 6, 1, 13, 0, 0, DateTimeKind.Utc);
await using var context = CreateContext();
var repo = new SiteCallAuditRepository(context);
await repo.UpsertAsync(NewRow(id, status: "Parked", retryCount: 3, lastError: "parked-reason", updatedAtUtc: t0, terminal: true, terminalAtUtc: t0));
await repo.UpsertAsync(NewRow(id, status: "Discarded", retryCount: 3, lastError: "Discarded by operator", updatedAtUtc: t0.AddMinutes(5), terminal: true, terminalAtUtc: t0.AddMinutes(5)));
var row = await repo.GetAsync(id);
Assert.Equal("Discarded", row!.Status);
}
/// <summary>
/// SCA-1: Parked is re-opened only by a STRICTLY NEWER Attempted/Delivered/Failed/Discarded — a
/// stale pre-park packet replayed late, or a newer Submitted/Forwarded, leaves it Parked.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task UpsertAsync_Parked_StaleOrLowRankPacket_StaysParked()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var id = TrackedOperationId.New();
var t0 = new DateTime(2026, 6, 1, 13, 0, 0, DateTimeKind.Utc);
await using var context = CreateContext();
var repo = new SiteCallAuditRepository(context);
await repo.UpsertAsync(NewRow(id, status: "Parked", retryCount: 3, lastError: "parked-reason", updatedAtUtc: t0, terminal: true, terminalAtUtc: t0));
await repo.UpsertAsync(NewRow(id, status: "Attempted", retryCount: 2, lastError: "old", updatedAtUtc: t0.AddMinutes(-5)));
await repo.UpsertAsync(NewRow(id, status: "Delivered", retryCount: 2, updatedAtUtc: t0, terminal: true, terminalAtUtc: t0));
await repo.UpsertAsync(NewRow(id, status: "Submitted", retryCount: 0, updatedAtUtc: t0.AddMinutes(5)));
var row = await repo.GetAsync(id);
Assert.Equal("Parked", row!.Status);
Assert.Equal("parked-reason", row.LastError);
}
/// <summary>Verifies that concurrent inserts of the same id leave only one row.</summary>