{"draft":null,"from":0,"to":318,"files":[{"path":"src/engine/test/projection-scratch.test.ts","added":101,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-scratch.test.ts\n@@ -0,0 +1,101 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { withProjectionScratch } from \"../projection-scratch.ts\";\n+import { CachedStateStorage, type GraphStateStorage } from \"../state-source.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"projection scratch is isolated, detached and rolled back inside canonical transactions\", () => {\n+\tconst source = new SqlState();\n+\tconst storage = new CachedStateStorage(source);\n+\ttry {\n+\t\tstorage.transaction(() => {\n+\t\t\tstorage.write(\"canonical\", \"before\", 1);\n+\t\t\tlet escaped: GraphStateStorage | undefined;\n+\t\t\tconst result = withProjectionScratch(\n+\t\t\t\tstorage,\n+\t\t\t\t() => {},\n+\t\t\t\t(scratch) => {\n+\t\t\t\t\tescaped = scratch;\n+\t\t\t\t\tscratch.write(\"canonical\", \"before\", 99);\n+\t\t\t\t\tconst inner = withProjectionScratch(\n+\t\t\t\t\t\tstorage,\n+\t\t\t\t\t\t() => {},\n+\t\t\t\t\t\t(nested) => {\n+\t\t\t\t\t\t\tassert.equal(nested.read(\"canonical\", \"before\"), undefined);\n+\t\t\t\t\t\t\tnested.write(\"canonical\", \"before\", 101);\n+\t\t\t\t\t\t\treturn nested.read(\"canonical\", \"before\");\n+\t\t\t\t\t\t},\n+\t\t\t\t\t);\n+\t\t\t\t\tassert.equal(inner, 101);\n+\t\t\t\t\treturn { value: scratch.read(\"canonical\", \"before\"), data: new Int32Array([1, 2]) };\n+\t\t\t\t},\n+\t\t\t);\n+\t\t\tassert.deepEqual(result, { value: 99, data: new Int32Array([1, 2]) });\n+\t\t\tassert.throws(() => escaped!.write(\"x\", \"y\", true), /ended/);\n+\t\t\t// A scratch rollback drops only what the scratch wrote (abso Capacity): the canonical value written before it, in\n+\t\t\t// the outer transaction, stays cached and is still right.\n+\t\t\tassert.equal(storage.retainedEntries, 1, \"only the canonical value written before the scratch remains\");\n+\t\t\tassert.equal(storage.read(\"canonical\", \"before\"), 1);\n+\t\t\tstorage.write(\"canonical\", \"after\", 2);\n+\t\t});\n+\t\tassert.equal(source.db.prepare(\"SELECT count(*) AS n FROM state\").get()!.n, 2);\n+\t\tassert.equal(storage.retainedEntries, 2, \"only the canonical read and successful counter write remain\");\n+\t\tassert.equal(storage.read(\"canonical\", \"after\"), 2);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"projection errors, changed identities and escaping capabilities cannot commit scratch\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tlet checks = 0;\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\twithProjectionScratch(\n+\t\t\t\t\tsource,\n+\t\t\t\t\t() => {\n+\t\t\t\t\t\tif (++checks === 2) throw new Error(\"identity changed\");\n+\t\t\t\t\t},\n+\t\t\t\t\t(scratch) => {\n+\t\t\t\t\t\tscratch.write(\"x\", \"y\", 1);\n+\t\t\t\t\t\treturn 2;\n+\t\t\t\t\t},\n+\t\t\t\t),\n+\t\t\t/identity changed/,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\twithProjectionScratch(\n+\t\t\t\t\tsource,\n+\t\t\t\t\t() => {},\n+\t\t\t\t\t(scratch) => {\n+\t\t\t\t\t\tscratch.write(\"x\", \"y\", 1);\n+\t\t\t\t\t\tthrow new Error(\"failed computation\");\n+\t\t\t\t\t},\n+\t\t\t\t),\n+\t\t\t/failed computation/,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\twithProjectionScratch(\n+\t\t\t\t\tsource,\n+\t\t\t\t\t() => {},\n+\t\t\t\t\t(scratch) => scratch,\n+\t\t\t\t),\n+\t\t\t/clone|cloned/i,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\twithProjectionScratch(\n+\t\t\t\t\tsource,\n+\t\t\t\t\t() => {},\n+\t\t\t\t\t() => Promise.resolve(1),\n+\t\t\t\t),\n+\t\t\t/synchronously/,\n+\t\t);\n+\t\tassert.equal(source.db.prepare(\"SELECT count(*) AS n FROM state\").get()!.n, 0);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"}],"unmatched":[]}