From 435e109070f7d3bfd3e4a593edde1dde349c7817 Mon Sep 17 00:00:00 2001 From: david raistrick <1108844+keen99@users.noreply.github.com> Date: Tue, 30 Jun 2026 14:05:29 -0400 Subject: [PATCH] tests: removeParticipant edge cases (5 green) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit turn.remove.test.js: current-removed picks next, last wraps to first, all-inactive → current null (BUG-9 candidate, broken state doc), non-current kept, dead-removed stays out (BUG-3 overlap explicit action). No RED. Documents removeParticipant robust. --- shared/tests/turn.remove.test.js | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 shared/tests/turn.remove.test.js 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(); + }); +});