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

schemaJson.test.ts

51 lines1.4 KB

1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vitest";
3​
4import { extractJsonObject, fromLenientJson } from "./schemaJson.ts";
5​
6const decodeLenientJson = Schema.decodeUnknownSync(fromLenientJson(Schema.Unknown));
7​
8describe("schemaJson helpers", () => {
9 it("extracts a balanced JSON object from surrounding text", () => {
10 expect(
11 extractJsonObject(`Sure, here is the JSON:
12\`\`\`json
14 "subject": "Update README",
15 "body": ""
17\`\`\`
18Done.`),
19 ).toBe(`{
20 "subject": "Update README",
21 "body": ""
22}`);
23 });
24​
25 it("ignores braces inside strings while finding the object boundary", () => {
26 expect(
27 extractJsonObject('prefix {"message":"literal } brace","nested":{"ok":true}} suffix'),
28 ).toBe('{"message":"literal } brace","nested":{"ok":true}}');
29 });
30​
31 it("returns trimmed input when no JSON object starts", () => {
32 expect(extractJsonObject(" no structured output ")).toBe("no structured output");
33 });
34​
35 it("decodes JSON with comments and trailing commas", () => {
36 expect(
37 decodeLenientJson(`{
38 // Comments are valid in settings files.
39 "enabled": true,
40 "values": [1, 2,],
41 }`),
42 ).toEqual({
43 enabled: true,
44 values: [1, 2],
45 });
46 });
47​
48 it("rejects malformed JSON after lenient preprocessing", () => {
49 expect(() => decodeLenientJson('{ "enabled": true,, }')).toThrow();
50 });
51});