| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/i18n" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestExplainError(t *testing.T) { |
| 14 | if explainError(nil) != nil { |
| 15 | t.Error("nil should stay nil") |
| 16 | } |
| 17 | |
| 18 | bal := explainError(&provider.APIError{Provider: "deepseek", Status: 402, Body: "Insufficient Balance"}) |
| 19 | if !strings.Contains(bal.Error(), i18n.M.ProviderErrInsufficientBalance) || !strings.Contains(bal.Error(), "Insufficient Balance") { |
| 20 | t.Errorf("402 = %q, want the insufficient-balance message plus the provider body", bal.Error()) |
| 21 | } |
| 22 | |
| 23 | auth := explainError(&provider.AuthError{Provider: "deepseek", KeyEnv: "DEEPSEEK_API_KEY", Status: 401}) |
| 24 | if !strings.Contains(auth.Error(), "DEEPSEEK_API_KEY") { |
| 25 | t.Errorf("401 should name the key env: %q", auth.Error()) |
| 26 | } |
| 27 | if !strings.Contains(auth.Error(), i18n.M.ProviderErrAuth) { |
| 28 | t.Errorf("401 without a key should use the missing-key message: %q", auth.Error()) |
| 29 | } |
| 30 | |
| 31 | rejected := explainError(&provider.AuthError{Provider: "mimo", KeyEnv: "MIMO_API_KEY", Status: 401, HasKey: true}) |
| 32 | if !strings.Contains(rejected.Error(), i18n.M.ProviderErrAuthRejected) { |
| 33 | t.Errorf("401 with a key present should use the server-rejected message: %q", rejected.Error()) |
| 34 | } |
| 35 | if !strings.Contains(rejected.Error(), "MIMO_API_KEY") { |
| 36 | t.Errorf("401 should still name the key env: %q", rejected.Error()) |
| 37 | } |
| 38 | |
| 39 | sourced := explainError(&provider.AuthError{Provider: "deepseek", KeyEnv: "DEEPSEEK_API_KEY", KeySource: "project .env", Status: 401, HasKey: true}) |
| 40 | if !strings.Contains(sourced.Error(), "DEEPSEEK_API_KEY from project .env") { |
| 41 | t.Errorf("401 should name the key source: %q", sourced.Error()) |
| 42 | } |
| 43 | |
| 44 | authBody := explainError(&provider.AuthError{Provider: "relay", KeyEnv: "RELAY_API_KEY", Status: 401, HasKey: true, Body: `{"error":{"message":"令牌已过期","type":"new_api_error"}}`}) |
| 45 | for _, want := range []string{i18n.M.ProviderErrAuthRejected, "RELAY_API_KEY", "令牌已过期"} { |
| 46 | if !strings.Contains(authBody.Error(), want) { |
| 47 | t.Errorf("401 with a body = %q, want it to contain %q", authBody.Error(), want) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | authEcho := explainError(&provider.AuthError{Provider: "deepseek", KeyEnv: "DEEPSEEK_API_KEY", Status: 401, HasKey: true, Body: `{"error":{"message":"Authentication Fails, Your api key: ****ae54 is invalid"}}`}) |
| 52 | if !strings.Contains(authEcho.Error(), "Authentication Fails") { |
| 53 | t.Errorf("401 should keep the readable reason, got %q", authEcho.Error()) |
| 54 | } |
| 55 | if strings.Contains(authEcho.Error(), "ae54") { |
| 56 | t.Errorf("401 must not surface the masked key tail, got %q", authEcho.Error()) |
| 57 | } |
| 58 | |
| 59 | for _, status := range []int{400, 422, 429, 500, 503} { |
| 60 | got := explainError(&provider.APIError{Provider: "p", Status: status}) |
| 61 | if got.Error() == "" || got.Error() == (&provider.APIError{Provider: "p", Status: status}).Error() { |
| 62 | t.Errorf("status %d should map to a localized message, got %q", status, got.Error()) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | jsonBody := explainError(&provider.APIError{Provider: "deepseek", Status: 400, Body: `{"error":{"message":"This model's maximum context length is 65536 tokens.","type":"invalid_request_error"}}`}) |
| 67 | if !strings.Contains(jsonBody.Error(), i18n.M.ProviderErrBadRequest) || !strings.Contains(jsonBody.Error(), "maximum context length") { |
| 68 | t.Errorf("400 should append the provider reason from a JSON body, got %q", jsonBody.Error()) |
| 69 | } |
| 70 | |
| 71 | toolSchema := explainError(&provider.APIError{ |
| 72 | Provider: "mimo", |
| 73 | Status: 400, |
| 74 | Body: `{"error":{"message":"Tool 197 function has invalid 'parameters' schema"}}`, |
| 75 | ToolContext: `Provider tool 197 maps to Reasonix tool "mcp__files__search" (MCP server "files", tool "search").`, |
| 76 | }) |
| 77 | for _, want := range []string{"invalid 'parameters' schema", `MCP server "files"`} { |
| 78 | if !strings.Contains(toolSchema.Error(), want) { |
| 79 | t.Errorf("400 tool schema error = %q, want %q", toolSchema.Error(), want) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | rawBody := explainError(&provider.APIError{Provider: "deepseek", Status: 422, Body: "some unparseable detail"}) |
| 84 | if !strings.Contains(rawBody.Error(), "some unparseable detail") { |
| 85 | t.Errorf("422 should fall back to the raw body, got %q", rawBody.Error()) |
| 86 | } |
| 87 | |
| 88 | miniMaxInput := explainError(&provider.APIError{ |
| 89 | Provider: "custom-m3", |
| 90 | Status: 422, |
| 91 | Body: `{"error":{"message":"input new_sensitive (1026)","code":"1026"}}`, |
| 92 | TraceID: "minimax-trace-123", |
| 93 | }) |
| 94 | for _, want := range []string{i18n.M.ProviderErrInputSensitive, "input new_sensitive", "Trace ID: minimax-trace-123"} { |
| 95 | if !strings.Contains(miniMaxInput.Error(), want) { |
| 96 | t.Errorf("MiniMax 1026 = %q, want %q", miniMaxInput.Error(), want) |
| 97 | } |
| 98 | } |
| 99 | if strings.Contains(miniMaxInput.Error(), i18n.M.ProviderErrUnprocessable) { |
| 100 | t.Errorf("MiniMax 1026 must not use the generic 422 message: %q", miniMaxInput.Error()) |
| 101 | } |
| 102 | |
| 103 | miniMaxOutput := explainError(&provider.APIError{ |
| 104 | Provider: "minimax-cn-api", |
| 105 | Status: 422, |
| 106 | Body: `{"base_resp":{"status_code":1027,"status_msg":"output new_sensitive"}}`, |
| 107 | }) |
| 108 | for _, want := range []string{i18n.M.ProviderErrOutputSensitive, "output new_sensitive"} { |
| 109 | if !strings.Contains(miniMaxOutput.Error(), want) { |
| 110 | t.Errorf("MiniMax 1027 = %q, want %q", miniMaxOutput.Error(), want) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | unrelated1026 := explainError(&provider.APIError{Provider: "other", Status: 422, Body: `{"code":1026,"message":"other meaning"}`}) |
| 115 | if !strings.Contains(unrelated1026.Error(), i18n.M.ProviderErrUnprocessable) { |
| 116 | t.Errorf("another provider's numeric code 1026 must remain generic: %q", unrelated1026.Error()) |
| 117 | } |
| 118 | |
| 119 | rate := explainError(&provider.APIError{Provider: "deepseek", Status: 429, Body: `{"error":{"message":"slow down"}}`}) |
| 120 | if !strings.Contains(rate.Error(), i18n.M.ProviderErrRateLimited) || !strings.Contains(rate.Error(), "slow down") { |
| 121 | t.Errorf("429 should append the provider reason, got %q", rate.Error()) |
| 122 | } |
| 123 | |
| 124 | // Relay gateways (one-api/new-api style) wrap the real failure — dead |
| 125 | // upstream channel, unsupported tools, exhausted quota — in a 5xx JSON |
| 126 | // body; the category line alone made those undiagnosable. |
| 127 | relay := explainError(&provider.APIError{Provider: "relay", Status: 500, Body: `{"error":{"message":"no available channel for model claude-fable-5 in group default","type":"new_api_error"}}`}) |
| 128 | if !strings.Contains(relay.Error(), i18n.M.ProviderErrServer) || !strings.Contains(relay.Error(), "no available channel") { |
| 129 | t.Errorf("500 should append the provider reason from a JSON body, got %q", relay.Error()) |
| 130 | } |
| 131 | |
| 132 | busy := explainError(&provider.APIError{Provider: "relay", Status: 503, Body: "upstream unavailable"}) |
| 133 | if !strings.Contains(busy.Error(), i18n.M.ProviderErrServerBusy) || !strings.Contains(busy.Error(), "upstream unavailable") { |
| 134 | t.Errorf("503 should fall back to the raw body, got %q", busy.Error()) |
| 135 | } |
| 136 | |
| 137 | bare := explainError(&provider.APIError{Provider: "relay", Status: 500}) |
| 138 | if bare.Error() != i18n.M.ProviderErrServer { |
| 139 | t.Errorf("500 without a body = %q, want exactly the localized message", bare.Error()) |
| 140 | } |
| 141 | |
| 142 | interrupted := explainError(&provider.StreamInterruptedError{Err: io.ErrUnexpectedEOF}) |
| 143 | if !strings.Contains(interrupted.Error(), "model stream interrupted") || !strings.Contains(interrupted.Error(), "continue") { |
| 144 | t.Errorf("stream interruption should be actionable, got %q", interrupted.Error()) |
| 145 | } |
| 146 | |
| 147 | disconnected := explainError(io.ErrUnexpectedEOF) |
| 148 | if !strings.Contains(disconnected.Error(), "model stream disconnected") || !strings.Contains(disconnected.Error(), "retry") { |
| 149 | t.Errorf("connection reset should be actionable, got %q", disconnected.Error()) |
| 150 | } |
| 151 | |
| 152 | plain := errors.New("some other failure") |
| 153 | if explainError(plain) != plain { |
| 154 | t.Error("unknown errors should pass through unchanged") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestRedactAuthReason(t *testing.T) { |
| 159 | cases := []struct{ name, in, want string }{ |
| 160 | {"masked tail", "Your api key: ****ae54 is invalid", "Your api key: **** is invalid"}, |
| 161 | {"masked prefix form", "key sk-ab**** was rejected", "key **** was rejected"}, |
| 162 | {"full key echoed by a relay", "Invalid key sk-proj-abc123def456ghi789 provided", "Invalid key **** provided"}, |
| 163 | {"digit-free sk key via secrets.Redact", "api key: sk-proj-abcdefghijklmnop is invalid", "api key: **** is invalid"}, |
| 164 | {"digit-free value after credential word", "api key: relaykey_abcdefghijklmn rejected", "api key: **** rejected"}, |
| 165 | {"bearer value collapses fully", "Bearer abc.def-ghijklmnopqrs rejected", "Bearer **** rejected"}, |
| 166 | {"mixed-case token without context", "rejected AbCdEfGhIjKlMnOpQr", "rejected ****"}, |
| 167 | {"digit-free identifier survives", "code: invalid_authentication_token", "code: invalid_authentication_token"}, |
| 168 | {"all-caps code survives", "code INVALID_AUTHENTICATION_TOKEN", "code INVALID_AUTHENTICATION_TOKEN"}, |
| 169 | {"short tokens survive", "token expired at gateway", "token expired at gateway"}, |
| 170 | {"empty", "", ""}, |
| 171 | } |
| 172 | for _, c := range cases { |
| 173 | if got := redactAuthReason(c.in); got != c.want { |
| 174 | t.Errorf("%s: redactAuthReason(%q) = %q, want %q", c.name, c.in, got, c.want) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 |