test(#259): id-based reconcile matrix + docs (api-conventions §7c, decisions)
Add the id-based reconcile tests to ReplaceProgramScheduleItemsReconcileTests: reorder moves state with the logical item (the non-vacuous core — proven to fail under forced-positional), insert-in-middle, delete-unreferenced, unknown-id→422, duplicate-id→422, and stale-version+unknown-id→412 (412 precedes 422, §7c). The GET→map→PUT lossless round-trip now round-trips r.Id so it exercises id-mode. Threads the new int? Id through all command/wire construction sites in tests. Docs: api-conventions §7c (stable child identity + the deliberate #2-#5 positional asymmetry) and a decisions.md entry. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+263
-1
@@ -3,6 +3,7 @@ using ErsatzTV.Application;
|
||||
using ErsatzTV.Application.ProgramSchedules;
|
||||
using ErsatzTV.Core;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Errors;
|
||||
using ErsatzTV.Core.Scheduling;
|
||||
using ErsatzTV.Infrastructure.Data;
|
||||
using ErsatzTV.Tests.Support;
|
||||
@@ -10,6 +11,7 @@ using LanguageExt;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NUnit.Framework;
|
||||
using Shouldly;
|
||||
using static LanguageExt.Prelude;
|
||||
|
||||
namespace ErsatzTV.Tests.Application.ProgramSchedules;
|
||||
|
||||
@@ -234,6 +236,265 @@ public class ReplaceProgramScheduleItemsReconcileTests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Reorder_ById_Should_Move_State_With_The_Logical_Item_Not_The_Slot()
|
||||
{
|
||||
// The non-vacuous core of #259: two SAME-typed items are swapped while round-tripping their ids.
|
||||
// In id-mode each item is matched by id, so idA MOVES to index 1 and keeps its own fill-group state.
|
||||
// In the old positional reconcile idA would have been reused in place at index 0 (its slot), silently
|
||||
// pairing item "a"'s enumerator state with item "b"'s content — so `idA.Index == 1` is the assertion
|
||||
// that fails under positional and passes under id-based reconcile.
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
|
||||
var seedItems = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.Multiple, "a") with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups },
|
||||
MakeItem(1, PlayoutMode.Multiple, "b") with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups }
|
||||
};
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, seedItems), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
int idA, idB;
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> persisted = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).OrderBy(i => i.Index).ToListAsync();
|
||||
idA = persisted[0].Id;
|
||||
idB = persisted[1].Id;
|
||||
AddFillGroup(ctx, idA, seed: 100);
|
||||
AddFillGroup(ctx, idB, seed: 200);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// Act: swap the two items, round-tripping their ids.
|
||||
var swapped = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.Multiple, "b", id: idB) with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups },
|
||||
MakeItem(1, PlayoutMode.Multiple, "a", id: idA) with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups }
|
||||
};
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, swapped), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> after = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).ToListAsync();
|
||||
after.Count.ShouldBe(2);
|
||||
|
||||
// identity mapping: each id moved to its new position (positional would have kept idA at 0)
|
||||
after.Single(i => i.Id == idA).Index.ShouldBe(1);
|
||||
after.Single(i => i.Id == idB).Index.ShouldBe(0);
|
||||
|
||||
// each item's fill-group state followed its own id — no misattribution, no cascade
|
||||
List<PlayoutScheduleItemFillGroupIndex> groups = await ctx.Set<PlayoutScheduleItemFillGroupIndex>()
|
||||
.Include(x => x.EnumeratorState).ToListAsync();
|
||||
groups.Single(g => g.ProgramScheduleItemId == idA).EnumeratorState.Seed.ShouldBe(100);
|
||||
groups.Single(g => g.ProgramScheduleItemId == idB).EnumeratorState.Seed.ShouldBe(200);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Insert_ById_In_Middle_Should_Keep_Existing_Ids_And_State()
|
||||
{
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
|
||||
var seed = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.One, "a"),
|
||||
MakeItem(1, PlayoutMode.One, "b")
|
||||
};
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, seed), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
int idA, idB;
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> p = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).OrderBy(i => i.Index).ToListAsync();
|
||||
idA = p[0].Id;
|
||||
idB = p[1].Id;
|
||||
AddFillGroup(ctx, idA, seed: 111);
|
||||
AddFillGroup(ctx, idB, seed: 222);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// insert a brand-new (id-less) item between the two existing ones
|
||||
var withInsert = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.One, "a", id: idA),
|
||||
MakeItem(1, PlayoutMode.One, "new"),
|
||||
MakeItem(2, PlayoutMode.One, "b", id: idB)
|
||||
};
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, withInsert), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> after = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).ToListAsync();
|
||||
after.Count.ShouldBe(3);
|
||||
after.Single(i => i.Id == idA).Index.ShouldBe(0);
|
||||
after.Single(i => i.Id == idB).Index.ShouldBe(2); // pushed down by the inserted middle item
|
||||
|
||||
List<PlayoutScheduleItemFillGroupIndex> groups = await ctx.Set<PlayoutScheduleItemFillGroupIndex>()
|
||||
.Include(x => x.EnumeratorState).ToListAsync();
|
||||
groups.Single(g => g.ProgramScheduleItemId == idA).EnumeratorState.Seed.ShouldBe(111);
|
||||
groups.Single(g => g.ProgramScheduleItemId == idB).EnumeratorState.Seed.ShouldBe(222);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ById_Should_Remove_Unreferenced_And_Keep_Survivor_State()
|
||||
{
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
|
||||
var seed = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.One, "a"),
|
||||
MakeItem(1, PlayoutMode.One, "b")
|
||||
};
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, seed), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
int idA;
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> p = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).OrderBy(i => i.Index).ToListAsync();
|
||||
idA = p[0].Id;
|
||||
AddFillGroup(ctx, idA, seed: 333);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// PUT only item A (by id) — B is no longer referenced and must be deleted
|
||||
var onlyA = new List<ReplaceProgramScheduleItem> { MakeItem(0, PlayoutMode.One, "a", id: idA) };
|
||||
(await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, onlyA), CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
List<ProgramScheduleItem> after = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).ToListAsync();
|
||||
after.Count.ShouldBe(1);
|
||||
after.Single().Id.ShouldBe(idA);
|
||||
|
||||
// survivor kept its id and its fill-group state (no cascade on the item that stayed)
|
||||
PlayoutScheduleItemFillGroupIndex survivor = await ctx.Set<PlayoutScheduleItemFillGroupIndex>()
|
||||
.Include(x => x.EnumeratorState).SingleAsync(g => g.ProgramScheduleItemId == idA);
|
||||
survivor.EnumeratorState.Seed.ShouldBe(333);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Unknown_Id_Should_Return_422_And_Persist_Nothing()
|
||||
{
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
(await handler.Handle(
|
||||
new ReplaceProgramScheduleItems(scheduleId, [MakeItem(0, PlayoutMode.One, "a")]),
|
||||
CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
(int idA, int versionBefore) = await ReadSingle(scheduleId);
|
||||
|
||||
// a request id that doesn't belong to this schedule is rejected (stale/foreign identity)
|
||||
var bad = new List<ReplaceProgramScheduleItem> { MakeItem(0, PlayoutMode.One, "x", id: 999999) };
|
||||
Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>> result =
|
||||
await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, bad), CancellationToken.None);
|
||||
|
||||
BaseError error = LeftOrThrow(result);
|
||||
error.GetType().ShouldBe(typeof(BaseError)); // exactly BaseError (→422), not PreconditionFailedError (→412)
|
||||
error.ToString().ShouldContain("999999");
|
||||
await AssertUnchanged(scheduleId, idA, versionBefore);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Duplicate_Id_Should_Return_422_And_Persist_Nothing()
|
||||
{
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
(await handler.Handle(
|
||||
new ReplaceProgramScheduleItems(scheduleId, [MakeItem(0, PlayoutMode.One, "a")]),
|
||||
CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
(int idA, int versionBefore) = await ReadSingle(scheduleId);
|
||||
|
||||
// the same id twice in one payload would map two request items onto one row — reject
|
||||
var dup = new List<ReplaceProgramScheduleItem>
|
||||
{
|
||||
MakeItem(0, PlayoutMode.One, "a", id: idA),
|
||||
MakeItem(1, PlayoutMode.One, "a-again", id: idA)
|
||||
};
|
||||
Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>> result =
|
||||
await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, dup), CancellationToken.None);
|
||||
|
||||
BaseError error = LeftOrThrow(result);
|
||||
error.GetType().ShouldBe(typeof(BaseError));
|
||||
error.ToString().ShouldContain(idA.ToString());
|
||||
await AssertUnchanged(scheduleId, idA, versionBefore);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Stale_Version_And_Unknown_Id_Should_Return_412_Not_422()
|
||||
{
|
||||
// Ordering ruling (§7c): the concurrency pre-check runs before the id guards, so a client that is
|
||||
// BOTH version-stale and id-stale gets 412 (reload signal), never 422 (which reads as a payload bug).
|
||||
int scheduleId = await SeedSchedule();
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
(await handler.Handle(
|
||||
new ReplaceProgramScheduleItems(scheduleId, [MakeItem(0, PlayoutMode.One, "a")]),
|
||||
CancellationToken.None))
|
||||
.IsRight.ShouldBeTrue();
|
||||
|
||||
(int idA, int versionBefore) = await ReadSingle(scheduleId);
|
||||
|
||||
var bad = new List<ReplaceProgramScheduleItem> { MakeItem(0, PlayoutMode.One, "x", id: 999999) };
|
||||
Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>> result = await handler.Handle(
|
||||
new ReplaceProgramScheduleItems(scheduleId, bad, ExpectedVersion: Some(versionBefore + 1)),
|
||||
CancellationToken.None);
|
||||
|
||||
LeftOrThrow(result).GetType().ShouldBe(typeof(PreconditionFailedError));
|
||||
await AssertUnchanged(scheduleId, idA, versionBefore);
|
||||
}
|
||||
|
||||
private static void AddFillGroup(TvContext ctx, int itemId, int seed) =>
|
||||
ctx.Add(new PlayoutScheduleItemFillGroupIndex
|
||||
{
|
||||
PlayoutId = 1,
|
||||
ProgramScheduleItemId = itemId,
|
||||
EnumeratorState = new CollectionEnumeratorState { Seed = seed, Index = 0 }
|
||||
});
|
||||
|
||||
private static BaseError LeftOrThrow(Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>> result) =>
|
||||
result.Match(Left: e => e, Right: _ => throw new ShouldAssertException("expected a Left error"));
|
||||
|
||||
// read the single seeded item's id and the schedule's current version
|
||||
private async Task<(int Id, int Version)> ReadSingle(int scheduleId)
|
||||
{
|
||||
await using TvContext ctx = _db.CreateContext();
|
||||
int id = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).Select(i => i.Id).SingleAsync();
|
||||
int version = await ctx.ProgramSchedules
|
||||
.Where(s => s.Id == scheduleId).Select(s => s.Version).SingleAsync();
|
||||
return (id, version);
|
||||
}
|
||||
|
||||
// a rejected replace must leave the schedule exactly as it was (no partial write, no version bump)
|
||||
private async Task AssertUnchanged(int scheduleId, int expectedItemId, int expectedVersion)
|
||||
{
|
||||
await using TvContext ctx = _db.CreateContext();
|
||||
List<int> ids = await ctx.ProgramScheduleItems
|
||||
.Where(i => i.ProgramScheduleId == scheduleId).Select(i => i.Id).ToListAsync();
|
||||
ids.ShouldBe([expectedItemId]);
|
||||
int version = await ctx.ProgramSchedules
|
||||
.Where(s => s.Id == scheduleId).Select(s => s.Version).SingleAsync();
|
||||
version.ShouldBe(expectedVersion);
|
||||
}
|
||||
|
||||
private async Task<int> SeedSchedule()
|
||||
{
|
||||
await using TvContext ctx = _db.CreateContext();
|
||||
@@ -249,8 +510,9 @@ public class ReplaceProgramScheduleItemsReconcileTests
|
||||
return schedule.Id;
|
||||
}
|
||||
|
||||
private static ReplaceProgramScheduleItem MakeItem(int index, PlayoutMode mode, string searchQuery) =>
|
||||
private static ReplaceProgramScheduleItem MakeItem(int index, PlayoutMode mode, string searchQuery, int? id = null) =>
|
||||
new(
|
||||
id,
|
||||
index,
|
||||
StartType.Dynamic,
|
||||
StartTime: null,
|
||||
|
||||
Reference in New Issue
Block a user