1
import { describe, expect, it } from "vitest";2
import { parseStreamingJson, parseToolCallArguments } from "../src/utils/json-parse.ts";4
describe("parseStreamingJson", () => {5
// #108686
it("keeps invalid \\u escapes as literal text without dropping fields", () => {7
expect(parseStreamingJson(String.raw`{"text":"\url{x}"}`)).toEqual({ text: "\\url{x}" });8
expect(parseStreamingJson(String.raw`{"path":"a.tex","content":"\usepackage{x}","mode":"w"}`)).toEqual({9
path: "a.tex",10
content: "\\usepackage{x}",11
mode: "w",12
});13
});15
it("decodes valid \\u escapes", () => {16
expect(parseStreamingJson(String.raw`{"text":"caf\u00e9 \url"}`)).toEqual({ text: "café \\url" });17
});19
it("omits a \\u escape that is cut off mid-stream", () => {20
expect(parseStreamingJson(String.raw`{"text":"ab\u00`)).toEqual({ text: "ab" });21
});22
});24
// #1089725
describe("parseToolCallArguments", () => {26
it("parses complete JSON and repairs invalid escapes", () => {27
expect(parseToolCallArguments(String.raw`{"kind":"seek","note":"\url"}`)).toEqual({28
arguments: { kind: "seek", note: "\\url" },29
});30
});32
it("uses empty arguments for empty text", () => {33
expect(parseToolCallArguments("")).toEqual({ arguments: {} });34
});36
it("does not keep the valid prefix of malformed JSON", () => {37
const raw = '{"action":"apply","actions":[{"kind":"seek","time":.5}]}';38
const result = parseToolCallArguments(raw);39
expect(result.arguments).toEqual({});40
expect(result.argumentsError).toMatch(/\n\nReceived arguments:\n/);41
expect(result.argumentsError?.endsWith(raw)).toBe(true);42
});44
it("does not complete cut-off JSON", () => {45
const raw = '{"kind":"seek","clipId":"clip-fl';46
const result = parseToolCallArguments(raw);47
expect(result.arguments).toEqual({});48
expect(result.argumentsError?.endsWith(raw)).toBe(true);49
});51
it("rejects JSON that is not an object", () => {52
expect(parseToolCallArguments("[1,2]")).toEqual({53
arguments: {},54
argumentsError: "Tool arguments must be a JSON object\n\nReceived arguments:\n[1,2]",55
});56
});57
});