(Adaptive Loop) Pass 4
This commit is contained in:
1 parent
bd9d5e59d2
commit
9cdcaed1f1
36 files changed
+12943
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { canonical } from "../src/domain/core.ts";
|
||||
import { buildInitialState, getScenario } from "../src/domain/scenarios.ts";
|
||||
import { nextState } from "../src/domain/transition.ts";
|
||||
import { loadGame, saveGame } from "../src/persistence/save.ts";
|
||||
|
||||
const values = new Map<string, string>();
|
||||
Object.defineProperty(globalThis, "localStorage", {
|
||||
value: {
|
||||
getItem: (key: string) => values.get(key) ?? null,
|
||||
setItem: (key: string, value: string) => values.set(key, value),
|
||||
removeItem: (key: string) => values.delete(key),
|
||||
},
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
beforeEach(() => values.clear());
|
||||
|
||||
describe("exact persistence", () => {
|
||||
it("restores canonical state and its exact next transition", () => {
|
||||
const scenario = getScenario("s1-heat");
|
||||
const state = nextState(buildInitialState(scenario, "campaign"), { ring: "inner", dir: "cw" });
|
||||
const progress = { unlocked: [scenario.id], inherited: { producer: [], grazer: [] }, entrySnapshot: { producer: [], grazer: [] }, victory: false };
|
||||
saveGame({ schemaVersion: 1, mode: "campaign", scenarioId: scenario.id, state, progress, actionLog: state.actionLog, outcome: state.outcome });
|
||||
const loaded = loadGame();
|
||||
expect(loaded.ok).toBe(true);
|
||||
if (!loaded.ok) return;
|
||||
expect(canonical(loaded.env.state)).toBe(canonical(state));
|
||||
const action = { ring: "middle", dir: "ccw" } as const;
|
||||
expect(canonical(nextState(loaded.env.state, action))).toBe(canonical(nextState(state, action)));
|
||||
});
|
||||
|
||||
it("rejects a structurally valid save whose state cannot be reproduced from its action log", () => {
|
||||
const scenario = getScenario("s1-heat");
|
||||
const state = nextState(buildInitialState(scenario, "campaign"), { ring: "inner", dir: "cw" });
|
||||
const progress = { unlocked: [scenario.id], inherited: { producer: [], grazer: [] }, entrySnapshot: { producer: [], grazer: [] }, victory: false };
|
||||
saveGame({ schemaVersion: 1, mode: "campaign", scenarioId: scenario.id, state, progress, actionLog: state.actionLog, outcome: state.outcome });
|
||||
const tampered = JSON.parse(values.get("adaptive-loop-save-v1")!);
|
||||
tampered.state.rings.inner.tiles[0].nutrient++;
|
||||
values.set("adaptive-loop-save-v1", JSON.stringify(tampered));
|
||||
expect(loadGame().ok).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects impossible campaign unlock and victory progress", () => {
|
||||
const scenario = getScenario("s1-heat");
|
||||
const state = buildInitialState(scenario, "campaign");
|
||||
const progress = { unlocked: [scenario.id], inherited: { producer: [], grazer: [] }, entrySnapshot: { producer: [], grazer: [] }, victory: false };
|
||||
saveGame({ schemaVersion: 1, mode: "campaign", scenarioId: scenario.id, state, progress, actionLog: [], outcome: state.outcome });
|
||||
const tampered = JSON.parse(values.get("adaptive-loop-save-v1")!);
|
||||
tampered.progress.unlocked.push("s5-convergence");
|
||||
tampered.progress.victory = true;
|
||||
values.set("adaptive-loop-save-v1", JSON.stringify(tampered));
|
||||
expect(loadGame().ok).toBe(false);
|
||||
});
|
||||
|
||||
it("canonicalizes nested object keys recursively", () => {
|
||||
expect(canonical({ z: { b: 2, a: 1 }, a: [{ d: 4, c: 3 }] })).toBe(canonical({ a: [{ c: 3, d: 4 }], z: { a: 1, b: 2 } }));
|
||||
});
|
||||
|
||||
it("rejects malformed JSON safely", () => {
|
||||
values.set("adaptive-loop-save-v1", "{bad");
|
||||
expect(loadGame()).toEqual({ ok: false, reason: "Saved game is corrupt (invalid JSON)." });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user