diff --git a/internal/provider/balance.go b/internal/provider/balance.go index 4c69be7e0..1b1863db4 100644 --- a/internal/provider/balance.go +++ b/internal/provider/balance.go @@ -926,7 +926,8 @@ func KeyBalances(ctx context.Context) []SubscriptionQuota { if clineKeyCard(j.p) { // ClinePass's limits beside the credits (cline_usage.go) if ws, amount, err := clineKeyUsage(ctx, j.p); err != nil { - q.Error = err.Error() + // a balance read beside limits that weren't is kept + q.Error, q.Balance = err.Error(), amount } else { q.Windows = append(q.Windows, ws...) q.Balance = amount diff --git a/internal/provider/cline_usage.go b/internal/provider/cline_usage.go index 257670257..c5855ca77 100644 --- a/internal/provider/cline_usage.go +++ b/internal/provider/cline_usage.go @@ -80,9 +80,20 @@ func clineKeyUsage(ctx context.Context, p Provider) (windows []QuotaWindow, bala } return nil, "", limErr } + if limErr != nil { + // the credits alone, but the limits failed for a reason other + // than no ClinePass: the card says so beside the balance, rather + // than look like an account without ClinePass (#79) + return nil, balance, fmt.Errorf("%s: %w", clineLimitsUnread, limErr) + } return windows, balance, nil } +// clineLimitsUnread is how a card whose ClinePass limits failed to read +// begins its error, the words the Cline plugin uses too: the GUI says them +// (quotaError) beside the balance read. +const clineLimitsUnread = "ClinePass limits couldn't be read" + // clineCredits is the account's credit balance, {balance} in millionths of // a dollar, at /users/{id}/balance with the id /users/me tells. func clineCredits(ctx context.Context, key string) (string, error) { diff --git a/internal/provider/cline_usage_test.go b/internal/provider/cline_usage_test.go index 28ca9d852..cf5a796ce 100644 --- a/internal/provider/cline_usage_test.go +++ b/internal/provider/cline_usage_test.go @@ -32,7 +32,7 @@ func TestClineKeyUsage(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") - if key != "sk-pass" && key != "sk-free" { + if key != "sk-pass" && key != "sk-free" && key != "sk-down" && key != "sk-denied" && key != "sk-nobal" { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte(`{"success":false,"error":"invalid API key"}`)) return @@ -42,13 +42,24 @@ func TestClineKeyUsage(t *testing.T) { w.Write([]byte(`{"success":true,"data":{"id":"usr_` + key + `","email":"a@b.c"}}`)) case "/api/v1/users/usr_sk-pass/balance": w.Write([]byte(`{"success":true,"data":{"balance":4250000,"userId":"usr_sk-pass"}}`)) - case "/api/v1/users/usr_sk-free/balance": + case "/api/v1/users/usr_sk-free/balance", "/api/v1/users/usr_sk-down/balance", "/api/v1/users/usr_sk-denied/balance": w.Write([]byte(`{"success":true,"data":{"balance":12000000}}`)) + case "/api/v1/users/usr_sk-nobal/balance": + w.WriteHeader(http.StatusBadGateway) case "/api/v1/users/me/plan/usage-limits": - if key == "sk-free" { + switch key { + case "sk-free": w.WriteHeader(http.StatusNotFound) w.Write([]byte(`{"success":false,"error":"no active plan"}`)) return + case "sk-down": + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte(`{"success":false,"error":"try again later"}`)) + return + case "sk-denied": + w.WriteHeader(http.StatusForbidden) + w.Write([]byte(`{"success":false,"error":"forbidden"}`)) + return } w.Write([]byte(`{"success":true,"data":{"limits":[ {"type":"monthly","percentUsed":12.5,"resetsAt":"2026-11-01T00:00:00Z"}, @@ -64,7 +75,8 @@ func TestClineKeyUsage(t *testing.T) { t.Cleanup(func() { clineUsageAPI = old }) if err := Save(Provider{ID: "clinepass", Name: "ClinePass", Preset: "clinepass", Chat: "https://api.cline.bot/api/v1", - Key: "sk-pass", KeyName: "pass", Keys: []KeyAccount{{Name: "free", Key: "sk-free"}, {Name: "bad", Key: "sk-bad"}}}); err != nil { + Key: "sk-pass", KeyName: "pass", Keys: []KeyAccount{{Name: "free", Key: "sk-free"}, {Name: "bad", Key: "sk-bad"}, + {Name: "down", Key: "sk-down"}, {Name: "denied", Key: "sk-denied"}, {Name: "nobal", Key: "sk-nobal"}}}); err != nil { t.Fatal(err) } got := map[string]SubscriptionQuota{} @@ -88,4 +100,16 @@ func TestClineKeyUsage(t *testing.T) { if bad := got["bad"]; !strings.Contains(bad.Error, "401") || bad.Balance != "" { t.Fatalf("a key Cline refuses: %+v", bad) } + // #79: limits that fail for a reason other than no ClinePass (a 404) + // say so beside the balance, rather than look like no ClinePass + for user, code := range map[string]string{"down": "503", "denied": "403"} { + q := got[user] + if !strings.HasPrefix(q.Error, "ClinePass limits couldn't be read: ") || !strings.Contains(q.Error, code) || q.Balance != "$12.00" || len(q.Windows) != 0 { + t.Fatalf("a key whose limits fail with %s: %+v", code, q) + } + } + // a balance that fails keeps the limits read + if q := got["nobal"]; q.Error != "" || q.Balance != "" || len(q.Windows) != 3 { + t.Fatalf("a key whose balance fails: %+v", q) + } }