{"draft":null,"from":3,"to":318,"files":[{"path":"src/engine/test/graph-long-text.test.ts","added":100,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/graph-long-text.test.ts\n@@ -0,0 +1,100 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { MemoryStore, Project } from \"../../../worker/project/project.ts\";\n+import { Graph } from \"../graph.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"large insertion text stays lazy even when the run has few lines\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst memory = new MemoryStore();\n+\tconst line = \"x\".repeat(100000);\n+\tnew Project(memory).init({ \"a.txt\": `${line}\\n${line}\\n${line}\\n` }, 0);\n+\tconst entry = memory.entries[0]!;\n+\tif (entry.type !== \"init\") throw Error(\"expected init\");\n+\tnew Graph({ storage }).add(entry.atom);\n+\tconst read = storage.read.bind(storage);\n+\tlet textReads = 0;\n+\tt.mock.method(storage, \"read\", (collection: string, key: string) => {\n+\t\tif (collection === \"atom-text:long:chunks\") textReads++;\n+\t\treturn read(collection, key);\n+\t});\n+\tconst atom = new Graph({ storage }).atom(\"a0\");\n+\tassert.equal(atom.inserts[0]!.lines.length, 3);\n+\tassert.equal(textReads, 0, \"reading line count must not decode the complete run\");\n+\tassert.equal(atom.inserts[0]!.lines[1], line);\n+\tassert.equal(textReads, Math.ceil(line.length / 16384), \"an indexed read decodes only the selected line\");\n+});\n+\n+test(\"persistent graph stores long lines in chunks and reopens exact text\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst memory = new MemoryStore();\n+\t\tconst value = `${\"x\".repeat(100000)}\\uD83D\\uDE00`;\n+\t\tnew Project(memory).init({ \"a.txt\": value }, 0);\n+\t\tconst entry = memory.entries[0]!;\n+\t\tassert.equal(entry.type, \"init\");\n+\t\tif (entry.type !== \"init\") throw Error(\"expected init\");\n+\t\tnew Graph({ storage }).add(entry.atom);\n+\t\tassert.deepEqual(storage.read(\"line-text\", \"0\"), { format: 1, text: JSON.stringify([\"0\", 0]) });\n+\t\tassert.equal(new Graph({ storage }).lineText(\"a0.0\"), value);\n+\t\tassert.ok(\n+\t\t\t(storage.db.prepare(\"SELECT COUNT(*) AS n FROM state WHERE collection = 'atom-text:long:chunks'\").get()!\n+\t\t\t\t.n as number) > 1,\n+\t\t);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"long atom acceptance rolls back partial chunks and detects missing text\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst memory = new MemoryStore();\n+\tconst value = \"z\".repeat(100000);\n+\tnew Project(memory).init({ \"a.txt\": value }, 0);\n+\tconst entry = memory.entries[0]!;\n+\tif (entry.type !== \"init\") throw Error(\"expected init\");\n+\tconst graph = new Graph({ storage });\n+\tconst rows = () => storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\tconst before = rows();\n+\tconst write = storage.write.bind(storage);\n+\tlet chunks = 0;\n+\tconst failure = t.mock.method(storage, \"write\", (collection: string, key: string, value: unknown) => {\n+\t\twrite(collection, key, value);\n+\t\tif (collection === \"atom-text:long:chunks\" && ++chunks === 2) throw Error(\"injected partial atom text\");\n+\t});\n+\tassert.throws(() => graph.add(entry.atom), /partial atom text/);\n+\tassert.deepEqual(rows(), before);\n+\tassert.equal(graph.size, 0);\n+\tfailure.mock.restore();\n+\tgraph.add(entry.atom);\n+\tassert.equal(graph.lineText(\"a0.0\"), value);\n+\tconst textKey = JSON.stringify([\"0\", 0]);\n+\tstorage.remove(\"atom-text:long:chunks\", JSON.stringify([textKey, 1]));\n+\tconst reopened = new Graph({ storage });\n+\tassert.throws(() => reopened.lineText(\"a0.0\"), /incomplete stored text/);\n+\tassert.throws(() => reopened.atom(\"a0\").inserts[0]!.lines[0], /incomplete stored text/);\n+});\n+\n+test(\"persistent graph detaches accepted lists without freezing a second input copy\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst memory = new MemoryStore();\n+\t\tnew Project(memory).init({ \"a.txt\": \"before\\nsecond\" }, 0);\n+\t\tconst entry = memory.entries[0]!;\n+\t\tif (entry.type !== \"init\") throw Error(\"expected init\");\n+\t\tconst atom = structuredClone(entry.atom);\n+\t\tconst expected = structuredClone(atom);\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(atom);\n+\t\t(atom.inserts[0]!.lines as string[])[0] = \"changed\";\n+\t\t(atom.writes as string[]).length = 0;\n+\t\tfor (const loaded of [graph, new Graph({ storage })]) {\n+\t\t\tassert.deepEqual(JSON.parse(JSON.stringify(loaded.atom(\"a0\"))), expected);\n+\t\t\tassert.equal(loaded.lineText(\"a0.0\"), expected.inserts[0]!.lines[0]);\n+\t\t}\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"unmatched":[]}