diff --git a/shared/tests/turn.remove.test.js b/shared/tests/turn.remove.test.js new file mode 100644 index 0000000..c9c98c4 --- /dev/null +++ b/shared/tests/turn.remove.test.js @@ -0,0 +1,63 @@ +// 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 → current null (BUG-9 candidate)', () => { + let e = enc([p('a',20),p('b',15),p('c',10)]); + e = { ...e, ...startEncounter(e).patch }; + // deactivate b + c + e = { ...e, ...toggleParticipantActive(e, 'b').patch }; + e = { ...e, ...toggleParticipantActive(e, 'c').patch }; + // remove current a + e = { ...e, ...removeParticipant(e, 'a').patch }; + expect(e.currentTurnParticipantId).toBeNull(); + expect(e.turnOrderIds).toEqual([]); + // 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(); + }); +});