const { challengeXp, encounterDifficulty, xpProgress } = require('./calculations'); const { CR_XP, XP_THRESHOLDS, LEVEL_XP } = require('./data/xp'); const { MONSTER_LIBRARY } = require('./data/library'); describe('challengeXp', () => { test('reads a rating however it was typed', () => { expect(challengeXp('0')).toBe(10); expect(challengeXp('1/8')).toBe(25); expect(challengeXp(' 1/4 ')).toBe(50); expect(challengeXp('0.5')).toBe(100); expect(challengeXp('CR 3')).toBe(700); expect(challengeXp(5)).toBe(1800); expect(challengeXp('30')).toBe(155000); }); test('is null for what is not a rating', () => { for (const text of [null, undefined, '', 'boss', '31', '1/3', '-1']) expect(challengeXp(text)).toBeNull(); }); test('every monster in the built-in bestiary has a rating it can read', () => { for (const monster of MONSTER_LIBRARY) expect(challengeXp(monster.challengeRating)).not.toBeNull(); }); }); describe('the tables', () => { test('cover every rating from 0 to 30 and every level from 1 to 20, always rising', () => { expect(Object.keys(CR_XP)).toHaveLength(34); expect(XP_THRESHOLDS).toHaveLength(20); expect(LEVEL_XP).toHaveLength(20); for (let level = 1; level < 20; level++) { expect(LEVEL_XP[level]).toBeGreaterThan(LEVEL_XP[level - 1]); for (let i = 0; i < 4; i++) expect(XP_THRESHOLDS[level][i]).toBeGreaterThanOrEqual(XP_THRESHOLDS[level - 1][i]); } for (const [easy, medium, hard, deadly] of XP_THRESHOLDS) expect(easy < medium && medium < hard && hard < deadly).toBe(true); }); }); describe('encounterDifficulty', () => { const party = [1, 1, 1, 1]; // easy 100, medium 200, hard 300, deadly 400 test('adds up the party\'s thresholds and the monsters\' XP', () => { const one = encounterDifficulty(party, ['1/2']); expect(one).toEqual({ xp: 100, adjustedXp: 100, multiplier: 1, rating: 'EASY', thresholds: { easy: 100, medium: 200, hard: 300, deadly: 400 }, monsters: 1, unrated: 0 }); expect(encounterDifficulty([3, 5], []).thresholds).toEqual({ easy: 325, medium: 650, hard: 975, deadly: 1500 }); }); test('more monsters make a harder fight than their XP alone', () => { expect(encounterDifficulty(party, ['1/8'])).toMatchObject({ xp: 25, adjustedXp: 25, rating: 'TRIVIAL' }); expect(encounterDifficulty(party, ['1/4', '1/4'])).toMatchObject({ xp: 100, multiplier: 1.5, adjustedXp: 150, rating: 'EASY' }); expect(encounterDifficulty(party, ['1/4', '1/4', '1/4'])).toMatchObject({ xp: 150, multiplier: 2, adjustedXp: 300, rating: 'HARD' }); expect(encounterDifficulty(party, ['1/4', '1/4', '1/4', '1/4'])).toMatchObject({ xp: 200, adjustedXp: 400, rating: 'DEADLY' }); expect(encounterDifficulty(party, Array(7).fill('0')).multiplier).toBe(2.5); expect(encounterDifficulty(party, Array(11).fill('0')).multiplier).toBe(3); expect(encounterDifficulty(party, Array(15).fill('0')).multiplier).toBe(4); }); test('leaves monsters with no rating out, and says how many', () => { expect(encounterDifficulty(party, ['1', 'boss', null])).toMatchObject({ xp: 200, adjustedXp: 200, monsters: 1, unrated: 2, rating: 'MEDIUM' }); }); test('has no rating without a party, or with nothing rated to fight', () => { expect(encounterDifficulty([], ['1'])).toMatchObject({ xp: 200, rating: null, thresholds: null }); expect(encounterDifficulty(party, [])).toMatchObject({ xp: 0, rating: null }); expect(encounterDifficulty(party, ['boss'])).toMatchObject({ rating: null, unrated: 1 }); }); }); describe('xpProgress', () => { test('says what the next level takes and whether it is due', () => { expect(xpProgress(0, 1)).toEqual({ nextLevelAt: 300, levelUpDue: false }); expect(xpProgress(299, 1)).toEqual({ nextLevelAt: 300, levelUpDue: false }); expect(xpProgress(300, 1)).toEqual({ nextLevelAt: 300, levelUpDue: true }); expect(xpProgress(300, 2)).toEqual({ nextLevelAt: 900, levelUpDue: false }); expect(xpProgress(999999, 20)).toEqual({ nextLevelAt: null, levelUpDue: false }); }); });