91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
// Death saves: broken model. Current = single counter, 3=dying. D&D 5e needs
|
|||
|
|
// separate success/fail tracking:
|
||
|
|
// 3 successes = stable (0hp unconscious, safe until healed)
|
||
|
|
// 3 failures = dead
|
||
|
|
// Current tracks "saves" (really fails via red X UI), no successes at all.
|
||
|
|
// "Stable" state doesn't exist.
|
||
|
|
//
|
||
|
|
// RED: prove current can't model 3-success stability.
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
const shared = require('@ttrpg/shared');
|
||
|
|
const { makeParticipant, startEncounter, applyHpChange, deathSave } = shared;
|
||
|
|
|
||
|
|
function p(id) {
|
||
|
|
return makeParticipant({ id, name: id, type: 'character',
|
||
|
|
initiative: 10, maxHp: 100, currentHp: 100 });
|
||
|
|
}
|
||
|
|
function enc(ps, extra = {}) {
|
||
|
|
return { name:'t', participants:ps, isStarted:false, isPaused:false,
|
||
|
|
round:0, currentTurnParticipantId:null, turnOrderIds:[], ...extra };
|
||
|
|
}
|
||
|
|
const apply = (e, r) => r && r.patch ? { ...e, ...r.patch } : e;
|
||
|
|
|
||
|
|
describe('death saves: missing success/stable model', () => {
|
||
|
|
test('3 successes makes character stable (not dead, not dying)', () => {
|
||
|
|
// Character at 0hp. Roll 3 successful death saves.
|
||
|
|
let e = enc([p('a')]);
|
||
|
|
e = apply(e, startEncounter(e));
|
||
|
|
e = apply(e, applyHpChange(e, 'a', 'damage', 100)); // 0hp
|
||
|
|
expect(e.participants[0].currentHp).toBe(0);
|
||
|
|
|
||
|
|
// Roll 3 successful saves. Should mark stable.
|
||
|
|
e = apply(e, deathSave(e, 'a', 'success', 1));
|
||
|
|
e = apply(e, deathSave(e, 'a', 'success', 2));
|
||
|
|
const r3 = deathSave(e, 'a', 'success', 3);
|
||
|
|
e = apply(e, r3);
|
||
|
|
|
||
|
|
// RED: current deathSave signature = (enc, id, saveNumber). No type arg.
|
||
|
|
// Will throw or misbehave. Want: status field.
|
||
|
|
expect(r3.status).toBe('stable');
|
||
|
|
expect(e.participants[0].isDying).toBe(false);
|
||
|
|
expect(e.participants[0].isStable).toBe(true);
|
||
|
|
expect(e.participants[0].deathFails).toBe(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('3 failures makes character dead (isDying for removal)', () => {
|
||
|
|
let e = enc([p('a')]);
|
||
|
|
e = apply(e, startEncounter(e));
|
||
|
|
e = apply(e, applyHpChange(e, 'a', 'damage', 100));
|
||
|
|
|
||
|
|
const r1 = deathSave(e, 'a', 'fail', 1);
|
||
|
|
e = apply(e, r1);
|
||
|
|
const r2 = deathSave(e, 'a', 'fail', 2);
|
||
|
|
e = apply(e, r2);
|
||
|
|
const r3 = deathSave(e, 'a', 'fail', 3);
|
||
|
|
e = apply(e, r3);
|
||
|
|
|
||
|
|
expect(r3.status).toBe('dead');
|
||
|
|
expect(e.participants[0].isDying).toBe(true);
|
||
|
|
expect(e.participants[0].deathFails).toBe(3);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('successes and fails tracked independently', () => {
|
||
|
|
// Mixed: 1 success, 2 fails, then 2 more successes = stable.
|
||
|
|
let e = enc([p('a')]);
|
||
|
|
e = apply(e, startEncounter(e));
|
||
|
|
e = apply(e, applyHpChange(e, 'a', 'damage', 100));
|
||
|
|
|
||
|
|
e = apply(e, deathSave(e, 'a', 'success', 1));
|
||
|
|
expect(e.participants[0].deathSaves).toBe(1);
|
||
|
|
expect(e.participants[0].deathFails).toBe(0);
|
||
|
|
|
||
|
|
e = apply(e, deathSave(e, 'a', 'fail', 1));
|
||
|
|
e = apply(e, deathSave(e, 'a', 'fail', 2));
|
||
|
|
expect(e.participants[0].deathSaves).toBe(1);
|
||
|
|
expect(e.participants[0].deathFails).toBe(2);
|
||
|
|
|
||
|
|
// 2 more successes → total 3 successes → stable
|
||
|
|
e = apply(e, deathSave(e, 'a', 'success', 2));
|
||
|
|
const r = deathSave(e, 'a', 'success', 3);
|
||
|
|
expect(r.status).toBe('stable');
|
||
|
|
expect(e.participants[0].deathFails).toBe(2); // fails unchanged
|
||
|
|
});
|
||
|
|
|
||
|
|
test('deathSave signature: (enc, id, type, n)', () => {
|
||
|
|
// type = 'success' | 'fail'. n = 1|2|3.
|
||
|
|
expect(deathSave.length).toBe(4);
|
||
|
|
});
|
||
|
|
});
|