| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { resolveMagicAttack, resolveMagicEffect, resolveSpellCast } from "../../shared/combat/magic.js"; |
| 4 | import { spellDefinition, SpellId } from "../../shared/definitions/spells.js"; |
| 5 | |
| 6 | test("spell cast fails when difficulty roll beats magic roll", () => |
| 7 | { |
| 8 | let i = 0; |
| 9 | const rng = () => [0.25, 0.45][i++]; |
| 10 | const cast = resolveSpellCast({ magicSkillLevel: 5, spellDifficulty: 10, rng }); |
| 11 | assert.equal(cast.success, false); |
| 12 | assert.equal(cast.magicRoll, 2); |
| 13 | assert.equal(cast.difficultyRoll, 5); |
| 14 | }); |
| 15 | |
| 16 | test("spell cast succeeds on equal magic and difficulty rolls", () => |
| 17 | { |
| 18 | let i = 0; |
| 19 | const rng = () => [0.4, 0.4][i++]; |
| 20 | const cast = resolveSpellCast({ magicSkillLevel: 5, spellDifficulty: 5, rng }); |
| 21 | assert.equal(cast.magicRoll, cast.difficultyRoll); |
| 22 | assert.equal(cast.success, true); |
| 23 | }); |
| 24 | |
| 25 | test("fizzled magic attack deals no damage", () => |
| 26 | { |
| 27 | let i = 0; |
| 28 | const rng = () => [0, 0.99][i++]; |
| 29 | const result = resolveMagicAttack({ |
| 30 | magicSkillLevel: 3, |
| 31 | spellDifficulty: 10, |
| 32 | mindSkillLevel: 10, |
| 33 | baseDamage: 5, |
| 34 | element: "fire", |
| 35 | rng |
| 36 | }); |
| 37 | assert.equal(result.outcome, "fizzle"); |
| 38 | assert.equal(result.damage, 0); |
| 39 | }); |
| 40 | |
| 41 | test("successful magic attack rolls mind damage and elemental multiplier", () => |
| 42 | { |
| 43 | let i = 0; |
| 44 | const rng = () => [0.9, 0, 0, 0][i++]; |
| 45 | const neutral = resolveMagicAttack({ |
| 46 | magicSkillLevel: 10, |
| 47 | spellDifficulty: 1, |
| 48 | mindSkillLevel: 5, |
| 49 | baseDamage: 2, |
| 50 | element: "fire", |
| 51 | targetAffinities: {}, |
| 52 | rng |
| 53 | }); |
| 54 | assert.equal(neutral.outcome, "hit"); |
| 55 | assert.equal(neutral.multiplier, 1); |
| 56 | |
| 57 | let j = 0; |
| 58 | const rngWeak = () => [0.9, 0, 0, 0][j++]; |
| 59 | const weak = resolveMagicAttack({ |
| 60 | magicSkillLevel: 10, |
| 61 | spellDifficulty: 1, |
| 62 | mindSkillLevel: 5, |
| 63 | baseDamage: 2, |
| 64 | element: "fire", |
| 65 | targetAffinities: { fire: "weak" }, |
| 66 | rng: rngWeak |
| 67 | }); |
| 68 | assert.equal(weak.multiplier, 2); |
| 69 | assert.equal(weak.damage, neutral.damage * 2); |
| 70 | }); |
| 71 | |
| 72 | test("non-damage spell applies effect only after a successful cast roll", () => |
| 73 | { |
| 74 | let i = 0; |
| 75 | const rng = () => [0.9, 0][i++]; |
| 76 | const result = resolveMagicEffect({ |
| 77 | magicSkillLevel: 10, |
| 78 | spellDifficulty: 3, |
| 79 | effectId: "slow", |
| 80 | rng |
| 81 | }); |
| 82 | assert.equal(result.outcome, "effect"); |
| 83 | assert.equal(result.effectId, "slow"); |
| 84 | }); |
| 85 | |
| 86 | test("spell definitions expose per-spell difficulty", () => |
| 87 | { |
| 88 | const spark = spellDefinition(SpellId.spark); |
| 89 | assert.equal(spark.difficulty, 4); |
| 90 | assert.equal(spark.element, "fire"); |
| 91 | }); |