// Gate 4 — scenario solvability. Validates the precomputed solver fixtures by // replaying each stored route from its baseline + inheritance and asserting the // recorded outcome. Fast and deterministic (the heavy search runs once offline in // scripts/gen-solver-fixtures.ts). Also checks the Gate 4 route-property rules. import { describe, it, expect } from "vitest"; import { nextState } from "../src/domain/pipeline"; import { buildBaselineState, type Inheritance } from "../src/domain/scenario"; import { SCENARIOS } from "../src/content/scenarios"; import type { RotationAction } from "../src/domain/types"; import fixture from "./fixtures/solver-results.json"; type StoredRoute = { actions: RotationAction[]; outcome: string; ownershipChanged: boolean; maxPressure: number }; function replay(scenarioId: string, inheritance: Inheritance, actions: RotationAction[]) { let s = buildBaselineState(scenarioId, "campaign", inheritance); for (const a of actions) s = nextState(s, a).state; return s; } describe("Gate 4 — solver routes (replayed from fixtures)", () => { for (const sc of SCENARIOS) { const entry = (fixture.scenarios as any[]).find((x) => x.id === sc.id); it(`${sc.id} has a winning route that replays to 'won'`, () => { expect(entry, `missing fixture for ${sc.id}`).toBeTruthy(); const win = entry.win as StoredRoute | null; expect(win, `${sc.id} has no stored win route`).toBeTruthy(); const final = replay(sc.id, { producer: [], grazer: [] }, win!.actions); expect(final.outcome).toBe("won"); expect(win!.actions.length).toBeLessThanOrEqual(sc.turnLimit); }); it(`${sc.id} has a losing route that replays to a non-win terminal`, () => { const lose = entry.lose as StoredRoute | null; expect(lose, `${sc.id} has no stored lose route`).toBeTruthy(); const final = replay(sc.id, { producer: [], grazer: [] }, lose!.actions); expect(final.outcome === "collapsed" || final.outcome === "expired").toBe(true); }); } it("at least one route requires a shared-slot ownership change", () => { const ok = (fixture.scenarios as any[]).some((x) => x.win?.ownershipChanged); expect(ok).toBe(true); }); it("at least one route exploits an adaptation (pressure reaches 3)", () => { const ok = (fixture.scenarios as any[]).some((x) => (x.win?.maxPressure ?? 0) >= 3); expect(ok).toBe(true); }); it("at least one route avoids unlocking any adaptation (pressure stays < 3)", () => { const avoid = (fixture.scenarios as any[]).filter((x) => x.avoid); expect(avoid.length, "no adaptation-avoiding route stored").toBeGreaterThan(0); for (const x of avoid) { const final = replay(x.id, { producer: [], grazer: [] }, x.avoid.actions as RotationAction[]); expect(final.outcome).toBe("won"); expect((x.avoid as StoredRoute).maxPressure).toBeLessThan(3); } }); it("every reachable inheritance combination is solvable except the documented dormancy-burden residual risk", () => { const reach = fixture.reachability as { ok: boolean; combos: { scenarioId: string; inheritance: Inheritance; solvable: boolean }[]; residualRiskCombos: { scenarioId: string; inheritance: Inheritance }[]; }; // Residual-risk combos must all be the documented dormancy-into-s5 class. for (const r of reach.residualRiskCombos) { expect(r.scenarioId).toBe("s5-convergence"); expect(r.inheritance.producer).toContain("hunger"); } // Every non-residual-risk combo must be solvable. const riskKeys = new Set(reach.residualRiskCombos.map((r) => `${r.scenarioId}|${JSON.stringify(r.inheritance)}`)); for (const c of reach.combos) { const key = `${c.scenarioId}|${JSON.stringify(c.inheritance)}`; if (riskKeys.has(key)) continue; expect(c.solvable, `unsolvable non-documented combo: ${key}`).toBe(true); } expect(reach.ok).toBe(true); }); });