// Adaptive Loop — solver/replay proof (SPEC §15 Gate 4). Replays the verified // win/lose routes stored in tests/fixtures/solver-results.json (generated by the // bounded solver) and asserts the exploit/avoid/counterfactual evidence. import { describe, it, expect } from "vitest"; import { nextState } from "../src/domain/pipeline"; import { createScenarioState } from "../src/domain/scenario"; import { SCENARIOS, SCENARIO_BY_ID } from "../src/content/scenarios"; import type { RotationAction, LineageId, PressureId } from "../src/domain/types"; import results from "./fixtures/solver-results.json"; interface RouteEntry { win: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[]; lose: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[]; winTurns: number; loseTurns: number; loseOutcome: "collapsed" | "expired"; winVerified: boolean; loseVerified: boolean; pressure: PressureId; ownPressureUnlocked: boolean; usesMultipleLoops: boolean; } const R = results as unknown as { scenarios: Record; adaptationEvidence: { exploit: { scenario: string; pressure: PressureId } | null; avoid: { scenario: string; pressure: PressureId } | null }; counterfactual: { scenario: string; solvableNormal: boolean; frozenBeamFoundWin: boolean; frozenExhaustiveFoundWin: boolean; conclusion: string }; }; function replay(scenarioId: string, route: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[], inheritance: Partial> = {}) { let s = createScenarioState(scenarioId, "standalone", inheritance); for (const a of route) { s = nextState(s, a as RotationAction).state; if (s.outcome !== "playing") break; } return s; } describe("Gate 4 — solver routes (replayed from fixtures)", () => { for (const sc of SCENARIOS) { const entry = R.scenarios[sc.id]!; describe(sc.id, () => { it("has a verified winning route", () => { expect(entry, `fixture for ${sc.id}`).toBeDefined(); expect(entry.win.length).toBeGreaterThan(0); expect(entry.win.length).toBeLessThanOrEqual(sc.turnLimit); const final = replay(sc.id, entry.win); expect(final.outcome).toBe("won"); }); it("has a verified losing route", () => { expect(entry.lose.length).toBeGreaterThan(0); const final = replay(sc.id, entry.lose); expect(["collapsed", "expired"]).toContain(final.outcome); }); it("uses rotations of multiple loops (ownership-relevant)", () => { expect(entry.usesMultipleLoops).toBe(true); }); }); } it("at least one route exploits an adaptation (unlocks its scenario's pressure trait)", () => { expect(R.adaptationEvidence.exploit, "exploit route").not.toBeNull(); const ex = R.adaptationEvidence.exploit!; const entry = R.scenarios[ex.scenario]!; expect(entry.ownPressureUnlocked).toBe(true); // replay and confirm the trait actually unlocked during the winning route let s = createScenarioState(ex.scenario, "standalone", {}); let unlocked = false; for (const a of entry.win) { s = nextState(s, a as RotationAction).state; if (s.lineage.producer.unlocked[ex.pressure] || s.lineage.grazer.unlocked[ex.pressure]) unlocked = true; if (s.outcome !== "playing") break; } expect(unlocked).toBe(true); }); it("at least one route avoids an adaptation (its scenario's pressure trait stays locked)", () => { expect(R.adaptationEvidence.avoid, "avoid route").not.toBeNull(); const av = R.adaptationEvidence.avoid!; const entry = R.scenarios[av.scenario]!; expect(entry.ownPressureUnlocked).toBe(false); const final = replay(av.scenario, entry.win); expect(final.outcome).toBe("won"); expect(final.lineage.producer.unlocked[av.pressure]).toBe(false); expect(final.lineage.grazer.unlocked[av.pressure]).toBe(false); }); it("counterfactual: ownership materially changes solvability", () => { const cf = R.counterfactual; expect(cf.solvableNormal, "normal game must be winnable").toBe(true); const normalFinal = replay(cf.scenario, R.scenarios[cf.scenario]!.win); expect(normalFinal.outcome).toBe("won"); // No winning route exists under frozen ownership (bounded-search evidence recorded). expect(cf.frozenBeamFoundWin).toBe(false); expect(cf.frozenExhaustiveFoundWin).toBe(false); }); }); describe("scenario definitions are well-formed", () => { it("all five scenarios present and unique", () => { expect(SCENARIOS.length).toBe(5); expect(new Set(SCENARIOS.map((s) => s.id)).size).toBe(5); }); for (const sc of SCENARIOS) { it(`${sc.id} fixtures reference valid slots`, () => { for (const f of sc.fixtures) expect(f.slot).toBeGreaterThanOrEqual(0); for (const slotStr of Object.keys(sc.start)) { expect(Number(slotStr)).toBeGreaterThanOrEqual(0); expect(Number(slotStr)).toBeLessThan(12); } }); } it("scenario lookup works for all ids", () => { for (const sc of SCENARIOS) expect(SCENARIO_BY_ID[sc.id]).toBeDefined(); }); });