error.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, test } from "bun:test"
  2. import { AccountTransportError } from "../../src/account/schema"
  3. import { FormatError } from "../../src/cli/error"
  4. import { UI } from "../../src/cli/ui"
  5. describe("cli.error", () => {
  6. test("formats legacy and tagged config errors the same way", () => {
  7. const cases = [
  8. {
  9. tag: "ConfigJsonError",
  10. data: { path: "/tmp/kirincode.jsonc", message: "Unexpected token" },
  11. expected: "Config file at /tmp/kirincode.jsonc is not valid JSON(C): Unexpected token",
  12. },
  13. {
  14. tag: "ConfigDirectoryTypoError",
  15. data: { path: "/tmp/kirincode.jsonc", dir: ".kirincode", suggestion: "kirincode" },
  16. expected:
  17. 'Directory ".kirincode" in /tmp/kirincode.jsonc is not valid. Rename the directory to "kirincode" or remove it. This is a common typo.',
  18. },
  19. {
  20. tag: "ConfigFrontmatterError",
  21. data: { path: "/tmp/AGENTS.md", message: "failed frontmatter" },
  22. expected: "failed frontmatter",
  23. },
  24. {
  25. tag: "ConfigInvalidError",
  26. data: {
  27. path: "/tmp/kirincode.jsonc",
  28. message: "schema mismatch",
  29. issues: [{ message: "Expected string", path: ["provider", "id"] }],
  30. },
  31. expected: "Configuration is invalid at /tmp/kirincode.jsonc: schema mismatch\n↳ Expected string provider.id",
  32. },
  33. ]
  34. for (const item of cases) {
  35. expect(FormatError({ name: item.tag, data: item.data })).toBe(item.expected)
  36. expect(FormatError({ _tag: item.tag, ...item.data })).toBe(item.expected)
  37. }
  38. })
  39. test("preserves multiline JSONC diagnostics for tagged config errors", () => {
  40. const data = {
  41. path: "/tmp/kirincode.jsonc",
  42. message:
  43. '\n--- JSONC Input ---\n{\n "model": \n}\n--- Errors ---\nValueExpected at line 3, column 1\n Line 3: }\n ^\n--- End ---',
  44. }
  45. const expected = `Config file at ${data.path} is not valid JSON(C): ${data.message}`
  46. expect(FormatError({ name: "ConfigJsonError", data })).toBe(expected)
  47. expect(FormatError({ _tag: "ConfigJsonError", ...data })).toBe(expected)
  48. })
  49. test("formats account transport errors clearly", () => {
  50. const error = new AccountTransportError({
  51. method: "POST",
  52. url: "https://console.kirincode.ai/auth/device/code",
  53. })
  54. const formatted = FormatError(error)
  55. expect(formatted).toContain("Could not reach POST https://console.kirincode.ai/auth/device/code.")
  56. expect(formatted).toContain("This failed before the server returned an HTTP response.")
  57. expect(formatted).toContain("Check your network, proxy, or VPN configuration and try again.")
  58. })
  59. test("formats legacy and tagged provider model errors the same way", () => {
  60. const data = {
  61. providerID: "anthropic",
  62. modelID: "claude-sonet-4",
  63. suggestions: ["claude-sonnet-4"],
  64. }
  65. const expected = [
  66. "Model not found: anthropic/claude-sonet-4",
  67. "Did you mean: claude-sonnet-4",
  68. "Try: `opencode models` to list available models",
  69. "Or check your config (kirincode.json) provider/model names",
  70. ].join("\n")
  71. expect(FormatError({ name: "ProviderModelNotFoundError", data })).toBe(expected)
  72. expect(FormatError({ _tag: "ProviderModelNotFoundError", ...data })).toBe(expected)
  73. })
  74. test("formats legacy and tagged provider init errors the same way", () => {
  75. const data = { providerID: "anthropic" }
  76. const expected = 'Failed to initialize provider "anthropic". Check credentials and configuration.'
  77. expect(FormatError({ name: "ProviderInitError", data })).toBe(expected)
  78. expect(FormatError({ _tag: "ProviderInitError", ...data })).toBe(expected)
  79. })
  80. test("formats cancelled UI errors as empty output", () => {
  81. expect(FormatError(new UI.CancelledError())).toBe("")
  82. })
  83. })