{"draft":null,"from":85,"to":318,"files":[{"path":"src/engine/test/graph-insertion-storage.test.ts","added":52,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/graph-insertion-storage.test.ts\n@@ -0,0 +1,52 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase, Graph } from \"../index.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"persisted insertion lines stay out of atom metadata\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst text = Array.from({ length: 513 }, (_, i) => `line ${i}\\n`).join(\"\");\n+\tconst graph = new Graph({ storage });\n+\tconst atom = buildBase({ \"a.txt\": text }, { author: \"fixture\", message: \"base\", time: 0 });\n+\tconst oracle = new Graph();\n+\toracle.add(atom);\n+\tgraph.add(atom);\n+\tconst bytes = Buffer.byteLength(JSON.stringify(storage.read(\"atoms\", \"0\")));\n+\tassert.ok(bytes < 2048, `atom metadata retained ${bytes} bytes for 513 insertion lines`);\n+\tassert.deepEqual([...new Graph({ storage }).atom(\"a0\").inserts[0]!.lines], oracle.atom(\"a0\").inserts[0]!.lines);\n+\tconst read = storage.read.bind(storage);\n+\tlet lineReads = 0;\n+\tstorage.read = (collection, key) => {\n+\t\tif (collection === \"atom-inserts:0:lines:0\") lineReads++;\n+\t\treturn read(collection, key);\n+\t};\n+\tconst saved = new Graph({ storage }).atom(\"a0\");\n+\tassert.equal(lineReads, 0);\n+\tassert.equal(saved.inserts[0]!.lines.length, 513);\n+\tassert.equal(lineReads, 0);\n+\tassert.equal(saved.inserts[0]!.lines[512], oracle.atom(\"a0\").inserts[0]!.lines[512]);\n+\tassert.equal(lineReads, 1);\n+});\n+\n+test(\"partial insertion persistence rolls back and retries on the same graph\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst graph = new Graph({ storage });\n+\tconst atom = buildBase(\n+\t\t{ \"a.txt\": Array.from({ length: 513 }, (_, i) => `line ${i}\\n`).join(\"\") },\n+\t\t{ author: \"fixture\", message: \"base\", time: 0 },\n+\t);\n+\tconst before = storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\tconst write = storage.write.bind(storage);\n+\tstorage.write = (collection, key, value) => {\n+\t\twrite(collection, key, value);\n+\t\tif (collection === \"atom-inserts:0:lines:0\" && key === \"128\") throw Error(\"injected insertion failure\");\n+\t};\n+\tassert.throws(() => graph.add(atom), /injected insertion failure/);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);\n+\tassert.equal(graph.size, 0);\n+\tstorage.write = write;\n+\tgraph.add(atom);\n+\tassert.deepEqual([...new Graph({ storage }).atom(\"a0\").inserts[0]!.lines], atom.inserts[0]!.lines);\n+});"}],"unmatched":[]}