{"draft":null,"from":0,"to":318,"files":[{"path":"server/test/json-events.test.ts","added":148,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/json-events.test.ts\n@@ -0,0 +1,148 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { jsonEvents } from \"../src/json-events.ts\";\n+\n+test(\"structural JSON decoding is invariant at every chunk boundary\", () => {\n+\tconst documents = [\n+\t\t'{\"type\":\"init\",\"atom\":{\"lines\":[\"a\\\\n\",\"\\\\uD83D\\\\uDE00\"],\"seq\":0}}',\n+\t\t\"[true,false,null,-0,1.2e-3,{},[]]\",\n+\t\t'\"plain\"',\n+\t];\n+\tfor (const document of documents) {\n+\t\tconst expected = [...jsonEvents([document])];\n+\t\tfor (let split = 0; split <= document.length; split++)\n+\t\t\tassert.deepEqual([...jsonEvents([document.slice(0, split), document.slice(split)])], expected);\n+\t}\n+\tfor (const invalid of [\n+\t\t'{\"a\":1,}',\n+\t\t\"[1,]\",\n+\t\t'{\"a\" 1}',\n+\t\t\"[01]\",\n+\t\t\"[+1]\",\n+\t\t\"[.1]\",\n+\t\t\"[1e]\",\n+\t\t\"[undefined]\",\n+\t\t\"true false\",\n+\t\t\"[\",\n+\t\t\"{\",\n+\t])\n+\t\tassert.throws(() => [...jsonEvents([invalid])], /JSON/);\n+\tassert.throws(() => [...jsonEvents([\"[\".repeat(130) + \"0\" + \"]\".repeat(130)])], /nesting/);\n+});\n+\n+test(\"structural JSON decoding streams large arrays and closes early input\", () => {\n+\tlet closed = false;\n+\tconst source = function* () {\n+\t\ttry {\n+\t\t\tyield \"[\";\n+\t\t\tfor (let i = 0; i < 100000; i++) yield `${i ? \",\" : \"\"}${i}`;\n+\t\t\tyield \"]\";\n+\t\t} finally {\n+\t\t\tclosed = true;\n+\t\t}\n+\t};\n+\tlet count = 0;\n+\tfor (const event of jsonEvents(source()))\n+\t\tif (event.kind === \"value\") {\n+\t\t\tassert.equal(event.value, count);\n+\t\t\tcount++;\n+\t\t}\n+\tassert.equal(count, 100000);\n+\tassert.equal(closed, true);\n+\tclosed = false;\n+\tfor (const event of jsonEvents(source())) {\n+\t\tassert.equal(event.kind, \"array\");\n+\t\tbreak;\n+\t}\n+\tassert.equal(closed, true);\n+});\n+\n+function materialize(document: string): unknown {\n+\tconst events = jsonEvents(document.split(\"\"));\n+\tconst next = () => {\n+\t\tconst event = events.next();\n+\t\tif (event.done) throw Error(\"missing event\");\n+\t\treturn event.value;\n+\t};\n+\tconst value = (event = next()): unknown => {\n+\t\tif (event.kind === \"value\") return event.value;\n+\t\tif (event.kind === \"string\") {\n+\t\t\tlet text = \"\";\n+\t\t\tfor (;;) {\n+\t\t\t\tconst fragment = next();\n+\t\t\t\tif (fragment.kind === \"string-end\") return text;\n+\t\t\t\tif (fragment.kind !== \"text\") throw Error(\"bad string event\");\n+\t\t\t\ttext += fragment.value;\n+\t\t\t}\n+\t\t}\n+\t\tif (event.kind === \"array\") {\n+\t\t\tconst result: unknown[] = [];\n+\t\t\tfor (;;) {\n+\t\t\t\tconst child = next();\n+\t\t\t\tif (child.kind === \"end\") return result;\n+\t\t\t\tresult.push(value(child));\n+\t\t\t}\n+\t\t}\n+\t\tif (event.kind === \"object\") {\n+\t\t\tconst result: Record<string, unknown> = {};\n+\t\t\tfor (;;) {\n+\t\t\t\tconst key = next();\n+\t\t\t\tif (key.kind === \"end\") return result;\n+\t\t\t\tif (key.kind !== \"key\") throw Error(\"bad key event\");\n+\t\t\t\tObject.defineProperty(result, key.value, {\n+\t\t\t\t\tvalue: value(),\n+\t\t\t\t\tenumerable: true,\n+\t\t\t\t\twritable: true,\n+\t\t\t\t\tconfigurable: true,\n+\t\t\t\t});\n+\t\t\t}\n+\t\t}\n+\t\tthrow Error(\"unexpected event\");\n+\t};\n+\tconst result = value();\n+\tassert.equal(events.next().done, true);\n+\treturn result;\n+}\n+\n+test(\"streamed JSON agrees with JSON.parse on generated and adversarial documents\", () => {\n+\tlet seed = 731;\n+\tconst random = () => (seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0);\n+\tconst generate = (depth: number): unknown => {\n+\t\tconst choice = random() % (depth ? 6 : 4);\n+\t\tif (choice === 0) return null;\n+\t\tif (choice === 1) return random() % 2 === 0;\n+\t\tif (choice === 2) return (random() - 0x80000000) / 17;\n+\t\tif (choice === 3) return `quotes\"\\\\\\n${String.fromCharCode(random() % 65536)}\\uD83D\\uDE00`;\n+\t\tif (choice === 4) return Array.from({ length: random() % 5 }, () => generate(depth - 1));\n+\t\treturn Object.fromEntries(Array.from({ length: random() % 5 }, (_, i) => [`key${i}`, generate(depth - 1)]));\n+\t};\n+\tfor (let i = 0; i < 200; i++) {\n+\t\tconst document = JSON.stringify(generate(4));\n+\t\tassert.deepEqual(materialize(document), JSON.parse(document));\n+\t}\n+\tfor (const document of [\n+\t\t'{\"__proto__\":{\"polluted\":true},\"constructor\":0,\"a\":1,\"a\":2}',\n+\t\t\"[-0,1e999,-1e999,1e-999]\",\n+\t\t'{\"\\\\u0061\":\"\\\\uD800\\\\uDC00\"}',\n+\t])\n+\t\tassert.deepEqual(materialize(document), JSON.parse(document));\n+});\n+\n+import { MemoryStore, Project } from \"../../worker/project/project.ts\";\n+\n+test(\"streamed JSON preserves real canonical project entries\", () => {\n+\tconst memory = new MemoryStore();\n+\tconst project = new Project(memory);\n+\tproject.init({ \"a.txt\": \"one\\ntwo\\n\" }, 0);\n+\tproject.record(\"alice\", { \"a.txt\": \"ONE\\ntwo\\n\" }, 1);\n+\tproject.publish(\"alice\", \"Publish\", 2);\n+\tproject.switchTo(\"alice\", \"draft\", 3);\n+\tproject.record(\"alice\", { \"a.txt\": \"ONE\\nTWO\\n\" }, 4);\n+\tproject.publish(\"alice\", \"Draft publish\", 5);\n+\tproject.land(\"alice\", \"Land\", 6);\n+\tfor (const entry of memory.entries) {\n+\t\tconst document = JSON.stringify(entry);\n+\t\tassert.deepEqual(materialize(document), JSON.parse(document));\n+\t}\n+\tassert.ok(memory.entries.length > 5);\n+});"}],"unmatched":[]}