- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/test/json-events.test.ts?format=json
148 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 1 version changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { jsonEvents } from "../src/json-events.ts";
5test("structural JSON decoding is invariant at every chunk boundary", () => {
6 const documents = [
7 '{"type":"init","atom":{"lines":["a\\n","\\uD83D\\uDE00"],"seq":0}}',
8 "[true,false,null,-0,1.2e-3,{},[]]",
9 '"plain"',
10 ];
11 for (const document of documents) {
12 const expected = [...jsonEvents([document])];
13 for (let split = 0; split <= document.length; split++)
14 assert.deepEqual([...jsonEvents([document.slice(0, split), document.slice(split)])], expected);
15 }
16 for (const invalid of [
17 '{"a":1,}',
18 "[1,]",
19 '{"a" 1}',
20 "[01]",
21 "[+1]",
22 "[.1]",
23 "[1e]",
24 "[undefined]",
25 "true false",
26 "[",
27 "{",
28 ])
29 assert.throws(() => [...jsonEvents([invalid])], /JSON/);
30 assert.throws(() => [...jsonEvents(["[".repeat(130) + "0" + "]".repeat(130)])], /nesting/);
31});
33test("structural JSON decoding streams large arrays and closes early input", () => {
34 let closed = false;
35 const source = function* () {
36 try {
37 yield "[";
38 for (let i = 0; i < 100000; i++) yield `${i ? "," : ""}${i}`;
39 yield "]";
40 } finally {
41 closed = true;
42 }
43 };
44 let count = 0;
45 for (const event of jsonEvents(source()))
46 if (event.kind === "value") {
47 assert.equal(event.value, count);
48 count++;
49 }
50 assert.equal(count, 100000);
51 assert.equal(closed, true);
52 closed = false;
53 for (const event of jsonEvents(source())) {
54 assert.equal(event.kind, "array");
55 break;
56 }
57 assert.equal(closed, true);
58});
60function materialize(document: string): unknown {
61 const events = jsonEvents(document.split(""));
62 const next = () => {
63 const event = events.next();
64 if (event.done) throw Error("missing event");
65 return event.value;
66 };
67 const value = (event = next()): unknown => {
68 if (event.kind === "value") return event.value;
69 if (event.kind === "string") {
70 let text = "";
71 for (;;) {
72 const fragment = next();
73 if (fragment.kind === "string-end") return text;
74 if (fragment.kind !== "text") throw Error("bad string event");
75 text += fragment.value;
76 }
77 }
78 if (event.kind === "array") {
79 const result: unknown[] = [];
80 for (;;) {
81 const child = next();
82 if (child.kind === "end") return result;
83 result.push(value(child));
84 }
85 }
86 if (event.kind === "object") {
87 const result: Record<string, unknown> = {};
88 for (;;) {
89 const key = next();
90 if (key.kind === "end") return result;
91 if (key.kind !== "key") throw Error("bad key event");
92 Object.defineProperty(result, key.value, {
93 value: value(),
94 enumerable: true,
95 writable: true,
96 configurable: true,
97 });
98 }
99 }
100 throw Error("unexpected event");
101 };
102 const result = value();
103 assert.equal(events.next().done, true);
104 return result;
105}
107test("streamed JSON agrees with JSON.parse on generated and adversarial documents", () => {
108 let seed = 731;
109 const random = () => (seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0);
110 const generate = (depth: number): unknown => {
111 const choice = random() % (depth ? 6 : 4);
112 if (choice === 0) return null;
113 if (choice === 1) return random() % 2 === 0;
114 if (choice === 2) return (random() - 0x80000000) / 17;
115 if (choice === 3) return `quotes"\\\n${String.fromCharCode(random() % 65536)}\uD83D\uDE00`;
116 if (choice === 4) return Array.from({ length: random() % 5 }, () => generate(depth - 1));
117 return Object.fromEntries(Array.from({ length: random() % 5 }, (_, i) => [`key${i}`, generate(depth - 1)]));
118 };
119 for (let i = 0; i < 200; i++) {
120 const document = JSON.stringify(generate(4));
121 assert.deepEqual(materialize(document), JSON.parse(document));
122 }
123 for (const document of [
124 '{"__proto__":{"polluted":true},"constructor":0,"a":1,"a":2}',
125 "[-0,1e999,-1e999,1e-999]",
126 '{"\\u0061":"\\uD800\\uDC00"}',
127 ])
128 assert.deepEqual(materialize(document), JSON.parse(document));
129});
131import { MemoryStore, Project } from "../../worker/project/project.ts";
133test("streamed JSON preserves real canonical project entries", () => {
134 const memory = new MemoryStore();
135 const project = new Project(memory);
136 project.init({ "a.txt": "one\ntwo\n" }, 0);
137 project.record("alice", { "a.txt": "ONE\ntwo\n" }, 1);
138 project.publish("alice", "Publish", 2);
139 project.switchTo("alice", "draft", 3);
140 project.record("alice", { "a.txt": "ONE\nTWO\n" }, 4);
141 project.publish("alice", "Draft publish", 5);
142 project.land("alice", "Land", 6);
143 for (const entry of memory.entries) {
144 const document = JSON.stringify(entry);
145 assert.deepEqual(materialize(document), JSON.parse(document));
146 }
147 assert.ok(memory.entries.length > 5);
148});