Files
the-sunken-reach/backend/src/game/calculations.test.js
T
joshuaandClaude Opus 5.5 4a6a09bae8 V1.1.0: gear levels, encounter overhaul, guided builder, trading, docs
Characters
- Builder: pick-then-Next on every step with live preview, Back button,
  plain-language help throughout, and "Suggest for me" for ability scores
- Motivation is picked from a list; new steps for special abilities,
  background and appearance, all composed from fixed lists and DM-only
  to edit afterwards
- Copy a character to another campaign
- Characters can give gold, weapons, items and spells to each other
- Money tracker: gold only changes through a logged ledger
- Fixes: level/XP/location are DM-only, HP stays within 0..max, physical
  dice ability rolls, every die is rolled once

Gear
- DM-managed item pool; weapons, items and spells are handed out by the DM
- Starting-option flag per pool entry
- Gear levels 1-9 per held copy with a damage ladder, growth rate and
  starting level; spells gated by spell level with a fixed damage value
- Spell library carries default damage; damage is picked from a list

Encounters
- Battle and NPC quest modes, turn tracking, scene description, DM notes
- NPCs, richer combatant cards, conditions from a list
- Rewards (gold, weapons, items) lined up and awarded, with gold splitting
- Character HP is shared between the sheet and the encounter
- Monster stat blocks and NPC secrets are never sent to players

Other
- Dice roller on every page
- Docs for characters, weapons, spells, items, encounters, monsters and
  NPCs, with full lists generated from the data (npm run docs:lists)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 16:47:28 +02:00

325 lines
12 KiB
JavaScript

const {
abilityModifier,
proficiencyBonus,
finalAbilityScore,
saveBonus,
skillBonus,
passivePerception,
armorClass,
initiative,
hpMax,
spellSaveDC,
spellAttackBonus,
weaponAttackBonus,
weaponDamageBonus,
computeCharacterSheet,
computeWeaponStats,
DAMAGE_LADDER,
diceAverage,
scaledDamageDice,
canUseGear,
} = require('./calculations');
describe('abilityModifier', () => {
test('floors toward negative infinity, not toward zero', () => {
expect(abilityModifier(7)).toBe(-2); // (7-10)/2 = -1.5 -> floor = -2
expect(abilityModifier(10)).toBe(0);
expect(abilityModifier(11)).toBe(0);
expect(abilityModifier(12)).toBe(1);
expect(abilityModifier(20)).toBe(5);
expect(abilityModifier(1)).toBe(-5);
});
});
describe('proficiencyBonus', () => {
test('matches the 5e level bands', () => {
expect(proficiencyBonus(1)).toBe(2);
expect(proficiencyBonus(4)).toBe(2);
expect(proficiencyBonus(5)).toBe(3);
expect(proficiencyBonus(8)).toBe(3);
expect(proficiencyBonus(9)).toBe(4);
expect(proficiencyBonus(12)).toBe(4);
expect(proficiencyBonus(13)).toBe(5);
expect(proficiencyBonus(16)).toBe(5);
expect(proficiencyBonus(17)).toBe(6);
expect(proficiencyBonus(20)).toBe(6);
});
});
describe('finalAbilityScore / species bonuses', () => {
test('applies a flat species bonus', () => {
expect(finalAbilityScore(14, 'DEX', 'ELF')).toBe(16);
expect(finalAbilityScore(14, 'STR', 'ELF')).toBe(14);
});
test('human gets +1 to every ability', () => {
expect(finalAbilityScore(10, 'STR', 'HUMAN')).toBe(11);
expect(finalAbilityScore(10, 'CHA', 'HUMAN')).toBe(11);
});
test('half-elf gets fixed +2 CHA plus chosen +1/+1', () => {
expect(finalAbilityScore(10, 'CHA', 'HALF_ELF', ['STR', 'INT'])).toBe(12);
expect(finalAbilityScore(10, 'STR', 'HALF_ELF', ['STR', 'INT'])).toBe(11);
expect(finalAbilityScore(10, 'INT', 'HALF_ELF', ['STR', 'INT'])).toBe(11);
expect(finalAbilityScore(10, 'WIS', 'HALF_ELF', ['STR', 'INT'])).toBe(10);
});
test('no species leaves score untouched', () => {
expect(finalAbilityScore(10, 'STR', null)).toBe(10);
});
});
describe('saveBonus / skillBonus', () => {
test('save bonus adds proficiency only when proficient', () => {
expect(saveBonus(3, true, 2)).toBe(5);
expect(saveBonus(3, false, 2)).toBe(3);
});
test('skill bonus with expertise doubles the proficiency portion', () => {
expect(skillBonus(2, true, 2, false)).toBe(4);
expect(skillBonus(2, true, 2, true)).toBe(6);
expect(skillBonus(2, false, 2, true)).toBe(2);
});
});
describe('passivePerception / armorClass / initiative', () => {
test('passive perception is 10 + perception bonus', () => {
expect(passivePerception(3)).toBe(13);
});
test('armor class is 10 + dex mod + armor bonus', () => {
expect(armorClass(2, 3)).toBe(15);
expect(armorClass(-1, 0)).toBe(9);
});
test('initiative equals dex modifier', () => {
expect(initiative(3)).toBe(3);
});
});
describe('hpMax — the level-1-max-die rule', () => {
test('level 1 uses the max die roll, not the average', () => {
expect(hpMax(1, 10, 2)).toBe(12); // 10 (max d10) + 2 CON
expect(hpMax(1, 6, 0)).toBe(6);
});
test('level 2+ adds the fixed average per additional level', () => {
// d10 average = floor(10/2)+1 = 6
expect(hpMax(2, 10, 2)).toBe(12 + (6 + 2)); // 20
expect(hpMax(5, 10, 2)).toBe(12 + 4 * (6 + 2)); // 44
});
test('d6 and d12 averages', () => {
expect(Math.floor(6 / 2) + 1).toBe(4);
expect(Math.floor(12 / 2) + 1).toBe(7);
expect(hpMax(2, 6, 0)).toBe(6 + 4);
expect(hpMax(2, 12, 0)).toBe(12 + 7);
});
});
describe('spellcasting', () => {
test('spell save DC and attack bonus', () => {
expect(spellSaveDC(3, 4)).toBe(15);
expect(spellAttackBonus(3, 4)).toBe(7);
});
});
describe('weapon bonuses', () => {
test('STR weapon uses STR modifier', () => {
expect(weaponAttackBonus({ attackAbility: 'STR', strMod: 3, dexMod: 1, proficient: true, profBonus: 2 })).toBe(5);
expect(weaponDamageBonus({ attackAbility: 'STR', strMod: 3, dexMod: 1 })).toBe(3);
});
test('DEX weapon uses DEX modifier', () => {
expect(weaponAttackBonus({ attackAbility: 'DEX', strMod: 3, dexMod: 1, proficient: false, profBonus: 2 })).toBe(1);
});
test('FINESSE weapon uses the better of STR/DEX', () => {
expect(weaponDamageBonus({ attackAbility: 'FINESSE', strMod: 1, dexMod: 4 })).toBe(4);
expect(weaponDamageBonus({ attackAbility: 'FINESSE', strMod: 5, dexMod: 2 })).toBe(5);
});
test('unknown attackAbility throws rather than silently defaulting', () => {
expect(() => weaponDamageBonus({ attackAbility: 'BOGUS', strMod: 1, dexMod: 1 })).toThrow();
});
});
describe('computeCharacterSheet — full assembly', () => {
test('level 1 human fighter', () => {
const sheet = computeCharacterSheet(
{
level: 1,
species: 'HUMAN',
characterClass: 'FIGHTER',
strScore: 15,
dexScore: 13,
conScore: 14,
intScore: 10,
wisScore: 12,
chaScore: 8,
armorBonus: 4,
},
[{ skill: 'ATHLETICS', proficient: true, expertise: false }],
[
{ ability: 'STR', proficient: true },
{ ability: 'CON', proficient: true },
]
);
// Human: +1 to every score -> STR 16 (+3), DEX 14 (+2), CON 15 (+2)
expect(sheet.abilities.STR.final).toBe(16);
expect(sheet.abilities.STR.modifier).toBe(3);
expect(sheet.abilities.CON.modifier).toBe(2);
expect(sheet.proficiencyBonus).toBe(2);
expect(sheet.armorClass).toBe(10 + sheet.abilities.DEX.modifier + 4);
expect(sheet.hp.max).toBe(10 + sheet.abilities.CON.modifier); // d10 fighter, level 1 max die
expect(sheet.saves.STR.proficient).toBe(true);
expect(sheet.saves.STR.bonus).toBe(sheet.abilities.STR.modifier + 2);
expect(sheet.saves.DEX.proficient).toBe(false);
expect(sheet.skills.ATHLETICS.bonus).toBe(sheet.abilities.STR.modifier + 2);
expect(sheet.spellcasting).toBeNull(); // Fighter has no spellcasting ability
});
test('spellcasting classes expose saveDC/attackBonus', () => {
const sheet = computeCharacterSheet({
level: 5,
species: 'ELF',
characterClass: 'WIZARD',
intScore: 16,
});
expect(sheet.spellcasting).not.toBeNull();
expect(sheet.spellcasting.ability).toBe('INT');
expect(sheet.spellcasting.saveDC).toBe(8 + sheet.proficiencyBonus + sheet.abilities.INT.modifier);
});
test('half-elf bonus abilities flow through the full sheet', () => {
const sheet = computeCharacterSheet({
level: 1,
species: 'HALF_ELF',
characterClass: 'BARD',
strScore: 10,
chaScore: 10,
halfElfBonusAbility1: 'STR',
halfElfBonusAbility2: 'INT',
});
expect(sheet.abilities.CHA.final).toBe(12); // fixed +2
expect(sheet.abilities.STR.final).toBe(11); // chosen +1
});
});
describe('computeWeaponStats', () => {
test('assembles attack/damage bonus for a weapon from a computed sheet', () => {
const sheet = computeCharacterSheet({ level: 1, strScore: 16, dexScore: 12 });
const stats = computeWeaponStats({ attackAbility: 'STR', damageDice: '1d8', damageType: 'slashing' }, sheet.abilities, sheet.proficiencyBonus, true);
expect(stats.attackBonus).toBe(sheet.abilities.STR.modifier + sheet.proficiencyBonus);
expect(stats.damageBonus).toBe(sheet.abilities.STR.modifier);
expect(stats.damageDice).toBe('1d8');
});
test('uses the dice for the level of the copy the character holds', () => {
const sheet = computeCharacterSheet({ level: 5, strScore: 16 });
const spear = { attackAbility: 'STR', damageDice: '1d6', damageType: 'piercing', damageScaling: 1 };
expect(computeWeaponStats(spear, sheet.abilities, sheet.proficiencyBonus, true, 3).damageDice).toBe('1d10');
// The level changes the dice only, never the bonuses.
expect(computeWeaponStats(spear, sheet.abilities, sheet.proficiencyBonus, true, 3).damageBonus).toBe(sheet.abilities.STR.modifier);
});
});
describe('gear levels — the damage ladder', () => {
test('starts with single dice, then pairs, in the smallest possible steps', () => {
expect(DAMAGE_LADDER.slice(0, 13).map((r) => r.dice)).toEqual([
'1d4', '1d6', '1d8', '1d10', '1d12',
'2d6', '1d6+1d8', '2d8', '1d8+1d10', '2d10', '1d10+1d12', '2d12',
'3d8',
]);
});
test('every rung is stronger than the last by 1 average damage, or 0.5 where a die is added', () => {
for (let i = 1; i < DAMAGE_LADDER.length; i++) {
const step = DAMAGE_LADDER[i].average - DAMAGE_LADDER[i - 1].average;
expect([0.5, 1]).toContain(step);
// The stated average really is the average of the dice shown.
expect(diceAverage(DAMAGE_LADDER[i].dice)).toBe(DAMAGE_LADDER[i].average);
}
});
test('diceAverage reads the expressions DMs type, and rejects free text', () => {
expect(diceAverage('1d8')).toBe(4.5);
expect(diceAverage('d8')).toBe(4.5);
expect(diceAverage('2d6')).toBe(7);
expect(diceAverage('1d6 + 1d8')).toBe(8);
expect(diceAverage('2d4+2')).toBe(7);
expect(diceAverage('1d6-1')).toBe(2.5);
expect(diceAverage('3')).toBe(3);
expect(diceAverage('special')).toBeNull();
expect(diceAverage('')).toBeNull();
expect(diceAverage(null)).toBeNull();
});
});
describe('scaledDamageDice', () => {
const byLevel = (base, scaling, levels) => levels.map((level) => scaledDamageDice(base, level, scaling));
test('standard gear climbs one rung per level', () => {
expect(byLevel('1d4', 1, [1, 2, 3, 4, 5, 6])).toEqual(['1d4', '1d6', '1d8', '1d10', '1d12', '2d6']);
expect(byLevel('1d8', 1, [1, 2, 3])).toEqual(['1d8', '1d10', '1d12']);
});
test('powerful and legendary gear climb two and three rungs per level', () => {
expect(byLevel('1d6', 2, [1, 2, 3, 4])).toEqual(['1d6', '1d10', '2d6', '2d8']);
expect(byLevel('1d8', 3, [1, 2, 3])).toEqual(['1d8', '2d6', '1d8+1d10']);
});
test('level 1 is always the base dice exactly as written', () => {
expect(scaledDamageDice('2d4+2', 1, 1)).toBe('2d4+2');
expect(scaledDamageDice('special', 1, 3)).toBe('special');
});
test('dice that are not on the ladder start from the highest rung that does not beat them', () => {
expect(scaledDamageDice('2d4+2', 2, 1)).toBe('1d6+1d8'); // avg 7 sits on 2d6
expect(scaledDamageDice('1d4+1', 2, 1)).toBe('1d8'); // avg 3.5 sits on 1d6
expect(scaledDamageDice('1', 2, 1)).toBe('1d4'); // below the ladder
});
test('free text never scales, and levels and scaling are clamped to their ranges', () => {
expect(scaledDamageDice('special', 9, 3)).toBe('special');
expect(scaledDamageDice('1d4', 99, 1)).toBe(scaledDamageDice('1d4', 9, 1));
expect(scaledDamageDice('1d4', 9, 1)).toBe('1d8+1d10');
expect(scaledDamageDice('1d4', 0, 1)).toBe('1d4');
expect(scaledDamageDice('1d4', 2, 99)).toBe(scaledDamageDice('1d4', 2, 3));
expect(scaledDamageDice('1d4', 2, undefined)).toBe('1d6');
});
test('gear with a starting level shows its base dice there and climbs from it', () => {
// A greatsword that only exists from level 3: 2d6 at 3, then one rung per level.
expect([3, 4, 5].map((level) => scaledDamageDice('2d6', level, 1, 3))).toEqual(['2d6', '1d6+1d8', '2d8']);
expect(scaledDamageDice('2d6', 1, 1, 3)).toBe('2d6'); // never below its base
expect(scaledDamageDice('2d4+2', 3, 2, 3)).toBe('2d4+2'); // as written at its starting level
const sheet = computeCharacterSheet({ level: 5, strScore: 16 });
const greatsword = { attackAbility: 'STR', damageDice: '2d6', damageType: 'slashing', damageScaling: 1, minLevel: 3 };
expect(computeWeaponStats(greatsword, sheet.abilities, sheet.proficiencyBonus, true, 4).damageDice).toBe('1d6+1d8');
});
test('a higher level is never weaker', () => {
for (const base of ['1d4', '1d12', '2d6', '2d4+2']) {
for (const scaling of [1, 2, 3]) {
for (let level = 2; level <= 9; level++) {
expect(diceAverage(scaledDamageDice(base, level, scaling))).toBeGreaterThan(
diceAverage(scaledDamageDice(base, level - 1, scaling))
);
}
}
}
});
});
describe('canUseGear', () => {
test('a character can use gear of their own level or lower', () => {
expect(canUseGear(1, 1)).toBe(true);
expect(canUseGear(3, 1)).toBe(true);
expect(canUseGear(3, 3)).toBe(true);
expect(canUseGear(2, 3)).toBe(false);
expect(canUseGear(1, 0)).toBe(true); // cantrips
});
});