(Adaptive Loop) Prepare for final pass

This commit is contained in:
AlexKucera committed 2026-08-14 09:57:44 +02:00
1 parent 4443836ac7
commit 91f8562ab6
1079 files changed
-1131878

No files matched your search

-270
View File
@@ -1,270 +0,0 @@
import { describe, it, expect } from "vitest";
import { buildTestState } from "../src/domain/scenarios.ts";
import { nextState, resolve } from "../src/domain/transition.ts";
import { canonical, canonicalEqual, cloneState } from "../src/domain/core.ts";
// Helper: trigger a tick that does NOT rotate the inner/middle/outer ring under test
// by rotating a different ring. Direction of the rotated ring is set; others keep initial cw.
const tickOuter = (s: Parameters<typeof nextState>[0]) => nextState(s, { ring: "outer", dir: "cw" });
const tickMiddle = (s: Parameters<typeof nextState>[0]) => nextState(s, { ring: "middle", dir: "cw" });
describe("rotation", () => {
it("cw moves payload i -> i+1 (heat marker; global cooldown -1)", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { heat: 5 } }] });
const n = nextState(s, { ring: "inner", dir: "cw" });
expect(n.rings.inner.direction).toBe("cw");
expect(n.rings.inner.tiles[1].heat).toBe(4); // 5 moved 0->1, then cooldown -1
expect(n.rings.inner.tiles[0].heat).toBe(0);
});
it("ccw moves payload i -> i-1", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { heat: 5 } }] });
const n = nextState(s, { ring: "inner", dir: "ccw" });
expect(n.rings.inner.direction).toBe("ccw");
expect(n.rings.inner.tiles[5].heat).toBe(4); // 0 -> 5 (ccw), cooldown -1
});
it("middle and outer rotate and update direction", () => {
for (const ring of ["middle", "outer"] as const) {
const s = buildTestState({ tiles: [{ ring, index: 0, chamber: { heat: 5 } }] });
const n = nextState(s, { ring, dir: "ccw" });
expect(n.rings[ring].direction).toBe("ccw");
const last = ring === "middle" ? 11 : 17;
expect(n.rings[ring].tiles[last].heat).toBe(4);
}
});
});
describe("gate alignment after rotation", () => {
it("rotating outer moves an on-gate payload off the gate", () => {
// O3 is on gate 1 (3*1). Put a marker; rotate outer cw -> payload moves to O4 (off gate).
const s = buildTestState({ tiles: [{ ring: "outer", index: 3, chamber: { heat: 5 } }] });
const n = nextState(s, { ring: "outer", dir: "cw" });
expect(n.rings.outer.tiles[4].heat).toBe(4); // marker now at O4 (off-gate, 4%3=1!=0)
expect(n.rings.outer.tiles[3].heat).toBe(0);
});
});
describe("channel flow", () => {
it("tangential: nutrient flows cw to next tile", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { nutrient: 6 } }] });
const n = tickOuter(s); // inner doesn't rotate; inner dir cw
expect(n.rings.inner.tiles[0].nutrient).toBe(5); // sent 1 tangential
expect(n.rings.inner.tiles[1].nutrient).toBe(1); // received
});
it("radial inward: outer on-gate sends nutrient to middle on-gate", () => {
const s = buildTestState({ tiles: [{ ring: "outer", index: 0, chamber: { nutrient: 6 } }] });
const n = tickMiddle(s); // middle rotates (empty); outer gate0 has nutrient
// outer0 sent 1 tangential (->O1) + 1 radial (->M0): 6-2=4
expect(n.rings.outer.tiles[0].nutrient).toBe(4);
expect(n.rings.middle.tiles[0].nutrient).toBe(1); // received radial at M0 (gate0)
});
it("caps at 6 (overflow discarded)", () => {
// producer at cap with high nutrient consumes nutrient, gains 0 (clamped)
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 6, nutrient: 6 } }] });
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(6);
expect(n.rings.inner.tiles[0].nutrient).toBe(4); // 6 -1 flow -1 growth = 4
});
});
describe("growth and biological interactions", () => {
it("producer grows consuming nutrient (high nutrient isolates from migration)", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 6 } }] });
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(2); // 1 -> 2
expect(n.rings.inner.tiles[0].nutrient).toBe(4); // 6 -1 flow -1 growth
});
it("grazer consumes one producer and gains one grazer", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, grazer: 1, nutrient: 6 } }] });
const n = tickOuter(s);
// growth: producer 2->3 (nutrient 6->4 after flow); grazer eats 1 -> producer 2, grazer 2
expect(n.rings.inner.tiles[0].producer).toBe(2);
expect(n.rings.inner.tiles[0].grazer).toBe(2);
});
it("phage consumes a grazer and gains; otherwise starves", () => {
const fed = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, grazer: 2, phage: 1, nutrient: 6 } }] });
const nf = tickOuter(fed);
// growth prod2->3; grazer eats prod 3->2 grazer2->3; phage eats grazer 3->2 phage1->2
expect(nf.rings.inner.tiles[0].phage).toBe(2);
const starve = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { phage: 2 } }] });
const ns = tickOuter(starve);
expect(ns.rings.inner.tiles[0].phage).toBe(1); // starved -1
});
});
describe("migration", () => {
it("producer migrates one step toward strictly-greater food (nutrient)", () => {
// I0 producer3 + nutrient6 (survives, stays put mostly); I1 nutrient6 attracts migration
const s = buildTestState({
tiles: [
{ ring: "inner", index: 0, chamber: { producer: 3, nutrient: 6 } },
{ ring: "inner", index: 1, chamber: { nutrient: 6 } },
],
});
const n = tickOuter(s);
// I0 grows 3->4 (nutrient 6->4 after flow); food at I0=4, I1 nutrient=6 >4 -> migrate 1 to I1
expect(n.rings.inner.tiles[0].producer).toBe(3); // 4 -1 migrated
expect(n.rings.inner.tiles[1].producer).toBe(1); // migrated in
});
it("radial migration through a gate toward greater food", () => {
// I0 producer2 + low nutrient; M0 (gate0) high nutrient attracts radial migration outward
const s = buildTestState({
tiles: [
{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 2 } },
{ ring: "middle", index: 0, chamber: { nutrient: 6 } },
],
});
const n = tickOuter(s);
// I0 nutrient stays low; M0 nutrient > I0 -> producer migrates radially to M0
expect(n.rings.middle.tiles[0].producer).toBe(1); // producer reached M0 via gate
expect(n.rings.inner.tiles[0].producer).toBe(2); // grew 2->3 then -1 migrated
});
});
describe("adaptations: benefit, cost, spillover", () => {
it("heat shield: prevents loss (benefit), suppresses growth (cost), cools tile (spillover)", () => {
// nutrient present (no hunger); heat-adapted suppresses growth (cost) and prevents heat loss (benefit)
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, heat: 3 } }],
unlocked: { producer: ["heat"], grazer: [] },
});
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(2); // no growth (cost), no loss (benefit)
expect(n.rings.inner.tiles[0].nutrient).toBe(5); // flow -1; growth suppressed so no consume
});
it("unadapted heat loses 2 population", () => {
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, heat: 3 } }],
});
const n = tickOuter(s);
// growth 2->3 (nutrient 6->4); heat loss -2 -> producer 1
expect(n.rings.inner.tiles[0].producer).toBe(1);
});
it("dormancy: prevents hunger loss (benefit)", () => {
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 0 } }],
unlocked: { producer: ["hunger"], grazer: [] },
});
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(2); // no loss
});
it("unadapted hunger loses 1 population", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 0 } }] });
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(1);
});
it("efflux: prevents toxin pop loss, pays nutrient (cost), reduces toxin (spillover)", () => {
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, toxin: 4 } }],
unlocked: { producer: ["toxin"], grazer: [] },
});
const n = tickOuter(s);
// growth 2->3 (nutrient 6->4 after flow); toxin 4->3 after flow; efflux pays 1 nutrient; spillover -1 toxin
expect(n.rings.inner.tiles[0].producer).toBe(3); // no toxin pop loss (benefit)
expect(n.rings.inner.tiles[0].nutrient).toBe(3); // 4 -1 efflux pay
expect(n.rings.inner.tiles[0].toxin).toBe(2); // 3 -1 spillover
});
it("unadapted toxin loses 1 population", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, toxin: 4 } }] });
const n = tickOuter(s);
expect(n.rings.inner.tiles[0].producer).toBe(2); // grew 2->3 then toxin loss -1 -> 2
});
it("immune node damages the selected lineage by 2", () => {
const s = buildTestState({
tiles: [{ ring: "inner", index: 1, chamber: { producer: 3, nutrient: 6 } }],
fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }],
});
const n = tickOuter(s);
// growth 3->4; immune damage -2 -> producer 2
expect(n.rings.inner.tiles[1].producer).toBe(2);
});
it("cloak: cloaked lineage is protected from immune selection (spillover retarget)", () => {
const cloaked = buildTestState({
tiles: [{ ring: "inner", index: 1, chamber: { producer: 2, grazer: 3 } }],
unlocked: { producer: [], grazer: ["immunity"] },
fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }],
});
const nc = tickOuter(cloaked);
// grazer feeds (producer 2->1, grazer 3->4); immune selects producer (grazer cloaked) -> producer 1->0
expect(nc.rings.inner.tiles[1].grazer).toBe(4); // protected by cloak, survived + fed
const exposed = buildTestState({
tiles: [{ ring: "inner", index: 1, chamber: { producer: 2, grazer: 3 } }],
fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }],
});
const ne = tickOuter(exposed);
// grazer feeds (3->4); immune selects largest (grazer 4) -> grazer 4-2=2
expect(ne.rings.inner.tiles[1].grazer).toBe(2); // damaged without cloak
});
});
describe("environmental response", () => {
it("heat response active: heat source adds 3 (vs 2 when inactive)", () => {
const active = buildTestState({
tiles: [{ ring: "outer", index: 3, chamber: {} }],
fixtures: [{ kind: "heatSource", ring: "outer", index: 3 }],
environmentActive: ["heat"],
});
const na = tickOuter(active);
expect(na.rings.outer.tiles[3].heat).toBe(2); // +3 then -1 cooldown
const inactive = buildTestState({
tiles: [{ ring: "outer", index: 3, chamber: {} }],
fixtures: [{ kind: "heatSource", ring: "outer", index: 3 }],
});
const ni = tickOuter(inactive);
expect(ni.rings.outer.tiles[3].heat).toBe(1); // +2 then -1 cooldown
});
});
describe("immutability, determinism, terminal", () => {
it("input is not mutated", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 1 } }] });
const before = cloneState(s);
nextState(s, { ring: "inner", dir: "cw" });
expect(canonicalEqual(before, s)).toBe(true);
});
it("canonical determinism: same input+action -> same canonical", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 3 } }] });
const a = nextState(s, { ring: "inner", dir: "cw" });
const b = nextState(s, { ring: "inner", dir: "cw" });
expect(canonical(a)).toBe(canonical(b));
});
it("collapse when total producer+grazer = 0", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, heat: 6 } }] });
const n = tickOuter(s);
expect(n.outcome).toBe("collapsed");
});
it("expiry when turn limit reached without win/collapse", () => {
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6 } }],
scenarioId: "expire1",
turnLimit: 1,
});
const n = tickOuter(s);
expect(n.outcome).toBe("expired");
});
it("terminal precedence: collapse beats win", () => {
// win would be true, but population hits 0 -> collapsed
const s = buildTestState({
tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, heat: 6 } }],
scenarioId: "winbutdie",
turnLimit: 5,
win: () => true,
});
const n = tickOuter(s);
expect(n.outcome).toBe("collapsed");
});
});
describe("preview/commit equality + replay", () => {
it("resolve preview state equals committed nextState", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 4, grazer: 1 } }] });
const { state: preview } = resolve(s, { ring: "inner", dir: "cw" });
const committed = nextState(s, { ring: "inner", dir: "cw" });
expect(canonicalEqual(preview, committed)).toBe(true);
});
it("event list is deterministic from prior state + action", () => {
const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 4 } }] });
const e1 = resolve(s, { ring: "inner", dir: "cw" }).events;
const e2 = resolve(s, { ring: "inner", dir: "cw" }).events;
expect(JSON.stringify(e1)).toBe(JSON.stringify(e2));
});
});
-76
View File
@@ -1,76 +0,0 @@
import { describe, it, expect } from "vitest";
import { readFileSync } from "fs";
import { SCENARIOS, buildInitialState, getScenario } from "../src/domain/scenarios.ts";
import { replay } from "../src/domain/solver.ts";
import type { GameState, LineageId, PressureId, RotationAction } from "../src/domain/types.ts";
type Fix = { fixtures: Record<string, { win: RotationAction[] }> };
const data = JSON.parse(readFileSync("test/solver-fixtures.json", "utf8")) as Fix;
/** Simulate campaign progression: win each scenario with the verified route, optionally
* preserving the first newly-unlocked trait, carrying inheritance forward. */
function runCampaign(preserveFirst: boolean): { victory: boolean; inherited: Record<LineageId, PressureId[]>; perScenario: GameState[] } {
let inherited: Record<LineageId, PressureId[]> = { producer: [], grazer: [] };
const perScenario: GameState[] = [];
for (const sc of SCENARIOS) {
const start = buildInitialState(sc, "campaign", inherited);
const end = replay(sc, inherited, data.fixtures[sc.id].win);
expect(end.outcome, `${sc.id} should be won in campaign`).toBe("won");
perScenario.push(end);
if (sc.id === SCENARIOS[SCENARIOS.length - 1].id) break; // no next after last
if (preserveFirst) {
// preserve first newly-unlocked trait not already inherited
for (const lin of ["producer", "grazer"] as LineageId[]) {
for (const p of ["heat", "hunger", "toxin", "immunity"] as PressureId[]) {
if (end.lineage[lin].unlocked[p] && !inherited[lin].includes(p)) {
inherited = { producer: [...inherited.producer], grazer: [...inherited.grazer] };
inherited[lin] = [...inherited[lin], p];
lin; p;
break;
}
}
break;
}
}
}
return { victory: true, inherited, perScenario };
}
describe("Gate 5 — campaign progression", () => {
it("wins all five scenarios in order with baseline lineages (campaign victory reachable)", () => {
const r = runCampaign(false);
expect(r.perScenario).toHaveLength(5);
expect(r.perScenario.every((s) => s.outcome === "won")).toBe(true);
});
it("a preserved lineage trait (gift+cost) still lets the next scenario be won", async () => {
// carry producer Heat Shield (from s1) into s2 and re-solve: campaign can proceed with the burden
const { solveWin } = await import("../src/domain/solver.ts");
const win = solveWin(getScenario("s2-hunger"), { producer: ["heat"], grazer: [] }, { nodeBudget: 3_000_000, timeMs: 60000 });
expect(win, "s2 must remain solvable when carrying inherited Heat Shield").not.toBeNull();
}, 90000);
it("pressure counters and env flags reset between scenarios even when traits inherited", () => {
const inh: Record<LineageId, PressureId[]> = { producer: ["heat"], grazer: [] };
const start = buildInitialState(getScenario("s2-hunger"), "campaign", inh);
expect(start.lineage.producer.pressure.heat).toBe(0); // counter reset
expect(start.environmentActive.heat).toBe(false); // env flag reset
expect(start.lineage.producer.unlocked.heat).toBe(true); // trait preserved
});
it("retry restores the scenario-entry inheritance snapshot", () => {
// entering s3 with a preserved trait, then retry must restore exactly that entry set
const entry: Record<LineageId, PressureId[]> = { producer: ["heat"], grazer: [] };
const s3 = buildInitialState(getScenario("s3-toxin"), "campaign", entry);
expect(s3.lineage.producer.unlocked.heat).toBe(true);
// a retry rebuilds from the same entry snapshot
const retried = buildInitialState(getScenario("s3-toxin"), "campaign", entry);
expect(JSON.stringify(retried)).toBe(JSON.stringify(s3));
});
it("standalone uses clean baseline (no carried traits)", () => {
const standalone = buildInitialState(getScenario("s4-immunity"), "standalone", { producer: [], grazer: [] });
expect(standalone.lineage.producer.unlocked.heat).toBe(false);
expect(standalone.mode).toBe("standalone");
});
});
-44
View File
@@ -1,44 +0,0 @@
import { describe, it, expect } from "vitest";
import { buildInitialState, getScenario } from "../src/domain/scenarios.ts";
import { nextState, resolve } from "../src/domain/transition.ts";
import { canonical, cloneState, canonicalEqual, gateIndex } from "../src/domain/core.ts";
describe("smoke", () => {
it("builds and runs scenario 1 initial rotation without throwing", () => {
const s = buildInitialState(getScenario("s1-heat"), "campaign");
const before = cloneState(s);
const next = nextState(s, { ring: "outer", dir: "cw" });
// input immutability
expect(canonicalEqual(before, s)).toBe(true);
// turn advanced
expect(next.turn).toBe(1);
// outer ring direction updated
expect(next.rings.outer.direction).toBe("cw");
// action logged
expect(next.actionLog).toHaveLength(1);
});
it("cw rotation moves payload at i to i+1", () => {
const s = buildInitialState(getScenario("s1-heat"), "campaign");
// O0 has producer 2, nutrient 3; rotate outer cw -> payload moves to O1, then grows to 3
const next = nextState(s, { ring: "outer", dir: "cw" });
expect(next.rings.outer.tiles[1].producer).toBe(3); // 2 from rotation + 1 growth
expect(next.rings.outer.tiles[0].producer).toBe(0);
});
it("resolve returns deterministic events", () => {
const s = buildInitialState(getScenario("s1-heat"), "campaign");
const a = nextState(s, { ring: "inner", dir: "cw" });
const b = nextState(s, { ring: "inner", dir: "cw" });
expect(canonical(a)).toBe(canonical(b));
const { events } = resolve(s, { ring: "inner", dir: "cw" });
expect(events.some((e) => e.phase === "rotate")).toBe(true);
expect(events.some((e) => e.phase === "outcome")).toBe(true);
});
it("gate index derivation", () => {
expect(gateIndex("inner", 0)).toBe(0);
expect(gateIndex("middle", 1)).toBe(2);
expect(gateIndex("outer", 2)).toBe(6);
});
});
-453
View File
@@ -1,453 +0,0 @@
{
"fixtures": {
"s1-heat": {
"scenarioId": "s1-heat",
"win": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "cw"
},
{
"ring": "outer",
"dir": "cw"
}
],
"winOutcome": "won",
"loss": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"lossOutcome": "expired",
"exploitsAdaptation": true,
"avoidsAdaptation": false
},
"s2-hunger": {
"scenarioId": "s2-hunger",
"win": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "middle",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "ccw"
},
{
"ring": "outer",
"dir": "cw"
},
{
"ring": "middle",
"dir": "cw"
},
{
"ring": "middle",
"dir": "cw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
}
],
"winOutcome": "won",
"loss": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"lossOutcome": "expired",
"exploitsAdaptation": true,
"avoidsAdaptation": false
},
"s3-toxin": {
"scenarioId": "s3-toxin",
"win": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "middle",
"dir": "cw"
}
],
"winOutcome": "won",
"loss": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"lossOutcome": "expired",
"exploitsAdaptation": true,
"avoidsAdaptation": false
},
"s4-immunity": {
"scenarioId": "s4-immunity",
"win": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "inner",
"dir": "ccw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "middle",
"dir": "ccw"
},
{
"ring": "middle",
"dir": "cw"
},
{
"ring": "middle",
"dir": "cw"
}
],
"winOutcome": "won",
"loss": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"lossOutcome": "expired",
"exploitsAdaptation": true,
"avoidsAdaptation": false
},
"s5-convergence": {
"scenarioId": "s5-convergence",
"win": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"winOutcome": "won",
"loss": [
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "outer",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
},
{
"ring": "inner",
"dir": "cw"
}
],
"lossOutcome": "expired",
"exploitsAdaptation": false,
"avoidsAdaptation": true
}
},
"reachOk": true,
"reachReport": [
"ok s2-hunger inh={\"producer\":[],\"grazer\":[]} win=10",
"ok s2-hunger inh={\"producer\":[\"heat\"],\"grazer\":[]} win=10",
"ok s2-hunger inh={\"producer\":[],\"grazer\":[\"heat\"]} win=10",
"ok s3-toxin inh={\"producer\":[],\"grazer\":[]} win=9",
"ok s3-toxin inh={\"producer\":[\"hunger\"],\"grazer\":[]} win=9",
"ok s3-toxin inh={\"producer\":[],\"grazer\":[\"hunger\"]} win=9",
"ok s3-toxin inh={\"producer\":[\"heat\"],\"grazer\":[]} win=9",
"ok s3-toxin inh={\"producer\":[\"heat\",\"hunger\"],\"grazer\":[]} win=9",
"ok s3-toxin inh={\"producer\":[\"heat\"],\"grazer\":[\"hunger\"]} win=9",
"ok s3-toxin inh={\"producer\":[],\"grazer\":[\"heat\"]} win=9",
"ok s3-toxin inh={\"producer\":[\"hunger\"],\"grazer\":[\"heat\"]} win=9",
"ok s3-toxin inh={\"producer\":[],\"grazer\":[\"heat\",\"hunger\"]} win=9",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[]} win=8",
"ok s4-immunity inh={\"producer\":[\"toxin\"],\"grazer\":[]} win=8",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"toxin\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"hunger\"],\"grazer\":[]} win=7",
"ok s4-immunity inh={\"producer\":[\"hunger\",\"toxin\"],\"grazer\":[]} win=7",
"ok s4-immunity inh={\"producer\":[\"hunger\"],\"grazer\":[\"toxin\"]} win=7",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"toxin\"],\"grazer\":[\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"hunger\",\"toxin\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\"],\"grazer\":[]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\",\"toxin\"],\"grazer\":[]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\"],\"grazer\":[\"toxin\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\",\"hunger\"],\"grazer\":[]} win=7",
"ok s4-immunity inh={\"producer\":[\"heat\",\"hunger\",\"toxin\"],\"grazer\":[]} win=7",
"ok s4-immunity inh={\"producer\":[\"heat\",\"hunger\"],\"grazer\":[\"toxin\"]} win=7",
"ok s4-immunity inh={\"producer\":[\"heat\"],\"grazer\":[\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\",\"toxin\"],\"grazer\":[\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"heat\"],\"grazer\":[\"hunger\",\"toxin\"]} win=8",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"heat\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"toxin\"],\"grazer\":[\"heat\"]} win=8",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"heat\",\"toxin\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"hunger\"],\"grazer\":[\"heat\"]} win=7",
"ok s4-immunity inh={\"producer\":[\"hunger\",\"toxin\"],\"grazer\":[\"heat\"]} win=7",
"ok s4-immunity inh={\"producer\":[\"hunger\"],\"grazer\":[\"heat\",\"toxin\"]} win=7",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"heat\",\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[\"toxin\"],\"grazer\":[\"heat\",\"hunger\"]} win=8",
"ok s4-immunity inh={\"producer\":[],\"grazer\":[\"heat\",\"hunger\",\"toxin\"]} win=8",
"ok s5-convergence inh={\"producer\":[],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"immunity\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[],\"grazer\":[\"immunity\"]} win=2",
"ok s5-convergence inh={\"producer\":[\"toxin\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"toxin\",\"immunity\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"toxin\"],\"grazer\":[\"immunity\"]} win=2",
"ok s5-convergence inh={\"producer\":[],\"grazer\":[\"toxin\"]} win=2",
"ok s5-convergence inh={\"producer\":[\"immunity\"],\"grazer\":[\"toxin\"]} win=2",
"ok s5-convergence inh={\"producer\":[],\"grazer\":[\"toxin\",\"immunity\"]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\",\"immunity\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\"],\"grazer\":[\"immunity\"]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\",\"toxin\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\",\"toxin\",\"immunity\"],\"grazer\":[]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\",\"toxin\"],\"grazer\":[\"immunity\"]} win=2",
"ok s5-convergence inh={\"producer\":[\"hunger\"],\"grazer\":[\"toxin\"]} win=2"
]
}
@@ -1,66 +0,0 @@
import { describe, it, expect } from "vitest";
import { readFileSync } from "fs";
import { getScenario, buildInitialState } from "../src/domain/scenarios.ts";
import { replay } from "../src/domain/solver.ts";
import { nextState } from "../src/domain/transition.ts";
import type { RotationAction, LineageId, PressureId } from "../src/domain/types.ts";
type Fixtures = {
fixtures: Record<string, { win: RotationAction[]; winOutcome: string; loss: RotationAction[]; lossOutcome: string; exploitsAdaptation: boolean; avoidsAdaptation: boolean }>;
reachOk: boolean;
reachReport: string[];
};
const data = JSON.parse(readFileSync("test/solver-fixtures.json", "utf8")) as Fixtures;
describe("Gate 4 — scenario solver routes (baseline)", () => {
for (const [id, f] of Object.entries(data.fixtures)) {
it(`${id}: win route replays to 'won'`, () => {
const sc = getScenario(id);
const end = replay(sc, { producer: [], grazer: [] }, f.win);
expect(end.outcome).toBe("won");
});
it(`${id}: loss route replays to a loss (collapsed or expired)`, () => {
const sc = getScenario(id);
const end = replay(sc, { producer: [], grazer: [] }, f.loss);
expect(end.outcome === "collapsed" || end.outcome === "expired").toBe(true);
});
}
it("at least one baseline route exploits an adaptation (unlocks a trait)", () => {
const anyExploits = Object.values(data.fixtures).some((f) => f.exploitsAdaptation);
expect(anyExploits).toBe(true);
});
it("at least one baseline route avoids adaptations (no trait unlocked)", () => {
const anyAvoids = Object.values(data.fixtures).some((f) => f.avoidsAdaptation);
expect(anyAvoids).toBe(true);
});
it("at least one route changes gate alignment to route flow/migration between rings", () => {
// any win route that rotates a ring whose on-gate membership changes is a gate-alignment route;
// rotation always re-aligns gates, so any multi-step win involving >1 distinct ring qualifies.
const route = data.fixtures["s1-heat"].win;
const rings = new Set(route.map((a) => a.ring));
expect(rings.size >= 1).toBe(true);
});
});
describe("Gate 4 — campaign inheritance reachability", () => {
it("every reachable (themed) inheritance set can complete its scenario", () => {
expect(data.reachOk, data.reachReport.filter((r) => r.startsWith("FAIL")).join("\n")).toBe(true);
});
});
describe("Gate 4 — replay reproduction (action log from baseline)", () => {
it("replaying a win's action log from baseline reproduces the exact final state", () => {
const sc = getScenario("s1-heat");
const viaReplay = replay(sc, { producer: [], grazer: [] }, data.fixtures["s1-heat"].win);
let viaLog = buildInitialState(sc, "campaign");
for (const a of data.fixtures["s1-heat"].win) viaLog = nextState(viaLog, a);
expect(JSON.stringify(viaReplay)).toBe(JSON.stringify(viaLog));
});
});
void (undefined as unknown as LineageId);
void (undefined as unknown as PressureId);
-37
View File
@@ -1,37 +0,0 @@
import { describe, it, expect } from "vitest";
import { getScenario, SCENARIOS } from "../src/domain/scenarios.ts";
import { solveWin, solveLoss, replay } from "../src/domain/solver.ts";
describe("solver scenario 1", () => {
it("finds a winning route for baseline scenario 1", () => {
const win = solveWin(getScenario("s1-heat"));
expect(win).not.toBeNull();
if (!win) return;
console.log("S1 win length:", win.length, win.map((a) => `${a.ring[0].toUpperCase()}${a.dir}`).join(","));
const end = replay(getScenario("s1-heat"), { producer: [], grazer: [] }, win);
expect(end.outcome).toBe("won");
}, 60000);
it("finds a losing route for baseline scenario 1", () => {
const loss = solveLoss(getScenario("s1-heat"));
expect(loss).not.toBeNull();
if (!loss) return;
const end = replay(getScenario("s1-heat"), { producer: [], grazer: [] }, loss);
expect(end.outcome === "collapsed" || end.outcome === "expired").toBe(true);
console.log("S1 loss:", end.outcome, loss.length);
}, 60000);
});
describe("solver all baseline scenarios", () => {
for (const sc of SCENARIOS) {
it(`finds win+loss for ${sc.id}`, () => {
const win = solveWin(sc);
expect(win, `no win for ${sc.id}`).not.toBeNull();
if (win) {
const end = replay(sc, { producer: [], grazer: [] }, win);
expect(end.outcome).toBe("won");
console.log(sc.id, "WIN len", win.length);
}
}, 120000);
}
});