2026-06-28 16:57:43 -04:00
|
|
|
// Characterization tests for shared/turn.js.
|
|
|
|
|
// Lock CURRENT behavior (bugs included). M3 will extend, M4 will fix.
|
|
|
|
|
// These tests assert what the code does NOW, not what it SHOULD do.
|
2026-07-06 10:31:46 -04:00
|
|
|
//
|
|
|
|
|
// New API: every mutating func is async, takes ctx last, writes encounter +
|
|
|
|
|
// log internally, returns newEnc. We assert against newEnc (merged state) or
|
|
|
|
|
// the raw persisted patch (storage.calls) for "field not written" checks.
|
2026-06-28 16:57:43 -04:00
|
|
|
|
|
|
|
|
const shared = require('@ttrpg/shared');
|
|
|
|
|
const {
|
|
|
|
|
sortParticipantsByInitiative,
|
|
|
|
|
computeTurnOrderAfterRemoval,
|
|
|
|
|
startEncounter,
|
|
|
|
|
nextTurn,
|
|
|
|
|
togglePause,
|
|
|
|
|
addParticipant,
|
|
|
|
|
removeParticipant,
|
|
|
|
|
toggleParticipantActive,
|
|
|
|
|
applyHpChange,
|
|
|
|
|
deathSave,
|
|
|
|
|
toggleCondition,
|
|
|
|
|
reorderParticipants,
|
|
|
|
|
endEncounter,
|
|
|
|
|
makeParticipant,
|
|
|
|
|
} = shared;
|
2026-07-06 10:31:46 -04:00
|
|
|
const { mockCtx } = require('./_helpers');
|
2026-06-28 16:57:43 -04:00
|
|
|
|
|
|
|
|
// Helper: minimal encounter with given participants.
|
|
|
|
|
function enc(participants = [], extra = {}) {
|
|
|
|
|
return {
|
|
|
|
|
name: 'Test Encounter',
|
|
|
|
|
participants,
|
|
|
|
|
isStarted: false,
|
|
|
|
|
isPaused: false,
|
|
|
|
|
round: 0,
|
|
|
|
|
currentTurnParticipantId: null,
|
|
|
|
|
turnOrderIds: [],
|
|
|
|
|
...extra,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function p(id, initiative, extra = {}) {
|
|
|
|
|
return makeParticipant({
|
|
|
|
|
id, name: id, type: 'monster',
|
|
|
|
|
initiative, maxHp: 20, currentHp: 20,
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
// Last updateDoc patch written to storage — for "field absent from patch"
|
|
|
|
|
// assertions (field === undefined means func didn't write it).
|
|
|
|
|
const lastPatch = (storage) => {
|
|
|
|
|
const u = storage.calls.filter(c => c.fn === 'updateDoc').pop();
|
|
|
|
|
return u ? u.data : {};
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-28 16:57:43 -04:00
|
|
|
describe('sortParticipantsByInitiative', () => {
|
|
|
|
|
test('higher initiative first', () => {
|
|
|
|
|
const ps = [p('a', 5), p('b', 15), p('c', 10)];
|
|
|
|
|
const sorted = sortParticipantsByInitiative(ps, ps);
|
|
|
|
|
expect(sorted.map(x => x.id)).toEqual(['b', 'c', 'a']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('ties broken by original order', () => {
|
|
|
|
|
const ps = [p('a', 10), p('b', 10), p('c', 10)];
|
|
|
|
|
const sorted = sortParticipantsByInitiative(ps, ps);
|
|
|
|
|
expect(sorted.map(x => x.id)).toEqual(['a', 'b', 'c']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('startEncounter', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('throws if no participants', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
|
|
|
|
await expect(startEncounter(enc([]), ctx)).rejects.toThrow('participants');
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('throws if no active participants', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const e = enc([p('a', 10, { isActive: false })]);
|
2026-07-06 10:31:46 -04:00
|
|
|
await expect(startEncounter(e, ctx)).rejects.toThrow('active');
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('sets round 1, turn order sorted, current = highest init', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 5), p('b', 15), p('c', 10)];
|
|
|
|
|
const e = enc(ps);
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await startEncounter(e, ctx);
|
|
|
|
|
expect(newEnc.isStarted).toBe(true);
|
|
|
|
|
expect(newEnc.round).toBe(1);
|
|
|
|
|
expect(newEnc.turnOrderIds).toEqual(['b', 'c', 'a']);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('b');
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('inactive stays in turn order slot (1-list model)', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 5), p('b', 15, { isActive: false }), p('c', 10)];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await startEncounter(enc(ps), ctx);
|
2026-07-01 16:00:00 -04:00
|
|
|
// 1-list: all participants sorted by init (active+inactive), inactive stays in slot
|
2026-07-06 10:31:46 -04:00
|
|
|
expect(newEnc.turnOrderIds).toEqual(['b', 'c', 'a']);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('c'); // b inactive, skipped
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('nextTurn', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('throws if not started', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
|
|
|
|
await expect(nextTurn(enc([p('a', 10)], { isStarted: false }), ctx)).rejects.toThrow();
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('throws if paused', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
|
|
|
|
await expect(nextTurn(enc([p('a', 10)], { isStarted: true, isPaused: true, currentTurnParticipantId: 'a', turnOrderIds: ['a'] }), ctx)).rejects.toThrow();
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('advances to next in order, no round bump', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 5), p('b', 15), p('c', 10)];
|
|
|
|
|
const e = enc(ps, {
|
|
|
|
|
isStarted: true,
|
|
|
|
|
round: 1,
|
|
|
|
|
currentTurnParticipantId: 'b',
|
|
|
|
|
turnOrderIds: ['b', 'c', 'a'],
|
|
|
|
|
});
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await nextTurn(e, ctx);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('c');
|
|
|
|
|
expect(newEnc.round).toBe(1);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('wraps round when last in order', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 5), p('b', 15), p('c', 10)];
|
|
|
|
|
const e = enc(ps, {
|
|
|
|
|
isStarted: true,
|
|
|
|
|
round: 1,
|
|
|
|
|
currentTurnParticipantId: 'a',
|
|
|
|
|
turnOrderIds: ['b', 'c', 'a'],
|
|
|
|
|
});
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await nextTurn(e, ctx);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('b');
|
|
|
|
|
expect(newEnc.round).toBe(2);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('ends encounter if no active participants', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { isActive: false })];
|
|
|
|
|
const e = enc(ps, {
|
|
|
|
|
isStarted: true,
|
|
|
|
|
round: 1,
|
|
|
|
|
currentTurnParticipantId: 'a',
|
|
|
|
|
turnOrderIds: ['a'],
|
|
|
|
|
});
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await nextTurn(e, ctx);
|
|
|
|
|
expect(newEnc.isStarted).toBe(false);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe(null);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('togglePause', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('pauses started encounter', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const e = enc([p('a', 10)], { isStarted: true, isPaused: false });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await togglePause(e, ctx);
|
|
|
|
|
expect(newEnc.isPaused).toBe(true);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('resume preserves turn order (no re-sort)', async () => {
|
2026-07-01 11:42:43 -04:00
|
|
|
// BUG-5 fix: resume no longer re-sorts. Re-sort displaced current pointer
|
|
|
|
|
// and caused skips. Order frozen at startEncounter, patched incrementally.
|
2026-07-06 10:31:46 -04:00
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 5), p('b', 15)];
|
|
|
|
|
const e = enc(ps, { isStarted: true, isPaused: true, turnOrderIds: ['a', 'b'] });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await togglePause(e, ctx);
|
|
|
|
|
expect(newEnc.isPaused).toBe(false);
|
|
|
|
|
expect(newEnc.turnOrderIds).toEqual(['a', 'b']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('removeParticipant', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('removes from participants array', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 5)];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await removeParticipant(enc(ps), 'a', ctx);
|
|
|
|
|
expect(newEnc.participants.map(x => x.id)).toEqual(['b']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('not started: no turn order mutation', async () => {
|
|
|
|
|
const { storage, ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 5)];
|
2026-07-06 10:31:46 -04:00
|
|
|
await removeParticipant(enc(ps), 'a', ctx);
|
|
|
|
|
expect(lastPatch(storage).turnOrderIds).toBeUndefined();
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('started: removes from turnOrderIds', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
|
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'b' });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await removeParticipant(e, 'a', ctx);
|
|
|
|
|
expect(newEnc.turnOrderIds).toEqual(['b']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('started: removing current picks next active', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 5), p('c', 3)];
|
|
|
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b', 'c'], currentTurnParticipantId: 'a' });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await removeParticipant(e, 'a', ctx);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('b');
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('toggleParticipantActive', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('deactivates participant', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { isActive: true })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await toggleParticipantActive(enc(ps), 'a', ctx);
|
|
|
|
|
expect(newEnc.participants[0].isActive).toBe(false);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('started: deactivating current does not advance turn or round', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 5)];
|
2026-07-06 10:31:46 -04:00
|
|
|
const e = enc(ps, { isStarted: true, round: 1, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'a' });
|
|
|
|
|
const newEnc = await toggleParticipantActive(e, 'a', ctx);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe('a');
|
|
|
|
|
expect(newEnc.round).toBe(1);
|
|
|
|
|
expect(newEnc.participants.find(p => p.id === 'a').isActive).toBe(false);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('started: reactivating inserts by initiative', async () => {
|
2026-07-01 11:42:43 -04:00
|
|
|
// BUG-5 fix: reactivated participant slots by initiative (not appended
|
|
|
|
|
// to end). Preserves correct rotation order.
|
2026-07-06 10:31:46 -04:00
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { isActive: false }), p('b', 5)];
|
|
|
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['b'], currentTurnParticipantId: 'b' });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await toggleParticipantActive(e, 'a', ctx);
|
2026-07-01 11:42:43 -04:00
|
|
|
// a init=10 > b init=5 → a slots before b
|
2026-07-06 10:31:46 -04:00
|
|
|
expect(newEnc.turnOrderIds).toEqual(['a', 'b']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('applyHpChange', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('damage reduces hp, clamps 0', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 15, maxHp: 20 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await applyHpChange(enc(ps), 'a', 'damage', 5, ctx);
|
|
|
|
|
expect(newEnc.participants[0].currentHp).toBe(10);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('damage to 0 deactivates + keeps turn order (unified)', async () => {
|
2026-07-03 17:59:41 -04:00
|
|
|
// Unified: death flips isActive=false (removed from active rotation).
|
|
|
|
|
// turnOrderIds unchanged (no turn-order patch on death).
|
2026-07-06 10:31:46 -04:00
|
|
|
const { storage, ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 3 }), p('b', 5)];
|
|
|
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'a' });
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await applyHpChange(e, 'a', 'damage', 5, ctx);
|
|
|
|
|
expect(newEnc.participants[0].currentHp).toBe(0);
|
|
|
|
|
expect(newEnc.participants[0].isActive).toBe(false);
|
|
|
|
|
expect(lastPatch(storage).turnOrderIds).toBeUndefined();
|
|
|
|
|
expect(lastPatch(storage).currentTurnParticipantId).toBeUndefined();
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('heal above 0 reactivates + resets death saves (unified)', async () => {
|
2026-07-03 17:59:41 -04:00
|
|
|
// Unified: revive from 0 flips isActive=true, deathSaves reset.
|
2026-07-06 10:31:46 -04:00
|
|
|
const { ctx } = mockCtx();
|
2026-07-03 17:59:41 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 0, isActive: false, deathSaves: 2 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await applyHpChange(enc(ps), 'a', 'heal', 5, ctx);
|
|
|
|
|
expect(newEnc.participants[0].currentHp).toBe(5);
|
|
|
|
|
expect(newEnc.participants[0].isActive).toBe(true);
|
|
|
|
|
expect(newEnc.participants[0].deathSaves).toBe(0);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('heal clamps to maxHp', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 18, maxHp: 20 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await applyHpChange(enc(ps), 'a', 'heal', 10, ctx);
|
|
|
|
|
expect(newEnc.participants[0].currentHp).toBe(20);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('zero amount = no-op', async () => {
|
|
|
|
|
const { storage, ctx } = mockCtx();
|
|
|
|
|
const e = enc([p('a', 10, { currentHp: 10 })]);
|
|
|
|
|
const newEnc = await applyHpChange(e, 'a', 'damage', 0, ctx);
|
|
|
|
|
expect(newEnc).toBe(e); // same ref = no write
|
|
|
|
|
expect(storage.calls.filter(c => c.fn === 'updateDoc')).toHaveLength(0);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('deathSave', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('increments fails', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-07-04 17:56:14 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 0 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const { enc: newEnc } = await deathSave(enc(ps), 'a', 'fail', 1, ctx);
|
|
|
|
|
expect(newEnc.participants[0].deathFails).toBe(1);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('clicking same fail decrements (toggle)', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-07-04 17:56:14 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const { enc: newEnc } = await deathSave(enc(ps), 'a', 'fail', 2, ctx);
|
|
|
|
|
expect(newEnc.participants[0].deathFails).toBe(1);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('third fail sets isDying', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-07-04 17:56:14 -04:00
|
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const { enc: newEnc, isDying } = await deathSave(enc(ps), 'a', 'fail', 3, ctx);
|
|
|
|
|
expect(newEnc.participants[0].deathFails).toBe(3);
|
|
|
|
|
expect(newEnc.participants[0].isDying).toBe(true);
|
|
|
|
|
expect(isDying).toBe(true);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('toggleCondition', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('adds condition', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { conditions: [] })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await toggleCondition(enc(ps), 'a', 'poisoned', ctx);
|
|
|
|
|
expect(newEnc.participants[0].conditions).toEqual(['poisoned']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('removes condition', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10, { conditions: ['poisoned', 'blinded'] })];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await toggleCondition(enc(ps), 'a', 'poisoned', ctx);
|
|
|
|
|
expect(newEnc.participants[0].conditions).toEqual(['blinded']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('reorderParticipants', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('drag before target (same-init tie)', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const ps = [p('a', 10), p('b', 10), p('c', 10)];
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await reorderParticipants(enc(ps), 'a', 'c', ctx);
|
2026-07-01 16:00:00 -04:00
|
|
|
// drag a before c: remove a → [b,c], insert before c → [b,a,c]
|
2026-07-06 10:31:46 -04:00
|
|
|
expect(newEnc.participants.map(x => x.id)).toEqual(['b', 'a', 'c']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('cross-init drag blocked (no-op)', async () => {
|
|
|
|
|
const { storage, ctx } = mockCtx();
|
|
|
|
|
const e = enc([p('a', 10), p('b', 5)]);
|
|
|
|
|
const newEnc = await reorderParticipants(e, 'a', 'b', ctx);
|
|
|
|
|
expect(newEnc).toBe(e); // same ref = no write
|
|
|
|
|
expect(storage.calls.filter(c => c.fn === 'updateDoc')).toHaveLength(0);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('endEncounter', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('resets all combat state', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const e = enc([p('a', 10)], {
|
|
|
|
|
isStarted: true, round: 5, currentTurnParticipantId: 'a', turnOrderIds: ['a'],
|
|
|
|
|
});
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await endEncounter(e, ctx);
|
|
|
|
|
expect(newEnc.isStarted).toBe(false);
|
|
|
|
|
expect(newEnc.round).toBe(0);
|
|
|
|
|
expect(newEnc.currentTurnParticipantId).toBe(null);
|
|
|
|
|
expect(newEnc.turnOrderIds).toEqual([]);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('computeTurnOrderAfterRemoval', () => {
|
|
|
|
|
test('not started = empty', () => {
|
|
|
|
|
const out = computeTurnOrderAfterRemoval(enc([]), 'a', []);
|
|
|
|
|
expect(out).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-01 16:00:00 -04:00
|
|
|
test('removing non-current: no turnOrderIds patch (1-list syncs at call site)', () => {
|
2026-06-28 16:57:43 -04:00
|
|
|
const e = enc([], { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'b' });
|
|
|
|
|
const out = computeTurnOrderAfterRemoval(e, 'a', []);
|
2026-07-01 16:00:00 -04:00
|
|
|
// 1-list: removal syncs turnOrderIds via participants[] at call site.
|
|
|
|
|
// Helper only handles current-advance. Non-current = empty patch.
|
|
|
|
|
expect(out).toEqual({});
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('addParticipant', () => {
|
2026-07-06 10:31:46 -04:00
|
|
|
test('appends participant', async () => {
|
|
|
|
|
const { ctx } = mockCtx();
|
2026-06-28 16:57:43 -04:00
|
|
|
const np = p('z', 7);
|
2026-07-06 10:31:46 -04:00
|
|
|
const newEnc = await addParticipant(enc([p('a', 10)]), np, ctx);
|
|
|
|
|
expect(newEnc.participants.map(x => x.id)).toEqual(['a', 'z']);
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|
2026-06-29 15:49:39 -04:00
|
|
|
|
2026-07-06 10:31:46 -04:00
|
|
|
test('rejects duplicate id (skip-bug root cause)', async () => {
|
2026-06-29 15:49:39 -04:00
|
|
|
// Two participants with same id → togglePause resume rebuilds order with
|
|
|
|
|
// dup id twice → nextTurn gets stuck repeating that id forever.
|
|
|
|
|
// Audit found this in 100-round replay (addParticipant fired while paused
|
|
|
|
|
// because nextTurn threw, loop spun, same totalTurns %10 → re-added).
|
2026-07-06 10:31:46 -04:00
|
|
|
const { ctx } = mockCtx();
|
2026-06-29 15:49:39 -04:00
|
|
|
const existing = p('x', 5);
|
|
|
|
|
const dup = makeParticipant({ id: 'x', name: 'x2', type: 'monster', initiative: 10, maxHp: 100, currentHp: 100 });
|
2026-07-06 10:31:46 -04:00
|
|
|
await expect(addParticipant(enc([p('a', 10), existing]), dup, ctx)).rejects.toThrow();
|
2026-06-29 15:49:39 -04:00
|
|
|
});
|
2026-06-28 16:57:43 -04:00
|
|
|
});
|