ef95756354
- 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.
30 lines
1.3 KiB
TypeScript
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({});
|
|
});
|
|
});
|