- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/json-value-chunks.test.ts?format=json
66 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 165, 1 version changed it
1import assert from "node:assert/strict";
2import { createHash } from "node:crypto";
3import { test } from "node:test";
4import { equalJsonValues, jsonValueChunks } from "../json-value-chunks.ts";
6test("canonical JSON chunks preserve scalar, container and toJSON encoding", () => {
7 const values = [
8 null,
9 true,
10 false,
11 0,
12 -0,
13 1.25,
14 NaN,
15 Infinity,
16 { z: undefined, a: [undefined, null, 'quoted"\n\t\\', "\ud800", "\udc00", "\u2028\u2029"] },
17 new Array(3),
18 { 20: "twenty", 2: "two", before: 1, after: 2 },
19 {
20 nested: {
21 toJSON(key: string) {
22 return { key, value: "converted" };
23 },
24 },
25 },
26 JSON.parse('{"__proto__":"data","constructor":"also data"}'),
27 ];
28 for (const value of values) assert.equal([...jsonValueChunks(value)].join(""), JSON.stringify(value));
29 for (const left of values)
30 for (const right of values)
31 assert.equal(equalJsonValues(left, right), JSON.stringify(left) === JSON.stringify(right));
32});
34test("canonical JSON string chunks preserve every surrogate split and bound escaped output", () => {
35 for (const offset of [16382, 16383, 16384, 16385]) {
36 const value = { text: `${"a".repeat(offset)}\ud83d\ude80${"\u0000".repeat(40000)}\ud800` };
37 const hash = createHash("sha256");
38 for (const chunk of jsonValueChunks(value)) {
39 assert.ok(chunk.length <= 16384 * 6);
40 hash.update(chunk);
41 }
42 assert.equal(hash.digest("hex"), createHash("sha256").update(JSON.stringify(value)).digest("hex"));
43 }
44});
46test("canonical JSON visits large arrays incrementally and rejects cycles", () => {
47 let reads = 0;
48 const values = new Proxy(new Array(10000), {
49 get(target, key, receiver) {
50 if (typeof key === "string" && /^[0-9]+$/.test(key)) {
51 reads++;
52 return Number(key);
53 }
54 return Reflect.get(target, key, receiver);
55 },
56 });
57 const chunks = jsonValueChunks(values);
58 assert.equal(chunks.next().value, "[");
59 assert.equal(reads, 0);
60 assert.equal(chunks.next().value, "0");
61 assert.equal(reads, 1);
62 chunks.return?.();
63 const cyclic: unknown[] = [];
64 cyclic.push(cyclic);
65 assert.throws(() => [...jsonValueChunks(cyclic)], /cyclic/);
66});