11import { describe , expect , test } from "bun:test"
2- import type { ConfigInvalidError } from "./server-errors"
3- import { formatServerError , parseReabaleConfigInvalidError } from "./server-errors"
2+ import type { ConfigInvalidError , ProviderModelNotFoundError } from "./server-errors"
3+ import { formatServerError , parseReadableConfigInvalidError } from "./server-errors"
44
5- describe ( "parseReabaleConfigInvalidError" , ( ) => {
5+ function fill ( text : string , vars ?: Record < string , string | number > ) {
6+ if ( ! vars ) return text
7+ return text . replace ( / { { \s * ( \w + ) \s * } } / g, ( _ , key : string ) => {
8+ const value = vars [ key ]
9+ if ( value === undefined ) return ""
10+ return String ( value )
11+ } )
12+ }
13+
14+ function useLanguageMock ( ) {
15+ const dict : Record < string , string > = {
16+ "error.chain.unknown" : "Erro desconhecido" ,
17+ "error.chain.configInvalid" : "Arquivo de config em {{path}} invalido" ,
18+ "error.chain.configInvalidWithMessage" : "Arquivo de config em {{path}} invalido: {{message}}" ,
19+ "error.chain.modelNotFound" : "Modelo nao encontrado: {{provider}}/{{model}}" ,
20+ "error.chain.didYouMean" : "Voce quis dizer: {{suggestions}}" ,
21+ "error.chain.checkConfig" : "Revise provider/model no config" ,
22+ }
23+ return {
24+ t ( key : string , vars ?: Record < string , string | number > ) {
25+ const text = dict [ key ]
26+ if ( ! text ) return key
27+ return fill ( text , vars )
28+ } ,
29+ }
30+ }
31+
32+ const language = useLanguageMock ( )
33+
34+ describe ( "parseReadableConfigInvalidError" , ( ) => {
635 test ( "formats issues with file path" , ( ) => {
736 const error = {
837 name : "ConfigInvalidError" ,
@@ -15,10 +44,10 @@ describe("parseReabaleConfigInvalidError", () => {
1544 } ,
1645 } satisfies ConfigInvalidError
1746
18- const result = parseReabaleConfigInvalidError ( error )
47+ const result = parseReadableConfigInvalidError ( error , language . t )
1948
2049 expect ( result ) . toBe (
21- [ "Invalid configuration" , " opencode.config.ts" , " settings.host: Required", "mode: Invalid" ] . join ( "\n" ) ,
50+ [ "Arquivo de config em opencode.config.ts invalido: settings.host: Required" , "mode: Invalid" ] . join ( "\n" ) ,
2251 )
2352 } )
2453
@@ -31,9 +60,9 @@ describe("parseReabaleConfigInvalidError", () => {
3160 } ,
3261 } satisfies ConfigInvalidError
3362
34- const result = parseReabaleConfigInvalidError ( error )
63+ const result = parseReadableConfigInvalidError ( error , language . t )
3564
36- expect ( result ) . toBe ( [ "Invalid configuration" , " Bad value"] . join ( "\n" ) )
65+ expect ( result ) . toBe ( "Arquivo de config em config invalido: Bad value")
3766 } )
3867} )
3968
@@ -46,24 +75,57 @@ describe("formatServerError", () => {
4675 } ,
4776 } satisfies ConfigInvalidError
4877
49- const result = formatServerError ( error )
78+ const result = formatServerError ( error , language . t )
5079
51- expect ( result ) . toBe ( [ "Invalid configuration" , " Missing host"] . join ( "\n" ) )
80+ expect ( result ) . toBe ( "Arquivo de config em config invalido: Missing host")
5281 } )
5382
5483 test ( "returns error messages" , ( ) => {
55- expect ( formatServerError ( new Error ( "Request failed with status 503" ) ) ) . toBe ( "Request failed with status 503" )
84+ expect ( formatServerError ( new Error ( "Request failed with status 503" ) , language . t ) ) . toBe (
85+ "Request failed with status 503" ,
86+ )
5687 } )
5788
5889 test ( "returns provided string errors" , ( ) => {
59- expect ( formatServerError ( "Failed to connect to server" ) ) . toBe ( "Failed to connect to server" )
90+ expect ( formatServerError ( "Failed to connect to server" , language . t ) ) . toBe ( "Failed to connect to server" )
6091 } )
6192
62- test ( "falls back to unknown" , ( ) => {
63- expect ( formatServerError ( 0 ) ) . toBe ( "Unknown error " )
93+ test ( "uses translated unknown fallback " , ( ) => {
94+ expect ( formatServerError ( 0 , language . t ) ) . toBe ( "Erro desconhecido " )
6495 } )
6596
6697 test ( "falls back for unknown error objects and names" , ( ) => {
67- expect ( formatServerError ( { name : "ServerTimeoutError" , data : { seconds : 30 } } ) ) . toBe ( "Unknown error" )
98+ expect ( formatServerError ( { name : "ServerTimeoutError" , data : { seconds : 30 } } , language . t ) ) . toBe (
99+ "Erro desconhecido" ,
100+ )
101+ } )
102+
103+ test ( "formats provider model errors using provider/model" , ( ) => {
104+ const error = {
105+ name : "ProviderModelNotFoundError" ,
106+ data : {
107+ providerID : "openai" ,
108+ modelID : "gpt-4.1" ,
109+ } ,
110+ } satisfies ProviderModelNotFoundError
111+
112+ expect ( formatServerError ( error , language . t ) ) . toBe (
113+ [ "Modelo nao encontrado: openai/gpt-4.1" , "Revise provider/model no config" ] . join ( "\n" ) ,
114+ )
115+ } )
116+
117+ test ( "formats provider model suggestions" , ( ) => {
118+ const error = {
119+ name : "ProviderModelNotFoundError" ,
120+ data : {
121+ providerID : "x" ,
122+ modelID : "y" ,
123+ suggestions : [ "x/y2" , "x/y3" ] ,
124+ } ,
125+ } satisfies ProviderModelNotFoundError
126+
127+ expect ( formatServerError ( error , language . t ) ) . toBe (
128+ [ "Modelo nao encontrado: x/y" , "Voce quis dizer: x/y2, x/y3" , "Revise provider/model no config" ] . join ( "\n" ) ,
129+ )
68130 } )
69131} )
0 commit comments