Files
agentic-labs-challenge/adaptive-loop/test/campaign.test.ts
T
2026-08-15 19:22:22 +02:00

77 lines
4.0 KiB
TypeScript

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");
});
});