(Adaptive Loop) Pass 2
This commit is contained in:
1 parent
7fcbfcd476
commit
4c39fe67f1
34 files changed
+5990
No files matched your search
@@ -0,0 +1,114 @@
|
||||
// Gate 5 — campaign and persistence. Exact save/restore, deterministic replay,
|
||||
// malformed-save recovery, campaign progression with gift-and-burden inheritance,
|
||||
// and retry restoring the scenario-entry snapshot.
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { MemoryStorage, save, load, isValidEnvelope, replayFromBaseline, type SaveEnvelope } from "../src/persistence/save";
|
||||
import { startCampaign, startStandalone, commitAction, retry, advanceAfterWin, toEnvelope, fromEnvelope, eligiblePreservations } from "../src/session";
|
||||
import { nextState } from "../src/domain/pipeline";
|
||||
import { buildBaselineState } from "../src/domain/scenario";
|
||||
import { canonicalEqual } from "../src/domain/serialize";
|
||||
import { SCENARIOS } from "../src/content/scenarios";
|
||||
import fixture from "./fixtures/solver-results.json";
|
||||
import type { RotationAction } from "../src/domain/types";
|
||||
|
||||
const A = (loop: "A" | "B" | "C", direction: "cw" | "ccw"): RotationAction => ({ loop, direction });
|
||||
|
||||
describe("persistence — exact save/restore + replay", () => {
|
||||
it("save then load restores canonical state and the exact next transition", () => {
|
||||
const store = new MemoryStorage();
|
||||
let session = startCampaign();
|
||||
session = commitAction(session, A("A", "cw"));
|
||||
session = commitAction(session, A("B", "ccw"));
|
||||
save(store, toEnvelope(session, Date.now()));
|
||||
const restored = load(store);
|
||||
expect(restored).not.toBeNull();
|
||||
const again = fromEnvelope(restored!);
|
||||
expect(canonicalEqual(again.state, session.state)).toBe(true);
|
||||
// exact next transition matches
|
||||
const nextA = nextState(again.state, A("C", "cw")).state;
|
||||
const nextB = nextState(session.state, A("C", "cw")).state;
|
||||
expect(canonicalEqual(nextA, nextB)).toBe(true);
|
||||
});
|
||||
|
||||
it("replaying the action log from baseline + entry snapshot reproduces state", () => {
|
||||
let session = startCampaign();
|
||||
const log: RotationAction[] = [A("A", "cw"), A("C", "cw"), A("B", "ccw")];
|
||||
for (const a of log) session = commitAction(session, a);
|
||||
const replayed = replayFromBaseline(session.scenarioId, session.entrySnapshot, log);
|
||||
expect(canonicalEqual(replayed, session.state)).toBe(true);
|
||||
});
|
||||
|
||||
it("malformed or unsupported saves fail safely (load returns null, no crash)", () => {
|
||||
const store = new MemoryStorage();
|
||||
store.setItem("adaptive-loop.save.v1", "{not json");
|
||||
expect(load(store)).toBeNull();
|
||||
store.setItem("adaptive-loop.save.v1", JSON.stringify({ schemaVersion: 999 }));
|
||||
expect(load(store)).toBeNull();
|
||||
store.setItem("adaptive-loop.save.v1", JSON.stringify({ schemaVersion: 1, mode: "campaign", scenarioId: "nope", state: {}, unlockedScenarios: [], inheritedTraits: { producer: [], grazer: [] }, entrySnapshot: { producer: [], grazer: [] }, actionLog: [], outcome: "playing", savedAt: 1 }));
|
||||
expect(isValidEnvelope(JSON.parse(store.getItem("adaptive-loop.save.v1")!))).toBe(false);
|
||||
expect(load(store)).toBeNull();
|
||||
});
|
||||
|
||||
it("empty storage loads null", () => {
|
||||
expect(load(new MemoryStorage())).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("campaign — unlock, inheritance gift-and-burden, retry, standalone", () => {
|
||||
it("winning a scenario unlocks the next and carries a preserved trait", () => {
|
||||
// Drive s1 to a win using the solver fixture route.
|
||||
const s1win = (fixture.scenarios as any[]).find((s) => s.id === "s1-heat")!.win.actions as RotationAction[];
|
||||
let session = startCampaign();
|
||||
for (const a of s1win) session = commitAction(session, a);
|
||||
expect(session.state.outcome).toBe("won");
|
||||
const eligible = eligiblePreservations(session);
|
||||
expect(eligible.length).toBeGreaterThan(0);
|
||||
const choice = eligible[0]!;
|
||||
expect(eligible.length).toBeGreaterThan(0);
|
||||
const advanced = advanceAfterWin(session, choice);
|
||||
expect(advanced).not.toBeNull();
|
||||
expect(advanced!.scenarioId).toBe("s2-hunger");
|
||||
expect(advanced!.unlockedScenarios).toContain("s2-hunger");
|
||||
// preserved trait carried as a burden/gift into the next scenario
|
||||
expect(advanced!.inheritedTraits[eligible[0]!.lineage]).toContain(eligible[0]!.pressure);
|
||||
expect(advanced!.state.lineage[eligible[0]!.lineage].unlocked[eligible[0]!.pressure]).toBe(true);
|
||||
});
|
||||
|
||||
it("retry restores the scenario-entry inheritance snapshot", () => {
|
||||
let session = startCampaign();
|
||||
const before = buildBaselineState(session.scenarioId, "campaign", session.entrySnapshot);
|
||||
session = commitAction(session, A("A", "cw"));
|
||||
session = retry(session);
|
||||
expect(canonicalEqual(session.state, before)).toBe(true);
|
||||
});
|
||||
|
||||
it("standalone uses a clean baseline (no carried traits)", () => {
|
||||
const session = startStandalone("s3-toxin");
|
||||
expect(session.mode).toBe("standalone");
|
||||
expect(session.inheritedTraits.producer).toEqual([]);
|
||||
expect(session.inheritedTraits.grazer).toEqual([]);
|
||||
});
|
||||
|
||||
it("declining a trait still advances to the next scenario", () => {
|
||||
const s1win = (fixture.scenarios as any[]).find((s) => s.id === "s1-heat")!.win.actions as RotationAction[];
|
||||
let session = startCampaign();
|
||||
for (const a of s1win) session = commitAction(session, a);
|
||||
const advanced = advanceAfterWin(session, null);
|
||||
expect(advanced!.scenarioId).toBe("s2-hunger");
|
||||
expect(advanced!.inheritedTraits.producer).toEqual([]);
|
||||
});
|
||||
|
||||
it("winning the final scenario yields campaign victory (no further advance)", () => {
|
||||
const s5win = (fixture.scenarios as any[]).find((s) => s.id === "s5-convergence")!.win.actions as RotationAction[];
|
||||
// enter s5 directly with empty inheritance for the test
|
||||
let session = startStandalone("s5-convergence");
|
||||
session = { ...session, mode: "campaign" };
|
||||
for (const a of s5win) session = commitAction(session, a);
|
||||
expect(session.state.outcome).toBe("won");
|
||||
expect(advanceAfterWin({ ...session, scenarioId: SCENARIOS[SCENARIOS.length - 1].id }, null)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// Ensure the SaveEnvelope type is exercised (keeps the export meaningful to tsc).
|
||||
export type _EnvelopeRef = SaveEnvelope;
|
||||
Reference in new issue
Block a user