Initial commit

This commit is contained in:
2026-09-22 16:27:53 +02:00
commit 9b29dc8253
70 changed files with 14726 additions and 0 deletions
+214
View File
@@ -0,0 +1,214 @@
const {
abilityModifier,
proficiencyBonus,
finalAbilityScore,
saveBonus,
skillBonus,
passivePerception,
armorClass,
initiative,
hpMax,
spellSaveDC,
spellAttackBonus,
weaponAttackBonus,
weaponDamageBonus,
computeCharacterSheet,
computeWeaponStats,
} = 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');
});
});