Files

74 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

// removeParticipant + computeTurnOrderAfterRemoval edge cases.
//
// New API: mutating funcs are async, take ctx last, write encounter + log
// internally, return newEnc. Chained calls become `e = await fn(e, ctx)`.
const shared = require('@ttrpg/shared');
const { makeParticipant, startEncounter, nextTurn, removeParticipant, toggleParticipantActive, applyHpChange } = shared;
const { mockCtx } = require('./_helpers');
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', async () => {
const { ctx } = mockCtx();
let e = enc([p('a',20),p('b',15),p('c',10)]);
e = await startEncounter(e, ctx);
e = await removeParticipant(e, 'a', ctx); // a was current
expect(e.currentTurnParticipantId).toBe('b');
});
test('removing last in order wraps current to first', async () => {
const { ctx } = mockCtx();
let e = enc([p('a',20),p('b',15),p('c',10)]);
e = await startEncounter(e, ctx);
e = await nextTurn(e, ctx); // b
e = await nextTurn(e, ctx); // c (current)
e = await removeParticipant(e, 'c', ctx);
expect(e.currentTurnParticipantId).toBe('a');
});
test('removing current when all others inactive → no active, isStarted stays (BUG-9 candidate)', async () => {
const { ctx } = mockCtx();
let e = enc([p('a',20),p('b',15),p('c',10)]);
e = await startEncounter(e, ctx); // [a,b,c], cur=a
// deactivate b + c (stay in slot, inactive)
e = await toggleParticipantActive(e, 'b', ctx);
e = await toggleParticipantActive(e, 'c', ctx);
// remove current a
e = await removeParticipant(e, 'a', ctx);
// 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', async () => {
const { ctx } = mockCtx();
let e = enc([p('a',20),p('b',15),p('c',10)]);
e = await startEncounter(e, ctx);
e = await removeParticipant(e, 'b', ctx);
expect(e.currentTurnParticipantId).toBe('a');
expect(e.turnOrderIds).toEqual(['a', 'c']);
});
test('removing current that is dead (HP=0) - BUG-3 overlap', async () => {
// Dead participant removed mid-combat. Desired (M4): they STAY in order.
// removeParticipant is explicit DM action, distinct from auto-skip.
const { ctx } = mockCtx();
let e = enc([p('a',20),p('b',15),p('c',10)]);
e = await startEncounter(e, ctx);
e = await applyHpChange(e, 'b', 'damage', 100, ctx); // b dead
e = await removeParticipant(e, 'b', ctx);
expect(e.turnOrderIds).not.toContain('b');
expect(e.participants.find(x => x.id === 'b')).toBeUndefined();
});
});