From 02a493e95edb679a0fe4cff7c38a458adcd60ace Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 11 Jul 2026 21:43:52 +0200 Subject: [PATCH 1/4] feat(#259): id-based reconcile for schedule-items replace (backend + DTO) Add optional `int? Id` to ScheduleItemRequest/ReplaceProgramScheduleItem so a client can round-trip each existing item's server id. When ids are present, ReplaceProgramScheduleItemsHandler reconciles by id (not array position), so an item's persisted fill-group/shuffle state (PlayoutScheduleItemFillGroupIndex, FK OnDelete Cascade) follows the logical item across reorders/inserts instead of being inherited by whatever previously occupied its new slot (#259, split from #252/#253). A fully id-less payload keeps the verbatim positional fallback. Guards (inside PersistItems, after CheckVersion so 412 precedes 422): duplicate id -> 422; id not in this schedule -> 422 (a stale id under Phase-1 force-write is a live lost-update signal, not a new item). Index stays array-position derived. Regenerated v1.json + TS client. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Commands/ReplaceProgramScheduleItems.cs | 1 + .../ReplaceProgramScheduleItemsHandler.cs | 143 ++++++++++++++---- .../ProgramScheduleHandlerTests.cs | 1 + .../ScheduleItemTptIntegrationTests.cs | 1 + .../Api/Requests/ScheduleItemRequest.cs | 10 ++ ErsatzTV/wwwroot/openapi/v1.json | 8 + web/src/api/generated/v1.d.ts | 1 + 7 files changed, 133 insertions(+), 32 deletions(-) diff --git a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItems.cs b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItems.cs index 9a9d4568c..3d75257f2 100644 --- a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItems.cs +++ b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItems.cs @@ -5,6 +5,7 @@ using ErsatzTV.Core.Scheduling; namespace ErsatzTV.Application.ProgramSchedules; public record ReplaceProgramScheduleItem( + int? Id, int Index, StartType StartType, TimeSpan? StartTime, diff --git a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs index 43a626177..02e3fba8f 100644 --- a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs +++ b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs @@ -48,16 +48,22 @@ public class ReplaceProgramScheduleItemsHandler( ProgramSchedule programSchedule, CancellationToken cancellationToken) { - // Positional in-place reconcile (rather than delete-and-reinsert): a schedule item owns the - // persisted fill-group/shuffle enumerator state via PlayoutScheduleItemFillGroupIndex, whose + // In-place reconcile (rather than delete-and-reinsert): a schedule item owns persisted + // fill-group/shuffle enumerator state via PlayoutScheduleItemFillGroupIndex, whose // ProgramScheduleItemId FK is OnDelete(Cascade). Removing and re-inserting every item — as the // original handler did on every save, including a no-op PUT-back — cascade-deleted that state for - // all playouts using the schedule (#252). Reusing the existing item row for the same-typed slot - // keeps its id, so the cascade never fires and progression survives. The request DTO carries no - // stable item id, so position is the only key available here; true content-aware stable identity - // is deferred to the shared concurrency/round-trip contract in #253. + // all playouts using the schedule (#252). Reusing the existing item row keeps its id, so the cascade + // never fires and progression survives. + // + // Two reconcile modes (#259): + // * id-based (preferred) — when the client round-trips each existing item's server id, map request + // items to existing rows BY id, so per-child state follows the LOGICAL item across reorders and + // mid-list inserts rather than staying with whatever previously occupied a slot. + // * positional (legacy fallback) — a fully id-less payload keeps the original by-array-position + // reconcile. This preserves today's misattribution on legacy reorders; it is temporary and + // retires with the §7a Phase-2 If-Match flip. + // Index (ordering) is always derived from array position; identity (id) is a separate axis. var orderedItems = request.Items.OrderBy(i => i.Index).ToList(); - List existingItems = programSchedule.Items.OrderBy(i => i.Index).ToList(); // load the watermark/graphics join rows for the existing items so they can be rebuilt in place await dbContext.Entry(programSchedule) @@ -67,39 +73,112 @@ public class ReplaceProgramScheduleItemsHandler( .Include(i => i.ProgramScheduleItemGraphicsElements) .LoadAsync(cancellationToken); - int commonCount = Math.Min(existingItems.Count, orderedItems.Count); - for (var i = 0; i < commonCount; i++) + List existingItems = programSchedule.Items.ToList(); + List requestIds = orderedItems.Where(i => i.Id.HasValue).Select(i => i.Id.Value).ToList(); + + if (requestIds.Count == 0) { - ProgramScheduleItem existing = existingItems[i]; - ProgramScheduleItem built = BuildItem(programSchedule, i, orderedItems[i]); - if (existing.GetType() == built.GetType()) + // ---- positional fallback (verbatim pre-#259 behavior) ---- + List orderedExisting = existingItems.OrderBy(i => i.Index).ToList(); + int commonCount = Math.Min(orderedExisting.Count, orderedItems.Count); + for (var i = 0; i < commonCount; i++) { - // same TPT subtype: copy all scalar values in place (BuildItem is the single source of - // item construction, so no field is silently dropped) and rebuild the join rows, keeping - // the item's id — and with it the fill-group index that would otherwise cascade away. - built.Id = existing.Id; - dbContext.Entry(existing).CurrentValues.SetValues(built); - RebuildChildren(existing, orderedItems[i]); + ProgramScheduleItem existing = orderedExisting[i]; + ProgramScheduleItem built = BuildItem(programSchedule, i, orderedItems[i]); + if (existing.GetType() == built.GetType()) + { + built.Id = existing.Id; + dbContext.Entry(existing).CurrentValues.SetValues(built); + RebuildChildren(existing, orderedItems[i]); + } + else + { + dbContext.Remove(existing); + programSchedule.Items.Add(built); + } } - else + + for (int i = commonCount; i < orderedExisting.Count; i++) { - // EF can't change a TPT row's type in place; this slot must be replaced (its fill-group - // index resets, which is acceptable — the item fundamentally changed). - dbContext.Remove(existing); - programSchedule.Items.Add(built); + dbContext.Remove(orderedExisting[i]); + } + + for (int i = commonCount; i < orderedItems.Count; i++) + { + programSchedule.Items.Add(BuildItem(programSchedule, i, orderedItems[i])); } } - - // remove surplus existing items - for (int i = commonCount; i < existingItems.Count; i++) + else { - dbContext.Remove(existingItems[i]); - } + // ---- id-based reconcile ---- + // Guards run HERE (inside PersistItems, after CheckVersion in Handle) so a client that is BOTH + // version-stale and id-stale gets 412 (reload signal) — not 422, which reads as a payload bug + // (§7c). Both guards persist nothing (plain BaseError → 422). - // add surplus incoming items - for (int i = commonCount; i < orderedItems.Count; i++) - { - programSchedule.Items.Add(BuildItem(programSchedule, i, orderedItems[i])); + // 7b: a duplicated id would map two request items onto one row (SetValues twice, last-writer-wins, + // one item's config silently lost). Always a client bug. + int? duplicateId = requestIds.GroupBy(id => id).Where(g => g.Count() > 1).Select(g => (int?)g.Key) + .FirstOrDefault(); + if (duplicateId.HasValue) + { + return new BaseError($"Schedule item id {duplicateId.Value} appears more than once in the request."); + } + + // 7a: an id not belonging to THIS schedule is a stale/foreign identity. Under Phase-1 (a missing + // If-Match force-writes) it is a live lost-update signal; inserting-as-new would silently duplicate + // the item and return a different id than the client sent. Reject loudly rather than mask it. + var existingIds = existingItems.Select(e => e.Id).ToHashSet(); + foreach (int id in requestIds) + { + if (!existingIds.Contains(id)) + { + return new BaseError($"Schedule item id {id} does not belong to this schedule."); + } + } + + // delete pass: existing rows the request no longer references (their fill-group state cascades — + // correct, the logical item is gone) + var referencedIds = requestIds.ToHashSet(); + foreach (ProgramScheduleItem existing in existingItems) + { + if (!referencedIds.Contains(existing.Id)) + { + dbContext.Remove(existing); + } + } + + // match/insert pass in array order (Index = i) + var existingById = existingItems.ToDictionary(e => e.Id); + for (var i = 0; i < orderedItems.Count; i++) + { + ReplaceProgramScheduleItem requestItem = orderedItems[i]; + ProgramScheduleItem built = BuildItem(programSchedule, i, requestItem); + if (requestItem.Id.HasValue && existingById.TryGetValue(requestItem.Id.Value, out ProgramScheduleItem existing)) + { + if (existing.GetType() == built.GetType()) + { + // same TPT subtype: copy all scalars in place (BuildItem is the single source of item + // construction, so no field is dropped) and rebuild join rows, keeping the id — and + // with it the fill-group index that would otherwise cascade away. State follows the + // logical item regardless of its new position. + built.Id = existing.Id; + dbContext.Entry(existing).CurrentValues.SetValues(built); + RebuildChildren(existing, requestItem); + } + else + { + // EF can't retype a TPT row in place; replace it. Fill-group state resets and the + // response returns a NEW id (the item fundamentally changed — clients re-sync from it). + dbContext.Remove(existing); + programSchedule.Items.Add(built); + } + } + else + { + // id-less request item → new (unknown ids were already rejected above) + programSchedule.Items.Add(built); + } + } } // Unconditional bump: this handler frequently saves with only CHILD changes and no root-scalar diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ProgramScheduleHandlerTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ProgramScheduleHandlerTests.cs index f9224b8f6..277151938 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ProgramScheduleHandlerTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ProgramScheduleHandlerTests.cs @@ -172,6 +172,7 @@ public class ProgramScheduleHandlerTests { AddProgramScheduleItem add = MakeAdd(1, CollectionType.SearchQuery); return new ReplaceProgramScheduleItem( + null, index, add.StartType, add.StartTime, diff --git a/ErsatzTV.Tests/Integration/ScheduleItemTptIntegrationTests.cs b/ErsatzTV.Tests/Integration/ScheduleItemTptIntegrationTests.cs index afd7213c2..eea4e1e3b 100644 --- a/ErsatzTV.Tests/Integration/ScheduleItemTptIntegrationTests.cs +++ b/ErsatzTV.Tests/Integration/ScheduleItemTptIntegrationTests.cs @@ -143,6 +143,7 @@ public class ScheduleItemTptIntegrationTests { AddProgramScheduleItem add = MakeAdd(1, playoutMode); return new ReplaceProgramScheduleItem( + null, index, add.StartType, add.StartTime, diff --git a/ErsatzTV/Controllers/Api/Requests/ScheduleItemRequest.cs b/ErsatzTV/Controllers/Api/Requests/ScheduleItemRequest.cs index 9a3794106..c1e22c54f 100644 --- a/ErsatzTV/Controllers/Api/Requests/ScheduleItemRequest.cs +++ b/ErsatzTV/Controllers/Api/Requests/ScheduleItemRequest.cs @@ -4,7 +4,14 @@ using ErsatzTV.Core.Scheduling; namespace ErsatzTV.Controllers.Api.Requests; +/// +/// Server-assigned identity of an existing schedule item, as returned by GET /api/schedules/{id}/items. +/// Omit (or send null / 0) for a new item. On a replace, the item carrying this id keeps its persisted +/// fill-group/shuffle progression even when moved to a different position. Unknown or duplicated ids are +/// rejected with 422 — never fabricate an id. +/// public record ScheduleItemRequest( + int? Id, StartType StartType, TimeSpan? StartTime, FixedStartTimeBehavior? FixedStartTimeBehavior, @@ -86,6 +93,9 @@ public record ScheduleItemRequest( public ReplaceProgramScheduleItem ToReplaceCommand(int index) => new( + // normalize the two-state client value (null / 0 / positive) to the handler's null-or-real + // contract so 0-defaulting clients read as "new item" rather than "existing item 0" + Id is > 0 ? Id : null, index, StartType, StartTime, diff --git a/ErsatzTV/wwwroot/openapi/v1.json b/ErsatzTV/wwwroot/openapi/v1.json index 89b9f5d21..3b4d11c46 100644 --- a/ErsatzTV/wwwroot/openapi/v1.json +++ b/ErsatzTV/wwwroot/openapi/v1.json @@ -24363,6 +24363,7 @@ }, "ScheduleItemRequest": { "required": [ + "id", "startType", "startTime", "fixedStartTimeBehavior", @@ -24403,6 +24404,13 @@ ], "type": "object", "properties": { + "id": { + "type": [ + "null", + "integer" + ], + "format": "int32" + }, "startType": { "$ref": "#/components/schemas/StartType" }, diff --git a/web/src/api/generated/v1.d.ts b/web/src/api/generated/v1.d.ts index 2a1ed580b..d51908279 100644 --- a/web/src/api/generated/v1.d.ts +++ b/web/src/api/generated/v1.d.ts @@ -1279,6 +1279,7 @@ export interface components { "deepScan"?: boolean; }; "ScheduleItemRequest": { + "id": null | number; "startType": components["schemas"]["StartType"]; "startTime": null | string; "fixedStartTimeBehavior": null | components["schemas"]["FixedStartTimeBehavior"]; -- 2.47.3 From b7428a9b952b84cd4e08681dc1d00ac6711cb3b8 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 11 Jul 2026 21:51:24 +0200 Subject: [PATCH 2/4] feat(#259): SPA round-trips schedule item server id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backend (already on this branch) added an optional int? Id to ScheduleItemRequest so the server reconciles PUT /api/schedules/{id}/items rows by identity instead of by array position. The SPA previously discarded the server id on load (only a client-local _key survived) and never sent one back, so a reorder could misattribute fill-group/shuffle state onto the wrong persisted row. - itemRules.ts: fromResponse now captures the response item's id onto the draft; normalizeForSave emits it back unchanged. newDraftItem and copyDraftItem explicitly set id: null (a brand-new/copied row was never persisted under an id, and copyDraftItem must not duplicate the source's id onto a second row). - scheduleItem.ts (Add-to-schedule dialog, POST path): id: null for the same reason — it always creates a new row. - SchedulesScreen.tsx save(): the PUT-response re-seed already existed (fromResponse over the response array) but now carries ids through. This matters because a subtype/playout-mode switch can be a delete+insert server-side, so the response id for that row can differ from what was submitted — a second save must use the *response's* id or the server 422s it as unknown. Added a comment documenting this. - schedules.ts: replaced the stale "server reuses same-typed rows by position" comment with the current id-based reconcile contract. - Added/updated tests in itemRules.test.ts, SchedulesScreen.test.tsx, and AddToScheduleDialog.test.tsx covering id round-tripping, the null-id-for-new/copied-item cases, and a second-save-reuses-the- response-id regression test. Verified: npm run lint, tsc -b --noEmit, npm run build, and npm test (680/680) all pass. Co-Authored-By: Claude Opus 4.8 (1M context) --- web/src/api/schedules.ts | 12 +++++--- .../media/addTo/AddToScheduleDialog.test.tsx | 1 + web/src/media/addTo/scheduleItem.ts | 1 + web/src/schedules/itemRules.test.ts | 15 ++++++++++ web/src/schedules/itemRules.ts | 12 ++++++++ web/src/screens/SchedulesScreen.test.tsx | 29 +++++++++++++++++-- web/src/screens/SchedulesScreen.tsx | 5 ++++ 7 files changed, 69 insertions(+), 6 deletions(-) diff --git a/web/src/api/schedules.ts b/web/src/api/schedules.ts index b87ba5b28..aa85d14ce 100644 --- a/web/src/api/schedules.ts +++ b/web/src/api/schedules.ts @@ -57,10 +57,14 @@ export function addScheduleItem(scheduleId: number, body: ScheduleItemRequest): return request(`/api/schedules/${scheduleId}/items`, { body, method: 'POST' }); } -// Positional in-place reconcile: the server reuses same-typed item rows (keeping fill-group state) and -// triggers playout rebuilds. The editor batches all local draft edits into this single call. Pass the -// last-seen ETag as `If-Match` to reject a stale overwrite with 412; the resolved value carries the new -// ETag for a subsequent save (issue #253). See docs/decisions.md. +// Id-based reconcile (#259): each item's `id` (round-tripped from the GET response by the editor; +// `null` for a new/copied item never persisted) tells the server which existing row to update in place +// vs. insert, so fill-group/shuffle state on an existing row survives a reorder. The editor batches all +// local draft edits into this single call. Pass the last-seen ETag as `If-Match` to reject a stale +// overwrite with 412; the resolved value carries the new ETag for a subsequent save (issue #253). See +// docs/decisions.md. Note: switching an item's playout-mode/subtype can be a delete+insert server-side, +// so the response's ids may differ from what was sent — the editor must re-seed its draft from the PUT +// response (see SchedulesScreen.tsx `save()`), not reuse the ids it submitted. export function replaceScheduleItems( scheduleId: number, body: ReplaceScheduleItemsRequest, diff --git a/web/src/media/addTo/AddToScheduleDialog.test.tsx b/web/src/media/addTo/AddToScheduleDialog.test.tsx index 5d318237c..a502eeec8 100644 --- a/web/src/media/addTo/AddToScheduleDialog.test.tsx +++ b/web/src/media/addTo/AddToScheduleDialog.test.tsx @@ -14,6 +14,7 @@ const schedules = [ // Mirrors AddProgramScheduleItem.ForMediaItem defaults (AddProgramScheduleItem.cs:48-87), with the // mapped collectionType + mediaItemId for a TelevisionShow item. const expectedShowPayload = { + id: null, startType: 'Dynamic', startTime: null, fixedStartTimeBehavior: null, diff --git a/web/src/media/addTo/scheduleItem.ts b/web/src/media/addTo/scheduleItem.ts index 2ea111596..c18780e42 100644 --- a/web/src/media/addTo/scheduleItem.ts +++ b/web/src/media/addTo/scheduleItem.ts @@ -34,6 +34,7 @@ export function scheduleItemRequestForMediaItem( mediaItemId: number ): ScheduleItemRequest { return { + id: null, startType: 'Dynamic', startTime: null, fixedStartTimeBehavior: null, diff --git a/web/src/schedules/itemRules.test.ts b/web/src/schedules/itemRules.test.ts index 6f09a75fa..ad92709da 100644 --- a/web/src/schedules/itemRules.test.ts +++ b/web/src/schedules/itemRules.test.ts @@ -349,10 +349,14 @@ describe('factories', () => { graphicsElementIds: [] }); expect(item._key).toMatch(/^draft-/); + // A never-persisted item must not carry a server id (#259) — the save projection relies on this + // to tell the server it's an insert. + expect(item.id).toBeNull(); }); it('copyDraftItem deep-copies multi/smart/rerun refs and gives a new key + array identity', () => { const original = draft({ + id: 99, multiCollectionId: 3, multiCollectionName: 'M', smartCollectionId: 4, @@ -372,6 +376,9 @@ describe('factories', () => { expect(copy.watermarkIds).toEqual([1, 2]); expect(copy.watermarkIds).not.toBe(original.watermarkIds); expect(copy.graphicsElementIds).not.toBe(original.graphicsElementIds); + // A copy is a new row that was never persisted under its own id — it must not carry the source + // item's server id (#259), or a save would submit the same id twice. + expect(copy.id).toBeNull(); }); }); @@ -438,6 +445,9 @@ describe('fromResponse', () => { durationEstimate: '01:00:00' }; const item = fromResponse(model); + // The server id must round-trip onto the draft (#259) so a subsequent save reconciles this row by + // identity instead of by array position. + expect(item.id).toBe(42); expect(item.smartCollectionId).toBe(7); expect(item.smartCollectionName).toBe('Smarts'); expect(item.watermarkIds).toEqual([1, 2]); @@ -500,6 +510,11 @@ describe('normalizeForSave', () => { expect('collectionName' in req).toBe(false); expect('name' in req).toBe(false); }); + + it('passes the draft id through unchanged: null for a new item, the server id for a loaded one (#259)', () => { + expect(normalizeForSave(draft()).id).toBeNull(); + expect(normalizeForSave(draft({ id: 42 })).id).toBe(42); + }); }); describe('validation', () => { diff --git a/web/src/schedules/itemRules.ts b/web/src/schedules/itemRules.ts index e77fd4199..be2081d5f 100644 --- a/web/src/schedules/itemRules.ts +++ b/web/src/schedules/itemRules.ts @@ -253,6 +253,9 @@ export function applyPlayoutModeChange(item: DraftItem, newMode: PlayoutMode): D export function newDraftItem(): DraftItem { return { _key: nextDraftKey(), + // Never round-tripped from the server — the save projection must emit `id: null` for this item so + // the server treats it as an insert rather than colliding with (or misattributing) an existing row. + id: null, startType: 'Dynamic', startTime: null, fixedStartTimeBehavior: null, @@ -308,6 +311,9 @@ export function copyDraftItem(item: DraftItem): DraftItem { return { ...item, _key: nextDraftKey(), + // The copy is a new, never-persisted row — it must not carry the source item's server id, or the + // save projection would submit the same id twice (server would treat the copy as the same row). + id: null, watermarkIds: [...(item.watermarkIds ?? [])], graphicsElementIds: [...(item.graphicsElementIds ?? [])] }; @@ -318,6 +324,9 @@ export function copyDraftItem(item: DraftItem): DraftItem { export function fromResponse(model: ScheduleItem): DraftItem { return { _key: nextDraftKey(), + // Round-trip the server id so a subsequent save reconciles this row by identity (#259) rather + // than by array position. + id: model.id, startType: model.startType, startTime: model.startTime, fixedStartTimeBehavior: model.fixedStartTimeBehavior, @@ -379,6 +388,9 @@ export function normalizeForSave(item: DraftItem): ScheduleItemRequest { const fillWithGroupMode = fillWithGroupModeEligible(item) ? item.fillWithGroupMode : 'None'; return { + // null for a draft item never round-tripped from the server (new / copied) — the server treats a + // null id as an insert. A non-null id reconciles the existing row by identity (#259). + id: item.id, startType: item.startType, startTime: isFixed ? item.startTime : null, fixedStartTimeBehavior: isFixed ? item.fixedStartTimeBehavior : null, diff --git a/web/src/screens/SchedulesScreen.test.tsx b/web/src/screens/SchedulesScreen.test.tsx index 30aeff3b0..d6147890b 100644 --- a/web/src/screens/SchedulesScreen.test.tsx +++ b/web/src/screens/SchedulesScreen.test.tsx @@ -203,6 +203,10 @@ describe('SchedulesScreen — draft mutations issue a single PUT on Save', () => // exactly one PUT, and no POST/DELETE to items endpoints expect(handle.requests.filter((r) => r.method === 'PUT' && r.url === '/api/schedules/1/items')).toHaveLength(1); expect(handle.requests.filter((r) => r.url.includes('/items/'))).toHaveLength(0); + // #259: the pre-existing item (loaded via GET, id 10) round-trips its server id; the brand-new item + // (added in the editor, never persisted) sends a null id so the server treats it as an insert. + expect(items[0].id).toBe(10); + expect(items[1].id).toBeNull(); }); it('edit a field → PUT reflects it', async () => { @@ -214,9 +218,9 @@ describe('SchedulesScreen — draft mutations issue a single PUT on Save', () => expect(lastPut(handle)[0].customTitle).toBe('Nightly'); }); - it('copy item deep-copies references into the PUT payload', async () => { + it('copy item deep-copies references into the PUT payload, but the copy sends a null id', async () => { const handle = await renderReady({ - items: [responseItem({ collectionType: 'MultiCollection', collectionId: null, multiCollectionId: 8, multiCollectionName: 'Multi', playbackOrder: 'Shuffle', name: 'Multi' })] + items: [responseItem({ id: 10, collectionType: 'MultiCollection', collectionId: null, multiCollectionId: 8, multiCollectionName: 'Multi', playbackOrder: 'Shuffle', name: 'Multi' })] }); fireEvent.click(screen.getByRole('button', { name: /Copy Multi/ })); fireEvent.click(screen.getByRole('button', { name: /^Save$/ })); @@ -225,6 +229,27 @@ describe('SchedulesScreen — draft mutations issue a single PUT on Save', () => expect(items).toHaveLength(2); expect(items[0].multiCollectionId).toBe(8); expect(items[1].multiCollectionId).toBe(8); + // The original round-trips its server id; the copy is a new row that was never persisted under its + // own id, so it must not send the original's id (#259) — that would collide with it server-side. + expect(items[0].id).toBe(10); + expect(items[1].id).toBeNull(); + }); + + it('re-seeds ids from the PUT response so a second save reuses the fresh ids (#259)', async () => { + const handle = await renderReady(); + const title = screen.getByLabelText('Custom title') as HTMLInputElement; + fireEvent.change(title, { target: { value: 'First edit' } }); + fireEvent.click(screen.getByRole('button', { name: /^Save$/ })); + await waitFor(() => expect(handle.requests.filter((r) => r.method === 'PUT').length).toBe(1)); + // The mock echoes the submitted item back with a new id (500 + index) — simulating a server-side + // delete+insert reconcile. The editor must adopt that id, not keep serving the id it submitted. + const titleAfterSave = screen.getByLabelText('Custom title') as HTMLInputElement; + fireEvent.change(titleAfterSave, { target: { value: 'Second edit' } }); + fireEvent.click(screen.getByRole('button', { name: /^Save$/ })); + await waitFor(() => expect(handle.requests.filter((r) => r.method === 'PUT').length).toBe(2)); + const secondPut = lastPut(handle); + expect(secondPut[0].id).toBe(500); + expect(secondPut[0].customTitle).toBe('Second edit'); }); it('remove item → PUT omits it', async () => { diff --git a/web/src/screens/SchedulesScreen.tsx b/web/src/screens/SchedulesScreen.tsx index 797ddc545..5272857db 100644 --- a/web/src/screens/SchedulesScreen.tsx +++ b/web/src/screens/SchedulesScreen.tsx @@ -334,6 +334,11 @@ export function SchedulesScreen() { // Load-bearing: a same-tab second save must use the ETag this write produced, or it would 412 // against its own change (#253). etagRef.current = etag; + // Also load-bearing for #259: re-seed the draft's ids from the PUT response, not the ids that + // were submitted. A subtype/playout-mode switch can be a delete+insert server-side, so the + // response id for that row can differ from what was sent; fromResponse captures response.id + // onto each draft, so a subsequent save reconciles by the now-current id instead of a stale one + // (which the server would 422 as unknown). const drafts = [...response].sort((a, b) => a.index - b.index).map(fromResponse); baselineRef.current = drafts; setItems(drafts); -- 2.47.3 From 162b334e5da0da083bfb73754c98aee333c8b193 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 11 Jul 2026 21:53:50 +0200 Subject: [PATCH 3/4] =?UTF-8?q?test(#259):=20id-based=20reconcile=20matrix?= =?UTF-8?q?=20+=20docs=20(api-conventions=20=C2=A77c,=20decisions)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- ...ramScheduleItemsHandlerConcurrencyTests.cs | 1 + ...placeProgramScheduleItemsReconcileTests.cs | 264 +++++++++++++++++- .../ScheduleItemResponseRoundTripTests.cs | 2 + .../ScheduleItemWriteProjectionTests.cs | 2 +- .../Controllers/ScheduleControllerTests.cs | 1 + docs/api-conventions.md | 32 +++ docs/decisions.md | 24 ++ 7 files changed, 324 insertions(+), 2 deletions(-) diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsHandlerConcurrencyTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsHandlerConcurrencyTests.cs index d665cdf3b..bd6625a85 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsHandlerConcurrencyTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsHandlerConcurrencyTests.cs @@ -190,6 +190,7 @@ public class ReplaceProgramScheduleItemsHandlerConcurrencyTests private static ReplaceProgramScheduleItem MakeItem(int index, PlayoutMode mode, string searchQuery) => new( + null, index, StartType.Dynamic, StartTime: null, diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs index d73eb0698..1a0ba42f1 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs @@ -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 + { + 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 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 + { + 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 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 groups = await ctx.Set() + .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 + { + 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 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 + { + 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 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 groups = await ctx.Set() + .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 + { + 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 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 { 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 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() + .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 { MakeItem(0, PlayoutMode.One, "x", id: 999999) }; + Either> 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 + { + MakeItem(0, PlayoutMode.One, "a", id: idA), + MakeItem(1, PlayoutMode.One, "a-again", id: idA) + }; + Either> 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 { MakeItem(0, PlayoutMode.One, "x", id: 999999) }; + Either> 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> 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 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 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, diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemResponseRoundTripTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemResponseRoundTripTests.cs index edb5c9aad..0d34bd88d 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemResponseRoundTripTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemResponseRoundTripTests.cs @@ -256,6 +256,7 @@ public class ScheduleItemResponseRoundTripTests private static ReplaceProgramScheduleItem MakeReplace(int index, PlayoutMode playoutMode, CollectionType collectionType) => new( + null, index, StartType.Dynamic, StartTime: null, @@ -299,6 +300,7 @@ public class ScheduleItemResponseRoundTripTests // response DTO (the SPA does this same field-for-field copy in TypeScript before a PUT). private static ReplaceProgramScheduleItem ToReplaceCommand(ScheduleItemResponseModel r, int index) => new( + r.Id, index, r.StartType, r.StartTime, diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemWriteProjectionTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemWriteProjectionTests.cs index 6602fc198..bf8fbb2d4 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemWriteProjectionTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ScheduleItemWriteProjectionTests.cs @@ -139,7 +139,7 @@ public class ScheduleItemWriteProjectionTests private static ReplaceProgramScheduleItem ItemWithGraphics(int index) => new( - index, StartType.Dynamic, StartTime: null, FixedStartTimeBehavior: null, PlayoutMode.One, + null, index, StartType.Dynamic, StartTime: null, FixedStartTimeBehavior: null, PlayoutMode.One, CollectionType.Collection, CollectionId: 1, MultiCollectionId: null, SmartCollectionId: null, RerunCollectionId: null, MediaItemId: null, PlaylistId: null, SearchTitle: null, SearchQuery: null, PlaybackOrder: PlaybackOrder.Shuffle, MarathonGroupBy: MarathonGroupBy.None, diff --git a/ErsatzTV.Tests/Controllers/ScheduleControllerTests.cs b/ErsatzTV.Tests/Controllers/ScheduleControllerTests.cs index a0494db99..d4362da7c 100644 --- a/ErsatzTV.Tests/Controllers/ScheduleControllerTests.cs +++ b/ErsatzTV.Tests/Controllers/ScheduleControllerTests.cs @@ -403,6 +403,7 @@ public class ScheduleControllerTests private static ScheduleItemRequest MakeItemRequest(PlayoutMode playoutMode) => new( + Id: null, StartType.Dynamic, StartTime: null, FixedStartTimeBehavior: null, diff --git a/docs/api-conventions.md b/docs/api-conventions.md index 3ed6b2514..fb0543a37 100644 --- a/docs/api-conventions.md +++ b/docs/api-conventions.md @@ -424,6 +424,38 @@ making it explicit is optional cleanup, not required. Config handlers that commi `IConfigElementRepository.Upsert` calls are a distinct partial-commit-under-cancellation case not covered by this rule (tracked separately). +## 7c. Stable child identity in replace lists (schedule items) + +§7 indexes replace-list children by **array order**, which is the reconcile *key* for most replace PUTs. +That is correct only when a child row is pure config: reordering merely re-numbers otherwise-interchangeable +rows. **Schedule items are the exception** (issue #259): a schedule item anchors persisted runtime state — +`PlayoutScheduleItemFillGroupIndex` (fill-group / shuffle enumerator progression) FKs the item row with +`OnDelete(Cascade)`. Reconciling those by position makes a **moved** item inherit the state of whatever item +previously occupied its new slot. So `PUT /api/schedules/{id}/items` carries a stable child identity: + +- `ScheduleItemRequest.Id` (`int?`) round-trips each existing item's server id (as returned by the items GET). + **null / absent / 0 ⇒ a new item** (the controller normalizes `0`→null so the handler contract is + two-state). Never fabricate an id. +- When **any** request item carries an id, `ReplaceProgramScheduleItemsHandler` reconciles **by id**: matched + same-subtype rows are updated in place (keeping the id, so the fill-group index never cascades and follows + the logical item across reorders/inserts); a matched row whose TPT subtype changed is delete+insert (state + resets, a **new id is returned** — clients must re-sync from the PUT response); unreferenced existing rows are + deleted; id-less request items are inserted. `Index` is still array-position (ordering is a separate axis + from identity). +- **A fully id-less payload falls back to the verbatim positional reconcile** (legacy clients). This preserves + today's misattribution-on-reorder for such payloads — it is temporary and retires together with the §7a + Phase-2 `If-Match`→428 flip. +- **Guards run inside the handler, after the §7a `CheckVersion`** (so a client that is both version-stale and + id-stale gets **412**, the reload signal, not 422): a duplicate id in one payload → **422**; an id not + belonging to this schedule → **422** (under Phase-1 force-write a stale id is a live lost-update signal, not + a new item — reject rather than silently duplicate). Both persist nothing. + +**Deliberate asymmetry**: the other positional replace handlers (blocks #2, templates #3, deco-templates #4, +playlists #5) do **not** carry a child id — their children are stateless config rows where positional churn is +unobservable (#3/#4 don't even emit a child id on GET). Child ids are added only where a child row anchors +server-side state; positional replace stays the default. A per-endpoint child-id contract can be retrofitted +later without breaking anything (the field stays optional). + ## 8. Known API warts (don't "fix" without discussion — they're deliberate synthesized rows) `GET /api/blocks` and `GET /api/templates` (via `GetAllBlocksHandler` / `GetAllTemplatesHandler` in diff --git a/docs/decisions.md b/docs/decisions.md index d9f37517d..9c4e4017c 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -890,3 +890,27 @@ Also removed the now-dead ersatztv#25 razor-Sonar `S6966;S3267;… Date: Sat, 11 Jul 2026 22:03:33 +0200 Subject: [PATCH 4/4] test(#259): cover id-mode subtype-change (delete+insert) with a same-type sibling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review-caught coverage gap: the existing Subtype_Change test runs the POSITIONAL path (id-less payload). Add a handler-level test for the id-mode branch — one id-matched item changes subtype (One->Duration: delete+insert, new id) while a sibling id-matched item keeps its subtype (Multiple: in place, id + fill-group state preserved) in the same payload. Proves the delete pass + match-pass Remove/Add don't double-handle and the survivor's state is retained. Co-Authored-By: Claude Opus 4.8 (1M context) --- ...placeProgramScheduleItemsReconcileTests.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs index 1a0ba42f1..760fb4f37 100644 --- a/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs +++ b/ErsatzTV.Tests/Application/ProgramSchedules/ReplaceProgramScheduleItemsReconcileTests.cs @@ -461,6 +461,70 @@ public class ReplaceProgramScheduleItemsReconcileTests await AssertUnchanged(scheduleId, idA, versionBefore); } + [Test] + public async Task Subtype_Change_ById_Should_Replace_That_Item_But_Preserve_A_SameType_Sibling() + { + // id-mode counterpart of the positional Subtype_Change test: in ONE payload, one id-matched item + // changes subtype (One -> Duration: delete+insert, new id, state resets) while a sibling id-matched + // item keeps its subtype (Multiple: in-place, id + fill-group state preserved). This is the riskiest + // reconcile branch — it exercises the delete pass + match-pass Remove/Add without double-handling. + int scheduleId = await SeedSchedule(); + var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker); + + var seed = new List + { + MakeItem(0, PlayoutMode.One, "a"), + MakeItem(1, PlayoutMode.Multiple, "b") with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups } + }; + (await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, seed), CancellationToken.None)) + .IsRight.ShouldBeTrue(); + + int idA, idB; + await using (TvContext ctx = _db.CreateContext()) + { + List p = await ctx.ProgramScheduleItems + .Where(i => i.ProgramScheduleId == scheduleId).OrderBy(i => i.Index).ToListAsync(); + idA = p[0].Id; + idB = p[1].Id; + AddFillGroup(ctx, idB, seed: 500); // fill-group state on the same-type sibling + await ctx.SaveChangesAsync(); + } + + // A (idA) changes subtype One -> Duration; B (idB) stays Multiple. + var changed = new List + { + MakeItem(0, PlayoutMode.Duration, "a", id: idA) with + { + PlayoutDuration = TimeSpan.FromMinutes(30), DiscardToFillAttempts = 0 + }, + MakeItem(1, PlayoutMode.Multiple, "b", id: idB) with { FillWithGroupMode = FillWithGroupMode.FillWithShuffledGroups } + }; + (await handler.Handle(new ReplaceProgramScheduleItems(scheduleId, changed), CancellationToken.None)) + .IsRight.ShouldBeTrue(); + + await using (TvContext ctx = _db.CreateContext()) + { + List after = await ctx.ProgramScheduleItems + .Where(i => i.ProgramScheduleId == scheduleId).ToListAsync(); + after.Count.ShouldBe(2); + + // A was replaced: old id gone, a NEW Duration row (distinct id) sits at index 0 + after.ShouldNotContain(i => i.Id == idA); + ProgramScheduleItem duration = after.Single(i => i is ProgramScheduleItemDuration); + duration.Id.ShouldNotBe(idA); + duration.Id.ShouldNotBe(idB); + duration.Index.ShouldBe(0); + + // B was updated in place: same id, still Multiple, its fill-group state intact + ProgramScheduleItem b = after.Single(i => i.Id == idB); + b.ShouldBeOfType(); + b.Index.ShouldBe(1); + PlayoutScheduleItemFillGroupIndex bState = await ctx.Set() + .Include(x => x.EnumeratorState).SingleAsync(g => g.ProgramScheduleItemId == idB); + bState.EnumeratorState.Seed.ShouldBe(500); + } + } + private static void AddFillGroup(TvContext ctx, int itemId, int seed) => ctx.Add(new PlayoutScheduleItemFillGroupIndex { -- 2.47.3