Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

schemaJson.test.ts

31 lines836 Bytes

1import { describe, expect, it } from "vitest";
2​
3import { extractJsonObject } from "./schemaJson.ts";
4​
5describe("schemaJson helpers", () => {
6 it("extracts a balanced JSON object from surrounding text", () => {
7 expect(
8 extractJsonObject(`Sure, here is the JSON:
9\`\`\`json
11 "subject": "Update README",
12 "body": ""
14\`\`\`
15Done.`),
16 ).toBe(`{
17 "subject": "Update README",
18 "body": ""
19}`);
20 });
21​
22 it("ignores braces inside strings while finding the object boundary", () => {
23 expect(
24 extractJsonObject('prefix {"message":"literal } brace","nested":{"ok":true}} suffix'),
25 ).toBe('{"message":"literal } brace","nested":{"ok":true}}');
26 });
27​
28 it("returns trimmed input when no JSON object starts", () => {
29 expect(extractJsonObject(" no structured output ")).toBe("no structured output");
30 });
31});