Files
agentic-labs-challenge/adaptive-loop/tests/truth.test.ts
T
2026-08-13 07:33:43 +02:00

271 lines
14 KiB
TypeScript

// Simulation truth tests (SPEC Gate 3 / deterministic-game-core skill). Each test
// exercises a single behavior. Assertions are robust to the full 8-phase pipeline:
// movement is checked by presence, adaptations by adapted-vs-unadapted deltas,
// and flow/caps/structural checks use literal hand-computed values.
import { describe, it, expect } from "vitest";
import { nextState, outletOf } from "../src/domain/pipeline";
import { cloneState, canonicalStringify, canonicalEqual } from "../src/domain/serialize";
import { buildBaselineState } from "../src/domain/scenario";
import { ALL_ACTIONS } from "../src/domain/constants";
import { SCENARIOS } from "../src/content/scenarios";
import { makeState, chamber } from "./helpers";
import type { RotationAction } from "../src/domain/types";
const A = (loop: "A" | "B" | "C", direction: "cw" | "ccw"): RotationAction => ({ loop, direction });
describe("rotations — payload permutation + ownership", () => {
// Producers carry nutrient so they survive the tick (no starvation); we assert
// the payload LANDED at the destination slot and left the source.
it("cw: slot 0 payload lands at slot 1 (loop A)", () => {
const s = makeState([{ slot: 0, nutrient: 6, producer: 2 }]);
const { state } = nextState(s, A("A", "cw"));
expect(chamber(state, 1).producer).toBeGreaterThan(0);
expect(chamber(state, 0).producer).toBe(0);
});
it("ccw: slot 1 payload lands at slot 0 (loop A)", () => {
const s = makeState([{ slot: 1, nutrient: 6, producer: 2 }]);
const { state } = nextState(s, A("A", "ccw"));
expect(chamber(state, 0).producer).toBeGreaterThan(0);
expect(chamber(state, 1).producer).toBe(0);
});
it("cw: slot 6 payload lands at slot 7 (loop B)", () => {
const s = makeState([{ slot: 6, nutrient: 6, producer: 2 }]);
const { state } = nextState(s, A("B", "cw"));
expect(chamber(state, 7).producer).toBeGreaterThan(0);
expect(chamber(state, 6).producer).toBe(0);
});
it("cw: slot 10 payload lands at slot 11 (loop C)", () => {
const s = makeState([{ slot: 10, nutrient: 6, producer: 2 }]);
const { state } = nextState(s, A("C", "cw"));
expect(chamber(state, 11).producer).toBeGreaterThan(0);
expect(chamber(state, 10).producer).toBe(0);
});
it("rotating loop C reclaims shared slot 5 and changes its outlet", () => {
const s = makeState();
expect(outletOf(5, s)).toBe(0); // owned by A (cw): outlet = slot 0
const { state } = nextState(s, A("C", "cw"));
expect(state.sharedOwner["5"]).toBe("C");
expect(outletOf(5, state)).toBe(10); // owned by C (cw): outlet = slot 10
});
});
describe("channel flow + caps", () => {
it("flows nutrient one step downstream (literal)", () => {
const s = makeState([{ slot: 0, nutrient: 3 }]);
const { state } = nextState(s, A("A", "cw"));
// rotation slot0→slot1 (nutrient3); flow sends 1 from slot1→slot2
expect(chamber(state, 1).nutrient).toBe(2);
expect(chamber(state, 2).nutrient).toBe(1);
});
it("contention + overflow at a shared receiver (slot 2 fed by slots 1 and 9)", () => {
const s = makeState([{ slot: 0, nutrient: 3 }, { slot: 1, nutrient: 6 }, { slot: 9, nutrient: 3 }]);
const { state, events } = nextState(s, A("A", "cw"));
expect(chamber(state, 2).nutrient).toBe(6); // 6 -1 sent +2 recv = 7 → capped 6
expect(events.some((e) => e.kind === "nutrient-overflow" && (e.amount ?? 0) >= 1)).toBe(true);
expect(chamber(state, 3).nutrient).toBe(1); // slot2 forwarded its send to slot3
});
it("keeps every capped field within 0..6 after a turn", () => {
const s = makeState([
{ slot: 0, nutrient: 6, producer: 6, heat: 6, toxin: 6, grazer: 6, phage: 6 },
{ slot: 1, nutrient: 6, producer: 6 },
{ slot: 9, nutrient: 6 },
]);
const { state } = nextState(s, A("A", "cw"));
for (const c of state.chambers) {
for (const v of [c.nutrient, c.heat, c.toxin, c.producer, c.grazer, c.phage]) {
expect(v).toBeGreaterThanOrEqual(0);
expect(v).toBeLessThanOrEqual(6);
}
}
});
});
describe("producer growth + interactions", () => {
it("growth: a fed producer grows (more producers than a starved one)", () => {
const fed = makeState([{ slot: 0, nutrient: 6, producer: 2 }]);
const starved = makeState([{ slot: 0, nutrient: 0, producer: 2 }]);
const f = nextState(fed, A("A", "cw")).state;
const u = nextState(starved, A("A", "cw")).state;
expect(chamber(f, 1).producer).toBeGreaterThan(chamber(u, 1).producer);
});
it("grazer feeding reduces producers vs a no-grazer control", () => {
const withGrazer = makeState([{ slot: 0, nutrient: 6, producer: 3, grazer: 1 }]);
const noGrazer = makeState([{ slot: 0, nutrient: 6, producer: 3 }]);
const g = nextState(withGrazer, A("A", "cw")).state;
const n = nextState(noGrazer, A("A", "cw")).state;
expect(chamber(g, 1).producer).toBeLessThan(chamber(n, 1).producer);
expect(chamber(g, 1).grazer).toBeGreaterThan(chamber(n, 1).grazer);
});
it("phage eats a grazer and gains; starves without one", () => {
const eat = makeState([{ slot: 0, grazer: 3, phage: 1, nutrient: 6, producer: 3 }]);
const starve = makeState([{ slot: 0, phage: 1, nutrient: 6, producer: 3 }]);
const e = nextState(eat, A("A", "cw")).state;
const t = nextState(starve, A("A", "cw")).state;
expect(chamber(e, 1).phage).toBeGreaterThan(chamber(t, 1).phage);
});
});
describe("migration", () => {
it("moves a population unit toward strictly richer food", () => {
// slot10 producer (food=nutrient0); slot11 has nutrient (dest food). outlet10→11 (C cw).
const s = makeState([{ slot: 10, producer: 3, nutrient: 0 }, { slot: 11, nutrient: 3 }]);
const { state } = nextState(s, A("A", "cw")); // A rotation does not move 10/11
expect(chamber(state, 11).producer).toBeGreaterThan(0); // migrated in
expect(chamber(state, 10).producer).toBeLessThan(3); // sent one out
});
});
describe("heat adaptation — benefit + carrier cost (adapted vs unadapted)", () => {
// Carrier cost is isolated from heat-death by comparing two ADAPTED lineages
// that differ only in whether heat is locally present.
const adapted = (heat: number) =>
makeState([{ slot: 0, nutrient: 6, producer: 3, heat }], { unlocked: { producer: ["heat"] } });
it("carrier cost: an adapted producer in heat grows LESS than the same producer without heat", () => {
const hot = nextState(adapted(3), A("A", "cw")).state;
const cool = nextState(adapted(0), A("A", "cw")).state;
expect(chamber(hot, 1).producer).toBeLessThan(chamber(cool, 1).producer);
expect(chamber(hot, 1).producer).toBe(3); // no growth (blocked)
});
const heatState = (unlocked: boolean) =>
makeState([{ slot: 0, nutrient: 6, producer: 3, heat: 3 }], { unlocked: unlocked ? { producer: ["heat"] } : {} });
it("benefit: Heat Shield prevents heat population loss (adapted survives better)", () => {
const adapted = nextState(heatState(true), A("A", "cw")).state;
const plain = nextState(heatState(false), A("A", "cw")).state;
expect(chamber(adapted, 1).producer).toBeGreaterThan(chamber(plain, 1).producer);
});
it("heat-spillover: adapted lineages cool the chamber", () => {
const { state, events } = nextState(heatState(true), A("A", "cw"));
expect(events.some((e) => e.kind === "heat-spillover")).toBe(true);
expect(chamber(state, 1).heat).toBeLessThan(3);
});
});
describe("hunger adaptation — benefit + inedibility spillover", () => {
it("benefit: Dormancy prevents hunger loss (adapted producer survives starvation)", () => {
const adapted = makeState([{ slot: 0, producer: 3 }], { unlocked: { producer: ["hunger"] } });
const plain = makeState([{ slot: 0, producer: 3 }]);
const a = nextState(adapted, A("A", "cw")).state;
const p = nextState(plain, A("A", "cw")).state;
expect(chamber(a, 1).producer).toBeGreaterThan(chamber(p, 1).producer); // plain starves -1
expect(chamber(a, 1).producer).toBe(3);
});
it("spillover: dormant producer is inedible to grazers", () => {
const dormant = makeState([{ slot: 0, producer: 3, grazer: 1 }], { unlocked: { producer: ["hunger"] } });
const edible = makeState([{ slot: 0, producer: 3, grazer: 1 }]);
const d = nextState(dormant, A("A", "cw")).state;
const e = nextState(edible, A("A", "cw")).state;
expect(chamber(d, 1).producer).toBeGreaterThan(chamber(e, 1).producer); // dormant not eaten
expect(chamber(d, 1).grazer).toBeLessThanOrEqual(chamber(e, 1).grazer); // dormant grazer doesn't feed
});
});
describe("toxin adaptation — benefit + carrier cost + spillover", () => {
const toxinState = (unlocked: boolean) =>
makeState([{ slot: 0, nutrient: 6, producer: 3, toxin: 3 }], { unlocked: unlocked ? { producer: ["toxin"] } : {} });
it("Efflux prevents toxin loss (adapted survives better) and cleanses toxin (spillover)", () => {
const adapted = nextState(toxinState(true), A("A", "cw"));
const plain = nextState(toxinState(false), A("A", "cw"));
expect(chamber(adapted.state, 1).producer).toBeGreaterThan(chamber(plain.state, 1).producer);
expect(adapted.events.some((e) => e.kind === "toxin-spillover")).toBe(true);
expect(adapted.events.some((e) => e.kind === "toxin-cost")).toBe(true);
// adapted chamber toxin reduced by spillover relative to plain
expect(chamber(adapted.state, 1).toxin).toBeLessThanOrEqual(chamber(plain.state, 1).toxin);
});
});
describe("immunity adaptation — selection, cloak, retarget, carrier cost", () => {
it("immune node selects and damages the largest eligible lineage", () => {
const s = makeState([{ slot: 6, producer: 3, nutrient: 6 }], { scenarioId: "test-immune" });
const { state, events } = nextState(s, A("A", "cw")); // slot6 not in A, unmoved
expect(chamber(state, 6).producer).toBeLessThan(3); // took damage
expect(events.some((e) => e.kind === "immune-damage")).toBe(true);
});
it("Cloak excludes a lineage from immune selection (no immune damage)", () => {
const cloaked = makeState([{ slot: 6, producer: 3, nutrient: 6 }], { scenarioId: "test-immune", unlocked: { producer: ["immunity"] } });
const { state, events } = nextState(cloaked, A("A", "cw"));
expect(events.some((e) => e.kind === "immune-damage")).toBe(false);
expect(chamber(state, 6).producer).toBeGreaterThanOrEqual(3); // untouched by the node (may grow)
});
it("Cloak causes retarget spillover to the other lineage", () => {
// Cloaked grazer is the largest at the node; producer is selected instead.
const s = makeState([{ slot: 6, grazer: 4, producer: 2, nutrient: 6 }], { scenarioId: "test-immune", unlocked: { grazer: ["immunity"] } });
const { state, events } = nextState(s, A("A", "cw"));
expect(events.some((e) => e.kind === "immune-retarget")).toBe(true);
expect(chamber(state, 6).producer).toBeLessThanOrEqual(2); // producer (retarget target) takes damage
});
it("Cloak carrier cost: predator removes an extra prey at the immune node", () => {
const cloaked = makeState([{ slot: 6, grazer: 3, phage: 1, producer: 3, nutrient: 6 }], { scenarioId: "test-immune", unlocked: { grazer: ["immunity"] } });
const plain = makeState([{ slot: 6, grazer: 3, phage: 1, producer: 3, nutrient: 6 }], { scenarioId: "test-immune" });
const c = nextState(cloaked, A("A", "cw"));
const p = nextState(plain, A("A", "cw"));
expect(c.events.some((e) => e.kind === "cloak-cost")).toBe(true);
// cloaked grazer loses MORE to the phage than a non-cloaked one (2 vs 1 removed)
expect(chamber(c.state, 6).grazer).toBeLessThan(chamber(p.state, 6).grazer);
});
});
describe("immutability, determinism, terminal precedence", () => {
it("does not mutate its input", () => {
const s = makeState([{ slot: 0, nutrient: 3, producer: 2, grazer: 1 }]);
const before = canonicalStringify(s);
nextState(s, A("A", "cw"));
expect(canonicalStringify(s)).toBe(before);
});
it("produces identical canonical output for identical input + action", () => {
const s = makeState([{ slot: 0, nutrient: 3, producer: 2 }, { slot: 6, grazer: 2 }]);
const r1 = nextState(cloneState(s), A("A", "cw"));
const r2 = nextState(cloneState(s), A("A", "cw"));
expect(canonicalStringify(r1.state)).toBe(canonicalStringify(r2.state));
});
it("collapse takes precedence over a win condition", () => {
const s = makeState([{ slot: 0, toxin: 2 }], { scenarioId: "test-win" }); // no biomass; checkWin always true
const { state } = nextState(s, A("A", "cw"));
expect(state.outcome).toBe("collapsed");
});
it("win takes precedence over turn-limit expiration", () => {
const s = makeState([{ slot: 0, producer: 1, nutrient: 6 }], { scenarioId: "test-win", turn: 4 }); // survives; resolvedTurn 5 == limit
const { state } = nextState(s, A("A", "cw"));
expect(state.outcome).toBe("won");
});
it("expires at the turn limit when not won", () => {
const s = makeState([{ slot: 0, producer: 1, nutrient: 6 }], { turn: 98 }); // survives; resolvedTurn 99 == limit 99
const { state } = nextState(s, A("A", "cw"));
expect(state.outcome).toBe("expired");
});
it("rejects actions in a terminal state without changing domain state", () => {
const s = makeState([{ slot: 0, producer: 1 }], { outcome: "won" });
const { state, rejected } = nextState(s, A("A", "cw"));
expect(rejected).toBe(true);
expect(canonicalEqual(state, s)).toBe(true);
});
});
describe("preview == commit, and replay reproduction", () => {
it("preview equals commit for every action across all campaign baselines", () => {
for (const sc of SCENARIOS) {
const base = buildBaselineState(sc.id, "standalone");
for (const action of ALL_ACTIONS) {
const preview = nextState(cloneState(base), action).state;
const commit = nextState(cloneState(base), action).state;
expect(canonicalEqual(preview, commit)).toBe(true);
}
}
});
it("replaying an action log from baseline reproduces the final state", () => {
const base = buildBaselineState(SCENARIOS[0].id, "standalone");
let cur = cloneState(base);
const log: RotationAction[] = [];
const seq: RotationAction[] = [A("A", "cw"), A("B", "ccw"), A("C", "cw"), A("A", "ccw"), A("C", "ccw")];
for (const action of seq) {
if (cur.outcome !== "playing") break;
const { state } = nextState(cur, action);
cur = state;
log.push(action);
}
let replayed = cloneState(base);
for (const action of log) replayed = nextState(replayed, action).state;
expect(canonicalEqual(replayed, cur)).toBe(true);
});
});