Files

80 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

// 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).
//
// New API: reorderParticipants is async, takes ctx, returns newEnc.
// Blocked drag = no-op (returns same enc ref, no write).
'use strict';
const {
makeParticipant,
startEncounter, nextTurn, reorderParticipants,
} = require('@ttrpg/shared');
const { mockCtx } = require('./_helpers');
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:[] };
}
describe('BUG-13: cross-pointer reorder blocked during active encounter', () => {
test('dragging not-yet-acted participant ahead of pointer = no-op', async () => {
const { ctx } = mockCtx();
// [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 = await startEncounter(e, ctx); // a (idx0, pointer)
e = await nextTurn(e, ctx); // 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 newEnc = await reorderParticipants(e, 'c', 'b', ctx);
expect(newEnc).toBe(e); // no-op: same ref, no write
expect(e.turnOrderIds).toEqual(['a','b','c']); // unchanged
});
test('dragging already-acted participant behind pointer = no-op', async () => {
const { ctx } = mockCtx();
// [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 = await startEncounter(e, ctx); // a (pointer idx0)
e = await nextTurn(e, ctx); // b (pointer idx1)
// Drag a (already acted, ahead of pointer) after c (behind pointer).
// Crosses pointer. Would let a act again. Blocked.
const newEnc = await reorderParticipants(e, 'a', 'c', ctx);
expect(newEnc).toBe(e); // no-op: same ref, no write
});
test('reorder within same side of pointer = allowed', async () => {
const { storage, ctx } = mockCtx();
// [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 = await startEncounter(e, ctx); // a (idx0)
// swap b,c (both idx1,2 — behind pointer). No cross.
const newEnc = await reorderParticipants(e, 'c', 'b', ctx);
// startEncounter logs too; assert the reorder write specifically.
expect(storage.logs().filter(l => l.type === 'reorder')).toHaveLength(1);
expect(newEnc.participants.map(p => p.id)).toEqual(['a','c','b','d']);
});
test('pre-combat: free reorder (no pointer)', async () => {
const { storage, ctx } = mockCtx();
// isStarted=false. No pointer. Reorder allowed freely.
let e = enc([p('a',10),p('b',10),p('c',10)]);
const newEnc = await reorderParticipants(e, 'c', 'a', ctx);
expect(storage.logs().filter(l => l.type === 'reorder')).toHaveLength(1);
expect(newEnc.participants.map(p => p.id)).toEqual(['c','a','b']);
});
});