const { lifeState, settleDeathSaves, changeHp, rollDeathSave } = require('./hp'); const CLEAR = { deathSaveSuccesses: 0, deathSaveFailures: 0, stable: false }; const hero = (over = {}) => ({ currentHp: 10, tempHp: 0, ...CLEAR, ...over }); describe('lifeState', () => { test('reads up, dying, stable and dead off the row', () => { expect(lifeState(hero())).toBe('UP'); expect(lifeState(hero({ currentHp: 0 }))).toBe('DYING'); expect(lifeState(hero({ currentHp: 0, stable: true }))).toBe('STABLE'); expect(lifeState(hero({ currentHp: 0, deathSaveFailures: 3 }))).toBe('DEAD'); }); }); describe('changeHp', () => { test('ordinary damage and healing leave the death saves clear', () => { expect(changeHp(hero(), 4, 20)).toEqual({ currentHp: 4, tempHp: 0, ...CLEAR }); expect(changeHp(hero(), 99, 20)).toEqual({ currentHp: 20, tempHp: 0, ...CLEAR }); }); test('dropping to 0 starts the saves from nothing', () => { expect(changeHp(hero(), -5, 20)).toEqual({ currentHp: 0, tempHp: 0, ...CLEAR }); }); test('damage at 0 HP is a failed save and ends stable', () => { const down = hero({ currentHp: 0, deathSaveSuccesses: 2, deathSaveFailures: 1 }); expect(changeHp(down, -3, 20)).toMatchObject({ currentHp: 0, deathSaveSuccesses: 2, deathSaveFailures: 2, stable: false }); expect(changeHp(hero({ currentHp: 0, stable: true }), -3, 20)).toMatchObject({ deathSaveFailures: 1, stable: false }); expect(lifeState(changeHp(hero({ currentHp: 0, deathSaveFailures: 2 }), -1, 20))).toBe('DEAD'); }); test('temp HP that soaks the whole blow costs no save', () => { const down = hero({ currentHp: 0, tempHp: 5, deathSaveFailures: 1 }); expect(changeHp(down, -5, 20)).toEqual({ currentHp: 0, tempHp: 0 }); }); test('asking for the HP they already have at 0 changes nothing', () => { expect(changeHp(hero({ currentHp: 0, deathSaveFailures: 2 }), 0, 20)).toEqual({ currentHp: 0, tempHp: 0 }); }); test('damage of max HP or more past zero kills outright', () => { expect(lifeState(changeHp(hero(), -19, 20))).toBe('DYING'); expect(lifeState(changeHp(hero(), -20, 20))).toBe('DEAD'); expect(lifeState(changeHp(hero({ currentHp: 0 }), -20, 20))).toBe('DEAD'); // Temp HP counts against the blow first. expect(lifeState(changeHp(hero({ tempHp: 1 }), -20, 20))).toBe('DYING'); }); test('healing from 0 puts them back up with a clean slate', () => { const down = hero({ currentHp: 0, deathSaveSuccesses: 1, deathSaveFailures: 2 }); expect(changeHp(down, 1, 20)).toEqual({ currentHp: 1, tempHp: 0, ...CLEAR }); expect(changeHp(hero({ currentHp: 0, stable: true }), 6, 20)).toEqual({ currentHp: 6, tempHp: 0, ...CLEAR }); }); }); describe('rollDeathSave', () => { const down = (successes, failures) => hero({ currentHp: 0, deathSaveSuccesses: successes, deathSaveFailures: failures }); test('10 or more succeeds, less fails, a 1 fails twice', () => { expect(rollDeathSave(down(0, 0), 10)).toEqual({ deathSaveSuccesses: 1, deathSaveFailures: 0, stable: false }); expect(rollDeathSave(down(0, 0), 19)).toEqual({ deathSaveSuccesses: 1, deathSaveFailures: 0, stable: false }); expect(rollDeathSave(down(0, 0), 9)).toEqual({ deathSaveSuccesses: 0, deathSaveFailures: 1, stable: false }); expect(rollDeathSave(down(0, 0), 1)).toEqual({ deathSaveSuccesses: 0, deathSaveFailures: 2, stable: false }); }); test('the third success makes them stable and clears the tallies', () => { expect(rollDeathSave(down(2, 2), 14)).toEqual({ ...CLEAR, stable: true }); }); test('the third failure is death, and never counts past three', () => { expect(rollDeathSave(down(2, 2), 5)).toMatchObject({ deathSaveFailures: 3 }); expect(rollDeathSave(down(0, 2), 1)).toMatchObject({ deathSaveFailures: 3 }); }); test('a 20 is 1 HP and a clean slate', () => { expect(rollDeathSave(down(0, 2), 20)).toEqual({ currentHp: 1, ...CLEAR }); }); }); describe('settleDeathSaves', () => { test('failures win a tie at three', () => { expect(settleDeathSaves(3, 3)).toMatchObject({ deathSaveFailures: 3, stable: false }); }); });