Files
openclaw/src/llm/utils/json-parse.test.ts
Coder adcac404e1 fix(llm): repair invalid streaming unicode escapes
Repair invalid \u escapes during streaming JSON parsing without changing valid Unicode escapes. Split oversized node CI doctor/infra shards and fix the restart test mock deadlock so PR CI stays under the no-output threshold.\n\nCo-authored-by: Coder <83845889+coder999999999@users.noreply.github.com>
2026-05-30 20:53:26 +01:00

22 lines
983 B
TypeScript

import { describe, expect, it } from "vitest";
import { parseJsonWithRepair, parseStreamingJson, repairJson } from "./json-parse.js";
describe("json-parse repairJson invalid \\u escapes", () => {
it("repairs a \\u not followed by four hex digits so the result parses", () => {
// JS string is: {"path":"C:\users"} — a model emitting an unescaped Windows path.
const broken = '{"path":"C:\\users"}';
expect(() => JSON.parse(repairJson(broken))).not.toThrow();
expect(parseJsonWithRepair(broken)).toEqual({ path: "C:\\users" });
});
it("preserves valid \\uXXXX escapes", () => {
expect(parseJsonWithRepair('{"e":"\\u0041"}')).toEqual({ e: "A" });
});
it("recovers streaming tool-call arguments instead of dropping them to {}", () => {
// LaTeX-style \u (\underline) is a valid string value the model may emit in args.
const args = '{"cmd":"\\underline{x}"}';
expect(parseStreamingJson(args)).toEqual({ cmd: "\\underline{x}" });
});
});