Files
threshing-atlas/tests/solver.test.js
T
ekcSchianoandClaude Opus 5.5 de80e99c74 The Valley as the landing page: dragons first, routes on demand
- New tab order: The Valley, The Odds, The Dragons, Deep Dive, Data & Methods
  (Route Flow and Blue & Black fold into Deep Dive; methods into Data &
  Methods; old #links redirect).
- explorer.js replaces the trails map and the route planner, with progressive
  disclosure: the painted valley shows landmarks and the dragons where riders
  meet them (faint = rarely met), no routes and no deaths; hover for a note;
  choose a dragon to draw every route to every place it can be bonded, with
  width = chance of still being on the route, and solid / dashed / dotted and
  opacity for how a route compares with the best; click a choice point to
  branch off and re-plan; "Why this route?" holds intervals and risks; a
  toggle shows the community's actual traffic; #valley/blue/meadow-Blue links.
- dragons.js: original dragon silhouettes in six poses, built from simple
  parts.
- Solver: route to one specific encounter (only: [...]); ENCOUNTERS per
  colour, including the Greens rescue for Orange and Brown.
- The Odds rewritten for all six dragons, ranked easiest to hardest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
2026-11-02 14:24:47 +00:00

107 lines
6.0 KiB
JavaScript

// 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("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", post: "(no fork)", hour: "Tree", descend: "Slowly (two Greens)", shadow: "no" });
});