const { featuresAt, addFeatures, boonTaken, boonOptions, hardyTempHp, advance, spellsGainedAt, levelUpPlan, switchSubclass, LevelUpError } = require('./levelUp'); const { SUBCLASSES, SUBCLASS_LEVEL } = require('./data/subclasses'); const { LEVEL_FEATURES, BOONS } = require('./data/levelFeatures'); const { CLASSES } = require('./data/classes'); const { composeSpecialAbilities } = require('./buildFlow'); const boon = (key) => BOONS.find((b) => b.key === key); const CHAMPION = { key: 'CHAMPION' }; describe('the class tables', () => { test('cover every class from level 2 to 10, with a Boon at 4 and 8', () => { for (const cls of Object.keys(CLASSES)) { expect(LEVEL_FEATURES[cls]).toBeDefined(); for (const level of [4, 8]) expect(featuresAt(cls, level).some((f) => f.boon)).toBe(true); for (const level of Object.keys(LEVEL_FEATURES[cls])) expect(Number(level)).toBeGreaterThanOrEqual(2); } // Fighters get a third Boon at 6, rogues at 10. expect(featuresAt('FIGHTER', 6).some((f) => f.boon)).toBe(true); expect(featuresAt('ROGUE', 10).some((f) => f.boon)).toBe(true); expect(featuresAt('WIZARD', 6).some((f) => f.boon)).toBe(false); }); test('every feature has a name and a text', () => { for (const levels of Object.values(LEVEL_FEATURES)) { for (const features of Object.values(levels)) { for (const f of features) expect(f.boon || (f.name && f.text)).toBeTruthy(); } } }); test('casters are told at level 5 that cantrips double; others are not', () => { expect(featuresAt('WIZARD', 5).map((f) => f.name)).toContain('Cantrips'); expect(featuresAt('PALADIN', 5).map((f) => f.name)).toEqual(['Extra Attack', 'Cantrips']); expect(featuresAt('FIGHTER', 5).map((f) => f.name)).toEqual(['Extra Attack']); }); test('nothing is written past level 10', () => { expect(featuresAt('FIGHTER', 11)).toEqual([]); }); }); describe('addFeatures', () => { test('adds a new feature as a line of its own, once', () => { const once = addFeatures('Second Wind — heal.', [{ name: 'Action Surge', text: 'One extra action.' }]); expect(once).toBe('Second Wind — heal.\nAction Surge — One extra action.'); expect(addFeatures(once, [{ name: 'Action Surge', text: 'One extra action.' }])).toBe(once); }); test('an upgrade takes the place of the line with its name, however the dash was typed', () => { const upgrade = { name: 'Sneak Attack', text: '2d6.', upgrade: true }; expect(addFeatures('Sneak Attack — 1d6.\nThieves’ Cant — slang.', [upgrade])).toBe('Sneak Attack — 2d6.\nThieves’ Cant — slang.'); expect(addFeatures('sneak attack -- 1d6.', [upgrade])).toBe('Sneak Attack — 2d6.'); // With no line to rewrite it is simply added. expect(addFeatures(null, [upgrade])).toBe('Sneak Attack — 2d6.'); }); test('a feature that is not an upgrade leaves a line of the same name alone', () => { const text = addFeatures('Favored Enemy — Undead.', [{ name: 'Favored Enemy', text: 'Choose a second.' }]); expect(text).toBe('Favored Enemy — Undead.\nFavored Enemy — Choose a second.'); }); }); describe('advance', () => { test('a rogue reaching level 3 has a bigger Sneak Attack and two new features', () => { const level1 = composeSpecialAbilities('HUMAN', 'ROGUE', []); const { specialAbilities } = advance(advance(level1, 'ROGUE', 2).specialAbilities, 'ROGUE', 3, undefined, { key: 'THIEF', chosen: true }); const lines = specialAbilities.split('\n'); expect(lines.filter((l) => l.startsWith('Sneak Attack'))).toEqual([expect.stringContaining('2d6')]); expect(lines.some((l) => l.startsWith('Cunning Action — '))).toBe(true); expect(lines.some((l) => l.startsWith('Fast Hands — '))).toBe(true); expect(lines.some((l) => l.startsWith('Versatile — '))).toBe(true); // what was there stays }); test('a Boon is written down, and says what it does beyond the text', () => { expect(advance(null, 'FIGHTER', 4, 'TOUGHENED', CHAMPION)).toEqual({ specialAbilities: 'Toughened — +1 AC (counted in your Extra AC).', armorBonus: 1, tempHp: 0 }); const hardy = advance(null, 'FIGHTER', 4, 'HARDY', CHAMPION); expect(hardy).toMatchObject({ armorBonus: 0, tempHp: 5 }); // Taken again, its one line grows. const twice = advance(hardy.specialAbilities, 'FIGHTER', 6, 'HARDY', CHAMPION); expect(twice.specialAbilities).toBe('Hardy — +10 maximum HP: gain 10 Temp HP after each long rest.'); expect(twice.tempHp).toBe(10); expect(hardyTempHp(twice.specialAbilities)).toBe(10); }); test('a Boon cannot be taken more often than it allows', () => { const toughened = advance(null, 'FIGHTER', 4, 'TOUGHENED', CHAMPION).specialAbilities; expect(() => advance(toughened, 'FIGHTER', 6, 'TOUGHENED')).toThrow(LevelUpError); const mastery = advance(advance(null, 'FIGHTER', 4, 'WEAPON_MASTERY').specialAbilities, 'FIGHTER', 6, 'WEAPON_MASTERY').specialAbilities; expect(boonTaken(mastery, boon('WEAPON_MASTERY'))).toBe(2); expect(() => advance(mastery, 'FIGHTER', 8, 'WEAPON_MASTERY')).toThrow(LevelUpError); expect(() => advance(null, 'FIGHTER', 4, 'FLIGHT')).toThrow(LevelUpError); expect(boonOptions(mastery).find((o) => o.key === 'WEAPON_MASTERY').available).toBe(false); expect(boonOptions(mastery).find((o) => o.key === 'HARDY')).toMatchObject({ available: true, taken: 0 }); }); test('with no Boon picked the line says one is owed', () => { const { specialAbilities, armorBonus } = advance(null, 'WIZARD', 4, undefined, { key: 'EVOCATION' }); expect(specialAbilities).toBe('Boon (level 4) — Choose one with your DM: Weapon Mastery, Arcane Focus, Toughened, Hardy.'); expect(armorBonus).toBe(0); }); }); describe('spellsGainedAt', () => { test('follows the unlock schedule', () => { expect(spellsGainedAt('FIGHTER', 3)).toBeNull(); expect(spellsGainedAt('WIZARD', 3)).toEqual({ maxSpellLevel: 2, unlocksSpellLevel: 2, newSpells: 2, newCantrips: 0 }); expect(spellsGainedAt('CLERIC', 4)).toEqual({ maxSpellLevel: 2, unlocksSpellLevel: null, newSpells: 1, newCantrips: 1 }); expect(spellsGainedAt('WARLOCK', 5)).toMatchObject({ maxSpellLevel: 3, unlocksSpellLevel: 3, newSpells: 1 }); // Half casters: two spells at level 2, then one at each odd level. expect(spellsGainedAt('PALADIN', 2)).toEqual({ maxSpellLevel: 1, unlocksSpellLevel: 1, newSpells: 2, newCantrips: 0 }); expect(spellsGainedAt('RANGER', 4)).toMatchObject({ newSpells: 0, newCantrips: 0 }); expect(spellsGainedAt('RANGER', 5)).toMatchObject({ maxSpellLevel: 2, unlocksSpellLevel: 2, newSpells: 1 }); }); }); describe('levelUpPlan', () => { const held = (over) => ({ weaponId: 'w', equipped: true, level: 1, weapon: { name: 'Longsword', damageDice: '1d8', damageScaling: 1, minLevel: 1 }, ...over }); test('lists the features, the Boon and the weapons that can take the free upgrade', () => { const plan = levelUpPlan({ level: 3, characterClass: 'FIGHTER', specialAbilities: null, weapons: [held(), held({ weaponId: 'p', weapon: { name: 'Dawnblade', damageDice: '1d8', damageScaling: 2, minLevel: 1 } }), held({ weaponId: 'top', level: 4 })] }); expect(plan).toMatchObject({ fromLevel: 3, toLevel: 4, features: [], spells: null, beyondTables: false }); expect(plan.boon).toHaveLength(4); // Only standard growth, and never past the character's new level. expect(plan.weapons).toEqual([{ weaponId: 'w', name: 'Longsword', equipped: true, level: 1, damageDice: '1d8', nextDamageDice: '1d10' }]); }); test('has no Boon to pick at a level that grants none', () => { const plan = levelUpPlan({ level: 1, characterClass: 'FIGHTER', specialAbilities: null, weapons: [] }); expect(plan.boon).toBeNull(); expect(plan.subclass).toBeNull(); expect(plan.features.map((f) => f.name)).toEqual(['Action Surge']); }); test('asks for a subclass at the level the class chooses, and whenever one is still owed', () => { const fighter = { characterClass: 'FIGHTER', specialAbilities: null, weapons: [] }; const plan = levelUpPlan({ ...fighter, level: 2 }); expect(plan.subclass.level).toBe(3); expect(plan.subclass.options.map((o) => o.key)).toEqual(['CHAMPION', 'BATTLE_MASTER', 'SAMURAI']); expect(plan.subclass.options[0].features.map((f) => f.name)).toEqual(['Improved Critical']); // A wizard chooses a level earlier. expect(levelUpPlan({ ...fighter, characterClass: 'WIZARD', level: 1 }).subclass.level).toBe(2); // Still owed later on: the options then carry everything missed so far. const late = levelUpPlan({ ...fighter, level: 6 }); expect(late.subclass.options[0].features.map((f) => f.name)).toEqual(['Improved Critical', 'Remarkable Athlete']); // With one chosen there is nothing to ask, and its features are in the list. const chosen = levelUpPlan({ ...fighter, level: 6, subclass: 'CHAMPION' }); expect(chosen.subclass).toBeNull(); expect(chosen.features.map((f) => f.name)).toEqual(['Remarkable Athlete']); }); }); describe('a campaign without subclasses', () => { const fighter = { characterClass: 'FIGHTER', specialAbilities: null, weapons: [] }; test('asks for nothing: the class takes its first subclass at the level it would have chosen', () => { const plan = levelUpPlan({ ...fighter, level: 2 }, { subclasses: false }); expect(plan.subclass).toBeNull(); expect(plan.subclassTaken).toBe('CHAMPION'); expect(plan.features.map((f) => f.name)).toEqual(['Improved Critical']); // Before that level, and once it is taken, there is nothing to take. expect(levelUpPlan({ ...fighter, level: 1 }, { subclasses: false }).subclassTaken).toBeNull(); const later = levelUpPlan({ ...fighter, level: 6, subclass: 'CHAMPION' }, { subclasses: false }); expect(later.subclassTaken).toBeNull(); expect(later.features.map((f) => f.name)).toEqual(['Remarkable Athlete']); }); test('a character who joined above that level gets everything missed so far', () => { const plan = levelUpPlan({ ...fighter, level: 6 }, { subclasses: false }); expect(plan.subclassTaken).toBe('CHAMPION'); expect(plan.features.map((f) => f.name)).toEqual(['Improved Critical', 'Remarkable Athlete']); }); test('with subclasses on, the plan takes none by default', () => { expect(levelUpPlan({ ...fighter, level: 2 }).subclassTaken).toBeNull(); }); }); describe('subclasses', () => { test('every class has three, each with a summary and features from its choice level to 10', () => { for (const cls of Object.keys(CLASSES)) { expect(SUBCLASSES[cls]).toHaveLength(3); for (const subclass of SUBCLASSES[cls]) { expect(subclass.key && subclass.name && subclass.summary).toBeTruthy(); const levels = Object.keys(subclass.features).map(Number); expect(Math.min(...levels)).toBe(SUBCLASS_LEVEL[cls]); expect(Math.max(...levels)).toBeLessThanOrEqual(10); for (const f of Object.values(subclass.features).flat()) { expect(f.name && f.text).toBeTruthy(); expect(f.name).not.toMatch(/—|–|--/); // the dash is what ends a name on the sheet } } expect(new Set(SUBCLASSES[cls].map((s) => s.key)).size).toBe(3); } }); test('a subclass adds its features to the class\'s at each level', () => { expect(featuresAt('FIGHTER', 3).map((f) => f.name)).toEqual([]); expect(featuresAt('FIGHTER', 3, 'CHAMPION').map((f) => f.name)).toEqual(['Improved Critical']); expect(featuresAt('CLERIC', 8, 'WAR').map((f) => f.boon || f.name)).toEqual([true, 'Destroy Undead', 'Divine Strike']); expect(featuresAt('FIGHTER', 3, 'LIFE')).toEqual([]); // another class's subclass counts for nothing }); test('without one, from the level the class chooses at, a line says a subclass is owed', () => { expect(advance(null, 'FIGHTER', 2).specialAbilities).toBe('Action Surge — Once per short rest, take one extra action on your turn.'); const owed = advance(null, 'FIGHTER', 3).specialAbilities; expect(owed).toBe('Subclass — Choose one with your DM: Champion, Battle Master, Samurai.'); // Said once, however many levels go by. expect(advance(owed, 'FIGHTER', 5).specialAbilities.split('\n').filter((l) => l.startsWith('Subclass — '))).toHaveLength(1); }); test('picking one late writes everything it has given so far, and clears the line', () => { const owed = advance(advance(null, 'FIGHTER', 3).specialAbilities, 'FIGHTER', 5).specialAbilities; const { specialAbilities } = advance(owed, 'FIGHTER', 7, undefined, { key: 'BATTLE_MASTER', chosen: true }); const lines = specialAbilities.split('\n'); expect(lines.some((l) => l.startsWith('Subclass — '))).toBe(false); // The level 7 upgrade has taken the place of the level 3 line. expect(lines.filter((l) => l.startsWith('Combat Superiority — '))).toEqual([expect.stringContaining('Five superiority dice (d8)')]); expect(lines.filter((l) => l.startsWith('Manoeuvres — '))).toHaveLength(2); expect(lines.some((l) => l.startsWith('Know Your Enemy — '))).toBe(true); expect(() => advance(owed, 'FIGHTER', 7, undefined, { key: 'LIFE', chosen: true })).toThrow(LevelUpError); }); test('switching takes the old subclass\'s lines off and writes the new one\'s', () => { const champion = advance(advance('Second Wind — heal.', 'FIGHTER', 3, undefined, { key: 'CHAMPION', chosen: true }).specialAbilities, 'FIGHTER', 5, undefined, CHAMPION).specialAbilities; expect(champion.split('\n').map((l) => l.split(' — ')[0])).toEqual(['Second Wind', 'Improved Critical', 'Extra Attack']); const samurai = switchSubclass(champion, 'FIGHTER', 5, 'CHAMPION', 'SAMURAI'); expect(samurai.split('\n').map((l) => l.split(' — ')[0])).toEqual(['Second Wind', 'Extra Attack', 'Fighting Spirit', 'Bonus Proficiency']); // Cleared altogether, the choice is owed again. expect(switchSubclass(samurai, 'FIGHTER', 5, 'SAMURAI', null).split('\n').map((l) => l.split(' — ')[0])).toEqual(['Second Wind', 'Extra Attack', 'Subclass']); // A line the class also writes under the same name is not taken away. const valor = advance('Extra Attack — mine.', 'BARD', 6, undefined, { key: 'VALOR', chosen: true }).specialAbilities; expect(switchSubclass(valor, 'BARD', 6, 'VALOR', 'LORE')).not.toContain('Combat Inspiration'); }); });