pi-mono/packages/ai/test/json-parse.test.ts
David Brailovsky 4b994d5e5e fix(ai): repair invalid \u escapes in tool-call arguments
Invalid \u escapes such as \usepackage were treated as valid, so JSON
parsing failed and the partial-json fallback dropped the field and
everything after it. They are now kept as literal text like other
invalid escapes.

fixes #10868
2026-10-13 15:36:01 +02:00

22 lines
820 B
TypeScript

import { describe, expect, it } from "vitest";
import { parseStreamingJson } from "../src/utils/json-parse.ts";
describe("parseStreamingJson", () => {
// #10868
it("keeps invalid \\u escapes as literal text without dropping fields", () => {
expect(parseStreamingJson(String.raw`{"text":"\url{x}"}`)).toEqual({ text: "\\url{x}" });
expect(parseStreamingJson(String.raw`{"path":"a.tex","content":"\usepackage{x}","mode":"w"}`)).toEqual({
path: "a.tex",
content: "\\usepackage{x}",
mode: "w",
});
});
it("decodes valid \\u escapes", () => {
expect(parseStreamingJson(String.raw`{"text":"caf\u00e9 \url"}`)).toEqual({ text: "café \\url" });
});
it("omits a \\u escape that is cut off mid-stream", () => {
expect(parseStreamingJson(String.raw`{"text":"ab\u00`)).toEqual({ text: "ab" });
});
});