File size: 2,823 Bytes
cdd4fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, it } from "node:test";
import assert from "node:assert";
import {
    TabularJsonStreamParser,
    drainTabularJsonObjects,
    parseTabularExtractionObject,
} from "../../src/lib/tabularJsonStreamParser";

describe("tabularJsonStreamParser", () => {
    it("parses minified NDJSON lines", () => {
        const input =
            '{"column_index":0,"summary":"A","flag":"green","reasoning":"ok"}\n{"column_index":1,"summary":"B","flag":"grey","reasoning":"ok2"}';
        const { objects, remainder, parseFailures } =
            drainTabularJsonObjects(input);
        assert.strictEqual(parseFailures, 0);
        assert.strictEqual(objects.length, 2);
        assert.strictEqual(objects[0].column_index, 0);
        assert.strictEqual(objects[1].column_index, 1);
        assert.strictEqual(remainder.trim(), "");
    });

    it("parses pretty-printed JSON with multiline string values", () => {
        const input = `\`\`\`json
{
  "column_index": 1,
  "summary": "This is a
multiline
summary.",
  "flag": "green",
  "reasoning": "Because."
},
{"column_index": 2, "summary": "Line 2", "flag": "red"}
\`\`\``;
        const { objects, parseFailures } = drainTabularJsonObjects(input);
        assert.strictEqual(parseFailures, 0);
        assert.strictEqual(objects.length, 2);
        assert.strictEqual(objects[0].column_index, 1);
        assert.strictEqual(objects[0].summary, "This is a\nmultiline\nsummary.");
        assert.strictEqual(objects[1].column_index, 2);
    });

    it("streams objects incrementally across deltas", () => {
        const parser = new TabularJsonStreamParser();
        const first = parser.append('{"column_index":0,"summary":"');
        assert.deepStrictEqual(first, []);

        const second = parser.append('Part A","flag":"green","reasoning":"r"}');
        assert.strictEqual(second.length, 1);
        assert.strictEqual(second[0].column_index, 0);

        const third = parser.append('\n{"column_index":1,"summary":"B","flag":"grey","reasoning":"r2"}');
        assert.strictEqual(third.length, 1);
        assert.strictEqual(third[0].column_index, 1);

        const flushed = parser.flush();
        assert.strictEqual(flushed.length, 0);
        assert.strictEqual(parser.remainder.trim(), "");
    });

    it("leaves incomplete trailing object in remainder", () => {
        const parser = new TabularJsonStreamParser();
        parser.append('{"column_index":0,"summary":"done","flag":"green","reasoning":"r"}');
        parser.append('\n{"column_index":1,"summary":"partial');
        assert.strictEqual(parser.flush().length, 0);
        assert.ok(parser.remainder.includes('"column_index":1'));
    });

    it("returns null for invalid JSON", () => {
        assert.strictEqual(parseTabularExtractionObject("{not json}"), null);
    });
});