{"draft":null,"from":2,"to":318,"files":[{"path":"src/engine/test/json-value-chunks.test.ts","added":66,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/json-value-chunks.test.ts\n@@ -0,0 +1,66 @@\n+import assert from \"node:assert/strict\";\n+import { createHash } from \"node:crypto\";\n+import { test } from \"node:test\";\n+import { equalJsonValues, jsonValueChunks } from \"../json-value-chunks.ts\";\n+\n+test(\"canonical JSON chunks preserve scalar, container and toJSON encoding\", () => {\n+\tconst values = [\n+\t\tnull,\n+\t\ttrue,\n+\t\tfalse,\n+\t\t0,\n+\t\t-0,\n+\t\t1.25,\n+\t\tNaN,\n+\t\tInfinity,\n+\t\t{ z: undefined, a: [undefined, null, 'quoted\"\\n\\t\\\\', \"\\ud800\", \"\\udc00\", \"\\u2028\\u2029\"] },\n+\t\tnew Array(3),\n+\t\t{ 20: \"twenty\", 2: \"two\", before: 1, after: 2 },\n+\t\t{\n+\t\t\tnested: {\n+\t\t\t\ttoJSON(key: string) {\n+\t\t\t\t\treturn { key, value: \"converted\" };\n+\t\t\t\t},\n+\t\t\t},\n+\t\t},\n+\t\tJSON.parse('{\"__proto__\":\"data\",\"constructor\":\"also data\"}'),\n+\t];\n+\tfor (const value of values) assert.equal([...jsonValueChunks(value)].join(\"\"), JSON.stringify(value));\n+\tfor (const left of values)\n+\t\tfor (const right of values)\n+\t\t\tassert.equal(equalJsonValues(left, right), JSON.stringify(left) === JSON.stringify(right));\n+});\n+\n+test(\"canonical JSON string chunks preserve every surrogate split and bound escaped output\", () => {\n+\tfor (const offset of [16382, 16383, 16384, 16385]) {\n+\t\tconst value = { text: `${\"a\".repeat(offset)}\\ud83d\\ude80${\"\\u0000\".repeat(40000)}\\ud800` };\n+\t\tconst hash = createHash(\"sha256\");\n+\t\tfor (const chunk of jsonValueChunks(value)) {\n+\t\t\tassert.ok(chunk.length <= 16384 * 6);\n+\t\t\thash.update(chunk);\n+\t\t}\n+\t\tassert.equal(hash.digest(\"hex\"), createHash(\"sha256\").update(JSON.stringify(value)).digest(\"hex\"));\n+\t}\n+});\n+\n+test(\"canonical JSON visits large arrays incrementally and rejects cycles\", () => {\n+\tlet reads = 0;\n+\tconst values = new Proxy(new Array(10000), {\n+\t\tget(target, key, receiver) {\n+\t\t\tif (typeof key === \"string\" && /^[0-9]+$/.test(key)) {\n+\t\t\t\treads++;\n+\t\t\t\treturn Number(key);\n+\t\t\t}\n+\t\t\treturn Reflect.get(target, key, receiver);\n+\t\t},\n+\t});\n+\tconst chunks = jsonValueChunks(values);\n+\tassert.equal(chunks.next().value, \"[\");\n+\tassert.equal(reads, 0);\n+\tassert.equal(chunks.next().value, \"0\");\n+\tassert.equal(reads, 1);\n+\tchunks.return?.();\n+\tconst cyclic: unknown[] = [];\n+\tcyclic.push(cyclic);\n+\tassert.throws(() => [...jsonValueChunks(cyclic)], /cyclic/);\n+});"}],"unmatched":[]}