|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import type { ConfigInvalidError } from "./server-errors" |
| 3 | +import { formatServerError, parseReabaleConfigInvalidError } from "./server-errors" |
| 4 | + |
| 5 | +describe("parseReabaleConfigInvalidError", () => { |
| 6 | + test("formats issues with file path", () => { |
| 7 | + const error = { |
| 8 | + name: "ConfigInvalidError", |
| 9 | + data: { |
| 10 | + path: "opencode.config.ts", |
| 11 | + issues: [ |
| 12 | + { path: ["settings", "host"], message: "Required" }, |
| 13 | + { path: ["mode"], message: "Invalid" }, |
| 14 | + ], |
| 15 | + }, |
| 16 | + } satisfies ConfigInvalidError |
| 17 | + |
| 18 | + const result = parseReabaleConfigInvalidError(error) |
| 19 | + |
| 20 | + expect(result).toBe( |
| 21 | + ["Invalid configuration", "opencode.config.ts", "settings.host: Required", "mode: Invalid"].join("\n"), |
| 22 | + ) |
| 23 | + }) |
| 24 | + |
| 25 | + test("uses trimmed message when issues are missing", () => { |
| 26 | + const error = { |
| 27 | + name: "ConfigInvalidError", |
| 28 | + data: { |
| 29 | + path: "config", |
| 30 | + message: " Bad value ", |
| 31 | + }, |
| 32 | + } satisfies ConfigInvalidError |
| 33 | + |
| 34 | + const result = parseReabaleConfigInvalidError(error) |
| 35 | + |
| 36 | + expect(result).toBe(["Invalid configuration", "Bad value"].join("\n")) |
| 37 | + }) |
| 38 | +}) |
| 39 | + |
| 40 | +describe("formatServerError", () => { |
| 41 | + test("formats config invalid errors", () => { |
| 42 | + const error = { |
| 43 | + name: "ConfigInvalidError", |
| 44 | + data: { |
| 45 | + message: "Missing host", |
| 46 | + }, |
| 47 | + } satisfies ConfigInvalidError |
| 48 | + |
| 49 | + const result = formatServerError(error) |
| 50 | + |
| 51 | + expect(result).toBe(["Invalid configuration", "Missing host"].join("\n")) |
| 52 | + }) |
| 53 | + |
| 54 | + test("returns error messages", () => { |
| 55 | + expect(formatServerError(new Error("Request failed with status 503"))).toBe("Request failed with status 503") |
| 56 | + }) |
| 57 | + |
| 58 | + test("returns provided string errors", () => { |
| 59 | + expect(formatServerError("Failed to connect to server")).toBe("Failed to connect to server") |
| 60 | + }) |
| 61 | + |
| 62 | + test("falls back to unknown", () => { |
| 63 | + expect(formatServerError(0)).toBe("Unknown error") |
| 64 | + }) |
| 65 | + |
| 66 | + test("falls back for unknown error objects and names", () => { |
| 67 | + expect(formatServerError({ name: "ServerTimeoutError", data: { seconds: 30 } })).toBe("Unknown error") |
| 68 | + }) |
| 69 | +}) |
0 commit comments