Spaces:
Sleeping
Sleeping
Download backend/tests/unit/tabularJsonStreamParser.test.ts from bardd/mike-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/spaces/bardd/mike-backend/resolve/main/backend/tests/unit/tabularJsonStreamParser.test.ts
- Command line
-
hf download hf://spaces/bardd/mike-backend/backend/tests/unit/tabularJsonStreamParser.test.ts
-
curl -L -o tabularJsonStreamParser.test.ts https://huggingface.co/spaces/bardd/mike-backend/resolve/main/backend/tests/unit/tabularJsonStreamParser.test.ts
2.82 kB
| 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); | |
| }); | |
| }); | |