import { describe, it, expect } from "vitest"; import { buildTestState } from "../src/domain/scenarios.ts"; import { nextState, resolve } from "../src/domain/transition.ts"; import { canonical, canonicalEqual, cloneState } from "../src/domain/core.ts"; // Helper: trigger a tick that does NOT rotate the inner/middle/outer ring under test // by rotating a different ring. Direction of the rotated ring is set; others keep initial cw. const tickOuter = (s: Parameters[0]) => nextState(s, { ring: "outer", dir: "cw" }); const tickMiddle = (s: Parameters[0]) => nextState(s, { ring: "middle", dir: "cw" }); describe("rotation", () => { it("cw moves payload i -> i+1 (heat marker; global cooldown -1)", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { heat: 5 } }] }); const n = nextState(s, { ring: "inner", dir: "cw" }); expect(n.rings.inner.direction).toBe("cw"); expect(n.rings.inner.tiles[1].heat).toBe(4); // 5 moved 0->1, then cooldown -1 expect(n.rings.inner.tiles[0].heat).toBe(0); }); it("ccw moves payload i -> i-1", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { heat: 5 } }] }); const n = nextState(s, { ring: "inner", dir: "ccw" }); expect(n.rings.inner.direction).toBe("ccw"); expect(n.rings.inner.tiles[5].heat).toBe(4); // 0 -> 5 (ccw), cooldown -1 }); it("middle and outer rotate and update direction", () => { for (const ring of ["middle", "outer"] as const) { const s = buildTestState({ tiles: [{ ring, index: 0, chamber: { heat: 5 } }] }); const n = nextState(s, { ring, dir: "ccw" }); expect(n.rings[ring].direction).toBe("ccw"); const last = ring === "middle" ? 11 : 17; expect(n.rings[ring].tiles[last].heat).toBe(4); } }); }); describe("gate alignment after rotation", () => { it("rotating outer moves an on-gate payload off the gate", () => { // O3 is on gate 1 (3*1). Put a marker; rotate outer cw -> payload moves to O4 (off gate). const s = buildTestState({ tiles: [{ ring: "outer", index: 3, chamber: { heat: 5 } }] }); const n = nextState(s, { ring: "outer", dir: "cw" }); expect(n.rings.outer.tiles[4].heat).toBe(4); // marker now at O4 (off-gate, 4%3=1!=0) expect(n.rings.outer.tiles[3].heat).toBe(0); }); }); describe("channel flow", () => { it("tangential: nutrient flows cw to next tile", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { nutrient: 6 } }] }); const n = tickOuter(s); // inner doesn't rotate; inner dir cw expect(n.rings.inner.tiles[0].nutrient).toBe(5); // sent 1 tangential expect(n.rings.inner.tiles[1].nutrient).toBe(1); // received }); it("radial inward: outer on-gate sends nutrient to middle on-gate", () => { const s = buildTestState({ tiles: [{ ring: "outer", index: 0, chamber: { nutrient: 6 } }] }); const n = tickMiddle(s); // middle rotates (empty); outer gate0 has nutrient // outer0 sent 1 tangential (->O1) + 1 radial (->M0): 6-2=4 expect(n.rings.outer.tiles[0].nutrient).toBe(4); expect(n.rings.middle.tiles[0].nutrient).toBe(1); // received radial at M0 (gate0) }); it("caps at 6 (overflow discarded)", () => { // producer at cap with high nutrient consumes nutrient, gains 0 (clamped) const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 6, nutrient: 6 } }] }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(6); expect(n.rings.inner.tiles[0].nutrient).toBe(4); // 6 -1 flow -1 growth = 4 }); }); describe("growth and biological interactions", () => { it("producer grows consuming nutrient (high nutrient isolates from migration)", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 6 } }] }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(2); // 1 -> 2 expect(n.rings.inner.tiles[0].nutrient).toBe(4); // 6 -1 flow -1 growth }); it("grazer consumes one producer and gains one grazer", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, grazer: 1, nutrient: 6 } }] }); const n = tickOuter(s); // growth: producer 2->3 (nutrient 6->4 after flow); grazer eats 1 -> producer 2, grazer 2 expect(n.rings.inner.tiles[0].producer).toBe(2); expect(n.rings.inner.tiles[0].grazer).toBe(2); }); it("phage consumes a grazer and gains; otherwise starves", () => { const fed = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, grazer: 2, phage: 1, nutrient: 6 } }] }); const nf = tickOuter(fed); // growth prod2->3; grazer eats prod 3->2 grazer2->3; phage eats grazer 3->2 phage1->2 expect(nf.rings.inner.tiles[0].phage).toBe(2); const starve = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { phage: 2 } }] }); const ns = tickOuter(starve); expect(ns.rings.inner.tiles[0].phage).toBe(1); // starved -1 }); }); describe("migration", () => { it("producer migrates one step toward strictly-greater food (nutrient)", () => { // I0 producer3 + nutrient6 (survives, stays put mostly); I1 nutrient6 attracts migration const s = buildTestState({ tiles: [ { ring: "inner", index: 0, chamber: { producer: 3, nutrient: 6 } }, { ring: "inner", index: 1, chamber: { nutrient: 6 } }, ], }); const n = tickOuter(s); // I0 grows 3->4 (nutrient 6->4 after flow); food at I0=4, I1 nutrient=6 >4 -> migrate 1 to I1 expect(n.rings.inner.tiles[0].producer).toBe(3); // 4 -1 migrated expect(n.rings.inner.tiles[1].producer).toBe(1); // migrated in }); it("radial migration through a gate toward greater food", () => { // I0 producer2 + low nutrient; M0 (gate0) high nutrient attracts radial migration outward const s = buildTestState({ tiles: [ { ring: "inner", index: 0, chamber: { producer: 2, nutrient: 2 } }, { ring: "middle", index: 0, chamber: { nutrient: 6 } }, ], }); const n = tickOuter(s); // I0 nutrient stays low; M0 nutrient > I0 -> producer migrates radially to M0 expect(n.rings.middle.tiles[0].producer).toBe(1); // producer reached M0 via gate expect(n.rings.inner.tiles[0].producer).toBe(2); // grew 2->3 then -1 migrated }); }); describe("adaptations: benefit, cost, spillover", () => { it("heat shield: prevents loss (benefit), suppresses growth (cost), cools tile (spillover)", () => { // nutrient present (no hunger); heat-adapted suppresses growth (cost) and prevents heat loss (benefit) const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, heat: 3 } }], unlocked: { producer: ["heat"], grazer: [] }, }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(2); // no growth (cost), no loss (benefit) expect(n.rings.inner.tiles[0].nutrient).toBe(4); // flow -1; producer still consumes 1, only gain is suppressed }); it("heat-adapted grazer consumes prey but gains no population", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 3, grazer: 1, nutrient: 6, heat: 3 } }], unlocked: { producer: [], grazer: ["heat"] }, }); const result = resolve(s, { ring: "outer", dir: "cw" }); expect(result.state.rings.inner.tiles[0].producer).toBe(1); // grew, was eaten, then lost 2 to heat expect(result.state.rings.inner.tiles[0].grazer).toBe(1); expect(result.events.some((event) => event.phase === "feed" && event.suppressed)).toBe(true); }); it("unadapted heat loses 2 population", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, heat: 3 } }], }); const n = tickOuter(s); // growth 2->3 (nutrient 6->4); heat loss -2 -> producer 1 expect(n.rings.inner.tiles[0].producer).toBe(1); }); it("dormancy: prevents hunger loss (benefit)", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 0 } }], unlocked: { producer: ["hunger"], grazer: [] }, }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(2); // no loss }); it("unadapted hunger loses 1 population", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 0 } }] }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(1); }); it("efflux: prevents toxin pop loss, pays nutrient (cost), reduces toxin (spillover)", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, toxin: 4 } }], unlocked: { producer: ["toxin"], grazer: [] }, }); const n = tickOuter(s); // growth 2->3 (nutrient 6->4 after flow); toxin 4->3 after flow; efflux pays 1 nutrient; spillover -1 toxin expect(n.rings.inner.tiles[0].producer).toBe(3); // no toxin pop loss (benefit) expect(n.rings.inner.tiles[0].nutrient).toBe(3); // 4 -1 efflux pay expect(n.rings.inner.tiles[0].toxin).toBe(2); // 3 -1 spillover }); it("unadapted toxin loses 1 population", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6, toxin: 4 } }] }); const n = tickOuter(s); expect(n.rings.inner.tiles[0].producer).toBe(2); // grew 2->3 then toxin loss -1 -> 2 }); it("immune node damages the selected lineage by 2", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 1, chamber: { producer: 3, nutrient: 6 } }], fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }], }); const n = tickOuter(s); // growth 3->4; immune damage -2 -> producer 2 expect(n.rings.inner.tiles[1].producer).toBe(2); }); it("cloak: cloaked lineage is protected from immune selection (spillover retarget)", () => { const cloaked = buildTestState({ tiles: [{ ring: "inner", index: 1, chamber: { producer: 2, grazer: 3 } }], unlocked: { producer: [], grazer: ["immunity"] }, fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }], }); const nc = tickOuter(cloaked); // grazer feeds (producer 2->1, grazer 3->4); immune selects producer (grazer cloaked) -> producer 1->0 expect(nc.rings.inner.tiles[1].grazer).toBe(4); // protected by cloak, survived + fed const exposed = buildTestState({ tiles: [{ ring: "inner", index: 1, chamber: { producer: 2, grazer: 3 } }], fixtures: [{ kind: "immuneNode", ring: "inner", index: 1 }], }); const ne = tickOuter(exposed); // grazer feeds (3->4); immune selects largest (grazer 4) -> grazer 4-2=2 expect(ne.rings.inner.tiles[1].grazer).toBe(2); // damaged without cloak }); }); describe("environmental response", () => { it("heat response active: heat source adds 3 (vs 2 when inactive)", () => { const active = buildTestState({ tiles: [{ ring: "outer", index: 3, chamber: {} }], fixtures: [{ kind: "heatSource", ring: "outer", index: 3 }], environmentActive: ["heat"], }); const na = tickOuter(active); expect(na.rings.outer.tiles[3].heat).toBe(2); // +3 then -1 cooldown const inactive = buildTestState({ tiles: [{ ring: "outer", index: 3, chamber: {} }], fixtures: [{ kind: "heatSource", ring: "outer", index: 3 }], }); const ni = tickOuter(inactive); expect(ni.rings.outer.tiles[3].heat).toBe(1); // +2 then -1 cooldown }); }); describe("immutability, determinism, terminal", () => { it("input is not mutated", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 1 } }] }); const before = cloneState(s); nextState(s, { ring: "inner", dir: "cw" }); expect(canonicalEqual(before, s)).toBe(true); }); it("canonical determinism: same input+action -> same canonical", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, nutrient: 3 } }] }); const a = nextState(s, { ring: "inner", dir: "cw" }); const b = nextState(s, { ring: "inner", dir: "cw" }); expect(canonical(a)).toBe(canonical(b)); }); it("collapse when total producer+grazer = 0", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, heat: 6 } }] }); const n = tickOuter(s); expect(n.outcome).toBe("collapsed"); }); it("expiry when turn limit reached without win/collapse", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 6 } }], scenarioId: "expire1", turnLimit: 1, }); const n = tickOuter(s); expect(n.outcome).toBe("expired"); }); it("terminal precedence: collapse beats win", () => { // win would be true, but population hits 0 -> collapsed const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, heat: 6 } }], scenarioId: "winbutdie", turnLimit: 5, win: () => true, }); const n = tickOuter(s); expect(n.outcome).toBe("collapsed"); }); it("rejects actions in terminal states without changing canonical state", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 1, heat: 6 } }] }); const terminal = tickOuter(s); const after = nextState(terminal, { ring: "inner", dir: "cw" }); expect(canonical(after)).toBe(canonical(terminal)); }); }); describe("preview/commit equality + replay", () => { it("resolve preview state equals committed nextState", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 4, grazer: 1 } }] }); const { state: preview } = resolve(s, { ring: "inner", dir: "cw" }); const committed = nextState(s, { ring: "inner", dir: "cw" }); expect(canonicalEqual(preview, committed)).toBe(true); }); it("event list is deterministic from prior state + action", () => { const s = buildTestState({ tiles: [{ ring: "inner", index: 0, chamber: { producer: 2, nutrient: 4 } }] }); const e1 = resolve(s, { ring: "inner", dir: "cw" }).events; const e2 = resolve(s, { ring: "inner", dir: "cw" }).events; expect(JSON.stringify(e1)).toBe(JSON.stringify(e2)); }); });