|
| 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 | +}) |
0 commit comments