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