import test from "node:test"; import assert from "node:assert/strict"; import { resolveMagicAttack, resolveMagicEffect, resolveSpellCast } from "../../shared/combat/magic.js"; import { spellDefinition, spellDifficulty, SpellId } from "../../shared/definitions/spells.js"; import { spellDifficultyFromTier } from "../../shared/spells/constants.js"; test("spell cast fails when difficulty roll beats magic roll", () => { let i = 0; const rng = () => [0.25, 0.45][i++]; const cast = resolveSpellCast({ magicSkillLevel: 5, spellDifficulty: 10, rng }); assert.equal(cast.success, false); assert.equal(cast.magicRoll, 1); assert.equal(cast.difficultyRoll, 4); }); test("spell cast succeeds on equal magic and difficulty rolls", () => { let i = 0; const rng = () => [0.4, 0.4][i++]; const cast = resolveSpellCast({ magicSkillLevel: 5, spellDifficulty: 5, rng }); assert.equal(cast.magicRoll, cast.difficultyRoll); assert.equal(cast.success, true); }); test("fizzled magic attack deals no damage", () => { let i = 0; const rng = () => [0, 0.99][i++]; const result = resolveMagicAttack({ magicSkillLevel: 3, spellDifficulty: 10, mindSkillLevel: 10, element: "fire", rng }); assert.equal(result.outcome, "fizzle"); assert.equal(result.damage, 0); }); test("successful magic attack rolls mind damage and elemental multiplier", () => { let i = 0; const rng = () => [0.9, 0, 0.4][i++]; const neutral = resolveMagicAttack({ magicSkillLevel: 10, spellDifficulty: 1, mindSkillLevel: 5, element: "fire", targetAffinities: {}, rng }); assert.equal(neutral.outcome, "hit"); assert.equal(neutral.multiplier, 1); assert.equal(neutral.damage, 2); let j = 0; const rngWeak = () => [0.9, 0, 0.4][j++]; const weak = resolveMagicAttack({ magicSkillLevel: 10, spellDifficulty: 1, mindSkillLevel: 5, element: "fire", targetAffinities: { fire: "weak" }, rng: rngWeak }); assert.equal(weak.multiplier, 2); assert.equal(weak.damage, neutral.damage * 2); }); test("non-damage spell applies effect only after a successful cast roll", () => { let i = 0; const rng = () => [0.9, 0][i++]; const result = resolveMagicEffect({ magicSkillLevel: 10, spellDifficulty: 3, effectId: "slow", rng }); assert.equal(result.outcome, "effect"); assert.equal(result.effectId, "slow"); }); test("spell definitions expose tier-based difficulty", () => { const firebolt = spellDefinition(SpellId.firebolt); assert.equal(spellDifficulty(firebolt), spellDifficultyFromTier(3)); assert.equal(firebolt.element, "fire"); assert.equal(spellDefinition("spark"), null); assert.ok(spellDefinition(SpellId.light)); assert.ok(spellDefinition(SpellId.waterbolt)); assert.ok(spellDefinition(SpellId.maelstrom)); assert.ok(spellDefinition(SpellId.hurricane)); assert.ok(spellDefinition(SpellId.cataclysm)); assert.ok(spellDefinition(SpellId.judgment)); assert.ok(spellDefinition(SpellId.oblivion)); assert.ok(spellDefinition(SpellId.blight)); });