const { hitDiceProblem, shortRest, longRest } = require('./rest'); const { computeCharacterSheet } = require('../game/calculations'); const CLEAR = { deathSaveSuccesses: 0, deathSaveFailures: 0, stable: false }; // A level 5 fighter (d10) with CON 14 (+2): 10 + 2 + 4 × (6 + 2) = 44 HP. const hero = (over = {}) => ({ name: 'Aria', characterClass: 'FIGHTER', level: 5, conScore: 14, currentHp: 10, tempHp: 0, hitDiceUsed: 0, spellSlotsUsed: [], specialAbilities: null, ...CLEAR, ...over, }); const sheet = (character) => computeCharacterSheet(character); describe('shortRest', () => { test('spent Hit Dice heal what they rolled plus Constitution for each', () => { const c = hero(); const { data, result } = shortRest(c, sheet(c), { dice: 2, rolls: [7, 4] }); expect(data).toMatchObject({ currentHp: 25, hitDiceUsed: 2 }); expect(result).toMatchObject({ hpBefore: 10, hpAfter: 25, maxHp: 44 }); expect(result.hitDice).toMatchObject({ size: 10, spent: 2, total: 11, conBonus: 4, healed: 15, left: 3, of: 5 }); }); test('takes a total rolled at the table, stops at max HP, and never takes HP away', () => { const c = hero({ currentHp: 40 }); expect(shortRest(c, sheet(c), { dice: 1, rolled: 9 }).data.currentHp).toBe(44); const frail = hero({ conScore: 3, currentHp: 10 }); // CON -4 const rest = shortRest(frail, sheet(frail), { dice: 1, rolled: 2 }); expect(rest.data.currentHp).toBeUndefined(); expect(rest.data.hitDiceUsed).toBe(1); expect(rest.result.hitDice.healed).toBe(0); }); test('with no dice spent nothing changes for a fighter', () => { const c = hero({ hitDiceUsed: 3 }); expect(shortRest(c, sheet(c)).data).toEqual({ hitDiceUsed: 3, resourcesUsed: {} }); }); test('gives back the class counters that restore on a short rest, and only those', () => { const c = hero({ level: 9, resourcesUsed: { SECOND_WIND: 1, ACTION_SURGE: 1, INDOMITABLE: 1 } }); const { data, result } = shortRest(c, sheet(c)); expect(data.resourcesUsed).toEqual({ INDOMITABLE: 1 }); expect(result.notes).toEqual(['Back: Second Wind, Action Surge.']); }); test('gives a warlock their slots back, and nobody else', () => { const warlock = hero({ characterClass: 'WARLOCK', spellSlotsUsed: [0, 0, 2] }); const rest = shortRest(warlock, sheet(warlock)); expect(rest.data.spellSlotsUsed).toEqual([]); expect(rest.result.slotsRestored).toBe(true); const wizard = hero({ characterClass: 'WIZARD', spellSlotsUsed: [2, 1] }); const wizardRest = shortRest(wizard, sheet(wizard)); expect(wizardRest.data.spellSlotsUsed).toBeUndefined(); expect(wizardRest.result.slotsRestored).toBe(false); // ...but a wizard is reminded of Arcane Recovery: half their level, rounded up. expect(wizardRest.result.notes).toEqual([expect.stringContaining('Arcane Recovery')]); expect(wizardRest.result.notes[0]).toContain('3 slot levels'); const fresh = hero({ characterClass: 'WIZARD' }); expect(shortRest(fresh, sheet(fresh)).result.notes).toEqual([]); }); test('wakes a stable character who spends a Hit Die', () => { const c = hero({ currentHp: 0, stable: true }); expect(shortRest(c, sheet(c), { dice: 1, rolled: 5 }).data).toMatchObject({ currentHp: 7, ...CLEAR }); }); }); describe('hitDiceProblem', () => { test('refuses more dice than are left, an impossible total, and the dying', () => { const c = hero({ hitDiceUsed: 4 }); expect(hitDiceProblem(c, sheet(c), 0)).toBeNull(); expect(hitDiceProblem(c, sheet(c), 1, 10)).toBeNull(); expect(hitDiceProblem(c, sheet(c), 1)).toBeNull(); expect(hitDiceProblem(c, sheet(c), 2)).toMatch(/1 Hit Die left/); expect(hitDiceProblem(c, sheet(c), 1, 11)).toMatch(/between 1 and 10/); expect(hitDiceProblem(c, sheet(c), 1, 0)).toMatch(/between 1 and 10/); const dying = hero({ currentHp: 0 }); expect(hitDiceProblem(dying, sheet(dying), 1)).toMatch(/dying/); }); }); describe('longRest', () => { test('heals to full, gives back every slot and half the level in Hit Dice', () => { const c = hero({ characterClass: 'WIZARD', currentHp: 3, tempHp: 4, hitDiceUsed: 5, spellSlotsUsed: [4, 3, 2] }); const { data, result } = longRest(c, sheet(c)); expect(data).toEqual({ currentHp: sheet(c).hp.max, tempHp: 0, ...CLEAR, hitDiceUsed: 3, spellSlotsUsed: [], resourcesUsed: {}, exhaustion: 0 }); expect(result.hitDice).toMatchObject({ regained: 2, left: 2, of: 5 }); expect(result.slotsRestored).toBe(true); }); test('gives back at least one Hit Die, and no more than were spent', () => { const novice = hero({ level: 1, hitDiceUsed: 1 }); expect(longRest(novice, sheet(novice)).data.hitDiceUsed).toBe(0); const rested = hero({ hitDiceUsed: 1 }); expect(longRest(rested, sheet(rested)).result.hitDice).toMatchObject({ regained: 1, left: 5 }); }); test('gets a dying character back on their feet', () => { const c = hero({ currentHp: 0, deathSaveFailures: 2, deathSaveSuccesses: 1 }); expect(longRest(c, sheet(c)).data).toMatchObject({ currentHp: 44, ...CLEAR }); }); test('gives every class counter back and takes one level of exhaustion off', () => { const c = hero({ level: 9, exhaustion: 2, resourcesUsed: { SECOND_WIND: 1, INDOMITABLE: 1 } }); const { data, result } = longRest(c, sheet(c)); expect(data).toMatchObject({ resourcesUsed: {}, exhaustion: 1 }); expect(result.notes).toEqual(['Back: Second Wind, Indomitable.', 'Exhaustion down to level 1.']); const tired = hero({ exhaustion: 1 }); expect(longRest(tired, sheet(tired)).result.notes).toEqual(['No longer exhausted.']); }); test('leaves the Hardy Boon its Temp HP', () => { const c = hero({ tempHp: 2, specialAbilities: 'Hardy — +10 maximum HP: gain 10 Temp HP after each long rest.' }); const { data, result } = longRest(c, sheet(c)); expect(data.tempHp).toBe(10); expect(result.notes).toEqual(['Hardy: 10 Temp HP for the day.']); }); });