{"draft":null,"from":161,"to":162,"files":[{"path":"src/engine/test/graph-long-text.test.ts","added":123,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/graph-long-text.test.ts\n@@ -0,0 +1,123 @@\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(\"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+import { SqliteWriterState } from \"../../../server/src/writer-host.ts\";\n+import { memoryStorage } from \"../../../server/test/helpers.ts\";\n+\n+test(\"persistent atom metadata accepts text beyond the generic state limit\", (t) => {\n+\tconst database = memoryStorage();\n+\tt.after(() => database.db.close());\n+\tSqliteWriterState.initialize(database);\n+\tconst storage = new SqliteWriterState(database, \"long-atom\");\n+\tconst memory = new MemoryStore();\n+\tconst value = \"x\".repeat(8100000);\n+\tnew Project(memory).init({ \"large.txt\": value }, 0);\n+\tconst entry = memory.entries[0]!;\n+\tif (entry.type !== \"init\") throw Error(\"expected init\");\n+\tnew Graph({ storage }).add(entry.atom);\n+\tassert.ok(JSON.stringify(storage.read(\"atoms\", \"0\")).length < 4096);\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 graph = new Graph({ storage });\n+\tconst atom = graph.atom(\"a0\");\n+\tassert.equal(textReads, 0, \"reading atom metadata must not load long text\");\n+\tassert.equal(atom.inserts[0]!.lines[0], value);\n+\tassert.ok(textReads > 0);\n+\tassert.equal(graph.lineText(\"a0.0\"), value);\n+\tassert.equal(\n+\t\tdatabase.db\n+\t\t\t.prepare(\n+\t\t\t\t\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = 'long-atom' AND collection = 'atom-text:long:chunks' AND part = -1\",\n+\t\t\t)\n+\t\t\t.get()!.n,\n+\t\tMath.ceil(value.length / 16384),\n+\t);\n+\tassert.equal(\n+\t\tdatabase.db\n+\t\t\t.prepare(\n+\t\t\t\t\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = 'long-atom' AND collection = 'line-text:long:chunks'\",\n+\t\t\t)\n+\t\t\t.get()!.n,\n+\t\t0,\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":[]}