|
|
@@ -1,8 +1,37 @@
|
|
|
import { describe, expect, test } from "bun:test"
|
|
|
-import type { ConfigInvalidError } from "./server-errors"
|
|
|
-import { formatServerError, parseReabaleConfigInvalidError } from "./server-errors"
|
|
|
+import type { ConfigInvalidError, ProviderModelNotFoundError } from "./server-errors"
|
|
|
+import { formatServerError, parseReadableConfigInvalidError } from "./server-errors"
|
|
|
|
|
|
-describe("parseReabaleConfigInvalidError", () => {
|
|
|
+function fill(text: string, vars?: Record<string, string | number>) {
|
|
|
+ if (!vars) return text
|
|
|
+ return text.replace(/{{\s*(\w+)\s*}}/g, (_, key: string) => {
|
|
|
+ const value = vars[key]
|
|
|
+ if (value === undefined) return ""
|
|
|
+ return String(value)
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function useLanguageMock() {
|
|
|
+ const dict: Record<string, string> = {
|
|
|
+ "error.chain.unknown": "Erro desconhecido",
|
|
|
+ "error.chain.configInvalid": "Arquivo de config em {{path}} invalido",
|
|
|
+ "error.chain.configInvalidWithMessage": "Arquivo de config em {{path}} invalido: {{message}}",
|
|
|
+ "error.chain.modelNotFound": "Modelo nao encontrado: {{provider}}/{{model}}",
|
|
|
+ "error.chain.didYouMean": "Voce quis dizer: {{suggestions}}",
|
|
|
+ "error.chain.checkConfig": "Revise provider/model no config",
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ t(key: string, vars?: Record<string, string | number>) {
|
|
|
+ const text = dict[key]
|
|
|
+ if (!text) return key
|
|
|
+ return fill(text, vars)
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const language = useLanguageMock()
|
|
|
+
|
|
|
+describe("parseReadableConfigInvalidError", () => {
|
|
|
test("formats issues with file path", () => {
|
|
|
const error = {
|
|
|
name: "ConfigInvalidError",
|
|
|
@@ -15,10 +44,10 @@ describe("parseReabaleConfigInvalidError", () => {
|
|
|
},
|
|
|
} satisfies ConfigInvalidError
|
|
|
|
|
|
- const result = parseReabaleConfigInvalidError(error)
|
|
|
+ const result = parseReadableConfigInvalidError(error, language.t)
|
|
|
|
|
|
expect(result).toBe(
|
|
|
- ["Invalid configuration", "opencode.config.ts", "settings.host: Required", "mode: Invalid"].join("\n"),
|
|
|
+ ["Arquivo de config em opencode.config.ts invalido: settings.host: Required", "mode: Invalid"].join("\n"),
|
|
|
)
|
|
|
})
|
|
|
|
|
|
@@ -31,9 +60,9 @@ describe("parseReabaleConfigInvalidError", () => {
|
|
|
},
|
|
|
} satisfies ConfigInvalidError
|
|
|
|
|
|
- const result = parseReabaleConfigInvalidError(error)
|
|
|
+ const result = parseReadableConfigInvalidError(error, language.t)
|
|
|
|
|
|
- expect(result).toBe(["Invalid configuration", "Bad value"].join("\n"))
|
|
|
+ expect(result).toBe("Arquivo de config em config invalido: Bad value")
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -46,24 +75,57 @@ describe("formatServerError", () => {
|
|
|
},
|
|
|
} satisfies ConfigInvalidError
|
|
|
|
|
|
- const result = formatServerError(error)
|
|
|
+ const result = formatServerError(error, language.t)
|
|
|
|
|
|
- expect(result).toBe(["Invalid configuration", "Missing host"].join("\n"))
|
|
|
+ expect(result).toBe("Arquivo de config em config invalido: Missing host")
|
|
|
})
|
|
|
|
|
|
test("returns error messages", () => {
|
|
|
- expect(formatServerError(new Error("Request failed with status 503"))).toBe("Request failed with status 503")
|
|
|
+ expect(formatServerError(new Error("Request failed with status 503"), language.t)).toBe(
|
|
|
+ "Request failed with status 503",
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
test("returns provided string errors", () => {
|
|
|
- expect(formatServerError("Failed to connect to server")).toBe("Failed to connect to server")
|
|
|
+ expect(formatServerError("Failed to connect to server", language.t)).toBe("Failed to connect to server")
|
|
|
})
|
|
|
|
|
|
- test("falls back to unknown", () => {
|
|
|
- expect(formatServerError(0)).toBe("Unknown error")
|
|
|
+ test("uses translated unknown fallback", () => {
|
|
|
+ expect(formatServerError(0, language.t)).toBe("Erro desconhecido")
|
|
|
})
|
|
|
|
|
|
test("falls back for unknown error objects and names", () => {
|
|
|
- expect(formatServerError({ name: "ServerTimeoutError", data: { seconds: 30 } })).toBe("Unknown error")
|
|
|
+ expect(formatServerError({ name: "ServerTimeoutError", data: { seconds: 30 } }, language.t)).toBe(
|
|
|
+ "Erro desconhecido",
|
|
|
+ )
|
|
|
+ })
|
|
|
+
|
|
|
+ test("formats provider model errors using provider/model", () => {
|
|
|
+ const error = {
|
|
|
+ name: "ProviderModelNotFoundError",
|
|
|
+ data: {
|
|
|
+ providerID: "openai",
|
|
|
+ modelID: "gpt-4.1",
|
|
|
+ },
|
|
|
+ } satisfies ProviderModelNotFoundError
|
|
|
+
|
|
|
+ expect(formatServerError(error, language.t)).toBe(
|
|
|
+ ["Modelo nao encontrado: openai/gpt-4.1", "Revise provider/model no config"].join("\n"),
|
|
|
+ )
|
|
|
+ })
|
|
|
+
|
|
|
+ test("formats provider model suggestions", () => {
|
|
|
+ const error = {
|
|
|
+ name: "ProviderModelNotFoundError",
|
|
|
+ data: {
|
|
|
+ providerID: "x",
|
|
|
+ modelID: "y",
|
|
|
+ suggestions: ["x/y2", "x/y3"],
|
|
|
+ },
|
|
|
+ } satisfies ProviderModelNotFoundError
|
|
|
+
|
|
|
+ expect(formatServerError(error, language.t)).toBe(
|
|
|
+ ["Modelo nao encontrado: x/y", "Voce quis dizer: x/y2, x/y3", "Revise provider/model no config"].join("\n"),
|
|
|
+ )
|
|
|
})
|
|
|
})
|