{"draft":null,"from":2,"to":318,"files":[{"path":"src/engine/test/projection-root.test.ts","added":74,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-root.test.ts\n@@ -0,0 +1,74 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import {\n+\tcreateProjectionRoot,\n+\ttype ProjectionIdentity,\n+\ttype ProjectionRoots,\n+\treadProjectionRoot,\n+} from \"../experimental/projection-root.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"experimental roots bind identity before storage access and remain immutable\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst expected: ProjectionIdentity = {\n+\t\t\tdatabase: \"db\",\n+\t\t\tnamespace: \"index\",\n+\t\t\tsourceGeneration: 1,\n+\t\t\tsourceLength: 2,\n+\t\t\tsourceDigest: \"a\".repeat(64),\n+\t\t\tmembership: \"closed:2\",\n+\t\t\tregistry: \"files:1\",\n+\t\t\tdefaultFile: \"f.txt\",\n+\t\t};\n+\t\tconst pages: ProjectionRoots = {\n+\t\t\tsequence: 3,\n+\t\t\tsequenceRoot: 0,\n+\t\t\tsequenceNext: 1,\n+\t\t\tlocator: 4,\n+\t\t\tchildren: 5,\n+\t\t\tmembership: 6,\n+\t\t\tattributes: null,\n+\t\t};\n+\t\tconst handle = createProjectionRoot(storage, expected, 0, pages);\n+\t\tassert.deepEqual(readProjectionRoot(storage, handle, expected), pages);\n+\t\tfor (const field of Object.keys(expected) as (keyof ProjectionIdentity)[]) {\n+\t\t\tconst changed = {\n+\t\t\t\t...expected,\n+\t\t\t\t[field]:\n+\t\t\t\t\ttypeof expected[field] === \"number\"\n+\t\t\t\t\t\t? Number(expected[field]) + 1\n+\t\t\t\t\t\t: field === \"sourceDigest\"\n+\t\t\t\t\t\t\t? \"b\".repeat(64)\n+\t\t\t\t\t\t\t: `${expected[field]}:other`,\n+\t\t\t};\n+\t\t\tconst reads = storage.reads,\n+\t\t\t\twrites = storage.writes;\n+\t\t\tassert.throws(() => readProjectionRoot(storage, handle, changed), /identity mismatch/);\n+\t\t\tassert.equal(storage.reads, reads);\n+\t\t\tassert.equal(storage.writes, writes);\n+\t\t}\n+\t\tassert.throws(() => createProjectionRoot(storage, expected, 0, pages), /already exists/);\n+\t\tconst copy = readProjectionRoot(storage, handle, expected) as { sequence: number | null };\n+\t\tcopy.sequence = 999;\n+\t\tassert.deepEqual(readProjectionRoot(storage, handle, expected), pages);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tstorage.transaction(() => {\n+\t\t\t\t\tcreateProjectionRoot(storage, expected, 1, pages);\n+\t\t\t\t\tthrow Error(\"fail after root\");\n+\t\t\t\t}),\n+\t\t\t/fail after root/,\n+\t\t);\n+\t\tassert.throws(() => readProjectionRoot(storage, { ...handle, id: 1 }, expected), /invalid projection root record/);\n+\t\tstorage.write('experimental-projection:[\"db\",\"index\"]:roots', \"0\", {\n+\t\t\tformat: 1,\n+\t\t\tid: 0,\n+\t\t\tidentity: expected,\n+\t\t\troots: { ...pages, locator: -1 },\n+\t\t});\n+\t\tassert.throws(() => readProjectionRoot(storage, handle, expected), /invalid projection page reference/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"unmatched":[]}