reorderParticipants allowed dragging across current pointer mid-round. nextTurn walks turnOrderIds forward from pointer, so cross-pointer drag = ambiguous who-acts-next: upcoming dragged ahead of pointer = skipped (walk wraps past), acted dragged behind pointer = double-act. Full fix needs actedThisRound tracking. Pragmatic now: block both directions during active encounter. Pre-combat: free reorder (no pointer). reorderParticipants: added pointer check (currentTurn index). If drag crosses pointer, return null patch (no-op). Same-side reorder allowed. syncTurnOrder now always mirrors (was started-only — pre-combat reorder left turnOrderIds stale). Tests: - turn.bug13.test.js: cross-pointer blocked (both dirs), same-side allowed, pre-combat free. - turn.reorder.test.js: 3 tests updated to pre-combat (cross-pointer now blocked post-start). - turn.invariant.test.js: reorder test uses same-side upcoming pair. 214 tests green.
66 lines
2.6 KiB
JavaScript
66 lines
2.6 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, pre-combat)', () => {
|
|
const ps = [p('a', 10), p('b', 20), p('c', 20)]; // b,c tie
|
|
let e = enc(ps); // pre-combat, no pointer
|
|
const r = reorderParticipants(e, 'c', 'b');
|
|
// drag c before b: remove c → [a,b], insert before b → [a,c,b]
|
|
expect(r.patch.participants.map(p => p.id)).toEqual(['a', 'c', 'b']);
|
|
});
|
|
|
|
test('cross-init drag blocked (no-op)', () => {
|
|
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).toBeNull();
|
|
});
|
|
|
|
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 }; // started, but reorder pre-pointer advance
|
|
// startEncounter sets current=b (idx0). reorder c before b crosses pointer.
|
|
// Use pre-combat to test syncTurnOrder.
|
|
let pre = enc(ps);
|
|
pre = { ...pre, ...reorderParticipants(pre, 'c', 'b').patch };
|
|
expect(pre.turnOrderIds).toEqual(['a','c','b']);
|
|
expect(pre.turnOrderIds).toEqual(pre.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 (pre-combat)', () => {
|
|
const ps = [p('a', 10), p('b', 20), p('c', 20)];
|
|
let e = enc(ps); // pre-combat, no pointer
|
|
e = { ...e, ...reorderParticipants(e, 'c', 'b').patch };
|
|
expect(e.turnOrderIds).toEqual(['a', 'c', 'b']);
|
|
});
|
|
});
|