| @@ -1,10 +1,102 @@ |
| 1 | 1 | package auth |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 4 | 7 | "testing" |
| 5 | 8 | "time" |
| 9 | + |
| 10 | + internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 11 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" |
| 12 | + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" |
| 6 | 13 | ) |
| 7 | 14 | |
| 15 | +type antigravityCreditsFallbackExecutor struct { |
| 16 | + streamCreditsRequested []bool |
| 17 | +} |
| 18 | + |
| 19 | +func (e *antigravityCreditsFallbackExecutor) Identifier() string { return "antigravity" } |
| 20 | + |
| 21 | +func (e *antigravityCreditsFallbackExecutor) Execute(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { |
| 22 | + return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusNotImplemented, Message: "Execute not implemented"} |
| 23 | +} |
| 24 | + |
| 25 | +func (e *antigravityCreditsFallbackExecutor) ExecuteStream(ctx context.Context, _ *Auth, req cliproxyexecutor.Request, _ cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { |
| 26 | + creditsRequested := AntigravityCreditsRequested(ctx) |
| 27 | + e.streamCreditsRequested = append(e.streamCreditsRequested, creditsRequested) |
| 28 | + ch := make(chan cliproxyexecutor.StreamChunk, 1) |
| 29 | + if !creditsRequested { |
| 30 | + ch <- cliproxyexecutor.StreamChunk{Err: &Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota exhausted"}} |
| 31 | + close(ch) |
| 32 | + return &cliproxyexecutor.StreamResult{Headers: http.Header{"X-Initial": {req.Model}}, Chunks: ch}, nil |
| 33 | + } |
| 34 | + ch <- cliproxyexecutor.StreamChunk{Payload: []byte("credits fallback")} |
| 35 | + close(ch) |
| 36 | + return &cliproxyexecutor.StreamResult{Headers: http.Header{"X-Credits": {req.Model}}, Chunks: ch}, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (e *antigravityCreditsFallbackExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) { |
| 40 | + return auth, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (e *antigravityCreditsFallbackExecutor) CountTokens(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { |
| 44 | + return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusNotImplemented, Message: "CountTokens not implemented"} |
| 45 | +} |
| 46 | + |
| 47 | +func (e *antigravityCreditsFallbackExecutor) HttpRequest(context.Context, *Auth, *http.Request) (*http.Response, error) { |
| 48 | + return nil, &Error{HTTPStatus: http.StatusNotImplemented, Message: "HttpRequest not implemented"} |
| 49 | +} |
| 50 | + |
| 51 | +func TestManagerExecuteStream_AntigravityCreditsFallbackAfterBootstrap429(t *testing.T) { |
| 52 | + const model = "claude-opus-4-6-thinking" |
| 53 | + executor := &antigravityCreditsFallbackExecutor{} |
| 54 | + manager := NewManager(nil, nil, nil) |
| 55 | + manager.SetConfig(&internalconfig.Config{ |
| 56 | + QuotaExceeded: internalconfig.QuotaExceeded{AntigravityCredits: true}, |
| 57 | + }) |
| 58 | + manager.RegisterExecutor(executor) |
| 59 | + registry.GetGlobalRegistry().RegisterClient("ag-credits", "antigravity", []*registry.ModelInfo{{ID: model}}) |
| 60 | + t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient("ag-credits") }) |
| 61 | + if _, errRegister := manager.Register(context.Background(), &Auth{ID: "ag-credits", Provider: "antigravity"}); errRegister != nil { |
| 62 | + t.Fatalf("register auth: %v", errRegister) |
| 63 | + } |
| 64 | + |
| 65 | + streamResult, errExecute := manager.ExecuteStream(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) |
| 66 | + if errExecute != nil { |
| 67 | + t.Fatalf("execute stream: %v", errExecute) |
| 68 | + } |
| 69 | + |
| 70 | + var payload []byte |
| 71 | + for chunk := range streamResult.Chunks { |
| 72 | + if chunk.Err != nil { |
| 73 | + t.Fatalf("unexpected stream error: %v", chunk.Err) |
| 74 | + } |
| 75 | + payload = append(payload, chunk.Payload...) |
| 76 | + } |
| 77 | + if string(payload) != "credits fallback" { |
| 78 | + t.Fatalf("payload = %q, want %q", string(payload), "credits fallback") |
| 79 | + } |
| 80 | + if got := streamResult.Headers.Get("X-Credits"); got != model { |
| 81 | + t.Fatalf("X-Credits header = %q, want routed model", got) |
| 82 | + } |
| 83 | + if len(executor.streamCreditsRequested) != 2 { |
| 84 | + t.Fatalf("stream calls = %d, want 2", len(executor.streamCreditsRequested)) |
| 85 | + } |
| 86 | + if executor.streamCreditsRequested[0] || !executor.streamCreditsRequested[1] { |
| 87 | + t.Fatalf("credits flags = %v, want [false true]", executor.streamCreditsRequested) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestStatusCodeFromError_UnwrapsStreamBootstrap429(t *testing.T) { |
| 92 | + bootstrapErr := newStreamBootstrapError(&Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota exhausted"}, nil) |
| 93 | + wrappedErr := fmt.Errorf("conductor stream failed: %w", bootstrapErr) |
| 94 | + |
| 95 | + if status := statusCodeFromError(wrappedErr); status != http.StatusTooManyRequests { |
| 96 | + t.Fatalf("statusCodeFromError() = %d, want %d", status, http.StatusTooManyRequests) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 8 | 100 | func TestIsAuthBlockedForModel_ClaudeWithCreditsStillBlockedDuringCooldown(t *testing.T) { |
| 9 | 101 | auth := &Auth{ |
| 10 | 102 | ID: "ag-1", |