{"draft":null,"from":52,"to":318,"files":[{"path":"src/engine/test/json-string-chunks.test.ts","added":76,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/json-string-chunks.test.ts\n@@ -0,0 +1,76 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase } from \"../build.ts\";\n+import { Graph } from \"../graph.ts\";\n+import { JsonStringChunks, jsonValueChunks, registerJsonChunkValue } from \"../json-value-chunks.ts\";\n+import { StoredText } from \"../stored-text.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"many stored insertion runs serialize with linear reads and support backwards access\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst files = Object.fromEntries(Array.from({ length: 128 }, (_, i) => [`f${i}`, `${i}:${\"x\".repeat(17000)}\\n`]));\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(buildBase(files, { author: \"writer\", message: \"many runs\" }));\n+\tconst stored = graph.atom(\"a0\");\n+\tconst expected = JSON.stringify(stored);\n+\tconst before = storage.reads;\n+\tassert.equal([...jsonValueChunks(stored)].join(\"\"), expected);\n+\tassert.ok(storage.reads - before < 128 * 25, `serialization used ${storage.reads - before} reads`);\n+\tfor (const index of [127, 0, 64, 2, 126, 1]) {\n+\t\tconst run = stored.inserts[index]!;\n+\t\tassert.equal(run.lines[0], files[run.file]!.trimEnd());\n+\t}\n+\tassert.equal([...jsonValueChunks(stored)].join(\"\"), expected);\n+});\n+\n+test(\"chunk-backed JSON strings preserve escaping and split UTF-16 exactly\", () => {\n+\tconst values = [\n+\t\t\"\",\n+\t\t\"ordinary\",\n+\t\t'\"\\\\\\n\\r\\t\\u0000',\n+\t\t\"\\ud800\",\n+\t\t\"\\udc00\",\n+\t\t\"\\ud800\\udc00\",\n+\t\t\"a\\ud800b\\udc00c\",\n+\t\t\"x\".repeat(50000),\n+\t];\n+\tfor (const text of values)\n+\t\tfor (const size of [1, 2, 17, 16384]) {\n+\t\t\tconst value = new JsonStringChunks(function* () {\n+\t\t\t\tyield \"\";\n+\t\t\t\tfor (let offset = 0; offset < text.length; offset += size) {\n+\t\t\t\t\tyield text.slice(offset, offset + size);\n+\t\t\t\t\tyield \"\";\n+\t\t\t\t}\n+\t\t\t});\n+\t\t\tassert.equal([...jsonValueChunks({ value })].join(\"\"), JSON.stringify({ value: text }));\n+\t\t\tassert.equal(JSON.stringify(value), JSON.stringify(text));\n+\t\t}\n+});\n+\n+test(\"registered lazy values stream without invoking their whole-value toJSON\", () => {\n+\tconst value = {\n+\t\ttoJSON() {\n+\t\t\tthrow Error(\"whole value forbidden\");\n+\t\t},\n+\t};\n+\tregisterJsonChunkValue(value, () => ({ lines: [new JsonStringChunks(() => [\"abc\", \"def\"])] }));\n+\tassert.equal([...jsonValueChunks(value)].join(\"\"), '{\"lines\":[\"abcdef\"]}');\n+\tassert.throws(() => [...jsonValueChunks(new JsonStringChunks(() => [\"x\".repeat(16385)]))], /exceeds bound/);\n+});\n+\n+for (const length of [60000, 100000])\n+\ttest(`stored atom JSON streams a ${length}-character line without whole reads`, (t) => {\n+\t\tconst storage = new SqlState();\n+\t\tt.after(() => storage.db.close());\n+\t\tconst graph = new Graph({ storage });\n+\t\tconst atom = buildBase({ f: `${\"x\".repeat(length)}\\n` }, { author: \"writer\", message: \"init\" });\n+\t\tgraph.add(atom);\n+\t\tconst stored = graph.atom(\"a0\");\n+\t\tconst expected = JSON.stringify(stored);\n+\t\tt.mock.method(StoredText.prototype, \"read\", () => {\n+\t\t\tthrow Error(\"whole stored text forbidden\");\n+\t\t});\n+\t\tassert.equal([...jsonValueChunks(stored)].join(\"\"), expected);\n+\t});"}],"unmatched":[]}