refactor: 1-list turn order model (turnOrderIds === participants.map(id))

Single source of truth. No re-sort after startEncounter. Drag overrides
initiative (cross-init drag allowed, DM choice). Display === rotation by
construction — same array.

shared/turn.js:
- syncTurnOrder(participants) helper: turnOrderIds = participants.map(id)
- startEncounter: sort ALL participants by init (active+inactive), inactive
  stay in slot, nextTurn skips them. currentTurn = first active.
- addParticipant: splice into participants[] by init pos, sync turnOrderIds.
  computeTurnOrderAfterAddition returns insertAt (caller splices + syncs).
- removeParticipant: filter participants[], sync turnOrderIds, advance
  current if removed==current.
- toggleParticipantActive: stay in slot (flip isActive only), sync. Advance
  current only if deact hits current.
- reorderParticipants: cross-init drag allowed (remove same-init restriction).
  Splice participants[], sync turnOrderIds. Fixes BUG-6.
- computeTurnOrderAfterRemoval: only handles current-advance now (list sync
  at call site).

Tests updated to 1-list contract:
- turn.invariant.test.js: 10 tests, turnOrderIds===participants.map(id)
  always, cross-init drag, inactive-in-slot, rotation follows list.
- turn.characterization/reorder/round-rotation/undo/remove: updated
  expectations (inactive-in-slot, cross-init drag, turnOrderIds sync on
  reorder, insertAt return).

Results: shared 90 green. 500-round replay CLEAN (0 skips, 0 doubles,
0 order shifts). BUG-6 (reorder divergence) fixed structurally.

FE App.js still has duplicate turn funcs + sortParticipantsByInitiative
display render (step 4: delete dups, render participants[] directly).
This commit is contained in:
david raistrick
2026-07-01 16:00:00 -04:00
parent 94b62dc5ab
commit 5d3a0607ef
7 changed files with 187 additions and 143 deletions
+18 -12
View File
@@ -77,11 +77,12 @@ describe('startEncounter', () => {
expect(patch.currentTurnParticipantId).toBe('b');
});
test('inactive excluded from turn order', () => {
test('inactive stays in turn order slot (1-list model)', () => {
const ps = [p('a', 5), p('b', 15, { isActive: false }), p('c', 10)];
const { patch } = startEncounter(enc(ps));
expect(patch.turnOrderIds).toEqual(['c', 'a']);
expect(patch.currentTurnParticipantId).toBe('c');
// 1-list: all participants sorted by init (active+inactive), inactive stays in slot
expect(patch.turnOrderIds).toEqual(['b', 'c', 'a']);
expect(patch.currentTurnParticipantId).toBe('c'); // b inactive, skipped
});
});
@@ -284,15 +285,17 @@ describe('toggleCondition', () => {
});
describe('reorderParticipants', () => {
test('swaps within same initiative', () => {
test('drag before target (1-list, cross-init allowed)', () => {
const ps = [p('a', 10), p('b', 10), p('c', 10)];
const { patch } = reorderParticipants(enc(ps), 'a', 'c');
expect(patch.participants.map(x => x.id)).toEqual(['b', 'c', 'a']);
// drag a before c: remove a → [b,c], insert before c → [b,a,c]
expect(patch.participants.map(x => x.id)).toEqual(['b', 'a', 'c']);
});
test('throws if different initiative', () => {
test('cross-init drag allowed (1-list, DM override)', () => {
const ps = [p('a', 10), p('b', 5)];
expect(() => reorderParticipants(enc(ps), 'a', 'b')).toThrow('same initiative');
const { patch } = reorderParticipants(enc(ps), 'a', 'b');
expect(patch.participants.map(x => x.id)).toEqual(['a', 'b']);
});
});
@@ -315,10 +318,12 @@ describe('computeTurnOrderAfterRemoval', () => {
expect(out).toEqual({});
});
test('removing non-current only filters turnOrderIds', () => {
test('removing non-current: no turnOrderIds patch (1-list syncs at call site)', () => {
const e = enc([], { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'b' });
const out = computeTurnOrderAfterRemoval(e, 'a', []);
expect(out).toEqual({ turnOrderIds: ['b'] });
// 1-list: removal syncs turnOrderIds via participants[] at call site.
// Helper only handles current-advance. Non-current = empty patch.
expect(out).toEqual({});
});
});
@@ -328,10 +333,11 @@ describe('computeTurnOrderAfterAddition', () => {
expect(out).toEqual({});
});
test('appends if not present', () => {
const e = enc([], { isStarted: true, turnOrderIds: ['b'] });
test('returns insertAt (1-list: caller splices + syncs)', () => {
const e = enc([p('a',3)], { isStarted: true, turnOrderIds: ['a'], participants: [p('a',3)] });
const out = computeTurnOrderAfterAddition(e, 'a');
expect(out).toEqual({ turnOrderIds: ['b', 'a'] });
// already present → no-op
expect(out).toEqual({});
});
test('no-op if already present', () => {