Files
ttrpg-initiative-tracker/shared/tests/turn.stablehours.test.js
T

99 lines
3.0 KiB
JavaScript
Raw Normal View History

'use strict';
// Stable regain hours: stabilization rolls 1d4, persists until healed/revived.
// 3rd death save -> stable; manual stabilize -> stable; revive dead -> stable (5e).
const {
makeParticipant,
startEncounter,
applyHpChange,
deathSave,
stabilizeParticipant,
reviveParticipant,
} = require('@ttrpg/shared');
const { mockCtx } = require('./_helpers');
function pc(id, extra = {}) {
return makeParticipant({
id,
name: id,
type: 'character',
initiative: 10,
maxHp: 100,
currentHp: 100,
...extra,
});
}
function enc(participants, extra = {}) {
return {
id: 'e1',
name: 'stable-hours-test',
participants,
isStarted: false,
isPaused: false,
round: 0,
currentTurnParticipantId: null,
turnOrderIds: [],
...extra,
};
}
const part = e => e.participants.find(p => p.id === 'a');
async function dyingEnc() {
const { ctx } = mockCtx();
let e = enc([pc('a')]);
e = await startEncounter(e, ctx);
e = await applyHpChange(e, 'a', 'damage', 100, ctx);
return { e, ctx };
}
describe('stableRegainHours', () => {
test('3rd death save success rolls 1d4 and stores stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
let out = e;
for (let i = 0; i < 3; i++) out = (await deathSave(out, 'a', 'success', ctx)).enc;
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('manual stabilize also rolls and stores hours', async () => {
const { e, ctx } = await dyingEnc();
const out = await stabilizeParticipant(e, 'a', ctx);
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('heal clears stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
let out = await stabilizeParticipant(e, 'a', ctx);
expect(part(out).stableRegainHours).toBeGreaterThanOrEqual(1);
out = await applyHpChange(out, 'a', 'heal', 5, ctx);
expect(part(out).status).toBe('conscious');
expect(part(out).stableRegainHours == null).toBe(true);
});
test('reviveParticipant to stable rolls fresh hours', async () => {
const { e, ctx } = await dyingEnc();
let out = e;
for (let i = 0; i < 3; i++) out = (await deathSave(out, 'a', 'fail', ctx)).enc;
expect(part(out).status).toBe('dead');
out = await reviveParticipant(out, 'a', ctx);
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('dying (not yet stable) has no stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
const out = (await deathSave(e, 'a', 'fail', ctx)).enc;
expect(part(out).status).toBe('dying');
expect(part(out).stableRegainHours == null).toBe(true);
});
});