server-errors.test.ts 2.1 KB

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