Open History to see the last commit
1import * as Cause from "effect/Cause";
2import * as Exit from "effect/Exit";
3import * as Result from "effect/Result";
4import * as Schema from "effect/Schema";
5import { describe, expect, it } from "vite-plus/test";
6
7import {
8 decodeJsonResult,
9 extractJsonObject,
10 formatSchemaError,
11 fromLenientJson,
12} from "./schemaJson.ts";
13
14const decodeLenientJson = Schema.decodeUnknownSync(fromLenientJson(Schema.Unknown));
15
16describe("schemaJson helpers", () => {
17 it("extracts a balanced JSON object from surrounding text", () => {
18 expect(
19 extractJsonObject(`Sure, here is the JSON:
20\`\`\`json
21{
22 "subject": "Update README",
23 "body": ""
24}
25\`\`\`
26Done.`),
27 ).toBe(`{
28 "subject": "Update README",
29 "body": ""
30}`);
31 });
32
33 it("ignores braces inside strings while finding the object boundary", () => {
34 expect(
35 extractJsonObject('prefix {"message":"literal } brace","nested":{"ok":true}} suffix'),
36 ).toBe('{"message":"literal } brace","nested":{"ok":true}}');
37 });
38
39 it("decodes JSON with comments and trailing commas", () => {
40 expect(
41 decodeLenientJson(`{
42 // Comments are valid in settings files.
43 "enabled": /* See "https://example.com/config" // still in the block */ true,
44 "values": [1, 2,],
45 }`),
46 ).toEqual({
47 enabled: true,
48 values: [1, 2],
49 });
50 });
51
52 it("preserves comment markers and commas before brackets inside string values", () => {
53 expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
54 expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
55 expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
56 expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
57 });
58
59 it("formats schema failures with paths without exposing invalid values", () => {
60 const decodeCredential = decodeJsonResult(Schema.Struct({ token: Schema.Number }));
61 const decoded = decodeCredential('{"token":"credential=secret-value"}');
62
63 expect(Result.isFailure(decoded)).toBe(true);
64 if (Result.isFailure(decoded)) {
65 expect(formatSchemaError(decoded.failure)).toBe('Invalid type\n at ["token"]');
66 }
67 });
68
69 it("does not expose malformed lenient JSON input in diagnostics", () => {
70 const decode = Schema.decodeUnknownExit(fromLenientJson(Schema.Unknown));
71 const exit = decode('{"token":"credential=secret-value",,}');
72
73 expect(Exit.isFailure(exit)).toBe(true);
74 if (Exit.isFailure(exit)) {
75 const diagnostic = formatSchemaError(exit.cause);
76 expect(diagnostic).toBe("Invalid value");
77 expect(diagnostic).not.toContain("credential=secret-value");
78 }
79 });
80
81 it("summarizes unexpected defects without serializing their messages", () => {
82 const diagnostic = formatSchemaError(Cause.die(new Error("credential=secret-value")));
83
84 expect(diagnostic).toBe(
85 "Schema validation failed (failureCount=0, defectCount=1, interruptionCount=0).",
86 );
87 });
88
89 it("bounds the number of formatted schema issues", () => {
90 const decode = decodeJsonResult(Schema.Struct({ token: Schema.Number }));
91 const failures: Array<Cause.Cause<Schema.SchemaError>> = [];
92 for (let index = 0; index < 10; index += 1) {
93 const decoded = decode(`{"token":"credential=secret-value-${index}"}`);
94 if (Result.isFailure(decoded)) {
95 failures.push(decoded.failure);
96 }
97 }
98
99 const cause = Cause.fromReasons(failures.flatMap((cause) => cause.reasons));
100 const diagnostic = formatSchemaError(cause);
101 expect(diagnostic.match(/Invalid type/g)).toHaveLength(8);
102 expect(diagnostic).toContain("... and 2 more issue(s)");
103 });
104});