Files
sudoku-tools/tests/storage/aidMemoireProgress.test.ts

73 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { SUDOKU_DOCUMENT_SCHEMA } from "../../src/formats";
import {
ProjectLibrary,
createProjectRecord,
normalizeProjectRecord,
} from "../../src/storage";
import {
aidMemoireToPortable,
createAidMemoire,
enterAidMemoireCell,
} from "../../src/state/aidMemoire";
const puzzle = {
schema: SUDOKU_DOCUMENT_SCHEMA,
version: 1 as const,
size: 9,
givens: Array<number>(81).fill(0),
constraints: [],
};
describe("aid-mémoire Library progress", () => {
it("persists bounded scratch state in an independent local clone", async () => {
let aidMemoire = createAidMemoire(9, {
enabled: true,
cellCount: 4,
columns: 2,
});
aidMemoire = enterAidMemoireCell(aidMemoire, 2, "value", 6, 9);
const record = createProjectRecord(puzzle, {
id: "aid-progress",
now: 10,
progress: {
version: 1,
values: Array<number>(81).fill(0),
aidMemoire: aidMemoireToPortable(aidMemoire, 9),
},
});
const library = new ProjectLibrary({ indexedDB: null });
await library.put(record);
const loaded = await library.get("aid-progress");
expect(loaded?.progress?.aidMemoire).toEqual(record.progress?.aidMemoire);
(loaded!.progress!.aidMemoire!.cells[2]!.centerMarks as number[]).push(4);
expect(
(await library.get("aid-progress"))?.progress?.aidMemoire?.cells[2]
?.centerMarks,
).toEqual([]);
});
it("rejects malformed scratch progress at the storage boundary", () => {
const record = createProjectRecord(puzzle, {
id: "bad-aid-progress",
now: 1,
});
expect(() =>
normalizeProjectRecord({
...record,
progress: {
version: 1,
values: Array<number>(81).fill(0),
aidMemoire: {
version: 1,
enabled: true,
columns: 2,
cells: [{}],
},
},
}),
).toThrow(/columns must be an integer from 1 to 1/u);
});
});