110 lines
5.5 KiB
TypeScript
110 lines
5.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { readFileSync } from "fs";
|
|
import { SCENARIOS, getScenario, buildInitialState } from "../src/domain/scenarios.ts";
|
|
import { inheritanceKey, reachableInheritanceAt, replay } from "../src/domain/solver.ts";
|
|
import { nextState, resolve } from "../src/domain/transition.ts";
|
|
import type { RotationAction } from "../src/domain/types.ts";
|
|
|
|
type Fixtures = {
|
|
fixtures: Record<string, { win: RotationAction[]; winOutcome: string; loss: RotationAction[]; lossOutcome: string; exploitsAdaptation: boolean; avoidsAdaptation: boolean }>;
|
|
campaignRoutes: Record<string, RotationAction[]>;
|
|
adaptationEvidence: { scenarioId: string; inheritanceKey: string; route: RotationAction[]; evolved: boolean; benefit: boolean; cost: boolean; spillover: boolean; all: 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("a baseline winning route provokes and exercises adaptation benefit, cost, and spillover", () => {
|
|
const evidence = data.adaptationEvidence;
|
|
const scenario = getScenario(evidence.scenarioId);
|
|
const inherited = reachableInheritanceAt(scenario.index).find((candidate) => inheritanceKey(candidate) === evidence.inheritanceKey)!;
|
|
let state = buildInitialState(scenario, "campaign", inherited);
|
|
const effects = { evolved: false, benefit: false, cost: false, spillover: false };
|
|
for (const action of evidence.route) {
|
|
const result = resolve(state, action, true);
|
|
state = result.state;
|
|
for (const event of result.events) {
|
|
if (event.phase === "evolution") effects.evolved = true;
|
|
if (event.phase === "adaptation" && event.effect === "cost") effects.cost = true;
|
|
if (event.phase === "adaptation" && event.effect === "spillover") effects.spillover = true;
|
|
if (event.phase === "consequence" && event.kind === "prevented") effects.benefit = true;
|
|
if ((event.phase === "consequence" && (event.kind === "payNutrient" || event.kind === "payPop")) ||
|
|
(event.phase === "growth" && event.suppressed) ||
|
|
(event.phase === "feed" && (event.suppressed || event.preyRemoved === 2))) effects.cost = true;
|
|
if (event.phase === "spillover" || (event.phase === "immuneSelect" && event.retarget)) effects.spillover = true;
|
|
}
|
|
}
|
|
expect(evidence.inheritanceKey).toBe("P:|G:");
|
|
expect(state.outcome).toBe("won");
|
|
expect(effects).toEqual({ evolved: true, benefit: true, cost: true, spillover: true });
|
|
});
|
|
|
|
it("at least one baseline route avoids its scenario's matching adaptation", () => {
|
|
const anyAvoids = Object.values(data.fixtures).some((f) => f.avoidsAdaptation);
|
|
expect(anyAvoids).toBe(true);
|
|
});
|
|
|
|
it("a verified route changes gate alignment and produces radial flow", () => {
|
|
const scenario = getScenario("s1-heat");
|
|
let state = buildInitialState(scenario, "campaign");
|
|
let proved = false;
|
|
for (const action of data.fixtures[scenario.id].win) {
|
|
const result = resolve(state, action);
|
|
if (action.ring !== "inner" && result.events.some((event) => event.phase === "flow" && event.radial > 0)) proved = true;
|
|
state = result.state;
|
|
}
|
|
expect(proved).toBe(true);
|
|
});
|
|
|
|
it("Scenario 5 starts outside its objective and its winning route aligns multiple rings", () => {
|
|
const scenario = getScenario("s5-convergence");
|
|
const initial = buildInitialState(scenario, "campaign");
|
|
expect(scenario.sustained(initial, {} as never)).toBe(false);
|
|
expect(new Set(data.fixtures[scenario.id].win.map((action) => action.ring)).size).toBeGreaterThan(1);
|
|
expect(data.fixtures[scenario.id].win.length).toBeGreaterThan(2);
|
|
});
|
|
});
|
|
|
|
describe("Gate 4 — campaign inheritance reachability", () => {
|
|
it("replays a win for every conservative-superset inheritance set", () => {
|
|
expect(data.reachOk, data.reachReport.filter((r) => r.startsWith("FAIL")).join("\n")).toBe(true);
|
|
let checked = 0;
|
|
for (let scenarioIndex = 2; scenarioIndex <= 5; scenarioIndex++) {
|
|
const scenario = SCENARIOS[scenarioIndex - 1];
|
|
for (const inherited of reachableInheritanceAt(scenarioIndex)) {
|
|
const route = data.campaignRoutes[`${scenario.id}|${inheritanceKey(inherited)}`];
|
|
expect(route, `${scenario.id} ${inheritanceKey(inherited)}`).toBeDefined();
|
|
expect(replay(scenario, inherited, route).outcome).toBe("won");
|
|
checked++;
|
|
}
|
|
}
|
|
expect(checked).toBe(302);
|
|
});
|
|
});
|
|
|
|
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));
|
|
});
|
|
});
|
|
|