|
| 1 | +import { test, expect, describe } from "bun:test" |
| 2 | +import { FormatError } from "../../src/cli/error" |
| 3 | +import { Config } from "../../src/config/config" |
| 4 | +import z from "zod" |
| 5 | + |
| 6 | +function parseOrFail(schema: z.ZodType, input: unknown) { |
| 7 | + const result = schema.safeParse(input) |
| 8 | + if (!result.success) return result.error |
| 9 | + throw new Error("expected parse to fail") |
| 10 | +} |
| 11 | + |
| 12 | +describe("FormatError for ConfigInvalidError", () => { |
| 13 | + test("uses tree to show concrete field errors instead of generic message", () => { |
| 14 | + const McpLocal = z.object({ type: z.literal("local"), command: z.string() }).strict() |
| 15 | + const McpRemote = z.object({ type: z.literal("remote"), url: z.string() }).strict() |
| 16 | + const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote]) |
| 17 | + const schema = z.object({ mcp: z.record(z.string(), Mcp) }).strict() |
| 18 | + |
| 19 | + const err = parseOrFail(schema, { mcp: { postgres: { type: "local" } } }) |
| 20 | + const invalid = new Config.InvalidError({ |
| 21 | + path: "/test/opencode.json", |
| 22 | + issues: err.issues, |
| 23 | + tree: z.treeifyError(err), |
| 24 | + }) |
| 25 | + |
| 26 | + const formatted = FormatError(invalid) |
| 27 | + expect(formatted).toContain("Configuration is invalid at /test/opencode.json") |
| 28 | + expect(formatted).toContain("command") |
| 29 | + expect(formatted).not.toContain("Invalid input mcp.postgres") |
| 30 | + }) |
| 31 | + |
| 32 | + test("shows unrecognized keys from tree", () => { |
| 33 | + const schema = z.object({ model: z.string() }).strict() |
| 34 | + const err = parseOrFail(schema, { model: "test", unknown_key: 1 }) |
| 35 | + |
| 36 | + const invalid = new Config.InvalidError({ |
| 37 | + path: "/test/opencode.json", |
| 38 | + issues: err.issues, |
| 39 | + tree: z.treeifyError(err), |
| 40 | + }) |
| 41 | + |
| 42 | + const formatted = FormatError(invalid) |
| 43 | + expect(formatted).toContain("unknown_key") |
| 44 | + }) |
| 45 | + |
| 46 | + test("shows type mismatch details from tree", () => { |
| 47 | + const schema = z.object({ provider: z.object({ api_key: z.string() }).strict() }).strict() |
| 48 | + const err = parseOrFail(schema, { provider: { api_key: 123 } }) |
| 49 | + |
| 50 | + const invalid = new Config.InvalidError({ |
| 51 | + path: "/test/opencode.json", |
| 52 | + issues: err.issues, |
| 53 | + tree: z.treeifyError(err), |
| 54 | + }) |
| 55 | + |
| 56 | + const formatted = FormatError(invalid) |
| 57 | + expect(formatted).toContain("api_key") |
| 58 | + expect(formatted).toContain("string") |
| 59 | + }) |
| 60 | + |
| 61 | + test("falls back to issues when tree is not provided", () => { |
| 62 | + const invalid = new Config.InvalidError({ |
| 63 | + path: "/test/opencode.json", |
| 64 | + issues: [ |
| 65 | + { |
| 66 | + code: "invalid_type", |
| 67 | + expected: "string", |
| 68 | + path: ["mcp", "postgres", "command"], |
| 69 | + message: "Required", |
| 70 | + } as any, |
| 71 | + ], |
| 72 | + }) |
| 73 | + |
| 74 | + const formatted = FormatError(invalid) |
| 75 | + expect(formatted).toContain("Configuration is invalid at /test/opencode.json") |
| 76 | + expect(formatted).toContain("mcp.postgres.command") |
| 77 | + }) |
| 78 | + |
| 79 | + test("formats deeply nested errors", () => { |
| 80 | + const schema = z |
| 81 | + .object({ |
| 82 | + provider: z |
| 83 | + .object({ |
| 84 | + settings: z |
| 85 | + .object({ |
| 86 | + timeout: z.number(), |
| 87 | + }) |
| 88 | + .strict(), |
| 89 | + }) |
| 90 | + .strict(), |
| 91 | + }) |
| 92 | + .strict() |
| 93 | + |
| 94 | + const err = parseOrFail(schema, { provider: { settings: { timeout: "not a number" } } }) |
| 95 | + |
| 96 | + const invalid = new Config.InvalidError({ |
| 97 | + path: "/test/opencode.json", |
| 98 | + issues: err.issues, |
| 99 | + tree: z.treeifyError(err), |
| 100 | + }) |
| 101 | + |
| 102 | + const formatted = FormatError(invalid) |
| 103 | + expect(formatted).toContain("provider.settings.timeout") |
| 104 | + }) |
| 105 | +}) |
0 commit comments