(Adaptive Loop) Prepare for Pass 2
This commit is contained in:
1 parent
bd7203fc9e
commit
f262da55e5
32 files changed
+93
-7113
No files matched your search
@@ -1,134 +0,0 @@
|
||||
// Adaptive Loop — persistence & campaign flow tests (SPEC §11, §15 Gate 5).
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
type SaveEnvelope,
|
||||
type StorageAdapter,
|
||||
validateEnvelope,
|
||||
saveEnvelope,
|
||||
loadEnvelope,
|
||||
replayFromBaseline,
|
||||
emptyCampaign,
|
||||
SCHEMA_VERSION,
|
||||
} from "../src/persistence/save";
|
||||
import { startCampaign, commit, retry, enterScenario, preserveTrait, unlockNext, eligibleTraits, isCampaignVictory, nextScenarioId } from "../src/session";
|
||||
import { nextState } from "../src/domain/pipeline";
|
||||
import { canonicalEqual, cloneState } from "../src/domain/serialize";
|
||||
import type { RotationAction } from "../src/domain/types";
|
||||
import { SCENARIOS } from "../src/content/scenarios";
|
||||
|
||||
function memStore(): StorageAdapter {
|
||||
let v: string | null = null;
|
||||
return {
|
||||
loadRaw: () => v,
|
||||
saveRaw: (s) => {
|
||||
v = s;
|
||||
},
|
||||
removeRaw: () => {
|
||||
v = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("persistence", () => {
|
||||
it("round-trips a valid envelope and restores exact canonical state", () => {
|
||||
const store = memStore();
|
||||
const session = startCampaign();
|
||||
const env: SaveEnvelope = { schemaVersion: SCHEMA_VERSION, state: session.state, campaign: session.campaign, savedAt: 1 };
|
||||
saveEnvelope(env, store);
|
||||
const loaded = loadEnvelope(store);
|
||||
expect(loaded.env).not.toBeNull();
|
||||
expect(canonicalEqual(loaded.env!.state, session.state)).toBe(true);
|
||||
});
|
||||
|
||||
it("malformed save fails safely to null (no crash)", () => {
|
||||
const store = memStore();
|
||||
store.saveRaw("{not valid json");
|
||||
expect(loadEnvelope(store).env).toBeNull();
|
||||
store.saveRaw(JSON.stringify({ schemaVersion: 999, state: {} }));
|
||||
expect(loadEnvelope(store).env).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects out-of-range chamber values", () => {
|
||||
const s = startCampaign().state;
|
||||
s.chambers[0]!.producer = 99;
|
||||
const env: SaveEnvelope = { schemaVersion: SCHEMA_VERSION, state: s, campaign: emptyCampaign(), savedAt: 1 };
|
||||
expect(validateEnvelope(env)).toBeNull();
|
||||
});
|
||||
|
||||
it("replay-from-baseline reproduces canonical current state after a sequence", () => {
|
||||
let s = startCampaign().state;
|
||||
const seq: RotationAction[] = [
|
||||
{ loop: "A", direction: "cw" },
|
||||
{ loop: "B", direction: "ccw" },
|
||||
{ loop: "C", direction: "cw" },
|
||||
];
|
||||
for (const a of seq) s = nextState(s, a).state;
|
||||
const replayed = replayFromBaseline(s);
|
||||
expect(canonicalEqual(replayed, s)).toBe(true);
|
||||
});
|
||||
|
||||
it("save preserves the exact next transition (continue restores mid-game)", () => {
|
||||
const store = memStore();
|
||||
let session = startCampaign();
|
||||
session = commit(session, { loop: "A", direction: "cw" }).session;
|
||||
saveEnvelope({ schemaVersion: SCHEMA_VERSION, state: session.state, campaign: session.campaign, savedAt: 2 }, store);
|
||||
const restored = loadEnvelope(store).env!;
|
||||
// continuing: applying the same next action yields identical results
|
||||
const a: RotationAction = { loop: "B", direction: "cw" };
|
||||
const fromLive = nextState(session.state, a).state;
|
||||
const fromRestored = nextState(restored.state, a).state;
|
||||
expect(canonicalEqual(fromLive, fromRestored)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("campaign flow", () => {
|
||||
it("retry restores the scenario-entry inheritance snapshot", () => {
|
||||
let session = startCampaign();
|
||||
// play a couple turns
|
||||
session = commit(session, { loop: "A", direction: "cw" }).session;
|
||||
session = commit(session, { loop: "B", direction: "cw" }).session;
|
||||
const before = cloneState(session.state);
|
||||
const retried = retry(session);
|
||||
// retried is a fresh baseline (turn 0) of the same scenario
|
||||
expect(retried.state.turn).toBe(0);
|
||||
expect(retried.state.scenarioId).toBe(before.scenarioId);
|
||||
});
|
||||
|
||||
it("preserveTrait carries a gift into the cumulative inheritance", () => {
|
||||
let campaign = emptyCampaign();
|
||||
campaign = preserveTrait(campaign, { lineage: "producer", pressure: "heat" });
|
||||
expect(campaign.inherited.producer).toContain("heat");
|
||||
const s = enterScenario(campaign, SCENARIOS[1]!.id, "campaign");
|
||||
expect(s.lineage.producer.unlocked.heat).toBe(true); // gift carried
|
||||
expect(s.lineage.producer.unlocked.toxin).toBe(false); // others not
|
||||
});
|
||||
|
||||
it("unlockNext advances the unlocked set by one scenario", () => {
|
||||
let c = emptyCampaign();
|
||||
c = unlockNext(c, SCENARIOS[0]!.id);
|
||||
expect(c.unlocked).toContain(SCENARIOS[1]!.id);
|
||||
expect(c.unlocked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("eligibleTraits lists traits unlocked this run, excluding already-inherited", () => {
|
||||
const session = startCampaign();
|
||||
session.state.lineage.producer.unlocked.heat = true; // simulate an unlock during play
|
||||
const elig = eligibleTraits(session);
|
||||
expect(elig.some((t) => t.lineage === "producer" && t.pressure === "heat")).toBe(true);
|
||||
});
|
||||
|
||||
it("campaign victory only after winning the last scenario", () => {
|
||||
const session = startCampaign();
|
||||
expect(isCampaignVictory(session)).toBe(false);
|
||||
session.state.scenarioId = SCENARIOS[4]!.id;
|
||||
session.state.outcome = "won";
|
||||
expect(isCampaignVictory(session)).toBe(true);
|
||||
});
|
||||
|
||||
it("nextScenarioId returns the most recently unlocked scenario", () => {
|
||||
const c = emptyCampaign();
|
||||
expect(nextScenarioId(c)).toBe(SCENARIOS[0]!.id);
|
||||
const c2 = unlockNext(c, SCENARIOS[0]!.id);
|
||||
expect(nextScenarioId(c2)).toBe(SCENARIOS[1]!.id);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user