Files
ttrpg-initiative-tracker/shared/tests/turn.combat.test.js
T
david raistrick 2569cc4497 Builds replayable combat logs with first-class undo/redo, unified verification tooling, fast indexed log queries, and stricter CI.
feat(combat): add first-class undo and redo controls

Add undo and redo controls to the combat UI so the DM can recover from recent
actions without leaving the encounter view.

Undo and redo operate on the current encounter's combat history. Empty stacks
produce clear feedback instead of failing silently. Redo order follows normal
stack behavior after multiple undos.

This makes combat history actionable during play, not just visible in the log.

feat(logs): make combat logs replayable

Replace plain combat log messages with structured combat events that can be
used by the UI, exported as JSON, replayed, and verified.

Each new log entry records the action type, encounter identity, participant
identity, a small action delta, undo intent, and a turn snapshot. Download and
copy now export the event stream as JSON so a saved combat log is useful for
offline analysis and debugging.

Legacy logs remain viewable, but new logs use the structured event format.

feat(logs): make undo and redo transactional

Apply undo and redo as single storage operations so the encounter state and log
state cannot drift apart.

Server storage applies the encounter update and the log undone flag inside one
SQLite transaction. Firebase storage uses a batch write for the same behavior.
The storage contract now includes undo/redo semantics.

This replaces fragile multi-write undo behavior where a failure could update
the encounter without marking the log, or mark the log without updating the
encounter.

feat(combat): add unified replay and verification tool

Add one combat CLI for replaying live combat and verifying combat logs.

Replay drives the live backend through the same shared combat logic used by the
app, writes a JSON event log to an explicit output path, and automatically
verifies the result. Verification checks for DM-visible combat problems such as
skipped turns, double actions, bad round changes, and unexpected turn order
changes.

The tool uses the same JSON event stream produced by log downloads, supports
verbose turn output, and handles Ctrl-C by ending the encounter, writing the
partial log, and verifying what was captured.

fix(perf): keep long combat logging fast

Remove the combat-time log query bottleneck that made long replays slow as log
volume grew.

Combat controls no longer subscribe to the log collection just to keep undo and
redo state warm. Undo and redo now query the latest matching encounter log only
when clicked. Server collection queries support exact filters, ordering,
limits, and offsets, and SQLite indexes keep latest-log and per-encounter log
lookups fast.

Also fix duplicate WebSocket handler registration so realtime updates do not
double-fire under write load.

fix(turns): make toggle active a status change

Make toggle active a roster/status edit instead of a turn advance.

Deactivating the current participant no longer passes the turn or increments
the round. The current turn stays where it is until the DM explicitly clicks
Next Turn, and Next Turn skips inactive participants during normal rotation.

This matches the initiative design: slot order is stable, toggle active does
not move participants, and round changes only come from explicit turn advance.

chore(ci): make warnings and hangs fail fast

Tighten test and build checks so failures are visible instead of noisy or
silent.

Builds run with CI enabled so warnings fail production builds. The full test
command runs app, shared, and server suites with hard timeouts so hangs fail
quickly. Static eslint coverage fails on warnings as well as errors.

Tests were updated around the new async combat logging flow, structured log
events, transactional undo, replay verification, and toggle-active semantics.
2026-07-06 10:33:28 -04:00

293 lines
12 KiB
JavaScript

// Combat integrity test: replay exact op sequence through async turn.js,
// assert rotation + state invariants per round. This IS the test the audit
// was supposed to be. Deterministic (seeded RNG). RED on current code = BUG-5.
//
// Mirrors scripts/replay-combat.js op order:
// damage, heal (cleric), conditions, toggleActive, deathSave,
// removeParticipant, addParticipant, updateParticipant, pause/resume,
// reorderParticipants, revive-between-rounds.
//
// Rewritten for async turn.js API: funcs are awaited, take ctx, write own logs.
const shared = require('@ttrpg/shared');
const { mockCtx } = require('./_helpers');
const {
makeParticipant, buildCharacterParticipant, buildMonsterParticipant,
startEncounter, nextTurn, togglePause,
addParticipant, updateParticipant, removeParticipant,
toggleParticipantActive, applyHpChange, deathSave,
toggleCondition, reorderParticipants, endEncounter,
} = shared;
// ---- seeded RNG (deterministic, reproducible) ----
let _seed = 12345;
function rand() {
// LCG
_seed = (_seed * 1103515245 + 12345) & 0x7fffffff;
return _seed / 0x7fffffff;
}
const rnd = (n) => Math.floor(rand() * n);
const pick = (arr) => arr[rnd(arr.length)];
const CONDITIONS = [
'alchemist_fire','bardic_inspiration','blinded','charmed','deafened',
'exhaustion','frightened','grappled','grazed','incapacitated',
'invisible','paralyzed','petrified','poisoned','prone','restrained',
'sapped','shield','slowed','stunned','unconscious','vexed',
];
// Custom (non-built-in) condition ids: DM-added freeform strings.
// toggleCondition must accept ANY string (UI custom-condition contract).
const CUSTOM_CONDITIONS = ['hexed', 'rager', 'marked_for_death', '🛡️blessed'];
function p(id, init, extra = {}) {
return makeParticipant({
id, name: id, type: 'monster',
initiative: init, maxHp: 200, currentHp: 200,
...extra,
});
}
function setupEncounter() {
const ps = [
buildCharacterParticipant({ id:'c1', name:'Fighter', defaultMaxHp:200, defaultInitMod:2 }).participant,
buildCharacterParticipant({ id:'c2', name:'Cleric', defaultMaxHp:180, defaultInitMod:1 }).participant,
buildCharacterParticipant({ id:'c3', name:'Rogue', defaultMaxHp:160, defaultInitMod:3 }).participant,
buildMonsterParticipant({ name:'Goblin1', maxHp:100, initMod:2 }).participant,
buildMonsterParticipant({ name:'Goblin2', maxHp:100, initMod:2 }).participant,
buildMonsterParticipant({ name:'OrcBoss', maxHp:500, initMod:1 }).participant,
buildMonsterParticipant({ name:'Wolf', maxHp:120, initMod:3 }).participant,
buildMonsterParticipant({ name:'Merchant', maxHp:150, initMod:0, isNpc:true }).participant,
];
// give deterministic ids to monsters for assertions
const idMap = { Goblin1:'m1', Goblin2:'m2', OrcBoss:'m3', Wolf:'m4', Merchant:'n1' };
ps.forEach((part) => { if (idMap[part.name]) part.id = idMap[part.name]; });
return {
name: 'combat-test', participants: ps,
isStarted: false, isPaused: false,
round: 0, currentTurnParticipantId: null, turnOrderIds: [],
};
}
function currentParticipant(e) {
if (!e.currentTurnParticipantId) return null;
return (e.participants || []).find(x => x.id === e.currentTurnParticipantId) || null;
}
describe('combat integrity (100 rounds, full op coverage)', () => {
jest.setTimeout(30000);
const ROUNDS = 100;
const violations = [];
test('every round visits each active participant exactly once', async () => {
const { ctx } = mockCtx();
_seed = 12345; // reset for reproducibility
let e = setupEncounter();
e = await startEncounter(e, ctx);
let totalTurns = 0;
let lastPaused = false;
let lastReorder = 0;
let reinforcementsAdded = 0;
// built-ins first, then custom (proves arbitrary string accepted)
const condQueue = [...CONDITIONS, ...CUSTOM_CONDITIONS];
for (let roundN = 1; roundN <= ROUNDS; roundN++) {
const startRound = e.round;
const seenThisRound = [];
const cap = (e.participants.length + 2) * 2;
let guard = 0;
while (e.round === startRound && guard < cap) {
// resume if paused (must precede nextTurn)
if (lastPaused) { e = await togglePause(e, ctx); lastPaused = false; }
// advance
try {
e = await nextTurn(e, ctx);
} catch (err) {
violations.push({ round: roundN, type: 'nextTurn-throws', msg: err.message });
break;
}
totalTurns++;
// only count if turn belongs to THIS round (no wrap)
if (e.round === startRound) seenThisRound.push(e.currentTurnParticipantId);
const actor = currentParticipant(e);
// 1. damage
if (actor) {
const foes = e.participants.filter(
part => part.id !== actor.id && part.currentHp > 0 && part.isActive !== false
);
if (foes.length > 0) {
const tgt = pick(foes);
const dmg = 1 + rnd(5);
e = await applyHpChange(e, tgt.id, 'damage', dmg, ctx);
}
}
// 2. heal (cleric)
if (actor && actor.name === 'Cleric' && totalTurns % 2 === 0) {
const wounded = e.participants
.filter(part => part.currentHp > 0 && part.currentHp < part.maxHp && part.isActive !== false)
.sort((a,b)=>(a.currentHp/a.maxHp)-(b.currentHp/b.maxHp));
if (wounded.length > 0) {
const tgt = wounded[0];
const amt = 2 + rnd(5);
e = await applyHpChange(e, tgt.id, 'heal', amt, ctx);
}
}
// 3. conditions
if (condQueue.length > 0) {
const cond = condQueue[0];
const living = e.participants.filter(part => part.currentHp > 0 && part.isActive !== false);
if (living.length > 0) {
const tgt = pick(living);
try { e = await toggleCondition(e, tgt.id, cond, ctx); condQueue.shift(); }
catch (err) { condQueue.shift(); }
}
} else if (totalTurns % 6 === 0) {
const living = e.participants.filter(part => part.currentHp > 0 && part.isActive !== false);
if (living.length > 0) {
const tgt = pick(living);
const cond = pick([...CONDITIONS, ...CUSTOM_CONDITIONS]);
try { e = await toggleCondition(e, tgt.id, cond, ctx); } catch (err) {}
}
}
// 4. toggleParticipantActive
if (totalTurns % 9 === 0) {
const living = e.participants.filter(part => part.currentHp > 0);
if (living.length > 0) {
const tgt = pick(living);
try { e = await toggleParticipantActive(e, tgt.id, ctx); } catch (err) {}
}
}
// 5. deathSave
if (actor && actor.currentHp <= 0 && !actor.isNpc) {
try { const r = await deathSave(e, actor.id, 'fail', 1, ctx); e = r.enc; } catch (err) {}
}
// 6. removeParticipant
if (totalTurns % 5 === 0) {
const dead = e.participants.find(part => (part.isDying || part.currentHp <= 0) && (part.isNpc || part.name.startsWith('Goblin') || part.name === 'OrcBoss' || part.name === 'Wolf'));
if (dead) { try { e = await removeParticipant(e, dead.id, ctx); } catch (err) {} }
}
// 7. addParticipant
if (totalTurns % 10 === 0 && reinforcementsAdded < 4) {
const spec = pick([
{ name:`Reinforce${reinforcementsAdded+1}`, maxHp:120, initMod:1 },
{ name:`Summon${reinforcementsAdded+1}`, maxHp:80, initMod:4 },
]);
const built = buildMonsterParticipant(spec).participant;
try { e = await addParticipant(e, built, ctx); reinforcementsAdded++; } catch (err) {}
}
// 8. updateParticipant
if (totalTurns % 7 === 0) {
const living = e.participants.filter(part => part.currentHp > 0);
if (living.length > 0) {
const tgt = pick(living);
try { e = await updateParticipant(e, tgt.id, { notes:`edited@turn${totalTurns}` }, ctx); } catch (err) {}
}
}
// 9. pause
if (totalTurns % 12 === 0 && !lastPaused) { e = await togglePause(e, ctx); lastPaused = true; }
// 10. reorderParticipants (mirror replay's buggy signature usage — swallowed no-op)
if (totalTurns % 8 === 0 && lastReorder !== totalTurns) {
const living = e.participants.filter(part => part.currentHp > 0 && part.isActive !== false);
if (living.length >= 2) {
const tgt = living[0];
const newInit = (tgt.initiative || 0) + 1;
try {
const reordered = [...e.participants].map(part => part.id === tgt.id ? { ...part, initiative: newInit } : part);
e = await reorderParticipants(e, reordered); // array as draggedId throws → swallowed
lastReorder = totalTurns;
} catch (err) {}
}
}
guard++;
if (!e.isStarted) break;
}
if (!e.isStarted) break;
// === per-round invariants ===
const uniq = new Set(seenThisRound);
if (uniq.size !== seenThisRound.length) {
violations.push({ round: roundN, type: 'rotation-dupe',
seen: seenThisRound.map(id => e.participants.find(part=>part.id===id)?.name||id) });
}
// turnOrderIds no dup
const orderUniq = new Set(e.turnOrderIds);
if (orderUniq.size !== e.turnOrderIds.length) {
violations.push({ round: roundN, type: 'turnOrder-dup-id', order: e.turnOrderIds });
}
// currentTurn valid. It may be inactive immediately after DM toggles
// current actor inactive; toggle-active is status edit, not turn advance.
// Next Turn is responsible for skipping inactive current.
if (e.currentTurnParticipantId) {
const ct = e.participants.find(part => part.id === e.currentTurnParticipantId);
if (!ct) violations.push({ round: roundN, type: 'currentTurn-missing' });
}
// HP bounds
for (const part of e.participants) {
if (typeof part.currentHp !== 'number' || isNaN(part.currentHp) || part.currentHp < 0 || part.currentHp > part.maxHp) {
violations.push({ round: roundN, type: 'hp-invalid', id: part.id, hp: part.currentHp, max: part.maxHp });
}
// custom conditions persisted as raw strings (not dropped)
for (const c of (part.conditions || [])) {
if (typeof c !== 'string' || !c) {
violations.push({ round: roundN, type: 'condition-not-string', id: part.id, c });
}
}
}
// custom (freeform) conditions must survive toggle round-trip
// (presence verified via condition-not-string invariant above;
// queue drains them through toggleCondition which proves acceptance)
// revive dead between rounds
const dead = e.participants.filter(part => part.currentHp <= 0 || part.isActive === false);
for (const d of dead) {
try {
if (d.isActive === false) e = await toggleParticipantActive(e, d.id, ctx);
e = await applyHpChange(e, d.id, 'heal', d.maxHp, ctx);
} catch (err) {}
}
}
// Report
if (violations.length > 0) {
const byType = {};
violations.forEach(v => { byType[v.type] = (byType[v.type]||0) + 1; });
const summary = Object.entries(byType).sort((a,b)=>b[1]-a[1]).map(([k,n])=>`${n}x ${k}`).join(', ');
const first5 = violations.slice(0,5).map(v => `r${v.round} ${v.type}${v.seen?': '+JSON.stringify(v.seen):''}${v.order?': '+JSON.stringify(v.order):''}${v.msg?': '+v.msg:''}`).join('\n ');
// dump full state for first dupe for triage
throw new Error(`combat integrity violations: ${violations.length}\n ${summary}\n first 5:\n ${first5}`);
}
expect(violations).toHaveLength(0);
});
// Custom (freeform) condition strings: UI contract.
// toggleCondition must accept ANY string, not just built-ins.
test('custom condition strings survive add/remove round-trip', async () => {
const { ctx } = mockCtx();
let e = setupEncounter();
e = await startEncounter(e, ctx);
const tgt = e.participants[0].id;
// add custom (not in built-ins)
e = await toggleCondition(e, tgt, 'hexed', ctx);
let part = e.participants.find(x => x.id === tgt);
expect(part.conditions).toContain('hexed');
// emoji-bearing custom id
e = await toggleCondition(e, tgt, '🛡️blessed', ctx);
part = e.participants.find(x => x.id === tgt);
expect(part.conditions).toContain('🛡️blessed');
// remove
e = await toggleCondition(e, tgt, 'hexed', ctx);
part = e.participants.find(x => x.id === tgt);
expect(part.conditions).not.toContain('hexed');
expect(part.conditions).toContain('🛡️blessed');
});
});