{"draft":null,"from":41,"to":318,"files":[{"path":"src/engine/test/state-cache.test.ts","added":165,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/state-cache.test.ts\n@@ -0,0 +1,165 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { CachedStateStorage } from \"../state-source.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"numeric cache pages are detached on first and repeated reads, and rollback invalidates them\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 4096, 4);\n+\t\tcache.write(\"numbers\", \"page\", [1, 2, 3]);\n+\t\t(cache.read(\"numbers\", \"page\") as number[])[0] = 99;\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\t(cache.read(\"numbers\", \"page\") as number[]).push(4);\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"numbers\", \"page\", [8]);\n+\t\t\t\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [8]);\n+\t\t\t\t\tthrow new Error(\"rollback numeric page\");\n+\t\t\t\t}),\n+\t\t\t/rollback numeric page/,\n+\t\t);\n+\t\tassert.equal(cache.retainedBytes, 0);\n+\t\tassert.deepEqual(cache.read(\"numbers\", \"page\"), [1, 2, 3]);\n+\t\tcache.remove(\"numbers\", \"page\");\n+\t\tassert.equal(cache.read(\"numbers\", \"page\"), undefined);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"cached scalar metadata and bounded numeric pages need no JSON decode on hits\", (t) => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 8);\n+\t\tconst values = [undefined, null, true, \"metadata\", 42, Array.from({ length: 256 }, (_, n) => n)];\n+\t\tfor (let i = 0; i < values.length; i++) {\n+\t\t\tcache.write(\"rows\", String(i), values[i]);\n+\t\t\tcache.read(\"rows\", String(i));\n+\t\t}\n+\t\tconst reads = source.reads;\n+\t\tt.mock.method(JSON, \"parse\", () => {\n+\t\t\tthrow new Error(\"cache hit unexpectedly decoded JSON\");\n+\t\t});\n+\t\tfor (let i = 0; i < values.length; i++) assert.deepEqual(cache.read(\"rows\", String(i)), values[i]);\n+\t\tassert.equal(source.reads, reads);\n+\t\tassert.ok(cache.retainedBytes <= 8192);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"bounded writer cache copies values, invalidates writes and clears after rollback\", () => {\n+\tconst source = new SqlState();\n+\tconst cache = new CachedStateStorage(source, 1024, 3);\n+\tsource.write(\"rows\", \"a\", { list: [1] });\n+\tconst first = cache.read(\"rows\", \"a\") as { list: number[] };\n+\tfirst.list.push(2);\n+\tconst reads = source.reads;\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [1] });\n+\tassert.equal(source.reads, reads);\n+\tcache.write(\"rows\", \"a\", { list: [3] });\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [3] });\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tcache.transaction(() => {\n+\t\t\t\tcache.write(\"rows\", \"a\", { list: [4] });\n+\t\t\t\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [4] });\n+\t\t\t\tthrow new Error(\"rollback\");\n+\t\t\t}),\n+\t\t/rollback/,\n+\t);\n+\tassert.equal(cache.retainedEntries, 0);\n+\tassert.deepEqual(cache.read(\"rows\", \"a\"), { list: [3] });\n+\tcache.remove(\"rows\", \"a\");\n+\tassert.equal(cache.read(\"rows\", \"a\"), undefined);\n+\tcache.write(\"rows\", \"a\", \"restored\");\n+\tassert.equal(cache.read(\"rows\", \"a\"), \"restored\");\n+\tsource.db.close();\n+});\n+\n+test(\"writer cache evicts by both payload and entry count and skips oversized values\", () => {\n+\tconst source = new SqlState();\n+\tconst cache = new CachedStateStorage(source, 1024, 3);\n+\tfor (let i = 0; i < 100; i++) {\n+\t\tcache.write(\"rows\", String(i), \"small\");\n+\t\tassert.equal(cache.read(\"rows\", String(i)), \"small\");\n+\t\tassert.ok(cache.retainedBytes <= 1024);\n+\t\tassert.ok(cache.retainedEntries <= 3);\n+\t}\n+\tconst reads = source.reads;\n+\tcache.read(\"rows\", \"99\");\n+\tassert.equal(source.reads, reads);\n+\tcache.read(\"rows\", \"0\");\n+\tassert.equal(source.reads, reads + 1);\n+\tcache.write(\"rows\", \"large\", \"x\".repeat(1024));\n+\tconst before = cache.retainedBytes;\n+\tcache.read(\"rows\", \"large\");\n+\tassert.equal(cache.retainedBytes, before);\n+\tconst afterLarge = source.reads;\n+\tcache.read(\"rows\", \"large\");\n+\tassert.equal(source.reads, afterLarge + 1);\n+\tcache.clear();\n+\tassert.equal(cache.retainedBytes, 0);\n+\tsource.db.close();\n+});\n+\n+test(\"a rollback drops only what its transaction wrote; a nested commit's writes go with an outer rollback (abso Capacity)\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 16);\n+\t\tsource.write(\"rows\", \"kept\", \"k\");\n+\t\tsource.write(\"rows\", \"inner\", \"i0\");\n+\t\tsource.write(\"rows\", \"outer\", \"o0\");\n+\t\tfor (const key of [\"kept\", \"inner\", \"outer\"]) cache.read(\"rows\", key);\n+\t\tconst reads = source.reads;\n+\t\t// A projection scratch scope: rolled back on purpose, and the rows read around it stay cached.\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"scratch\", \"x\", \"temporary\");\n+\t\t\t\t\tthrow new Error(\"scratch rollback\");\n+\t\t\t\t}),\n+\t\t\t/scratch rollback/,\n+\t\t);\n+\t\tassert.equal(cache.read(\"rows\", \"kept\"), \"k\");\n+\t\tassert.equal(source.reads, reads, \"the rows read before were not read again\");\n+\t\tassert.equal(cache.read(\"scratch\", \"x\"), undefined);\n+\t\t// A nested transaction commits, then the outer one rolls back: both writes are undone, and the cache forgets both.\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"rows\", \"outer\", \"o1\");\n+\t\t\t\t\tcache.transaction(() => cache.write(\"rows\", \"inner\", \"i1\"));\n+\t\t\t\t\tassert.equal(cache.read(\"rows\", \"inner\"), \"i1\");\n+\t\t\t\t\tthrow new Error(\"outer rollback\");\n+\t\t\t\t}),\n+\t\t\t/outer rollback/,\n+\t\t);\n+\t\tassert.equal(cache.read(\"rows\", \"inner\"), \"i0\");\n+\t\tassert.equal(cache.read(\"rows\", \"outer\"), \"o0\");\n+\t\tassert.equal(cache.read(\"rows\", \"kept\"), \"k\");\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"a row over the per-row limit is read but not kept, and pushes nothing out (abso Capacity)\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 8192, 64, 1024);\n+\t\tsource.write(\"rows\", \"small\", \"s\");\n+\t\tsource.write(\"rows\", \"big\", \"x\".repeat(2000));\n+\t\tcache.read(\"rows\", \"small\");\n+\t\tconst reads = source.reads;\n+\t\tassert.equal((cache.read(\"rows\", \"big\") as string).length, 2000);\n+\t\tassert.equal(cache.read(\"rows\", \"small\"), \"s\");\n+\t\tassert.equal(source.reads, reads + 1, \"the big row was read from storage once and the small one stayed cached\");\n+\t\tassert.equal((cache.read(\"rows\", \"big\") as string).length, 2000);\n+\t\tassert.equal(source.reads, reads + 2, \"the big row isn't kept\");\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"}],"unmatched":[]}