|
5 | 5 | "errors" |
6 | 6 | "net/http" |
7 | 7 | "testing" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" |
10 | 11 | cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" |
@@ -36,6 +37,59 @@ func (e schedulerProviderTestExecutor) HttpRequest(ctx context.Context, auth *Au |
36 | 37 | return nil, nil |
37 | 38 | } |
38 | 39 |
|
| 40 | +type unauthorizedRefreshTestExecutor struct { |
| 41 | + schedulerProviderTestExecutor |
| 42 | +} |
| 43 | + |
| 44 | +func (e unauthorizedRefreshTestExecutor) Refresh(ctx context.Context, auth *Auth) (*Auth, error) { |
| 45 | + return nil, errors.New("token refresh failed with status 401: invalid_grant") |
| 46 | +} |
| 47 | + |
| 48 | +func TestManager_RefreshAuthUnauthorizedFailureStopsAutoRefreshRetry(t *testing.T) { |
| 49 | + ctx := context.Background() |
| 50 | + manager := NewManager(nil, &RoundRobinSelector{}, nil) |
| 51 | + manager.RegisterExecutor(unauthorizedRefreshTestExecutor{ |
| 52 | + schedulerProviderTestExecutor: schedulerProviderTestExecutor{provider: "codex"}, |
| 53 | + }) |
| 54 | + |
| 55 | + auth := &Auth{ |
| 56 | + ID: "unauthorized-refresh", |
| 57 | + Provider: "codex", |
| 58 | + Metadata: map[string]any{ |
| 59 | + "email": "x@example.com", |
| 60 | + }, |
| 61 | + } |
| 62 | + if _, errRegister := manager.Register(ctx, auth); errRegister != nil { |
| 63 | + t.Fatalf("register auth: %v", errRegister) |
| 64 | + } |
| 65 | + |
| 66 | + manager.refreshAuth(ctx, auth.ID) |
| 67 | + |
| 68 | + updated, ok := manager.GetByID(auth.ID) |
| 69 | + if !ok { |
| 70 | + t.Fatalf("expected auth %q after refresh", auth.ID) |
| 71 | + } |
| 72 | + if updated.LastError == nil { |
| 73 | + t.Fatal("expected unauthorized refresh failure to be recorded") |
| 74 | + } |
| 75 | + if got := updated.LastError.StatusCode(); got != http.StatusUnauthorized { |
| 76 | + t.Fatalf("LastError.StatusCode() = %d, want %d", got, http.StatusUnauthorized) |
| 77 | + } |
| 78 | + if updated.LastError.Code != "unauthorized" { |
| 79 | + t.Fatalf("LastError.Code = %q, want unauthorized", updated.LastError.Code) |
| 80 | + } |
| 81 | + if !updated.NextRefreshAfter.IsZero() { |
| 82 | + t.Fatalf("NextRefreshAfter = %s, want zero for unauthorized refresh failure", updated.NextRefreshAfter) |
| 83 | + } |
| 84 | + now := time.Now() |
| 85 | + if manager.shouldRefresh(updated, now) { |
| 86 | + t.Fatal("expected unauthorized auth to stop refresh attempts") |
| 87 | + } |
| 88 | + if _, shouldSchedule := nextRefreshCheckAt(now, updated, time.Second); shouldSchedule { |
| 89 | + t.Fatal("expected unauthorized auth to be removed from the auto-refresh schedule") |
| 90 | + } |
| 91 | +} |
| 92 | + |
39 | 93 | func TestManager_RefreshSchedulerEntry_RebuildsSupportedModelSetAfterModelRegistration(t *testing.T) { |
40 | 94 | ctx := context.Background() |
41 | 95 |
|
|
0 commit comments