Files
coder/aibridge/intercept/openai_errors_test.go
T
Susana Ferreira fec21e1a28 refactor(aibridge): apply key pool failover follow-ups (#26130)
Applies follow-ups from the key pool failover work:

- Add a test verifying key pool state is shared across bridged and passthrough routes.
- Refactor the key failover and passthrough tests to use the shared `MockUpstream` helper.
- Simplify how the request body option is passed through the Anthropic messages interceptor.
- Make `ResponseErrorFromKeyPool` nil-safe and cover it with a test.

Closes: https://linear.app/codercom/issue/AIGOV-398/small-follow-up-cleanups-for-key-failover

> [!NOTE]
> Initially generated by Claude Opus 4.7, modified and reviewed by @ssncferreira
2026-06-19 09:01:39 +01:00

64 lines
1.6 KiB
Go

package intercept_test
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/aibridge/intercept"
"github.com/coder/coder/v2/aibridge/keypool"
)
func TestResponseErrorFromKeyPool(t *testing.T) {
t.Parallel()
tests := []struct {
name string
keyPoolErr *keypool.Error
expectedStatus int
expectedRetryAfter time.Duration
}{
{
name: "nil_returns_nil",
keyPoolErr: nil,
},
{
// Rate-limited with no cooldown: 429, no Retry-After.
name: "rate_limited_zero_retry_after",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindRateLimited},
expectedStatus: http.StatusTooManyRequests,
expectedRetryAfter: 0,
},
{
// Rate-limited with cooldown: 429, Retry-After set.
name: "rate_limited_with_retry_after",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindRateLimited, RetryAfter: 5 * time.Second},
expectedStatus: http.StatusTooManyRequests,
expectedRetryAfter: 5 * time.Second,
},
{
// Permanent: 502 api_error.
name: "permanent_returns_502",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindPermanent},
expectedStatus: http.StatusBadGateway,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := intercept.ResponseErrorFromKeyPool(tc.keyPoolErr)
if tc.keyPoolErr == nil {
assert.Nil(t, got)
return
}
require.NotNil(t, got)
assert.Equal(t, tc.expectedStatus, got.StatusCode)
assert.Equal(t, tc.expectedRetryAfter, got.RetryAfter)
})
}
}