// node --test tests/solver.test.js const test = require("node:test"); const assert = require("node:assert"); const S = require("../packages/core/src/solver"); // A synthetic world: the meadow shows Blue 30% of the time, the shadow appears 2% of the time, // facing the valley Red always bonds it, the Greens burn half the time and bond Brown (rescue) a quarter of the time. function world(n = 4000, seed = 7) { let s = seed; const r = () => ((s = (s * 1103515245 + 12345) % 2147483648) / 2147483648); const runs = []; for (let i = 0; i < n; i++) { const run = { company: "Alone", direction: ["East", "West", "Center"][i % 3], first_sighting: ["Red", "Brown"][i % 2], scream: "Investigate & help", resolution: ["Killed the bullies yourself", "Squadmate's revenge"][i % 2], post_path: "Forest", goal: "Any (for fun)", malek_intent: "No", enc_Red: null, enc_Green: null, enc_Orange: null, enc_Brown: null, enc_Blue: null, enc_Black: null }; run.hour = r() < 0.5 ? "Tree" : "Valley"; if (run.hour === "Tree") { if (r() < 0.02) { run.branch = "Shadow (rare)"; run.enc_Black = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Black"; } else if (r() < 0.5) { run.branch = "Descend quickly: Red"; run.enc_Red = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Red"; } else { run.branch = "Descend slowly: two Greens"; const u = r(); if (u < 0.5) { run.enc_Green = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Green"; } else if (u < 0.75) { run.enc_Green = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Green"; } else { run.enc_Green = "Saved by another"; run.enc_Brown = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Brown"; } } } else if (r() < 0.2) { run.branch = "Valley Red"; run.enc_Red = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Red"; } else { run.branch = "Passed the Red: meadow"; const u = r(), c = u < 0.3 ? "Blue" : u < 0.65 ? "Orange" : "Brown"; const bond = r() < 0.5; run["enc_" + c] = bond ? "Bonded!" : "Incinerated"; run.outcome = bond ? "Bonded" : "Incinerated"; run.outcome_color = c; } runs.push(run); } return runs; } const policy = S.buildPolicy(world(), { draws: 10, colours: ["Blue", "Black", "Red", "Brown"] }); const choice = (X, h, obj = "thisAttempt") => S.advisor(policy, X, obj).next(h).choice; test("Blue: go to the valley, pass the Red, attempt only the Blue", () => { const t = policy.targets.Blue.thisAttempt; const h = { company: "Alone", direction: "Center", fs: "Red", cadet: "Killed the bullies yourself", post: "Forest" }; assert.strictEqual(choice("Blue", h), "Valley"); assert.strictEqual(choice("Blue", { ...h, hour: "Valley" }), "Pass the Red (meadow)"); const meadow = { ...h, hour: "Valley", valley: "Pass the Red (meadow)" }; assert.strictEqual(choice("Blue", { ...meadow, meadow: "Blue" }), "Attempt the bond"); assert.strictEqual(choice("Blue", { ...meadow, meadow: "Orange" }), "Die on purpose"); assert.strictEqual(S.locate(t.tree, {}).var, "company"); // 30% Blue in the meadow x 50% bond assert.ok(Math.abs(t.pX.point - 0.15) < 0.03, t.pX.point); }); test("Black: climb the tree; the value is about the shadow rate x bond rate", () => { const t = policy.targets.Black.thisAttempt; const h = { company: "Alone", direction: "East", fs: "Red", cadet: "Squadmate's revenge", post: "Forest" }; assert.strictEqual(choice("Black", h), "Tree"); assert.ok(t.pX.point > 0.01 && t.pX.point < 0.035, t.pX.point); }); test("Red: face the valley Red", () => { const h = { company: "Alone", direction: "West", fs: "Brown", cadet: "Killed the bullies yourself", post: "Forest", hour: "Valley" }; assert.strictEqual(choice("Red", h), "Face the Red"); }); test("Brown 'eventually' never risks a non-Brown bond that 'this attempt' accepts", () => { const a = policy.targets.Brown.thisAttempt, e = policy.targets.Brown.eventually; assert.ok(e.pOther.point <= a.pOther.point + 1e-12); assert.ok(e.ratio.mean >= a.ratio.mean - 1e-9); }); test("no history dependence in the data -> large fitted concentration", () => { assert.ok(policy.model.alphas.meadow >= 500, JSON.stringify(policy.model.alphas)); }); test("recommendOption maps the policy onto the bot's screen text", () => { const adv = S.advisor(policy, "Blue", "thisAttempt"); const rec = S.recommendOption(adv, { company: "Alone", direction: "Center", fs: "Red", cadet: "Killed the bullies yourself", post: "Forest" }, ["Continue toward the center of the valley", "Find the tallest tree and climb for a better view"]); assert.strictEqual(rec.index, 0); const none = S.recommendOption(adv, {}, ["Something we have never seen"]); assert.strictEqual(none.index, null); }); test("advisor matches the solver's own decisions on the recommended route", () => { const adv = S.advisor(policy, "Blue", "eventually"); assert.strictEqual(adv.next({}).choice, policy.targets.Blue.eventually.tree.choice); }); test("routing to one encounter: Blue only at the meadow equals the Blue policy; only at the tree Greens gives 0", () => { const meadow = S.advisor(policy, "Blue", "thisAttempt", { only: ["meadow-Blue"] }); const all = S.advisor(policy, "Blue", "thisAttempt"); assert.ok(Math.abs(meadow.value.x - all.value.x) < 1e-6); const greens = S.advisor(policy, "Blue", "thisAttempt", { only: ["tree-Greens"] }); assert.ok(greens.value.x < 1e-9); // Brown via the Greens rescue is possible in this world const rescue = S.advisor(policy, "Brown", "thisAttempt", { only: ["tree-Greens"] }); assert.ok(rescue.value.x > 0.05, rescue.value.x); assert.ok(meadow.attempts("meadow-Blue") > 100); }); test("the cadet encounter is asked in the game's order: scream, clearing or wounded cadet, the fight's moves, then the fate", () => { const adv = S.advisor(policy, "Blue", "thisAttempt"); // the first dragon: you react to it before the scream assert.strictEqual(adv.step({ company: "Alone", direction: "Center", fs: "Red" }).var, "react"); // entering with squadmates asks whether you stay with them assert.strictEqual(adv.step({ company: "1 Squadmate" }).var, "leave"); assert.strictEqual(adv.step({ company: "1 Squadmate", left: "Stay together" }).var, "direction"); const base = { company: "Alone", direction: "Center", fs: "Red", react: "Hold still" }; assert.strictEqual(adv.step(base).var, "scream"); // the synthetic world has no one who kept walking, so that option is (correctly) not offered assert.deepStrictEqual(Object.keys(adv.step(base).options), ["Investigate the scream"]); assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream" }).var, "clearing"); const help = { ...base, scream: "Investigate the scream", clearing: "Help your squadmate" }; assert.strictEqual(adv.step(help).var, "fight"); assert.strictEqual(adv.step({ ...help, fight: "Pause to assess" }).var, "dagger"); // charging straight in skips the dagger choice assert.strictEqual(adv.step({ ...help, fight: "Charge the smaller cadet" }).var, "fate"); const fate = adv.step({ ...help, fight: "Pause to assess", dagger: "Engage the smaller cadet" }); assert.strictEqual(fate.var, "fate"); assert.ok(fate.risk && fate.risk.label === "Killed in the fight"); assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream", clearing: "Walk away" }).var, "hour"); // a bot history that only knows the final resolution still lands in the right place assert.strictEqual(adv.next({ ...base, cadet: "Killed the bullies yourself" }).decision, "post"); }); test("solverHistory converts form variables", () => { const h = S.solverHistory({ company: "Alone", direction: "Center", first_sighting: "Red", resolution: "Tourniquet", hour: "Tree", branch: "Descend slowly: two Greens" }); assert.deepStrictEqual(h, { company: "Alone", direction: "Center", fs: "Red", cadet: "Tourniquet", wounded: "Tourniquet", post: "(no fork)", hour: "Tree", descend: "Slowly (two Greens)", shadow: "no" }); });