const { spellSlotsFor, spellSlotState, maxSpellLevel, hitDiceState, computeCharacterSheet } = require('./calculations'); describe('spellSlotsFor', () => { test('full casters follow the standard table', () => { expect(spellSlotsFor('WIZARD', 1)).toEqual({ kind: 'FULL', restoresOn: 'LONG', max: [2] }); expect(spellSlotsFor('CLERIC', 3).max).toEqual([4, 2]); expect(spellSlotsFor('BARD', 5).max).toEqual([4, 3, 2]); expect(spellSlotsFor('DRUID', 9).max).toEqual([4, 3, 3, 3, 1]); expect(spellSlotsFor('SORCERER', 10).max).toEqual([4, 3, 3, 3, 2]); expect(spellSlotsFor('WIZARD', 20).max).toEqual([4, 3, 3, 3, 3, 2, 2, 1, 1]); }); test('half casters start at level 2', () => { expect(spellSlotsFor('PALADIN', 1).max).toEqual([]); expect(spellSlotsFor('PALADIN', 2).max).toEqual([2]); expect(spellSlotsFor('RANGER', 4).max).toEqual([3]); expect(spellSlotsFor('RANGER', 5).max).toEqual([4, 2]); expect(spellSlotsFor('PALADIN', 10).max).toEqual([4, 3, 2]); }); test('a warlock has a few slots, all of one level, back on a short rest', () => { expect(spellSlotsFor('WARLOCK', 1)).toEqual({ kind: 'PACT', restoresOn: 'SHORT', max: [1] }); expect(spellSlotsFor('WARLOCK', 2).max).toEqual([2]); expect(spellSlotsFor('WARLOCK', 3).max).toEqual([0, 2]); expect(spellSlotsFor('WARLOCK', 9).max).toEqual([0, 0, 0, 0, 2]); }); test('classes without spells have none', () => { for (const cls of ['FIGHTER', 'BARBARIAN', 'ROGUE', 'MONK', null]) expect(spellSlotsFor(cls, 10)).toBeNull(); }); }); describe('spellSlotState', () => { test('counts what is spent against what there is', () => { expect(spellSlotState('WIZARD', 3, [1, 2]).levels).toEqual([ { level: 1, max: 4, used: 1, remaining: 3 }, { level: 2, max: 2, used: 2, remaining: 0 }, ]); }); test('never counts more spent than the class has, or a level it has lost', () => { expect(spellSlotState('WIZARD', 1, [9, 2]).levels).toEqual([{ level: 1, max: 2, used: 2, remaining: 0 }]); }); test('lists only the slot levels a warlock actually has', () => { expect(spellSlotState('WARLOCK', 5, [0, 0, 1])).toEqual({ kind: 'PACT', restoresOn: 'SHORT', levels: [{ level: 3, max: 2, used: 1, remaining: 1 }] }); }); test('is null where there is nothing to track', () => { expect(spellSlotState('FIGHTER', 5, [])).toBeNull(); expect(spellSlotState('PALADIN', 1, [])).toBeNull(); }); }); describe('maxSpellLevel', () => { test('is the highest slot level, as in the unlock schedule', () => { expect([1, 3, 5, 7, 9].map((level) => maxSpellLevel('WIZARD', level))).toEqual([1, 2, 3, 4, 5]); expect([1, 3, 5, 7, 9].map((level) => maxSpellLevel('WARLOCK', level))).toEqual([1, 2, 3, 4, 5]); expect([1, 2, 5, 9].map((level) => maxSpellLevel('PALADIN', level))).toEqual([0, 1, 2, 3]); expect(maxSpellLevel('ROGUE', 9)).toBe(0); }); }); describe('hitDiceState', () => { test('one die per level, of the class size, less what is spent', () => { expect(hitDiceState('BARBARIAN', 4, 1)).toEqual({ size: 12, count: 4, used: 1, remaining: 3 }); expect(hitDiceState('WIZARD', 2, 9)).toEqual({ size: 6, count: 2, used: 2, remaining: 0 }); }); }); describe('the sheet', () => { test('carries the slot tracker and the Hit Dice', () => { const sheet = computeCharacterSheet({ characterClass: 'CLERIC', level: 3, spellSlotsUsed: [1], hitDiceUsed: 2 }); expect(sheet.spellSlots.levels.map((s) => s.remaining)).toEqual([3, 2]); expect(sheet.hitDice).toEqual({ size: 8, count: 3, used: 2, remaining: 1 }); expect(computeCharacterSheet({ characterClass: 'ROGUE', level: 3 }).spellSlots).toBeNull(); }); });