Old model broken: single deathSaves counter, 3=revive. No fails tracked,
successes conflated with fails. DM couldn't model real death save outcomes.
New model (D&D 5e):
- deathSave(enc, id, type, n) — type='success'|'fail', n=1|2|3
- Participant fields: deathSaves (successes), deathFails, isStable, isDying
- 3 successes = stable (0hp unconscious, safe until healed)
- 3 failures = dead (isDying, caller animates removal)
- Toggling same pip = undo that pip
- Returns { patch, log, status } — status: 'stable'|'dead'|'pending'
isDying back-compat flag kept for App.js removal animation
App.js:
- UI split into green ✓ saves row + red ✕ fails row
- Status badges: Stabilized (emerald) / Dying (red pulse)
- handleDeathSaveChange(participantId, type, saveNumber) — new sig
- makeParticipant init: deathFails + isStable defaults
Data loss: old single-counter participants lose saves (feature didn't work,
no real state to preserve). User confirmed acceptable.
Tests:
- turn.deathsave.test.js: RED-then-GREEN, 3-success stable, 3-fail dead,
independent tracking, new signature
- Updated 6 callers across characterization/combat/dead-skip/logging/scenario
- Logs UI tests: new title selectors (Success N / Fail N)
243 tests green.
345 lines
12 KiB
JavaScript
345 lines
12 KiB
JavaScript
// 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.
|
|
|
|
const shared = require('@ttrpg/shared');
|
|
const {
|
|
sortParticipantsByInitiative,
|
|
computeTurnOrderAfterRemoval,
|
|
startEncounter,
|
|
nextTurn,
|
|
togglePause,
|
|
addParticipant,
|
|
removeParticipant,
|
|
toggleParticipantActive,
|
|
applyHpChange,
|
|
deathSave,
|
|
toggleCondition,
|
|
reorderParticipants,
|
|
endEncounter,
|
|
makeParticipant,
|
|
} = shared;
|
|
|
|
// 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,
|
|
});
|
|
}
|
|
|
|
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', () => {
|
|
test('throws if no participants', () => {
|
|
expect(() => startEncounter(enc([]))).toThrow('participants');
|
|
});
|
|
|
|
test('throws if no active participants', () => {
|
|
const e = enc([p('a', 10, { isActive: false })]);
|
|
expect(() => startEncounter(e)).toThrow('active');
|
|
});
|
|
|
|
test('sets round 1, turn order sorted, current = highest init', () => {
|
|
const ps = [p('a', 5), p('b', 15), p('c', 10)];
|
|
const e = enc(ps);
|
|
const { patch } = startEncounter(e);
|
|
expect(patch.isStarted).toBe(true);
|
|
expect(patch.round).toBe(1);
|
|
expect(patch.turnOrderIds).toEqual(['b', 'c', 'a']);
|
|
expect(patch.currentTurnParticipantId).toBe('b');
|
|
});
|
|
|
|
test('inactive stays in turn order slot (1-list model)', () => {
|
|
const ps = [p('a', 5), p('b', 15, { isActive: false }), p('c', 10)];
|
|
const { patch } = startEncounter(enc(ps));
|
|
// 1-list: all participants sorted by init (active+inactive), inactive stays in slot
|
|
expect(patch.turnOrderIds).toEqual(['b', 'c', 'a']);
|
|
expect(patch.currentTurnParticipantId).toBe('c'); // b inactive, skipped
|
|
});
|
|
});
|
|
|
|
describe('nextTurn', () => {
|
|
test('throws if not started', () => {
|
|
expect(() => nextTurn(enc([p('a', 10)], { isStarted: false }))).toThrow();
|
|
});
|
|
|
|
test('throws if paused', () => {
|
|
expect(() => nextTurn(enc([p('a', 10)], { isStarted: true, isPaused: true, currentTurnParticipantId: 'a', turnOrderIds: ['a'] }))).toThrow();
|
|
});
|
|
|
|
test('advances to next in order, no round bump', () => {
|
|
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'],
|
|
});
|
|
const { patch } = nextTurn(e);
|
|
expect(patch.currentTurnParticipantId).toBe('c');
|
|
expect(patch.round).toBe(1);
|
|
});
|
|
|
|
test('wraps round when last in order', () => {
|
|
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'],
|
|
});
|
|
const { patch } = nextTurn(e);
|
|
expect(patch.currentTurnParticipantId).toBe('b');
|
|
expect(patch.round).toBe(2);
|
|
});
|
|
|
|
test('ends encounter if no active participants', () => {
|
|
const ps = [p('a', 10, { isActive: false })];
|
|
const e = enc(ps, {
|
|
isStarted: true,
|
|
round: 1,
|
|
currentTurnParticipantId: 'a',
|
|
turnOrderIds: ['a'],
|
|
});
|
|
const { patch } = nextTurn(e);
|
|
expect(patch.isStarted).toBe(false);
|
|
expect(patch.currentTurnParticipantId).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe('togglePause', () => {
|
|
test('pauses started encounter', () => {
|
|
const e = enc([p('a', 10)], { isStarted: true, isPaused: false });
|
|
const { patch } = togglePause(e);
|
|
expect(patch.isPaused).toBe(true);
|
|
});
|
|
|
|
test('resume preserves turn order (no re-sort)', () => {
|
|
// BUG-5 fix: resume no longer re-sorts. Re-sort displaced current pointer
|
|
// and caused skips. Order frozen at startEncounter, patched incrementally.
|
|
const ps = [p('a', 5), p('b', 15)];
|
|
const e = enc(ps, { isStarted: true, isPaused: true, turnOrderIds: ['a', 'b'] });
|
|
const { patch } = togglePause(e);
|
|
expect(patch.isPaused).toBe(false);
|
|
expect(patch.turnOrderIds).toEqual(['a', 'b']);
|
|
});
|
|
});
|
|
|
|
describe('removeParticipant', () => {
|
|
test('removes from participants array', () => {
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
const { patch } = removeParticipant(enc(ps), 'a');
|
|
expect(patch.participants.map(x => x.id)).toEqual(['b']);
|
|
});
|
|
|
|
test('not started: no turn order mutation', () => {
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
const { patch } = removeParticipant(enc(ps), 'a');
|
|
expect(patch.turnOrderIds).toBeUndefined();
|
|
});
|
|
|
|
test('started: removes from turnOrderIds', () => {
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'b' });
|
|
const { patch } = removeParticipant(e, 'a');
|
|
expect(patch.turnOrderIds).toEqual(['b']);
|
|
});
|
|
|
|
test('started: removing current picks next active', () => {
|
|
const ps = [p('a', 10), p('b', 5), p('c', 3)];
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b', 'c'], currentTurnParticipantId: 'a' });
|
|
const { patch } = removeParticipant(e, 'a');
|
|
expect(patch.currentTurnParticipantId).toBe('b');
|
|
});
|
|
});
|
|
|
|
describe('toggleParticipantActive', () => {
|
|
test('deactivates participant', () => {
|
|
const ps = [p('a', 10, { isActive: true })];
|
|
const { patch } = toggleParticipantActive(enc(ps), 'a');
|
|
expect(patch.participants[0].isActive).toBe(false);
|
|
});
|
|
|
|
test('started: deactivating current advances turn', () => {
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'a' });
|
|
const { patch } = toggleParticipantActive(e, 'a');
|
|
expect(patch.currentTurnParticipantId).toBe('b');
|
|
});
|
|
|
|
test('started: reactivating inserts by initiative', () => {
|
|
// BUG-5 fix: reactivated participant slots by initiative (not appended
|
|
// to end). Preserves correct rotation order.
|
|
const ps = [p('a', 10, { isActive: false }), p('b', 5)];
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['b'], currentTurnParticipantId: 'b' });
|
|
const { patch } = toggleParticipantActive(e, 'a');
|
|
// a init=10 > b init=5 → a slots before b
|
|
expect(patch.turnOrderIds).toEqual(['a', 'b']);
|
|
});
|
|
});
|
|
|
|
describe('applyHpChange', () => {
|
|
test('damage reduces hp, clamps 0', () => {
|
|
const ps = [p('a', 10, { currentHp: 15, maxHp: 20 })];
|
|
const { patch } = applyHpChange(enc(ps), 'a', 'damage', 5);
|
|
expect(patch.participants[0].currentHp).toBe(10);
|
|
});
|
|
|
|
test('damage to 0 deactivates + keeps turn order (unified)', () => {
|
|
// Unified: death flips isActive=false (removed from active rotation).
|
|
// turnOrderIds unchanged (no turn-order patch on death).
|
|
const ps = [p('a', 10, { currentHp: 3 }), p('b', 5)];
|
|
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'a' });
|
|
const { patch } = applyHpChange(e, 'a', 'damage', 5);
|
|
expect(patch.participants[0].currentHp).toBe(0);
|
|
expect(patch.participants[0].isActive).toBe(false);
|
|
expect(patch.turnOrderIds).toBeUndefined();
|
|
expect(patch.currentTurnParticipantId).toBeUndefined();
|
|
});
|
|
|
|
test('heal above 0 reactivates + resets death saves (unified)', () => {
|
|
// Unified: revive from 0 flips isActive=true, deathSaves reset.
|
|
const ps = [p('a', 10, { currentHp: 0, isActive: false, deathSaves: 2 })];
|
|
const { patch } = applyHpChange(enc(ps), 'a', 'heal', 5);
|
|
expect(patch.participants[0].currentHp).toBe(5);
|
|
expect(patch.participants[0].isActive).toBe(true);
|
|
expect(patch.participants[0].deathSaves).toBe(0);
|
|
});
|
|
|
|
test('heal clamps to maxHp', () => {
|
|
const ps = [p('a', 10, { currentHp: 18, maxHp: 20 })];
|
|
const { patch } = applyHpChange(enc(ps), 'a', 'heal', 10);
|
|
expect(patch.participants[0].currentHp).toBe(20);
|
|
});
|
|
|
|
test('zero amount = no-op', () => {
|
|
const ps = [p('a', 10, { currentHp: 10 })];
|
|
const { patch } = applyHpChange(enc(ps), 'a', 'damage', 0);
|
|
expect(patch).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe('deathSave', () => {
|
|
test('increments fails', () => {
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 0 })];
|
|
const { patch } = deathSave(enc(ps), 'a', 'fail', 1);
|
|
expect(patch.participants[0].deathFails).toBe(1);
|
|
});
|
|
|
|
test('clicking same fail decrements (toggle)', () => {
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
|
|
const { patch } = deathSave(enc(ps), 'a', 'fail', 2);
|
|
expect(patch.participants[0].deathFails).toBe(1);
|
|
});
|
|
|
|
test('third fail sets isDying', () => {
|
|
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
|
|
const result = deathSave(enc(ps), 'a', 'fail', 3);
|
|
expect(result.patch.participants[0].deathFails).toBe(3);
|
|
expect(result.patch.participants[0].isDying).toBe(true);
|
|
expect(result.isDying).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('toggleCondition', () => {
|
|
test('adds condition', () => {
|
|
const ps = [p('a', 10, { conditions: [] })];
|
|
const { patch } = toggleCondition(enc(ps), 'a', 'poisoned');
|
|
expect(patch.participants[0].conditions).toEqual(['poisoned']);
|
|
});
|
|
|
|
test('removes condition', () => {
|
|
const ps = [p('a', 10, { conditions: ['poisoned', 'blinded'] })];
|
|
const { patch } = toggleCondition(enc(ps), 'a', 'poisoned');
|
|
expect(patch.participants[0].conditions).toEqual(['blinded']);
|
|
});
|
|
});
|
|
|
|
describe('reorderParticipants', () => {
|
|
test('drag before target (same-init tie)', () => {
|
|
const ps = [p('a', 10), p('b', 10), p('c', 10)];
|
|
const { patch } = reorderParticipants(enc(ps), 'a', 'c');
|
|
// drag a before c: remove a → [b,c], insert before c → [b,a,c]
|
|
expect(patch.participants.map(x => x.id)).toEqual(['b', 'a', 'c']);
|
|
});
|
|
|
|
test('cross-init drag blocked (no-op)', () => {
|
|
const ps = [p('a', 10), p('b', 5)];
|
|
const { patch } = reorderParticipants(enc(ps), 'a', 'b');
|
|
expect(patch).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('endEncounter', () => {
|
|
test('resets all combat state', () => {
|
|
const e = enc([p('a', 10)], {
|
|
isStarted: true, round: 5, currentTurnParticipantId: 'a', turnOrderIds: ['a'],
|
|
});
|
|
const { patch } = endEncounter(e);
|
|
expect(patch.isStarted).toBe(false);
|
|
expect(patch.round).toBe(0);
|
|
expect(patch.currentTurnParticipantId).toBe(null);
|
|
expect(patch.turnOrderIds).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('computeTurnOrderAfterRemoval', () => {
|
|
test('not started = empty', () => {
|
|
const out = computeTurnOrderAfterRemoval(enc([]), 'a', []);
|
|
expect(out).toEqual({});
|
|
});
|
|
|
|
test('removing non-current: no turnOrderIds patch (1-list syncs at call site)', () => {
|
|
const e = enc([], { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'b' });
|
|
const out = computeTurnOrderAfterRemoval(e, 'a', []);
|
|
// 1-list: removal syncs turnOrderIds via participants[] at call site.
|
|
// Helper only handles current-advance. Non-current = empty patch.
|
|
expect(out).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe('addParticipant', () => {
|
|
test('appends participant', () => {
|
|
const np = p('z', 7);
|
|
const { patch } = addParticipant(enc([p('a', 10)]), np);
|
|
expect(patch.participants.map(x => x.id)).toEqual(['a', 'z']);
|
|
});
|
|
|
|
test('rejects duplicate id (skip-bug root cause)', () => {
|
|
// 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).
|
|
const existing = p('x', 5);
|
|
const dup = makeParticipant({ id: 'x', name: 'x2', type: 'monster', initiative: 10, maxHp: 100, currentHp: 100 });
|
|
expect(() => addParticipant(enc([p('a', 10), existing]), dup)).toThrow();
|
|
});
|
|
});
|