sub2api/backend/internal/service/account_pool_retry_status_codes_test.go
StarryKira 21033dceb9 feat(account): configurable pool-mode same-account retry status codes
Pool mode currently retries the same account for a fixed set of
upstream HTTP statuses: 401, 403, 429. Some upstream pool deployments
also need same-account retry for transient provider/proxy statuses
such as 502, 503, 520, 529, but hard-coding more statuses changes
behavior for everyone.

Add a per-account credentials option `pool_mode_retry_status_codes`
that lets admins choose which upstream HTTP status codes trigger
same-account retry in pool mode:

- Unset (default): preserve the current 401/403/429 default
- Explicit list: override the defaults with the configured codes
- Codes normalized to the 100-599 range, deduplicated, sorted

The standalone `isPoolModeRetryableStatus` helper is kept as the
default-only fallback. All 15 gateway call sites switch to the new
`Account.IsPoolModeRetryableStatus` method so behavior is preserved
for accounts that do not configure the new field.

Frontend admin UI gains a "Retry Status Codes" comma-separated input
under the pool-mode section in both Create/Edit account modals
(en + zh i18n).

Fixes #2731

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 11:24:25 -07:00

194 lines
4.1 KiB
Go

//go:build unit
package service
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetPoolModeRetryStatusCodes(t *testing.T) {
tests := []struct {
name string
account *Account
expected []int
}{
{
name: "nil_account_returns_nil",
account: nil,
expected: nil,
},
{
name: "nil_credentials_returns_nil",
account: &Account{
Type: AccountTypeAPIKey,
Platform: PlatformOpenAI,
},
expected: nil,
},
{
name: "missing_key_returns_nil",
account: &Account{
Type: AccountTypeAPIKey,
Platform: PlatformOpenAI,
Credentials: map[string]any{"pool_mode": true},
},
expected: nil,
},
{
name: "empty_slice_is_preserved",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{},
},
},
expected: []int{},
},
{
name: "float64_values_from_json_are_normalized",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{float64(429), float64(401), float64(403)},
},
},
expected: []int{401, 403, 429},
},
{
name: "json_number_values_supported",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{json.Number("502"), json.Number("503")},
},
},
expected: []int{502, 503},
},
{
name: "string_values_supported",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{"520", "529"},
},
},
expected: []int{520, 529},
},
{
name: "duplicates_are_deduped",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{float64(429), float64(429), float64(401)},
},
},
expected: []int{401, 429},
},
{
name: "out_of_range_values_dropped",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{float64(99), float64(600), float64(429)},
},
},
expected: []int{429},
},
{
name: "invalid_string_dropped",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{"oops", float64(429)},
},
},
expected: []int{429},
},
{
name: "non_array_value_returns_nil",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": "not-an-array",
},
},
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.account.GetPoolModeRetryStatusCodes())
})
}
}
func TestIsPoolModeRetryableStatus_Account(t *testing.T) {
tests := []struct {
name string
account *Account
statusCode int
expected bool
}{
{
name: "nil_account_falls_back_to_default_401",
account: nil,
statusCode: 401,
expected: true,
},
{
name: "nil_account_falls_back_to_default_500",
account: nil,
statusCode: 500,
expected: false,
},
{
name: "unconfigured_uses_default_403",
account: &Account{
Credentials: map[string]any{"pool_mode": true},
},
statusCode: 403,
expected: true,
},
{
name: "unconfigured_uses_default_502_false",
account: &Account{
Credentials: map[string]any{"pool_mode": true},
},
statusCode: 502,
expected: false,
},
{
name: "configured_list_overrides_default_401_dropped",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{float64(502), float64(503)},
},
},
statusCode: 401,
expected: false,
},
{
name: "configured_list_overrides_default_502_added",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{float64(502), float64(503)},
},
},
statusCode: 502,
expected: true,
},
{
name: "empty_list_disables_all_default_codes",
account: &Account{
Credentials: map[string]any{
"pool_mode_retry_status_codes": []any{},
},
},
statusCode: 429,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.account.IsPoolModeRetryableStatus(tt.statusCode))
})
}
}