diff --git a/calc-sidecar/test/backends/rocalc/alchemist_buffs.test.ts b/calc-sidecar/test/backends/rocalc/alchemist_buffs.test.ts index 3d8eafe..d703db2 100644 --- a/calc-sidecar/test/backends/rocalc/alchemist_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/alchemist_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -15,7 +15,29 @@ const AXE_2H = 1360; // Two-Handed Axe (W_2HAXE) const DAGGER = 1207; // Main Gauche (W_DAGGER) -- non-axe negative const MACE = 1501; // Club (W_MACE) -- non-axe negative -function alShim(weaponId: number, className = "alchemist") { +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused for the Alchemist tests. reset() preserves the class but +// clears the buff banks and rolls level / stats / equipment back to baseline, so +// fresh(weaponId) re-applies the build for a clean, leak-free baseline without +// re-paying createShim+setClass. The Creator case keeps its own session because +// reset() does not roll back rocalc's class globals (that needs the expensive +// class change). +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("alchemist"); +}); +function fresh(weaponId: number) { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 40, vit: 40, int: 40, dex: 90, luk: 20 }); + shim.equip("weapon", { id: weaponId }); + return shim; +} + +// Creator (and any non-Alchemist) needs its own session: reset() preserves the +// class, so the shared Alchemist shim cannot stand in for it. +function otherClassShim(weaponId: number, className: string) { const s = createShim(); s.setClass(className); s.setLevel({ base: 99, job: 70 }); @@ -44,7 +66,10 @@ function aveWith( buffs: Buff[], className = "alchemist", ): number { - const s = alShim(weaponId, className); + const s = + className === "alchemist" + ? fresh(weaponId) + : otherClassShim(weaponId, className); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(ENEMY); const ave = s.readCombatResults().damage.ave; @@ -98,7 +123,7 @@ test("axe_mastery raises damage.ave for Creator too (identical bank)", () => { test("reset() clears axe_mastery (no damage.ave leak)", () => { const plain = aveWith(AXE_2H, []); - const s = alShim(AXE_2H); + const s = fresh(AXE_2H); s.setBuffs([{ name: "axe_mastery", level: 10 }]); s.setEnemyInline(ENEMY); s.readCombatResults(); diff --git a/calc-sidecar/test/backends/rocalc/attack_skills.test.ts b/calc-sidecar/test/backends/rocalc/attack_skills.test.ts index b8227e9..a3bbbd0 100644 --- a/calc-sidecar/test/backends/rocalc/attack_skills.test.ts +++ b/calc-sidecar/test/backends/rocalc/attack_skills.test.ts @@ -1,26 +1,41 @@ -import { test } from "node:test"; +import { test, before, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; const PORING = 1002; // iRO mob id; translates to rocalc m_Monster index 272 -function tkBare(s: ReturnType) { - s.setClass("taekwon_kid"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 80, vit: 40, int: 1, dex: 70, luk: 1 }); +// The shim (jsdom + class load) is created once and reused; reset() preserves +// the class but clears pending attack skills and the last breakdown, so +// reset()+reconfigure in beforeEach gives every test a clean baseline without +// re-paying the ~4s createShim+setClass per test. +let shim: ReturnType; + +// reset() rolls level / stats back to the class baseline; re-apply the bare TK +// build (no weapon) after each reset. The class itself survives reset(). +function reconfig(): void { + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 80, vit: 40, int: 1, dex: 70, luk: 1 }); } +before(() => { + shim = createShim(); + shim.setClass("taekwon_kid"); +}); + +beforeEach(() => { + shim.reset(); + reconfig(); +}); + test("primary attack skill drives top-level damage above auto-attack", () => { - const s = createShim(); - tkBare(s); - s.setEnemy(PORING); - const auto = s.readCombatResults().damage.ave; - - s.reset(); - tkBare(s); - s.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); - s.setEnemy(PORING); - const c = s.readCombatResults(); + shim.setEnemy(PORING); + const auto = shim.readCombatResults().damage.ave; + + shim.reset(); + reconfig(); + shim.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); + shim.setEnemy(PORING); + const c = shim.readCombatResults(); assert.ok(c.damage.ave !== null && auto !== null); assert.ok( c.damage.ave > auto, @@ -31,14 +46,12 @@ test("primary attack skill drives top-level damage above auto-attack", () => { }); test("breakdown reports each declared skill with hit counts; primary drives top-level", () => { - const s = createShim(); - tkBare(s); - s.setAttackSkills([ + shim.setAttackSkills([ { name: "tornado_kick", level: 7, primary: true }, { name: "counter_kick", level: 7 }, ]); - s.setEnemy(PORING); - const c = s.readCombatResults(); + shim.setEnemy(PORING); + const c = shim.readCombatResults(); const byName = Object.fromEntries((c.skills ?? []).map((k) => [k.name, k])); assert.ok(byName["tornado_kick"] && byName["counter_kick"]); assert.equal(byName["counter_kick"].hits, 3); // confirmed in Task 1 @@ -47,32 +60,26 @@ test("breakdown reports each declared skill with hit counts; primary drives top- }); test("absent attack_skills leaves auto-attack behavior unchanged (no skills field)", () => { - const s = createShim(); - tkBare(s); - s.setEnemy(PORING); - const c = s.readCombatResults(); + shim.setEnemy(PORING); + const c = shim.readCombatResults(); assert.equal(c.skills, undefined); }); test("unknown attack-skill name throws", () => { - const s = createShim(); - tkBare(s); assert.throws(() => - s.setAttackSkills([{ name: "fireball", level: 1, primary: true }]), + shim.setAttackSkills([{ name: "fireball", level: 1, primary: true }]), ); }); test("zero primaries throws (top-level damage would be undefined)", () => { - const s = createShim(); - tkBare(s); - assert.throws(() => s.setAttackSkills([{ name: "tornado_kick", level: 7 }])); + assert.throws(() => + shim.setAttackSkills([{ name: "tornado_kick", level: 7 }]), + ); }); test("two primaries throws (ambiguous top-level damage)", () => { - const s = createShim(); - tkBare(s); assert.throws(() => - s.setAttackSkills([ + shim.setAttackSkills([ { name: "tornado_kick", level: 7, primary: true }, { name: "roundhouse", level: 7, primary: true }, ]), @@ -80,8 +87,7 @@ test("two primaries throws (ambiguous top-level damage)", () => { }); test("supportedAttackSkills returns the binding keys", () => { - const s = createShim(); - const names = s.supportedAttackSkills(); + const names = shim.supportedAttackSkills(); assert.ok( names && names.includes("tornado_kick") && names.includes("roundhouse"), ); @@ -101,26 +107,22 @@ const INLINE = { }; test("breakdown is computed against an inline mob (before m_Monster restore)", () => { - const s = createShim(); - tkBare(s); - s.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); - s.setEnemyInline(INLINE); - const c = s.readCombatResults(); + shim.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); + shim.setEnemyInline(INLINE); + const c = shim.readCombatResults(); assert.equal(c.skills?.length, 1); assert.ok(c.skills?.[0].damage.ave !== null && c.skills[0].damage.ave > 0); assert.equal(c.damage.ave, c.skills?.[0].damage.ave); // primary drives top-level }); test("reset clears a scored skill: next auto-attack request is unbuffed baseline", () => { - const s = createShim(); - tkBare(s); - s.setEnemy(PORING); - const auto = s.readCombatResults().damage.ave; - s.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); - s.setEnemy(PORING); - assert.ok(s.readCombatResults().damage.ave! > auto!); - s.reset(); - tkBare(s); - s.setEnemy(PORING); - assert.equal(s.readCombatResults().damage.ave, auto); // back to auto-attack + shim.setEnemy(PORING); + const auto = shim.readCombatResults().damage.ave; + shim.setAttackSkills([{ name: "roundhouse", level: 7, primary: true }]); + shim.setEnemy(PORING); + assert.ok(shim.readCombatResults().damage.ave! > auto!); + shim.reset(); + reconfig(); + shim.setEnemy(PORING); + assert.equal(shim.readCombatResults().damage.ave, auto); // back to auto-attack }); diff --git a/calc-sidecar/test/backends/rocalc/buff_controls.test.ts b/calc-sidecar/test/backends/rocalc/buff_controls.test.ts index 4af8e46..7752971 100644 --- a/calc-sidecar/test/backends/rocalc/buff_controls.test.ts +++ b/calc-sidecar/test/backends/rocalc/buff_controls.test.ts @@ -1,15 +1,29 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; +// createShim (jsdom + calc engine) + setClass is the expensive part, so it runs +// once in before() and is reused. reset() preserves the class but clears the +// buff/debuff/land/music banks and rolls level/stats/equipment back to the class +// baseline, so fresh() = reset()+reconfigure yields a clean, leak-free shim +// without re-paying the createShim+setClass cost. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("high_priest"); +}); +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 40, agi: 80, vit: 60, int: 80, dex: 60, luk: 30 }); + return shim; +} + // The injected banks must exist and, with the section gates off (default), // have no effect on a baseline High Priest. setSkills/setStats path proves the // controls are present (no "Cannot read properties of undefined"). test("buff control banks are injected and inert until driven", () => { - const s = createShim(); - s.setClass("high_priest"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 40, agi: 80, vit: 60, int: 80, dex: 60, luk: 30 }); + const s = fresh(); // Reading derived stats forces a full StAllCalc; if any A2_Skill*/B_debuf* // referenced by a gated read were missing, rocalc would throw here once the // gate is on. With gates off it must simply succeed. diff --git a/calc-sidecar/test/backends/rocalc/buffs.test.ts b/calc-sidecar/test/backends/rocalc/buffs.test.ts index cea29a5..6526a71 100644 --- a/calc-sidecar/test/backends/rocalc/buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/buffs.test.ts @@ -1,20 +1,34 @@ -import { test } from "node:test"; +import { test, before, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; -function tkShim() { - const s = createShim(); - s.setClass("taekwon_kid"); - s.setLevel({ base: 99, job: 50 }); - s.setStats({ str: 80, agi: 90, vit: 40, int: 1, dex: 60, luk: 30 }); - return s; +// The shim (jsdom + class load) is created once and reused; reset() preserves +// the class but clears applied buffs / skills, so reset()+reconfigure in +// beforeEach gives every test a clean Taekwon Kid baseline without re-paying +// the ~4s createShim+setClass per test. +let shim: ReturnType; + +// reset() rolls level / stats back to the class baseline; re-apply the TK build +// after each reset. The class itself survives reset(). +function reconfig(): void { + shim.setLevel({ base: 99, job: 50 }); + shim.setStats({ str: 80, agi: 90, vit: 40, int: 1, dex: 60, luk: 30 }); } +before(() => { + shim = createShim(); + shim.setClass("taekwon_kid"); +}); + +beforeEach(() => { + shim.reset(); + reconfig(); +}); + test("taekwon_ranker triples maxHp/maxSp", () => { - const base = tkShim().readDerivedStats(); - const s = tkShim(); - s.setBuffs([{ name: "taekwon_ranker", level: 1 }]); - const buffed = s.readDerivedStats(); + const base = shim.readDerivedStats(); + shim.setBuffs([{ name: "taekwon_ranker", level: 1 }]); + const buffed = shim.readDerivedStats(); assert.ok( buffed.maxHp >= base.maxHp * 2.9 && buffed.maxHp <= base.maxHp * 3.1, `ranker maxHp ${buffed.maxHp} not ~3x base ${base.maxHp}`, @@ -34,31 +48,29 @@ test("mild_wind holy endow changes combat damage vs an undead target", () => { size: "Size_Medium", level: 90, }; - const a = tkShim(); - a.setEnemyInline(target); - const plain = a.readCombatResults().damage.ave; + shim.setEnemyInline(target); + const plain = shim.readCombatResults().damage.ave; - const b = tkShim(); - b.setBuffs([{ name: "mild_wind", level: 7, element: "holy" }]); - b.setEnemyInline(target); - const endowed = b.readCombatResults().damage.ave; + shim.reset(); + reconfig(); + shim.setBuffs([{ name: "mild_wind", level: 7, element: "holy" }]); + shim.setEnemyInline(target); + const endowed = shim.readCombatResults().damage.ave; assert.ok(plain != null && endowed != null, "damage.ave should be non-null"); assert.notEqual(endowed, plain, "holy endow should change damage vs undead"); }); test("unknown buff name throws", () => { - const s = tkShim(); - assert.throws(() => s.setBuffs([{ name: "not_a_buff" }]), /unknown buff/); + assert.throws(() => shim.setBuffs([{ name: "not_a_buff" }]), /unknown buff/); }); test("duplicate buff names throw (backend resilience)", () => { // The Go resolver rejects duplicates too, but the backend must not silently // last-write-wins on a repeated buff. - const s = tkShim(); assert.throws( () => - s.setBuffs([ + shim.setBuffs([ { name: "spurt", level: 7 }, { name: "spurt", level: 7 }, ]), @@ -72,6 +84,9 @@ test("a buff with no engine slot for the active class throws, not silently score // isn't in the class's buff list), but the backend must label an // entirely-inapplicable buff rather than silently apply nothing and report // unbuffed numbers as if the buff were active. + // + // Uses its own shim: setClass("knight") would otherwise leave the shared + // session on the wrong class (reset() preserves class, it does not revert it). const s = createShim(); s.setClass("knight"); s.setLevel({ base: 99, job: 50 }); @@ -83,9 +98,8 @@ test("a buff with no engine slot for the active class throws, not silently score }); test("endow element above resolved level throws", () => { - const s = tkShim(); assert.throws( - () => s.setBuffs([{ name: "mild_wind", level: 4, element: "holy" }]), + () => shim.setBuffs([{ name: "mild_wind", level: 4, element: "holy" }]), /exceeds.*level|not available at level/i, ); }); @@ -109,10 +123,9 @@ const BARE_TARGET = { test("spurt status (379) changes derived ATK", () => { // Status 379 = Sprint STR+State; raises STR which shows in A_ATK2 (base ATK). // This is the original weak test, now renamed to be explicit about WHAT it checks. - const base = tkShim().readDerivedStats(); - const s = tkShim(); - s.setBuffs([{ name: "spurt", level: 7 }]); - const buffed = s.readDerivedStats(); + const base = shim.readDerivedStats(); + shim.setBuffs([{ name: "spurt", level: 7 }]); + const buffed = shim.readDerivedStats(); assert.notDeepEqual( buffed, base, @@ -130,15 +143,15 @@ test("spurt mastery (329) drives barehanded combat damage independently of ATK", // At STR 80 lv99 baseline, mastery lv10 adds ~100 ave damage vs this target. // Baseline: no skills set, no buffs - const base = tkShim(); - base.setEnemyInline(BARE_TARGET); - const baseDmg = base.readCombatResults().damage; + shim.setEnemyInline(BARE_TARGET); + const baseDmg = shim.readCombatResults().damage; // Mastery only via setSkills (bypasses setBuffs to isolate 329 from 379) - const withMastery = tkShim(); - withMastery.setSkills([{ id: 329, level: 10 }]); - withMastery.setEnemyInline(BARE_TARGET); - const masteryDmg = withMastery.readCombatResults().damage; + shim.reset(); + reconfig(); + shim.setSkills([{ id: 329, level: 10 }]); + shim.setEnemyInline(BARE_TARGET); + const masteryDmg = shim.readCombatResults().damage; assert.ok( baseDmg.ave != null && masteryDmg.ave != null, @@ -149,11 +162,15 @@ test("spurt mastery (329) drives barehanded combat damage independently of ATK", `mastery lv10 ave damage ${masteryDmg.ave} should exceed baseline ${baseDmg.ave}`, ); // Sanity: mastery must not change derived ATK (it's a combat-sim bonus, not a stat) - const baseDerived = tkShim().readDerivedStats(); - const masteryDerived = tkShim(); - masteryDerived.setSkills([{ id: 329, level: 10 }]); + shim.reset(); + reconfig(); + const baseDerived = shim.readDerivedStats(); + shim.reset(); + reconfig(); + shim.setSkills([{ id: 329, level: 10 }]); + const masteryDerived = shim.readDerivedStats(); assert.equal( - masteryDerived.readDerivedStats().atk.base, + masteryDerived.atk.base, baseDerived.atk.base, "mastery must not change ATK base (explains why the old test missed it)", ); @@ -164,15 +181,15 @@ test("spurt setBuffs applies both status (379) and mastery (329) to combat", () // mastery (329) adds flat combat damage. Together they produce more damage // than status alone. - const statusOnly = tkShim(); - statusOnly.setSkills([{ id: 379, level: 1 }]); - statusOnly.setEnemyInline(BARE_TARGET); - const statusDmg = statusOnly.readCombatResults().damage; + shim.setSkills([{ id: 379, level: 1 }]); + shim.setEnemyInline(BARE_TARGET); + const statusDmg = shim.readCombatResults().damage; - const both = tkShim(); - both.setBuffs([{ name: "spurt", level: 7 }]); - both.setEnemyInline(BARE_TARGET); - const bothDmg = both.readCombatResults().damage; + shim.reset(); + reconfig(); + shim.setBuffs([{ name: "spurt", level: 7 }]); + shim.setEnemyInline(BARE_TARGET); + const bothDmg = shim.readCombatResults().damage; assert.ok( statusDmg.ave != null && bothDmg.ave != null, @@ -200,18 +217,16 @@ test("spurt setBuffs applies both status (379) and mastery (329) to combat", () test("mild_wind order: earth at level 1 is accepted (lowest unlock)", () => { // earth is index 1; level 1 satisfies the constraint (order 1 <= level 1). - const s = tkShim(); assert.doesNotThrow( - () => s.setBuffs([{ name: "mild_wind", level: 1, element: "earth" }]), + () => shim.setBuffs([{ name: "mild_wind", level: 1, element: "earth" }]), "earth at Mild Wind lv1 should be accepted", ); }); test("mild_wind order: wind at level 1 throws (wind needs level 2)", () => { // wind is index 2; level 1 does NOT satisfy the constraint (order 2 > level 1). - const s = tkShim(); assert.throws( - () => s.setBuffs([{ name: "mild_wind", level: 1, element: "wind" }]), + () => shim.setBuffs([{ name: "mild_wind", level: 1, element: "wind" }]), /exceeds.*level|not available at level/i, "wind at Mild Wind lv1 should throw: wind is unlock index 2", ); @@ -219,37 +234,34 @@ test("mild_wind order: wind at level 1 throws (wind needs level 2)", () => { test("mild_wind order: holy at level 7 is accepted (highest unlock)", () => { // holy is index 7; level 7 satisfies the constraint (order 7 <= level 7). - const s = tkShim(); assert.doesNotThrow( - () => s.setBuffs([{ name: "mild_wind", level: 7, element: "holy" }]), + () => shim.setBuffs([{ name: "mild_wind", level: 7, element: "holy" }]), "holy at Mild Wind lv7 should be accepted", ); }); test("mild_wind order: holy at level 6 throws (holy needs level 7)", () => { // holy is index 7; level 6 does NOT satisfy the constraint (order 7 > level 6). - const s = tkShim(); assert.throws( - () => s.setBuffs([{ name: "mild_wind", level: 6, element: "holy" }]), + () => shim.setBuffs([{ name: "mild_wind", level: 6, element: "holy" }]), /exceeds.*level|not available at level/i, "holy at Mild Wind lv6 should throw: holy is unlock index 7", ); }); test("reset() clears applied buffs (no leak across pooled requests)", () => { - const s = tkShim(); - const base = s.readDerivedStats(); - s.setBuffs([{ name: "taekwon_ranker", level: 1 }]); + const base = shim.readDerivedStats(); + shim.setBuffs([{ name: "taekwon_ranker", level: 1 }]); assert.notDeepEqual( - s.readDerivedStats(), + shim.readDerivedStats(), base, "buff should change derived stats", ); - s.reset(); - s.setLevel({ base: 99, job: 50 }); - s.setStats({ str: 80, agi: 90, vit: 40, int: 1, dex: 60, luk: 30 }); + shim.reset(); + shim.setLevel({ base: 99, job: 50 }); + shim.setStats({ str: 80, agi: 90, vit: 40, int: 1, dex: 60, luk: 30 }); assert.deepEqual( - s.readDerivedStats(), + shim.readDerivedStats(), base, "after reset, the ranker buff must be gone", ); diff --git a/calc-sidecar/test/backends/rocalc/champion_buffs.test.ts b/calc-sidecar/test/backends/rocalc/champion_buffs.test.ts index f001f7a..dd91480 100644 --- a/calc-sidecar/test/backends/rocalc/champion_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/champion_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -12,13 +12,21 @@ import { createShim } from "../../../src/shim.ts"; // against an Ele_Undead target (and shown inert vs a Neutral DemiHuman). const KNUCKLE = 1807; // Fist (knuckle) -function chShim() { - const s = createShim(); - s.setClass("champion"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 70, vit: 40, int: 1, dex: 60, luk: 20 }); - s.equip("weapon", { id: KNUCKLE }); - return s; +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff / debuff +// / land / music banks, so fresh() (reset + reconfigure) gives every test a +// clean, leak-free baseline without re-paying the ~4s createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("champion"); +}); +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 70, vit: 40, int: 1, dex: 60, luk: 20 }); + shim.equip("weapon", { id: KNUCKLE }); + return shim; } const NEUTRAL = { @@ -40,7 +48,7 @@ function damageVs( buffName: string, level: number, ): number { - const s = chShim(); + const s = fresh(); s.setBuffs([{ name: buffName, level }]); s.setEnemyInline(target); const ave = s.readCombatResults().damage.ave; @@ -49,7 +57,7 @@ function damageVs( } function baseDamageVs(target: typeof NEUTRAL | typeof UNDEAD_ELE): number { - const s = chShim(); + const s = fresh(); s.setEnemyInline(target); const ave = s.readCombatResults().damage.ave; assert.ok(ave != null, "base damage.ave must be numeric"); @@ -57,7 +65,7 @@ function baseDamageVs(target: typeof NEUTRAL | typeof UNDEAD_ELE): number { } function secondAveWith(buffs: { name: string; level: number }[]): number { - const s = chShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(NEUTRAL); const v = s.readCombatResults().damage.secondAve; @@ -93,7 +101,7 @@ test("triple_attack raises second-attack (proc) damage (secondAve)", () => { // --- Fury (Critical Explosion): raises crit rate (readDerivedStats().cri) --- test("fury raises crit rate (cri)", () => { - const s = chShim(); + const s = fresh(); const base = s.readDerivedStats().cri; s.setBuffs([{ name: "fury", level: 5 }]); const got = s.readDerivedStats().cri; @@ -122,7 +130,7 @@ test("demon_bane does NOT change damage vs a Neutral DemiHuman target", () => { // --- Dodge: flee steroid (readDerivedStats().flee) --- test("dodge raises flee", () => { - const s = chShim(); + const s = fresh(); const base = s.readDerivedStats().flee; s.setBuffs([{ name: "dodge", level: 10 }]); const got = s.readDerivedStats().flee; @@ -148,7 +156,7 @@ test("mental_strength applies cleanly and the build still scores", () => { // --- reset() isolation --- test("reset() clears the Champion buff banks (no leak across requests)", () => { - const s = chShim(); + const s = fresh(); s.setEnemyInline(NEUTRAL); const plain = s.readCombatResults().damage.ave; s.setBuffs([{ name: "iron_fists", level: 10 }]); diff --git a/calc-sidecar/test/backends/rocalc/classes.shared.ts b/calc-sidecar/test/backends/rocalc/classes.shared.ts new file mode 100644 index 0000000..b8ab3c0 --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes.shared.ts @@ -0,0 +1,128 @@ +// Lock-in checks: every name in the rocalc backend's CLASS_TO_ROCALC_ID must +// load cleanly via setClass. Catches regressions where a future rocalc-data +// refresh or form-template edit silently breaks a class. +// +// The 45 supported classes are split by job tier across classes_first_job / +// _second_job / _transcendent / _high_first / _expanded .test.ts so node --test +// runs them as parallel processes. setClass (~1.5-3s ClickJob, heavier for +// trans classes) is serial WITHIN a file and the calc work is synchronous, so +// the only way to parallelize the sweep is across files; one big file made this +// the slowest in the suite (~64s). Each shard reuses one shim (the ~2.4s jsdom +// + engine load is paid once per shard); back-to-back setClass on a reused shim +// mirrors how the production ShimPool reuses workers, and no buffs are applied +// here, so there is no bank state to leak between classes. +// +// Tiers are uneven (second job 14, trans 13 are the largest), but they're the +// natural class grouping and each shard stays well under the suite's +// contention-bound wall. SUPPORTED_CLASSES below is composed from the tier +// arrays, so the per-tier files and the full list can't drift apart. + +import { test, before } from "node:test"; +import assert from "node:assert/strict"; +import { createShim } from "../../../src/shim.ts"; + +// One canonical name per class id we promise to support. Aliases (mage, +// swordsman, taekwon_master, supernovice, sin_x, scholar, biochemist, minstrel) +// aren't tested separately; same id, same code path. Update these arrays in +// lockstep with CLASS_TO_ROCALC_ID in src/backends/rocalc/index.ts. +// +// Coverage: all 46 pre-renewal m_Job slots load EXCEPT id 34 (High Novice; +// rocalc has empty placeholder data). Confirmed via test/class-coverage.ts. + +export const FIRST_JOB = [ + "novice", + "swordman", + "thief", + "acolyte", + "archer", + "magician", + "merchant", +]; + +export const SECOND_JOB = [ + "knight", + "assassin", + "priest", + "hunter", + "wizard", + "blacksmith", + "crusader", + "rogue", + "monk", + "bard", + "dancer", + "sage", + "alchemist", + "super_novice", +]; + +export const TRANSCENDENT = [ + "lord_knight", + "assassin_cross", + "high_priest", + "sniper", + "high_wizard", + "whitesmith", + "paladin", + "stalker", + "champion", + "clown", + "gypsy", + "professor", + "creator", +]; + +// Transcendent first-class slots (id 34 High Novice excluded; rocalc placeholder). +export const HIGH_FIRST = [ + "high_swordman", + "high_thief", + "high_acolyte", + "high_archer", + "high_magician", + "high_merchant", +]; + +// Expanded classes: the Taekwon path plus Ninja / Gunslinger. +export const EXPANDED = [ + "taekwon_kid", + "star_gladiator", + "soul_linker", + "ninja", + "gunslinger", +]; + +// Single source of truth for the full supported set, composed from the tiers so +// the per-tier shard files can't drift out of sync with the whole. +export const SUPPORTED_CLASSES = [ + ...FIRST_JOB, + ...SECOND_JOB, + ...TRANSCENDENT, + ...HIGH_FIRST, + ...EXPANDED, +]; + +// Registers one "loads cleanly" test per class against a single reused shim. +// Called at module top-level by each classes_.test.ts shard file. +export function runClassLoadChecks(classes: string[]): void { + let shim: ReturnType; + before(() => { + shim = createShim(); + }); + + for (const className of classes) { + test(`setClass("${className}") loads cleanly`, () => { + shim.setClass(className); + // readDerivedStats forces the calc to run; throws if the shim is + // in a half-initialized state from a partial setClass. + const stats = shim.readDerivedStats(); + assert.ok( + stats.maxHp > 0, + `MaxHP should be positive after setClass(${className}), got ${stats.maxHp}`, + ); + assert.ok( + stats.aspd > 0, + `ASPD should be positive after setClass(${className}), got ${stats.aspd}`, + ); + }); + } +} diff --git a/calc-sidecar/test/backends/rocalc/classes.test.ts b/calc-sidecar/test/backends/rocalc/classes.test.ts deleted file mode 100644 index c071b01..0000000 --- a/calc-sidecar/test/backends/rocalc/classes.test.ts +++ /dev/null @@ -1,98 +0,0 @@ -// Lock-in test: every name in the rocalc backend's CLASS_TO_ROCALC_ID -// must load cleanly via setClass. Catches regressions where a future -// rocalc-data refresh or form-template edit silently breaks a class. -// -// Slower than the rest of the suite; spins one fresh shim per -// canonical class; but still well under a minute. Worth the cost -// because a broken class only surfaces here; the runtime treats -// unknown class errors as 4xx, masking the regression. - -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { createShim } from "../../../src/shim.ts"; - -// One canonical name per class id we promise to support. Aliases -// (mage, swordsman, taekwon_master, supernovice, sin_x, scholar, -// biochemist, minstrel) aren't tested separately; same id, same code -// path. Update this list in lockstep with CLASS_TO_ROCALC_ID in -// src/backends/rocalc/index.ts. -// -// Coverage: all 46 pre-renewal m_Job slots load EXCEPT id 34 (High -// Novice; rocalc has empty placeholder data). Confirmed via -// test/class-coverage.ts. -const SUPPORTED_CLASSES = [ - // First job - "novice", - "swordman", - "thief", - "acolyte", - "archer", - "magician", - "merchant", - - // Second job - "knight", - "assassin", - "priest", - "hunter", - "wizard", - "blacksmith", - "crusader", - "rogue", - "monk", - "bard", - "dancer", - "sage", - "alchemist", - "super_novice", - - // Trans - "lord_knight", - "assassin_cross", - "high_priest", - "sniper", - "high_wizard", - "whitesmith", - "paladin", - "stalker", - "champion", - "clown", - "gypsy", - "professor", - "creator", - - // High first-class (id 34 High Novice excluded; rocalc placeholder) - "high_swordman", - "high_thief", - "high_acolyte", - "high_archer", - "high_magician", - "high_merchant", - - // Taekwon path - "taekwon_kid", - "star_gladiator", - "soul_linker", - - // Expanded - "ninja", - "gunslinger", -]; - -for (const className of SUPPORTED_CLASSES) { - test(`setClass("${className}") loads cleanly`, () => { - const shim = createShim(); - shim.setClass(className); - // readDerivedStats forces the calc to run; throws if the shim is - // in a half-initialized state from a partial setClass. - const stats = shim.readDerivedStats(); - assert.ok( - stats.maxHp > 0, - `MaxHP should be positive after setClass(${className}), got ${stats.maxHp}`, - ); - assert.ok( - stats.aspd > 0, - `ASPD should be positive after setClass(${className}), got ${stats.aspd}`, - ); - }); -} diff --git a/calc-sidecar/test/backends/rocalc/classes_expanded.test.ts b/calc-sidecar/test/backends/rocalc/classes_expanded.test.ts new file mode 100644 index 0000000..21b5d2d --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes_expanded.test.ts @@ -0,0 +1,6 @@ +// Class-load lock-in checks for the expanded classes (Taekwon path + Ninja / +// Gunslinger). See classes.shared.ts for the shared class lists, tier-sharding +// rationale, and check logic. +import { runClassLoadChecks, EXPANDED } from "./classes.shared.ts"; + +runClassLoadChecks(EXPANDED); diff --git a/calc-sidecar/test/backends/rocalc/classes_first_job.test.ts b/calc-sidecar/test/backends/rocalc/classes_first_job.test.ts new file mode 100644 index 0000000..178c7e1 --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes_first_job.test.ts @@ -0,0 +1,5 @@ +// Class-load lock-in checks for the first-job tier. See classes.shared.ts for +// the shared class lists, tier-sharding rationale, and check logic. +import { runClassLoadChecks, FIRST_JOB } from "./classes.shared.ts"; + +runClassLoadChecks(FIRST_JOB); diff --git a/calc-sidecar/test/backends/rocalc/classes_high_first.test.ts b/calc-sidecar/test/backends/rocalc/classes_high_first.test.ts new file mode 100644 index 0000000..6b29653 --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes_high_first.test.ts @@ -0,0 +1,6 @@ +// Class-load lock-in checks for the transcendent first-class tier (High +// Swordman, etc.). See classes.shared.ts for the shared class lists, +// tier-sharding rationale, and check logic. +import { runClassLoadChecks, HIGH_FIRST } from "./classes.shared.ts"; + +runClassLoadChecks(HIGH_FIRST); diff --git a/calc-sidecar/test/backends/rocalc/classes_second_job.test.ts b/calc-sidecar/test/backends/rocalc/classes_second_job.test.ts new file mode 100644 index 0000000..3486575 --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes_second_job.test.ts @@ -0,0 +1,5 @@ +// Class-load lock-in checks for the second-job tier. See classes.shared.ts for +// the shared class lists, tier-sharding rationale, and check logic. +import { runClassLoadChecks, SECOND_JOB } from "./classes.shared.ts"; + +runClassLoadChecks(SECOND_JOB); diff --git a/calc-sidecar/test/backends/rocalc/classes_transcendent.test.ts b/calc-sidecar/test/backends/rocalc/classes_transcendent.test.ts new file mode 100644 index 0000000..2b71e36 --- /dev/null +++ b/calc-sidecar/test/backends/rocalc/classes_transcendent.test.ts @@ -0,0 +1,6 @@ +// Class-load lock-in checks for the transcendent (trans 2nd-job) tier. See +// classes.shared.ts for the shared class lists, tier-sharding rationale, and +// check logic. +import { runClassLoadChecks, TRANSCENDENT } from "./classes.shared.ts"; + +runClassLoadChecks(TRANSCENDENT); diff --git a/calc-sidecar/test/backends/rocalc/clown_gypsy_buffs.test.ts b/calc-sidecar/test/backends/rocalc/clown_gypsy_buffs.test.ts index 9177458..f0431fb 100644 --- a/calc-sidecar/test/backends/rocalc/clown_gypsy_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/clown_gypsy_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -12,22 +12,30 @@ const VIOLIN = 1901; // Instrument const ROPE = 1950; // Whip const BOW = 1701; -function shim(cls: string, weaponId: number) { - const s = createShim(); - s.setClass(cls); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 80, agi: 70, vit: 30, int: 30, dex: 70, luk: 20 }); - s.equip("weapon", { id: weaponId }); - return s; +// The shim (jsdom load) is created once and reused. Class varies per test +// (clown vs gypsy), so setClass is called per configure(); reset() first clears +// the prior test's bank/equipment state. Reusing the shim still avoids the +// ~2.4s createShim that the old per-call helper paid on every measurement. +let shim: ReturnType; +before(() => { + shim = createShim(); +}); + +function configure(cls: string, weaponId: number) { + shim.reset(); + shim.setClass(cls); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 80, agi: 70, vit: 30, int: 30, dex: 70, luk: 20 }); + shim.equip("weapon", { id: weaponId }); } function aveWith( cls: string, weaponId: number, buffs: { name: string; level: number }[], ): number { - const s = shim(cls, weaponId); - if (buffs.length) s.setBuffs(buffs); - const ave = s.readCombatResults().damage.ave; + configure(cls, weaponId); + if (buffs.length) shim.setBuffs(buffs); + const ave = shim.readCombatResults().damage.ave; assert.ok(ave != null, "damage.ave must be numeric"); return ave as number; } diff --git a/calc-sidecar/test/backends/rocalc/gunslinger_buffs.test.ts b/calc-sidecar/test/backends/rocalc/gunslinger_buffs.test.ts index c760e5f..0bdae97 100644 --- a/calc-sidecar/test/backends/rocalc/gunslinger_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/gunslinger_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -19,13 +19,24 @@ import { createShim } from "../../../src/shim.ts"; const REVOLVER = 13104; // Garrison const GATLING = 13157; // Drifter (W_GATLING; NOT Destroyer 13160, which is a grenade launcher) -function gsShim(weaponId: number) { - const s = createShim(); - s.setClass("gunslinger"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 50, agi: 90, vit: 30, int: 1, dex: 90, luk: 20 }); - s.equip("weapon", { id: weaponId }); - return s; +// createShim (jsdom + calc engine) and setClass are the expensive steps, so the +// class is set once in before() and the shim reused. reset() preserves the class +// but clears the buff banks and rolls back level / stats / equipment, so +// fresh() re-applies the build (with the requested weapon) to give every test a +// clean, leak-free baseline. +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("gunslinger"); +}); + +function fresh(weaponId: number) { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 50, agi: 90, vit: 30, int: 1, dex: 90, luk: 20 }); + shim.equip("weapon", { id: weaponId }); + return shim; } const NEUTRAL = { @@ -44,25 +55,25 @@ const NEUTRAL = { type Buff = { name: string; level: number }; function hitWith(buffs: Buff[], weaponId: number): number { - const s = gsShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().hit; } function aspdWith(buffs: Buff[], weaponId: number): number { - const s = gsShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().aspd; } function fleeWith(buffs: Buff[], weaponId: number): number { - const s = gsShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().flee; } function aveWith(buffs: Buff[], weaponId: number): number { - const s = gsShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(NEUTRAL); const ave = s.readCombatResults().damage.ave; @@ -71,7 +82,7 @@ function aveWith(buffs: Buff[], weaponId: number): number { } function secondAveWith(buffs: Buff[], weaponId: number): number | null { - const s = gsShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(NEUTRAL); return s.readCombatResults().damage.secondAve; @@ -175,7 +186,7 @@ test("flip_the_coin raises damage.ave (coin-damage model)", () => { // --- reset() isolation --- test("reset() clears Gunslinger buff banks (no hit leak)", () => { - const s = gsShim(REVOLVER); + const s = fresh(REVOLVER); const plain = s.readDerivedStats().hit; s.setBuffs([{ name: "single_action", level: 10 }]); s.readDerivedStats(); diff --git a/calc-sidecar/test/backends/rocalc/high_wizard_buffs.test.ts b/calc-sidecar/test/backends/rocalc/high_wizard_buffs.test.ts index 33c16f9..b71dc97 100644 --- a/calc-sidecar/test/backends/rocalc/high_wizard_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/high_wizard_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -11,13 +11,23 @@ import { createShim } from "../../../src/shim.ts"; const ROD = 1601; // Rod (W_ROD) -function hwShim() { - const s = createShim(); - s.setClass("high_wizard"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 1, agi: 1, vit: 50, int: 99, dex: 90, luk: 1 }); - s.equip("weapon", { id: ROD }); - return s; +// createShim (jsdom + calc engine) and setClass are the expensive steps, so the +// class is set once in before() and the shim reused. reset() preserves the class +// but clears the buff banks and rolls back level / stats / equipment, so +// fresh() re-applies the build to give every test a clean, leak-free baseline. +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("high_wizard"); +}); + +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 1, agi: 1, vit: 50, int: 99, dex: 90, luk: 1 }); + shim.equip("weapon", { id: ROD }); + return shim; } const ENEMY = { @@ -36,7 +46,7 @@ const ENEMY = { type Buff = { name: string; level: number }; function read(buffs: Buff[]): { maxSp: number; matkMax: number; ave: number } { - const s = hwShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(ENEMY); const d = s.readDerivedStats(); @@ -103,7 +113,7 @@ test("increase_sp_recovery is wired but inert", () => { test("reset() clears soul_drain (no maxSp leak)", () => { const baseMaxSp = read([]).maxSp; - const s = hwShim(); + const s = fresh(); s.setBuffs([{ name: "soul_drain", level: 10 }]); s.readDerivedStats(); s.reset(); diff --git a/calc-sidecar/test/backends/rocalc/hp_buffs.test.ts b/calc-sidecar/test/backends/rocalc/hp_buffs.test.ts index 70a86ea..ed126d3 100644 --- a/calc-sidecar/test/backends/rocalc/hp_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/hp_buffs.test.ts @@ -1,13 +1,22 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; +// createShim (jsdom + calc engine) + setClass is the expensive part, so it runs +// once in before() and is reused. reset() preserves the class but clears the +// buff/debuff/land/music banks and rolls level/stats/equipment back to the class +// baseline, so hpShim() = reset()+reconfigure gives each test a clean, leak-free +// shim without re-paying the createShim+setClass cost. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("high_priest"); +}); function hpShim() { - const s = createShim(); - s.setClass("high_priest"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 60, agi: 40, vit: 60, int: 80, dex: 60, luk: 20 }); - return s; + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 60, agi: 40, vit: 60, int: 80, dex: 60, luk: 20 }); + return shim; } // Neutral medium target so element/size mods are 1x; mace-less HP still hits. const TARGET = { diff --git a/calc-sidecar/test/backends/rocalc/hp_debuffs.test.ts b/calc-sidecar/test/backends/rocalc/hp_debuffs.test.ts index c0cef5d..d584ff8 100644 --- a/calc-sidecar/test/backends/rocalc/hp_debuffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/hp_debuffs.test.ts @@ -1,13 +1,22 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; +// createShim (jsdom + calc engine) + setClass is the expensive part, so it runs +// once in before() and is reused. reset() preserves the class but clears the +// buff/debuff/land/music banks and rolls level/stats/equipment back to the class +// baseline, so hpShim() = reset()+reconfigure gives each test a clean, leak-free +// shim without re-paying the createShim+setClass cost. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("high_priest"); +}); function hpShim() { - const s = createShim(); - s.setClass("high_priest"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 40, vit: 60, int: 40, dex: 80, luk: 20 }); - return s; + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 40, vit: 60, int: 40, dex: 80, luk: 20 }); + return shim; } const DEF_TARGET = { hp: 200000, diff --git a/calc-sidecar/test/backends/rocalc/lord_knight_buffs.test.ts b/calc-sidecar/test/backends/rocalc/lord_knight_buffs.test.ts index 46409f9..49fb6e7 100644 --- a/calc-sidecar/test/backends/rocalc/lord_knight_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/lord_knight_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -9,13 +9,22 @@ import { createShim } from "../../../src/shim.ts"; // 1407 Pike) for Spear Mastery. ATK/damage steroids move // readCombatResults().damage.ave; ASPD steroids move readDerivedStats().aspd; // HIT moves readDerivedStats().hit (uncapped -- combat.hit caps at 100). -function lkShim(weaponId: number) { - const s = createShim(); - s.setClass("lord_knight"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 99, agi: 60, vit: 50, int: 1, dex: 60, luk: 1 }); - s.equip("weapon", { id: weaponId }); - return s; +// +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff / debuff +// / land / music banks, so fresh(weaponId) (reset + reconfigure) gives every +// test a clean, leak-free baseline without re-paying the ~4s createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("lord_knight"); +}); +function fresh(weaponId: number) { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 99, agi: 60, vit: 50, int: 1, dex: 60, luk: 1 }); + shim.equip("weapon", { id: weaponId }); + return shim; } const TWO_HAND_SWORD = 1163; // Claymore @@ -36,7 +45,7 @@ const TARGET = { }; function damageWith(weaponId: number, buffName: string, level: number): number { - const s = lkShim(weaponId); + const s = fresh(weaponId); s.setBuffs([{ name: buffName, level }]); s.setEnemyInline(TARGET); const ave = s.readCombatResults().damage.ave; @@ -45,7 +54,7 @@ function damageWith(weaponId: number, buffName: string, level: number): number { } function baseDamage(weaponId: number): number { - const s = lkShim(weaponId); + const s = fresh(weaponId); s.setEnemyInline(TARGET); const ave = s.readCombatResults().damage.ave; assert.ok(ave != null, "base damage.ave must be numeric"); @@ -85,7 +94,7 @@ test("berserk raises 2H-sword damage (+ATK)", () => { // --- ASPD steroids (readDerivedStats().aspd) --- test("twohand_quicken raises ASPD (2H sword)", () => { - const s = lkShim(TWO_HAND_SWORD); + const s = fresh(TWO_HAND_SWORD); const base = s.readDerivedStats().aspd; s.setBuffs([{ name: "twohand_quicken", level: 10 }]); const got = s.readDerivedStats().aspd; @@ -96,7 +105,7 @@ test("twohand_quicken raises ASPD (2H sword)", () => { }); test("berserk raises ASPD", () => { - const s = lkShim(TWO_HAND_SWORD); + const s = fresh(TWO_HAND_SWORD); const base = s.readDerivedStats().aspd; s.setBuffs([{ name: "berserk", level: 1 }]); const got = s.readDerivedStats().aspd; @@ -104,7 +113,7 @@ test("berserk raises ASPD", () => { }); test("onehand_quicken raises ASPD (1H sword)", () => { - const s = lkShim(ONE_HAND_SWORD); + const s = fresh(ONE_HAND_SWORD); const base = s.readDerivedStats().aspd; s.setBuffs([{ name: "onehand_quicken", level: 1 }]); const got = s.readDerivedStats().aspd; @@ -116,7 +125,7 @@ test("onehand_quicken raises ASPD (1H sword)", () => { // --- HIT steroid --- test("concentration raises HIT", () => { - const s = lkShim(TWO_HAND_SWORD); + const s = fresh(TWO_HAND_SWORD); const base = s.readDerivedStats().hit; s.setBuffs([{ name: "concentration", level: 5 }]); const got = s.readDerivedStats().hit; @@ -180,7 +189,7 @@ for (const buff of [ // --- reset() isolation --- test("reset() clears the Lord Knight buff banks (no leak across requests)", () => { - const s = lkShim(TWO_HAND_SWORD); + const s = fresh(TWO_HAND_SWORD); s.setEnemyInline(TARGET); const plain = s.readCombatResults().damage.ave; s.setBuffs([{ name: "concentration", level: 5 }]); diff --git a/calc-sidecar/test/backends/rocalc/music_bank.test.ts b/calc-sidecar/test/backends/rocalc/music_bank.test.ts index 45b13e4..1c2f11f 100644 --- a/calc-sidecar/test/backends/rocalc/music_bank.test.ts +++ b/calc-sidecar/test/backends/rocalc/music_bank.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -11,25 +11,31 @@ import { createShim } from "../../../src/shim.ts"; // weapon-sensitive effects manifest. const VIOLIN = 1901; -function shim() { - const s = createShim(); - s.setClass("clown"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 80, agi: 70, vit: 30, int: 30, dex: 70, luk: 20 }); - s.equip("weapon", { id: VIOLIN }); - return s; +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("clown"); +}); + +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 80, agi: 70, vit: 30, int: 30, dex: 70, luk: 20 }); + shim.equip("weapon", { id: VIOLIN }); + return shim; } test("a_whistle raises flee (A3 music driver populates + drives the bank)", () => { - const base = shim().readDerivedStats().flee; - const s = shim(); + const base = fresh().readDerivedStats().flee; + const s = fresh(); s.setBuffs([{ name: "a_whistle", level: 10 }]); const got = s.readDerivedStats().flee; assert.ok(got > base, `a_whistle should raise flee (${base} -> ${got})`); }); test("reset() clears the A3 bank (no flee leak)", () => { - const s = shim(); + const s = fresh(); const plain = s.readDerivedStats().flee; s.setBuffs([{ name: "a_whistle", level: 10 }]); s.readDerivedStats(); @@ -55,8 +61,8 @@ const SCORED: [string, number, "flee" | "aspd" | "maxHp" | "hit" | "cri"][] = [ ]; for (const [name, lv, field] of SCORED) { test(`${name} raises ${field}`, () => { - const base = shim().readDerivedStats()[field]; - const s = shim(); + const base = fresh().readDerivedStats()[field]; + const s = fresh(); s.setBuffs([{ name, level: lv }]); const got = s.readDerivedStats()[field]; assert.ok(got > base, `${name} should raise ${field} (${base} -> ${got})`); @@ -64,8 +70,8 @@ for (const [name, lv, field] of SCORED) { } test("drum_on_the_battlefield raises damage.ave", () => { - const base = shim().readCombatResults().damage.ave ?? 0; - const s = shim(); + const base = fresh().readCombatResults().damage.ave ?? 0; + const s = fresh(); s.setBuffs([{ name: "drum_on_the_battlefield", level: 5 }]); const got = s.readCombatResults().damage.ave ?? 0; assert.ok(got > base, `drum should raise ave (${base} -> ${got})`); @@ -84,7 +90,7 @@ const INERT = [ ]; for (const name of INERT) { test(`${name} is wired and drives without error`, () => { - const s = shim(); + const s = fresh(); assert.doesNotThrow(() => s.setBuffs([{ name, level: 5 }])); const ave = s.readCombatResults().damage.ave; assert.ok(ave != null, `${name}: combat still computes`); diff --git a/calc-sidecar/test/backends/rocalc/ninja_buffs.test.ts b/calc-sidecar/test/backends/rocalc/ninja_buffs.test.ts index fc8b260..dc71ec6 100644 --- a/calc-sidecar/test/backends/rocalc/ninja_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/ninja_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -15,13 +15,21 @@ import { createShim } from "../../../src/shim.ts"; const HUUMA = 13300; // Huuma Wing Shuriken (W_HUUMA) -function njShim() { - const s = createShim(); - s.setClass("ninja"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 60, vit: 40, int: 60, dex: 90, luk: 40 }); - s.equip("weapon", { id: HUUMA }); - return s; +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff banks and +// rolls level / stats / equipment back to baseline, so fresh() re-applies the +// Ninja build for a clean, leak-free baseline without re-paying createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("ninja"); +}); +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 60, vit: 40, int: 60, dex: 90, luk: 40 }); + shim.equip("weapon", { id: HUUMA }); + return shim; } const ENEMY = { @@ -40,7 +48,7 @@ const ENEMY = { type Buff = { name: string; level: number }; function aveWith(buffs: Buff[]): number { - const s = njShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(ENEMY); const ave = s.readCombatResults().damage.ave; @@ -84,7 +92,7 @@ test("ninja_mastery is wired but inert", () => { }); test("reset() clears Ninja buffs (no atk.base leak)", () => { - const s = njShim(); + const s = fresh(); const plain = s.readDerivedStats().atk.base; s.setBuffs([{ name: "ninja_aura", level: 5 }]); s.readDerivedStats(); diff --git a/calc-sidecar/test/backends/rocalc/paladin_buffs.test.ts b/calc-sidecar/test/backends/rocalc/paladin_buffs.test.ts index 62a6920..32433f0 100644 --- a/calc-sidecar/test/backends/rocalc/paladin_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/paladin_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -12,13 +12,22 @@ import { createShim } from "../../../src/shim.ts"; const LANCE = 1410; // 2H spear (W_2HSPEAR) const PIKE = 1407; // 1H spear (W_1HSPEAR) -function palShim(weaponId: number) { - const s = createShim(); - s.setClass("paladin"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 70, vit: 40, int: 1, dex: 60, luk: 20 }); - s.equip("weapon", { id: weaponId }); - return s; +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff banks and +// rolls level / stats / equipment back to baseline, so fresh(weaponId) re-applies +// the Paladin build for a clean, leak-free baseline without re-paying +// createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("paladin"); +}); +function fresh(weaponId: number) { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 70, vit: 40, int: 1, dex: 60, luk: 20 }); + shim.equip("weapon", { id: weaponId }); + return shim; } const NEUTRAL = { @@ -35,7 +44,7 @@ const NEUTRAL = { } as const; function maxHpWith(buffs: { name: string; level: number }[]): number { - const s = palShim(LANCE); + const s = fresh(LANCE); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().maxHp; } @@ -44,7 +53,7 @@ function aspdWith( weaponId: number, buffs: { name: string; level: number }[], ): number { - const s = palShim(weaponId); + const s = fresh(weaponId); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().aspd; } @@ -87,10 +96,10 @@ test("spear_quicken does NOT change aspd on a 1H spear (Pike)", () => { // --- Regression lock: an inherited buff (Spear Mastery) drives on Paladin --- test("inherited spear_mastery raises damage on Paladin (shared bank drives)", () => { - const b = palShim(LANCE); + const b = fresh(LANCE); b.setEnemyInline(NEUTRAL); const base = b.readCombatResults().damage.ave; - const s = palShim(LANCE); + const s = fresh(LANCE); s.setBuffs([{ name: "spear_mastery", level: 10 }]); s.setEnemyInline(NEUTRAL); const got = s.readCombatResults().damage.ave; @@ -103,7 +112,7 @@ test("inherited spear_mastery raises damage on Paladin (shared bank drives)", () // --- reset() isolation --- test("reset() clears Paladin buff banks (no maxHp leak)", () => { - const s = palShim(LANCE); + const s = fresh(LANCE); const plain = s.readDerivedStats().maxHp; s.setBuffs([{ name: "faith", level: 10 }]); s.readDerivedStats(); diff --git a/calc-sidecar/test/backends/rocalc/scholar_buffs.test.ts b/calc-sidecar/test/backends/rocalc/scholar_buffs.test.ts index 639cdda..5acdde9 100644 --- a/calc-sidecar/test/backends/rocalc/scholar_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/scholar_buffs.test.ts @@ -1,13 +1,19 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("professor"); +}); + function profShim() { - const s = createShim(); - s.setClass("professor"); - s.setLevel({ base: 99, job: 50 }); - s.setStats({ str: 70, agi: 40, vit: 40, int: 80, dex: 80, luk: 20 }); - return s; + shim.reset(); + shim.setLevel({ base: 99, job: 50 }); + shim.setStats({ str: 70, agi: 40, vit: 40, int: 80, dex: 80, luk: 20 }); + return shim; } // Neutral medium target that also deals damage (for incoming/EHP checks). const TARGET = { diff --git a/calc-sidecar/test/backends/rocalc/scholar_land.test.ts b/calc-sidecar/test/backends/rocalc/scholar_land.test.ts index f035b17..c366459 100644 --- a/calc-sidecar/test/backends/rocalc/scholar_land.test.ts +++ b/calc-sidecar/test/backends/rocalc/scholar_land.test.ts @@ -1,13 +1,22 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; +// createShim (jsdom + calc engine) + setClass is the expensive part, so it runs +// once in before() and is reused. reset() preserves the class but clears the +// buff/debuff/land/music banks and rolls level/stats/equipment back to the class +// baseline, so profShim() = reset()+reconfigure gives each test a clean, +// leak-free shim without re-paying the createShim+setClass cost. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("professor"); +}); function profShim() { - const s = createShim(); - s.setClass("professor"); - s.setLevel({ base: 99, job: 50 }); - s.setStats({ str: 80, agi: 40, vit: 40, int: 60, dex: 80, luk: 20 }); - return s; + shim.reset(); + shim.setLevel({ base: 99, job: 50 }); + shim.setStats({ str: 80, agi: 40, vit: 40, int: 60, dex: 80, luk: 20 }); + return shim; } const TARGET = { hp: 50000, diff --git a/calc-sidecar/test/backends/rocalc/sinx_buffs.test.ts b/calc-sidecar/test/backends/rocalc/sinx_buffs.test.ts index 132fc38..e92cd92 100644 --- a/calc-sidecar/test/backends/rocalc/sinx_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/sinx_buffs.test.ts @@ -1,19 +1,35 @@ -import { test } from "node:test"; +import { test, before, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; // Assassin Cross with a Katar equipped (iRO 1252). EDP and the katar masteries // multiply WEAPON ATK, so unlike the Scholar caster endows these tests need a // real weapon, not a bare fist. -function sinxShim() { - const s = createShim(); - s.setClass("assassin_cross"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 90, vit: 40, int: 1, dex: 70, luk: 40 }); - s.equip("weapon", { id: 1252 }); - return s; +// +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff / debuff +// / land / music banks, so reset()+reconfigure in beforeEach gives every test a +// clean, leak-free baseline without re-paying the ~4s createShim+setClass. +let shim: ReturnType; + +// reset() rolls level / stats / equipment back to the class baseline, so the +// SinX build is re-applied after each reset (the class itself survives reset()). +function reconfig(): void { + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 90, vit: 40, int: 1, dex: 70, luk: 40 }); + shim.equip("weapon", { id: 1252 }); } +before(() => { + shim = createShim(); + shim.setClass("assassin_cross"); +}); + +beforeEach(() => { + shim.reset(); + reconfig(); +}); + const TARGET = { hp: 50000, atk_min: 300, @@ -28,13 +44,11 @@ const TARGET = { }; test("enchant_deadly_poison raises auto-attack damage", () => { - const base = sinxShim(); - base.setEnemyInline(TARGET); - const plain = base.readCombatResults().damage.ave; - const s = sinxShim(); - s.setBuffs([{ name: "enchant_deadly_poison", level: 5 }]); - s.setEnemyInline(TARGET); - const edp = s.readCombatResults().damage.ave; + shim.setEnemyInline(TARGET); + const plain = shim.readCombatResults().damage.ave; + shim.setBuffs([{ name: "enchant_deadly_poison", level: 5 }]); + shim.setEnemyInline(TARGET); + const edp = shim.readCombatResults().damage.ave; assert.ok(plain != null && edp != null, "damage.ave must be numeric"); assert.ok( (edp as number) > (plain as number), @@ -46,13 +60,11 @@ test("enchant_poison endows poison (changes damage vs an Ele_Poison target)", () // Plain neutral katar hits a poison target ~100%; a poison-endowed weapon // hits it for far less (poison vs poison), so the damage must change. const tgt = { ...TARGET, element: "Ele_Poison" as typeof TARGET.element }; - const base = sinxShim(); - base.setEnemyInline(tgt); - const plain = base.readCombatResults().damage.ave; - const s = sinxShim(); - s.setBuffs([{ name: "enchant_poison", level: 5, element: "poison" }]); - s.setEnemyInline(tgt); - const poison = s.readCombatResults().damage.ave; + shim.setEnemyInline(tgt); + const plain = shim.readCombatResults().damage.ave; + shim.setBuffs([{ name: "enchant_poison", level: 5, element: "poison" }]); + shim.setEnemyInline(tgt); + const poison = shim.readCombatResults().damage.ave; assert.ok(plain != null && poison != null, "damage.ave must be numeric"); assert.notEqual( poison, @@ -62,13 +74,11 @@ test("enchant_poison endows poison (changes damage vs an Ele_Poison target)", () }); test("katar_mastery raises katar damage", () => { - const base = sinxShim(); - base.setEnemyInline(TARGET); - const plain = base.readCombatResults().damage.ave; - const s = sinxShim(); - s.setBuffs([{ name: "katar_mastery", level: 10 }]); - s.setEnemyInline(TARGET); - const got = s.readCombatResults().damage.ave; + shim.setEnemyInline(TARGET); + const plain = shim.readCombatResults().damage.ave; + shim.setBuffs([{ name: "katar_mastery", level: 10 }]); + shim.setEnemyInline(TARGET); + const got = shim.readCombatResults().damage.ave; assert.ok(plain != null && got != null, "damage.ave must be numeric"); assert.ok( (got as number) > (plain as number), @@ -77,13 +87,11 @@ test("katar_mastery raises katar damage", () => { }); test("advanced_katar_mastery raises katar damage", () => { - const base = sinxShim(); - base.setEnemyInline(TARGET); - const plain = base.readCombatResults().damage.ave; - const s = sinxShim(); - s.setBuffs([{ name: "advanced_katar_mastery", level: 5 }]); - s.setEnemyInline(TARGET); - const got = s.readCombatResults().damage.ave; + shim.setEnemyInline(TARGET); + const plain = shim.readCombatResults().damage.ave; + shim.setBuffs([{ name: "advanced_katar_mastery", level: 5 }]); + shim.setEnemyInline(TARGET); + const got = shim.readCombatResults().damage.ave; assert.ok(plain != null && got != null, "damage.ave must be numeric"); assert.ok( (got as number) > (plain as number), @@ -92,11 +100,10 @@ test("advanced_katar_mastery raises katar damage", () => { }); test("improve_dodge raises flee", () => { - const base = sinxShim().readDerivedStats().flee; - const s = sinxShim(); - s.setBuffs([{ name: "improve_dodge", level: 10 }]); + const base = shim.readDerivedStats().flee; + shim.setBuffs([{ name: "improve_dodge", level: 10 }]); assert.ok( - s.readDerivedStats().flee > base, + shim.readDerivedStats().flee > base, "improve dodge should raise flee", ); }); @@ -105,35 +112,33 @@ test("double_attack / hand masteries / sonic_acceleration apply cleanly", () => // With a katar these are no-ops in the auto-attack sim (Double Attack is // dagger-only, the hand masteries need dual-wield, Sonic Acceleration only // affects Sonic Blow). The contract: the rocalc id resolves and the build - // still scores. + // still scores. reset() between buffs isolates each one (no accumulation). for (const name of [ "double_attack", "right_hand_mastery", "left_hand_mastery", "sonic_acceleration", ]) { - const s = sinxShim(); - s.setBuffs([{ name, level: 1 }]); - s.setEnemyInline(TARGET); + shim.reset(); + reconfig(); + shim.setBuffs([{ name, level: 1 }]); + shim.setEnemyInline(TARGET); assert.ok( - s.readCombatResults().damage.ave != null, + shim.readCombatResults().damage.ave != null, `${name} should not break scoring`, ); } }); test("reset() clears the SinX buff bank (no leak across requests)", () => { - const s = sinxShim(); - s.setEnemyInline(TARGET); - const plain = s.readCombatResults().damage.ave; - s.setBuffs([{ name: "enchant_deadly_poison", level: 5 }]); - s.readCombatResults(); - s.reset(); - s.setClass("assassin_cross"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 90, vit: 40, int: 1, dex: 70, luk: 40 }); - s.equip("weapon", { id: 1252 }); - s.setEnemyInline(TARGET); - const after = s.readCombatResults().damage.ave; + shim.setEnemyInline(TARGET); + const plain = shim.readCombatResults().damage.ave; + shim.setBuffs([{ name: "enchant_deadly_poison", level: 5 }]); + shim.readCombatResults(); + shim.reset(); + shim.setClass("assassin_cross"); + reconfig(); + shim.setEnemyInline(TARGET); + const after = shim.readCombatResults().damage.ave; assert.equal(after, plain, "reset must clear EDP (no leak)"); }); diff --git a/calc-sidecar/test/backends/rocalc/sniper_buffs.test.ts b/calc-sidecar/test/backends/rocalc/sniper_buffs.test.ts index 4a878b0..fcd5f7c 100644 --- a/calc-sidecar/test/backends/rocalc/sniper_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/sniper_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -6,13 +6,22 @@ import { createShim } from "../../../src/shim.ts"; // DEX/ATK steroids move scored damage; HIT/flee steroids move readDerivedStats. // rocalc scores bows without an arrow (damage is lower than in-game, but the // buffed-vs-unbuffed comparison is valid). -function sniperShim() { - const s = createShim(); - s.setClass("sniper"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 1, agi: 90, vit: 40, int: 1, dex: 99, luk: 40 }); - s.equip("weapon", { id: 1718 }); - return s; +// +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff / debuff +// / land / music banks, so fresh() (reset + reconfigure) gives every test a +// clean, leak-free baseline without re-paying the ~4s createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("sniper"); +}); +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 1, agi: 90, vit: 40, int: 1, dex: 99, luk: 40 }); + shim.equip("weapon", { id: 1718 }); + return shim; } const TARGET = { @@ -29,10 +38,10 @@ const TARGET = { }; test("owls_eye raises auto-attack damage (DEX -> bow ATK)", () => { - const base = sniperShim(); + const base = fresh(); base.setEnemyInline(TARGET); const plain = base.readCombatResults().damage.ave; - const s = sniperShim(); + const s = fresh(); s.setBuffs([{ name: "owls_eye", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -49,29 +58,28 @@ test("owls_eye and vultures_eye scale with level (5 < 10)", () => { // HIT (readDerivedStats, uncapped) -- it rises monotonically with level either // way. combat.hit caps at 100 so is not used here. for (const name of ["owls_eye", "vultures_eye"]) { - const lo = sniperShim(); - lo.setBuffs([{ name, level: 5 }]); - const hi = sniperShim(); - hi.setBuffs([{ name, level: 10 }]); - assert.ok( - hi.readDerivedStats().hit > lo.readDerivedStats().hit, - `${name} level 10 HIT should exceed level 5`, - ); + fresh(); + shim.setBuffs([{ name, level: 5 }]); + const loHit = shim.readDerivedStats().hit; + fresh(); + shim.setBuffs([{ name, level: 10 }]); + const hiHit = shim.readDerivedStats().hit; + assert.ok(hiHit > loHit, `${name} level 10 HIT should exceed level 5`); } }); test("vultures_eye raises HIT", () => { - const base = sniperShim().readDerivedStats().hit; - const s = sniperShim(); + const base = fresh().readDerivedStats().hit; + const s = fresh(); s.setBuffs([{ name: "vultures_eye", level: 10 }]); assert.ok(s.readDerivedStats().hit > base, "vultures_eye should raise HIT"); }); test("improve_concentration raises auto-attack damage (AGI/DEX steroid)", () => { - const base = sniperShim(); + const base = fresh(); base.setEnemyInline(TARGET); const plain = base.readCombatResults().damage.ave; - const s = sniperShim(); + const s = fresh(); s.setBuffs([{ name: "improve_concentration", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -83,10 +91,10 @@ test("improve_concentration raises auto-attack damage (AGI/DEX steroid)", () => }); test("true_sight raises auto-attack damage (all stats + ATK)", () => { - const base = sniperShim(); + const base = fresh(); base.setEnemyInline(TARGET); const plain = base.readCombatResults().damage.ave; - const s = sniperShim(); + const s = fresh(); s.setBuffs([{ name: "true_sight", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -98,18 +106,18 @@ test("true_sight raises auto-attack damage (all stats + ATK)", () => { }); test("wind_walk raises flee", () => { - const base = sniperShim().readDerivedStats().flee; - const s = sniperShim(); + const base = fresh().readDerivedStats().flee; + const s = fresh(); s.setBuffs([{ name: "wind_walk", level: 10 }]); assert.ok(s.readDerivedStats().flee > base, "wind_walk should raise flee"); }); test("beast_bane raises damage vs a Brute target", () => { // Beast Bane only adds ATK vs Brute/Insect; TARGET is RC_Brute. - const base = sniperShim(); + const base = fresh(); base.setEnemyInline(TARGET); const plain = base.readCombatResults().damage.ave; - const s = sniperShim(); + const s = fresh(); s.setBuffs([{ name: "beast_bane", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -124,10 +132,10 @@ test("steel_crow applies cleanly and leaves auto-attack damage unchanged", () => // Steel Crow only boosts Blitz Beat damage; the auto-attack sim does not score // Blitz Beat, so driving it is a no-op here. Contract: the rocalc id resolves // and the build still scores, unchanged. - const base = sniperShim(); + const base = fresh(); base.setEnemyInline(TARGET); const plain = base.readCombatResults().damage.ave; - const s = sniperShim(); + const s = fresh(); s.setBuffs([{ name: "steel_crow", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -136,7 +144,7 @@ test("steel_crow applies cleanly and leaves auto-attack damage unchanged", () => }); test("reset() clears the Sniper buff bank (no leak across requests)", () => { - const s = sniperShim(); + const s = fresh(); s.setEnemyInline(TARGET); const plain = s.readCombatResults().damage.ave; s.setBuffs([{ name: "true_sight", level: 10 }]); diff --git a/calc-sidecar/test/backends/rocalc/soul_linker_buffs.test.ts b/calc-sidecar/test/backends/rocalc/soul_linker_buffs.test.ts index 79355f3..80d6b80 100644 --- a/calc-sidecar/test/backends/rocalc/soul_linker_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/soul_linker_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -11,13 +11,19 @@ import { createShim } from "../../../src/shim.ts"; const ROD = 1601; // Rod (W_ROD) +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("soul_linker"); +}); + function slShim() { - const s = createShim(); - s.setClass("soul_linker"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 40, agi: 50, vit: 50, int: 80, dex: 70, luk: 30 }); - s.equip("weapon", { id: ROD }); - return s; + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 40, agi: 50, vit: 50, int: 80, dex: 70, luk: 30 }); + shim.equip("weapon", { id: ROD }); + return shim; } const ENEMY = { diff --git a/calc-sidecar/test/backends/rocalc/stalker_buffs.test.ts b/calc-sidecar/test/backends/rocalc/stalker_buffs.test.ts index 83092c0..fd70bb6 100644 --- a/calc-sidecar/test/backends/rocalc/stalker_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/stalker_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -11,13 +11,21 @@ import { createShim } from "../../../src/shim.ts"; // shared bank drives on Stalker. const DAGGER = 1207; -function stShim() { - const s = createShim(); - s.setClass("stalker"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 70, agi: 90, vit: 30, int: 1, dex: 70, luk: 20 }); - s.equip("weapon", { id: DAGGER }); - return s; +// The shim (jsdom + class load) is the expensive part, so it is created once in +// before() and reused. reset() preserves the class but clears the buff banks and +// rolls level / stats / equipment back to baseline, so fresh() re-applies the +// Stalker build for a clean, leak-free baseline without re-paying createShim+setClass. +let shim: ReturnType; +before(() => { + shim = createShim(); + shim.setClass("stalker"); +}); +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 70, agi: 90, vit: 30, int: 1, dex: 70, luk: 20 }); + shim.equip("weapon", { id: DAGGER }); + return shim; } const NEUTRAL = { @@ -34,13 +42,13 @@ const NEUTRAL = { } as const; function fleeWith(buffs: { name: string; level: number }[]): number { - const s = stShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); return s.readDerivedStats().flee; } function aveWith(buffs: { name: string; level: number }[]): number { - const s = stShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(NEUTRAL); const ave = s.readCombatResults().damage.ave; @@ -85,7 +93,7 @@ test("inherited sword_mastery raises damage on Stalker (shared bank drives)", () // --- reset() isolation --- test("reset() clears Stalker buff banks (no flee leak)", () => { - const s = stShim(); + const s = fresh(); const plain = s.readDerivedStats().flee; s.setBuffs([{ name: "close_confine", level: 1 }]); s.readDerivedStats(); diff --git a/calc-sidecar/test/backends/rocalc/star_gladiator_buffs.test.ts b/calc-sidecar/test/backends/rocalc/star_gladiator_buffs.test.ts index 38f4e64..b8d6aea 100644 --- a/calc-sidecar/test/backends/rocalc/star_gladiator_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/star_gladiator_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -19,12 +19,18 @@ import { createShim } from "../../../src/shim.ts"; // sls_union -> readCombatResults().damage.ave // sls_demon/knowledge/blessing -> inert (status; no scored field) +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("star_gladiator"); +}); + function sgShim() { - const s = createShim(); - s.setClass("star_gladiator"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 30, vit: 60, int: 20, dex: 90, luk: 60 }); - return s; // barehanded + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 30, vit: 60, int: 20, dex: 90, luk: 60 }); + return shim; // barehanded } function enemy(size: string, hp: number) { diff --git a/calc-sidecar/test/backends/rocalc/super_novice_buffs.test.ts b/calc-sidecar/test/backends/rocalc/super_novice_buffs.test.ts index 0492b61..37f274b 100644 --- a/calc-sidecar/test/backends/rocalc/super_novice_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/super_novice_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -12,13 +12,23 @@ import { createShim } from "../../../src/shim.ts"; const DAGGER = 1207; -function snShim() { - const s = createShim(); - s.setClass("super_novice"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 50, agi: 50, vit: 50, int: 50, dex: 50, luk: 50 }); - s.equip("weapon", { id: DAGGER }); - return s; +// createShim (jsdom + calc engine) and setClass are the expensive steps, so the +// class is set once in before() and the shim reused. reset() preserves the class +// but clears the buff banks and rolls back level / stats / equipment, so +// fresh() re-applies the build to give every test a clean, leak-free baseline. +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("super_novice"); +}); + +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 50, agi: 50, vit: 50, int: 50, dex: 50, luk: 50 }); + shim.equip("weapon", { id: DAGGER }); + return shim; } const ENEMY = { @@ -37,7 +47,7 @@ const ENEMY = { type Buff = { name: string; level: number }; function read(buffs: Buff[]) { - const s = snShim(); + const s = fresh(); if (buffs.length) s.setBuffs(buffs); s.setEnemyInline(ENEMY); const d = s.readDerivedStats(); @@ -84,7 +94,7 @@ test("inherited blessing drives on super_novice (support_slot)", () => { test("reset() clears no_death_bonus (no maxHp leak)", () => { const baseMaxHp = read([]).maxHp; - const s = snShim(); + const s = fresh(); s.setBuffs([{ name: "no_death_bonus", level: 1 }]); s.readDerivedStats(); s.reset(); diff --git a/calc-sidecar/test/backends/rocalc/whitesmith_buffs.test.ts b/calc-sidecar/test/backends/rocalc/whitesmith_buffs.test.ts index bbe13bc..2941515 100644 --- a/calc-sidecar/test/backends/rocalc/whitesmith_buffs.test.ts +++ b/calc-sidecar/test/backends/rocalc/whitesmith_buffs.test.ts @@ -1,4 +1,4 @@ -import { test } from "node:test"; +import { test, before } from "node:test"; import assert from "node:assert/strict"; import { createShim } from "../../../src/shim.ts"; @@ -7,13 +7,23 @@ import { createShim } from "../../../src/shim.ts"; // Weapon Perfection observable. ATK/damage steroids move damage.ave; ASPD // steroids move readDerivedStats().aspd; HIT moves readDerivedStats().hit // (uncapped -- combat.hit caps at 100). -function wsShim() { - const s = createShim(); - s.setClass("whitesmith"); - s.setLevel({ base: 99, job: 70 }); - s.setStats({ str: 90, agi: 60, vit: 40, int: 1, dex: 60, luk: 1 }); - s.equip("weapon", { id: 1360 }); - return s; +// createShim (jsdom + calc engine) and setClass are the expensive steps, so the +// class is set once in before() and the shim reused. reset() preserves the class +// but clears the buff banks and rolls back level / stats / equipment, so +// fresh() re-applies the build to give every test a clean, leak-free baseline. +let shim: ReturnType; + +before(() => { + shim = createShim(); + shim.setClass("whitesmith"); +}); + +function fresh() { + shim.reset(); + shim.setLevel({ base: 99, job: 70 }); + shim.setStats({ str: 90, agi: 60, vit: 40, int: 1, dex: 60, luk: 1 }); + shim.equip("weapon", { id: 1360 }); + return shim; } const TARGET = { @@ -30,7 +40,7 @@ const TARGET = { }; function damageWith(buffName: string, level: number): number { - const s = wsShim(); + const s = fresh(); s.setBuffs([{ name: buffName, level }]); s.setEnemyInline(TARGET); const ave = s.readCombatResults().damage.ave; @@ -39,7 +49,7 @@ function damageWith(buffName: string, level: number): number { } function baseDamage(): number { - const s = wsShim(); + const s = fresh(); s.setEnemyInline(TARGET); const ave = s.readCombatResults().damage.ave; assert.ok(ave != null, "base damage.ave must be numeric"); @@ -106,8 +116,8 @@ test("hilt_binding raises auto-attack damage (+ATK)", () => { }); test("adrenaline_rush raises ASPD (Axe/Mace-gated, axe equipped)", () => { - const base = wsShim().readDerivedStats().aspd; - const s = wsShim(); + const base = fresh().readDerivedStats().aspd; + const s = fresh(); s.setBuffs([{ name: "adrenaline_rush", level: 5 }]); assert.ok( s.readDerivedStats().aspd > base, @@ -116,8 +126,8 @@ test("adrenaline_rush raises ASPD (Axe/Mace-gated, axe equipped)", () => { }); test("advanced_adrenaline_rush raises ASPD", () => { - const base = wsShim().readDerivedStats().aspd; - const s = wsShim(); + const base = fresh().readDerivedStats().aspd; + const s = fresh(); s.setBuffs([{ name: "advanced_adrenaline_rush", level: 1 }]); assert.ok( s.readDerivedStats().aspd > base, @@ -127,8 +137,8 @@ test("advanced_adrenaline_rush raises ASPD", () => { test("weaponry_research raises damage and HIT", () => { const plainDmg = baseDamage(); - const baseHit = wsShim().readDerivedStats().hit; - const s = wsShim(); + const baseHit = fresh().readDerivedStats().hit; + const s = fresh(); s.setBuffs([{ name: "weaponry_research", level: 10 }]); s.setEnemyInline(TARGET); const got = s.readCombatResults().damage.ave; @@ -167,7 +177,7 @@ test("skin_tempering applies cleanly and leaves auto-attack damage unchanged", ( }); test("reset() clears the Whitesmith buff banks (no leak across requests)", () => { - const s = wsShim(); + const s = fresh(); s.setEnemyInline(TARGET); const plain = s.readCombatResults().damage.ave; s.setBuffs([{ name: "over_thrust", level: 5 }]);