Skip to content

Commit 808c12c

Browse files
committed
test: CLI error formatting + YAML frontmatter sanitization (31 new tests)
Cover two zero-test-coverage areas that produce user-facing output: FormatError/FormatUnknownError (error messages) and ConfigMarkdown.fallbackSanitization (messy YAML from other AI tools). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> https://claude.ai/code/session_01KYDdfMxVRzntwFi66wMnEr
1 parent 4c01aac commit 808c12c

2 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { FormatError, FormatUnknownError } from "../../src/cli/error"
3+
4+
describe("FormatError: known error types", () => {
5+
test("MCP.Failed returns helpful message", () => {
6+
const err = { name: "MCPFailed", data: { name: "my-server" } }
7+
const result = FormatError(err)
8+
expect(result).toContain("my-server")
9+
expect(result).toContain("failed")
10+
})
11+
12+
test("Provider.ModelNotFoundError with suggestions", () => {
13+
const err = {
14+
name: "ProviderModelNotFoundError",
15+
data: { providerID: "openai", modelID: "gpt-5", suggestions: ["gpt-4", "gpt-4o"] },
16+
}
17+
const result = FormatError(err)
18+
expect(result).toContain("gpt-5")
19+
expect(result).toContain("Did you mean")
20+
expect(result).toContain("gpt-4")
21+
})
22+
23+
test("Provider.ModelNotFoundError without suggestions", () => {
24+
const err = {
25+
name: "ProviderModelNotFoundError",
26+
data: { providerID: "openai", modelID: "gpt-5", suggestions: [] },
27+
}
28+
const result = FormatError(err)
29+
expect(result).toContain("gpt-5")
30+
expect(result).not.toContain("Did you mean")
31+
})
32+
33+
test("Provider.InitError returns provider name", () => {
34+
const err = { name: "ProviderInitError", data: { providerID: "anthropic" } }
35+
const result = FormatError(err)
36+
expect(result).toContain("anthropic")
37+
})
38+
39+
test("Config.JsonError with message", () => {
40+
const err = { name: "ConfigJsonError", data: { path: "/home/user/.config/altimate.json", message: "Unexpected token" } }
41+
const result = FormatError(err)
42+
expect(result).toContain("altimate.json")
43+
expect(result).toContain("Unexpected token")
44+
})
45+
46+
test("Config.JsonError without message", () => {
47+
const err = { name: "ConfigJsonError", data: { path: "/path/to/config.json" } }
48+
const result = FormatError(err)
49+
expect(result).toContain("config.json")
50+
expect(result).toContain("not valid JSON")
51+
})
52+
53+
test("Config.ConfigDirectoryTypoError", () => {
54+
const err = {
55+
name: "ConfigDirectoryTypoError",
56+
data: { dir: ".openCode", path: "/project/.openCode", suggestion: ".opencode" },
57+
}
58+
const result = FormatError(err)
59+
expect(result).toContain(".openCode")
60+
expect(result).toContain(".opencode")
61+
expect(result).toContain("typo")
62+
})
63+
64+
test("ConfigMarkdown.FrontmatterError", () => {
65+
const err = {
66+
name: "ConfigFrontmatterError",
67+
data: { path: "CLAUDE.md", message: "CLAUDE.md: Failed to parse YAML frontmatter: invalid key" },
68+
}
69+
const result = FormatError(err)
70+
expect(result).toContain("Failed to parse")
71+
})
72+
73+
test("Config.InvalidError with issues", () => {
74+
const err = {
75+
name: "ConfigInvalidError",
76+
data: {
77+
path: "provider.model",
78+
message: "Invalid model",
79+
issues: [{ message: "must be string", path: ["provider", "model"] }],
80+
},
81+
}
82+
const result = FormatError(err)
83+
expect(result).toContain("provider.model")
84+
expect(result).toContain("must be string")
85+
})
86+
87+
test("Config.InvalidError without path shows generic header", () => {
88+
const err = {
89+
name: "ConfigInvalidError",
90+
data: { path: "config", issues: [] },
91+
}
92+
const result = FormatError(err)
93+
expect(result).toContain("Configuration is invalid")
94+
// "config" path should not appear as a location qualifier
95+
expect(result).not.toContain("at config")
96+
})
97+
98+
test("UI.CancelledError returns empty string", () => {
99+
const err = { name: "UICancelledError" }
100+
const result = FormatError(err)
101+
expect(result).toBe("")
102+
})
103+
104+
test("unknown error returns undefined", () => {
105+
const err = new Error("random error")
106+
expect(FormatError(err)).toBeUndefined()
107+
})
108+
109+
test("null input returns undefined", () => {
110+
expect(FormatError(null)).toBeUndefined()
111+
})
112+
})
113+
114+
describe("FormatUnknownError", () => {
115+
test("Error with stack returns stack", () => {
116+
const err = new Error("boom")
117+
const result = FormatUnknownError(err)
118+
expect(result).toContain("boom")
119+
expect(result).toContain("Error")
120+
})
121+
122+
test("Error without stack returns name + message", () => {
123+
const err = new Error("boom")
124+
err.stack = undefined
125+
const result = FormatUnknownError(err)
126+
expect(result).toBe("Error: boom")
127+
})
128+
129+
test("plain object is JSON stringified", () => {
130+
const result = FormatUnknownError({ code: 42, msg: "fail" })
131+
expect(result).toContain('"code": 42')
132+
expect(result).toContain('"msg": "fail"')
133+
})
134+
135+
test("circular object returns fallback message", () => {
136+
const obj: any = { a: 1 }
137+
obj.self = obj
138+
const result = FormatUnknownError(obj)
139+
expect(result).toBe("Unexpected error (unserializable)")
140+
})
141+
142+
test("string input returns itself", () => {
143+
expect(FormatUnknownError("something went wrong")).toBe("something went wrong")
144+
})
145+
146+
test("number input returns string representation", () => {
147+
expect(FormatUnknownError(404)).toBe("404")
148+
})
149+
150+
test("undefined returns string 'undefined'", () => {
151+
expect(FormatUnknownError(undefined)).toBe("undefined")
152+
})
153+
154+
test("null returns string 'null'", () => {
155+
expect(FormatUnknownError(null)).toBe("null")
156+
})
157+
})

‎packages/opencode/test/config/markdown.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,78 @@ describe("ConfigMarkdown: frontmatter has weird model id", async () => {
226226
expect(result.content.trim()).toBe("Strictly follow da rules")
227227
})
228228
})
229+
230+
describe("ConfigMarkdown.fallbackSanitization", () => {
231+
test("converts unquoted value with colon to block scalar that gray-matter can parse", async () => {
232+
const input = `---\nurl: https://example.com:8080\n---\nContent`
233+
const result = ConfigMarkdown.fallbackSanitization(input)
234+
expect(result).toContain("url: |-")
235+
expect(result).toContain(" https://example.com:8080")
236+
// Verify the sanitized output is actually parseable
237+
const matter = await import("gray-matter")
238+
const parsed = matter.default(result)
239+
expect(parsed.data.url).toBe("https://example.com:8080")
240+
})
241+
242+
test("preserves already double-quoted values", () => {
243+
const input = `---\nurl: "https://example.com:8080"\n---\nContent`
244+
const result = ConfigMarkdown.fallbackSanitization(input)
245+
expect(result).toContain('url: "https://example.com:8080"')
246+
})
247+
248+
test("preserves already single-quoted values", () => {
249+
const input = `---\nurl: 'https://example.com:8080'\n---\nContent`
250+
const result = ConfigMarkdown.fallbackSanitization(input)
251+
expect(result).toContain("url: 'https://example.com:8080'")
252+
})
253+
254+
test("preserves empty values", () => {
255+
const input = `---\nempty:\n---\nContent`
256+
const result = ConfigMarkdown.fallbackSanitization(input)
257+
expect(result).toContain("empty:")
258+
})
259+
260+
test("preserves YAML comments", () => {
261+
const input = `---\n# This is a comment\nkey: value\n---\nContent`
262+
const result = ConfigMarkdown.fallbackSanitization(input)
263+
expect(result).toContain("# This is a comment")
264+
})
265+
266+
test("preserves indented continuation lines", () => {
267+
const input = `---\nlist:\n - item1\n - item2\n---\nContent`
268+
const result = ConfigMarkdown.fallbackSanitization(input)
269+
expect(result).toContain(" - item1")
270+
expect(result).toContain(" - item2")
271+
})
272+
273+
test("returns input unchanged when no frontmatter present", () => {
274+
const input = "Just plain content"
275+
expect(ConfigMarkdown.fallbackSanitization(input)).toBe(input)
276+
})
277+
278+
test("handles multiple values with colons", async () => {
279+
const input = `---\nurl: http://a:1\nmodel: org/repo:tag\n---\nContent`
280+
const result = ConfigMarkdown.fallbackSanitization(input)
281+
expect(result).toContain("url: |-")
282+
expect(result).toContain("model: |-")
283+
// Verify both values parse correctly
284+
const matter = await import("gray-matter")
285+
const parsed = matter.default(result)
286+
expect(parsed.data.url).toBe("http://a:1")
287+
expect(parsed.data.model).toBe("org/repo:tag")
288+
})
289+
290+
test("values without colons are left unchanged", () => {
291+
const input = `---\nname: John\nage: 30\n---\nContent`
292+
const result = ConfigMarkdown.fallbackSanitization(input)
293+
expect(result).toContain("name: John")
294+
expect(result).toContain("age: 30")
295+
})
296+
297+
test("content after frontmatter is not modified", () => {
298+
const input = `---\nkey: val:ue\n---\nurl: https://example.com:8080/path`
299+
const result = ConfigMarkdown.fallbackSanitization(input)
300+
// Body content should be preserved verbatim
301+
expect(result).toContain("url: https://example.com:8080/path")
302+
})
303+
})

0 commit comments

Comments
 (0)