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).
65 lines
2.8 KiB
JavaScript
65 lines
2.8 KiB
JavaScript
// removeParticipant + computeTurnOrderAfterRemoval edge cases.
|
|
|
|
const shared = require('@ttrpg/shared');
|
|
const { makeParticipant, startEncounter, nextTurn, removeParticipant, toggleParticipantActive, applyHpChange } = 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('removeParticipant turn-order edges', () => {
|
|
test('removing current picks next active as current', () => {
|
|
let e = enc([p('a',20),p('b',15),p('c',10)]);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
e = { ...e, ...removeParticipant(e, 'a').patch }; // a was current
|
|
expect(e.currentTurnParticipantId).toBe('b');
|
|
});
|
|
|
|
test('removing last in order wraps current to first', () => {
|
|
let e = enc([p('a',20),p('b',15),p('c',10)]);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
e = { ...e, ...nextTurn(e).patch }; // b
|
|
e = { ...e, ...nextTurn(e).patch }; // c (current)
|
|
e = { ...e, ...removeParticipant(e, 'c').patch };
|
|
expect(e.currentTurnParticipantId).toBe('a');
|
|
});
|
|
|
|
test('removing current when all others inactive → no active, isStarted stays (BUG-9 candidate)', () => {
|
|
let e = enc([p('a',20),p('b',15),p('c',10)]);
|
|
e = { ...e, ...startEncounter(e).patch }; // [a,b,c], cur=a
|
|
// deactivate b + c (stay in slot, inactive)
|
|
e = { ...e, ...toggleParticipantActive(e, 'b').patch };
|
|
e = { ...e, ...toggleParticipantActive(e, 'c').patch };
|
|
// remove current a
|
|
e = { ...e, ...removeParticipant(e, 'a').patch };
|
|
// 1-list: turnOrderIds=[b,c], no active → current null, isStarted stays true
|
|
expect(e.turnOrderIds).toEqual(['b', 'c']);
|
|
expect(e.currentTurnParticipantId).toBeNull();
|
|
// isStarted still true but no turn → nextTurn throws (stale state)
|
|
});
|
|
|
|
test('removing non-current keeps currentTurn', () => {
|
|
let e = enc([p('a',20),p('b',15),p('c',10)]);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
e = { ...e, ...removeParticipant(e, 'b').patch };
|
|
expect(e.currentTurnParticipantId).toBe('a');
|
|
expect(e.turnOrderIds).toEqual(['a', 'c']);
|
|
});
|
|
|
|
test('removing current that is dead (HP=0) - BUG-3 overlap', () => {
|
|
// Dead participant removed mid-combat. Desired (M4): they STAY in order.
|
|
// removeParticipant is explicit DM action, distinct from auto-skip.
|
|
let e = enc([p('a',20),p('b',15),p('c',10)]);
|
|
e = { ...e, ...startEncounter(e).patch };
|
|
e = { ...e, ...applyHpChange(e, 'b', 'damage', 100).patch }; // b dead
|
|
e = { ...e, ...removeParticipant(e, 'b').patch };
|
|
expect(e.turnOrderIds).not.toContain('b');
|
|
expect(e.participants.find(x => x.id === 'b')).toBeUndefined();
|
|
});
|
|
});
|