Files
sudoku-tools/tests/state/playHistoryPersistence.test.ts
T

37 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
createGameplayHistory,
parseGameplayHistory,
serializeGameplayHistory,
} from "../../src/state/playHistory";
import { createSession } from "../../src/state/session";
describe("gameplay history persistence", () => {
it("round-trips independent bounded history", () => {
const history = createGameplayHistory(
createSession(Array<number>(16).fill(0)),
);
const encoded = serializeGameplayHistory(history, 4);
const decoded = parseGameplayHistory(encoded, 4);
expect(decoded).toEqual(history);
(decoded.moments[0]!.state.values as number[])[0] = 4;
expect(history.moments[0]!.state.values[0]).toBe(0);
});
it("rejects dangling and oversized persisted state", () => {
const history = createGameplayHistory(
createSession(Array<number>(16).fill(0)),
);
const raw = JSON.parse(serializeGameplayHistory(history, 4)) as {
history: { currentMomentId: string };
};
raw.history.currentMomentId = "missing";
expect(() => parseGameplayHistory(JSON.stringify(raw), 4)).toThrow(
/missing active branch or moment/u,
);
expect(() => parseGameplayHistory("x".repeat(1_048_577), 4)).toThrow(
/too large/u,
);
});
});