// BUG-13: cross-pointer reorder blocked (skip/double-act otherwise). // nextTurn walks turnOrderIds forward from current pointer. Dragging across // pointer = ambiguous who-acts-next: // - upcoming dragged ahead of pointer → walk wraps past → SKIPPED // - acted dragged behind pointer → acts again → DOUBLE-ACT // Full fix needs actedThisRound tracking. Pragmatic now: block both dirs // during active encounter. Pre-combat: free reorder (no pointer). 'use strict'; const shared = require('@ttrpg/shared'); const { makeParticipant, startEncounter, nextTurn, reorderParticipants, } = shared; function p(id, init) { return makeParticipant({ id, name: id, type: 'monster', initiative: init, maxHp: 100, currentHp: 100 }); } function enc(ps) { return { name:'t', participants:ps, isStarted:false, isPaused:false, round:0, currentTurnParticipantId:null, turnOrderIds:[] }; } const apply = (e, r) => r && r.patch ? { ...e, ...r.patch } : e; describe('BUG-13: cross-pointer reorder blocked during active encounter', () => { test('dragging not-yet-acted participant ahead of pointer = no-op', () => { // [a(10), b(10), c(10)]. a acts -> b current. c not yet acted. let e = enc([p('a',10),p('b',10),p('c',10)]); e = apply(e, startEncounter(e)); // a (idx0, pointer) e = apply(e, nextTurn(e)); // b (idx1, pointer) expect(e.currentTurnParticipantId).toBe('b'); // Drag c (idx2, behind pointer) before b (idx1, pointer). Crosses pointer. // Would let c act by initiative-reinsert but nextTurn forward-walk skips. // Blocked instead. const r = reorderParticipants(e, 'c', 'b'); expect(r.patch).toBeNull(); expect(e.turnOrderIds).toEqual(['a','b','c']); // unchanged }); test('dragging already-acted participant behind pointer = no-op', () => { // [a(10), b(10), c(10)]. a acts (idx0, ahead of pointer). let e = enc([p('a',10),p('b',10),p('c',10)]); e = apply(e, startEncounter(e)); // a (pointer idx0) e = apply(e, nextTurn(e)); // b (pointer idx1) // Drag a (already acted, ahead of pointer) after c (behind pointer). // Crosses pointer. Would let a act again. Blocked. const r = reorderParticipants(e, 'a', 'c'); expect(r.patch).toBeNull(); }); test('reorder within same side of pointer = allowed', () => { // [a(10), b(10), c(10), d(10)]. a current (pointer idx0). // b,c,d all behind pointer (upcoming). Reorder among them = safe. let e = enc([p('a',10),p('b',10),p('c',10),p('d',10)]); e = apply(e, startEncounter(e)); // a (idx0) // swap b,c (both idx1,2 — behind pointer). No cross. const r = reorderParticipants(e, 'c', 'b'); expect(r.patch).not.toBeNull(); expect(r.patch.participants.map(p => p.id)).toEqual(['a','c','b','d']); }); test('pre-combat: free reorder (no pointer)', () => { // isStarted=false. No pointer. Reorder allowed freely. let e = enc([p('a',10),p('b',10),p('c',10)]); const r = reorderParticipants(e, 'c', 'a'); expect(r.patch).not.toBeNull(); expect(r.patch.participants.map(p => p.id)).toEqual(['c','a','b']); }); });