5d3a0607ef
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).
68 lines
2.5 KiB
JavaScript
68 lines
2.5 KiB
JavaScript
// Characterization for reorderParticipants correct usage.
|
|
// replay-combat.js calls it with wrong signature (swallowed by try/catch),
|
|
// so real behavior untested. Lock what it actually does.
|
|
|
|
const shared = require('@ttrpg/shared');
|
|
const { makeParticipant, startEncounter, nextTurn, reorderParticipants } = shared;
|
|
|
|
function p(id, init, extra = {}) {
|
|
return makeParticipant({
|
|
id, name: id, type: 'monster',
|
|
initiative: init, maxHp: 100, currentHp: 100,
|
|
...extra,
|
|
});
|
|
}
|
|
function enc(ps) {
|
|
return { name:'t', participants:ps, isStarted:false, isPaused:false,
|
|
round:0, currentTurnParticipantId:null, turnOrderIds:[] };
|
|
}
|
|
|
|
describe('reorderParticipants', () => {
|
|
test('drag before target (1-list model)', () => {
|
|
const ps = [p('a', 10), p('b', 20), p('c', 20)]; // b,c tie
|
|
let e = enc(ps);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
// initial order: b,c,a (init 20,20,10)
|
|
expect(e.turnOrderIds).toEqual(['b', 'c', 'a']);
|
|
const r = reorderParticipants(e, 'c', 'b');
|
|
// drag c before b: remove c → [b,a], insert before b → [c,b,a]
|
|
expect(r.patch.participants.map(p => p.id)).toEqual(['c', 'b', 'a']);
|
|
});
|
|
|
|
test('cross-init drag allowed (1-list, DM override)', () => {
|
|
const ps = [p('a', 10), p('b', 20)];
|
|
let e = enc(ps);
|
|
e = { ...e, ...startEncounter(e).patch }; // [b,a]
|
|
const r = reorderParticipants(e, 'a', 'b');
|
|
expect(r.patch.participants.map(p => p.id)).toEqual(['a', 'b']);
|
|
});
|
|
|
|
test('throws if id not found', () => {
|
|
const ps = [p('a', 10), p('b', 20)];
|
|
let e = enc(ps);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
expect(() => reorderParticipants(e, 'a', 'zzz')).toThrow();
|
|
});
|
|
|
|
test('syncs turnOrderIds = participants order (1-list, fixes BUG-6)', () => {
|
|
const ps = [p('a', 10), p('b', 20), p('c', 20)];
|
|
let e = enc(ps);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
const r = reorderParticipants(e, 'c', 'b');
|
|
expect(r.patch.turnOrderIds).toEqual(['c', 'b', 'a']);
|
|
expect(r.patch.turnOrderIds).toEqual(r.patch.participants.map(p => p.id));
|
|
});
|
|
|
|
// BUG-6 candidate: reorder should affect turnOrderIds so mid-combat
|
|
// drag-drop changes who goes next within same-initiative tie.
|
|
// Currently RED (turnOrderIds not in patch).
|
|
test('reorder updates turnOrderIds to reflect new participant order', () => {
|
|
const ps = [p('a', 10), p('b', 20), p('c', 20)];
|
|
let e = enc(ps);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
// order: b,c,a
|
|
e = { ...e, ...reorderParticipants(e, 'c', 'b').patch };
|
|
expect(e.turnOrderIds).toEqual(['c', 'b', 'a']);
|
|
});
|
|
});
|