Files
oh-my-pi/packages/ai/test/json-parse.test.ts
T
can1357 ef95756354 refactor(ai): simplified streaming JSON parsing and removed unused user agent helper
- Removed the unused `antigravity-user-agent.ts` file from the AI providers.
- Updated `parseStreamingJson` to trim only leading whitespace, try `JSON.parse` first, then partial parsing on repaired input before returning an empty object on failure.
- Removed a streaming JSON test that depended on preserving trailing spaces in incomplete string fragments.
2026-05-02 06:59:25 +02:00

30 lines
1.3 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { parseJsonWithRepair, parseStreamingJson, repairJson } from "@oh-my-pi/pi-ai/utils/json-parse";
describe("JSON repair", () => {
it("leaves valid string escapes unchanged", () => {
const json = String.raw`{"text":"quote: \" unicode: \u2028 slash: \/ newline: \n"}`;
expect(repairJson(json)).toBe(json);
const expectedText = ['quote: " unicode: ', String.fromCharCode(0x2028), " slash: / newline: \n"].join("");
expect(parseJsonWithRepair<{ text: string }>(json)).toEqual({ text: expectedText });
});
it("escapes raw control characters inside string literals", () => {
const json = '{"text":"a\nb\u0001c"}';
expect(repairJson(json)).toBe(String.raw`{"text":"a\nb\u0001c"}`);
expect(parseJsonWithRepair<{ text: string }>(json)).toEqual({ text: "a\nb\u0001c" });
});
it("preserves invalid simple escapes as literal backslashes", () => {
const json = String.raw`{"value":"a\qb"}`;
expect(repairJson(json)).toBe(String.raw`{"value":"a\\qb"}`);
expect(parseJsonWithRepair<{ value: string }>(json)).toEqual({ value: String.raw`a\qb` });
});
it("returns an empty object for whitespace-only streaming JSON", () => {
expect(parseStreamingJson<Record<string, unknown>>(" \t\n\r")).toEqual({});
});
});