Files
magpie/internal/provider/quota_last_test.go
T
yetone 9bf6d43527 claude: magpie no longer asks Anthropic for a Claude account's usage, and adding a Claude account warns of a ban (the user: 不要伪造任何的 Claude 请求,能否通过 claude cli 获取 / 我害怕被封; 添加账号时要像其他的 Subscription Provider 一样显示风险提示) — the usage was read by magpie itself calling Anthropic's /api/oauth/usage with the account's token, on a timer, for every saved account, and Claude's usage-limit resets (cedar_ember) were listed, spent and auto-spent the same way. Now the usage is what Claude Code's own /usage prints (claude -p /usage, a command Claude Code answers itself without asking a model), run only when Usage is opened or Refresh pressed (GUI, magpie quota, accounts, TUI), for the account Claude Code is signed in to, at most once in 30s; a saved account shows only what Claude Code reported while using it, or the last reading kept. The Claude resets, Auto-use and their settings are gone (Codex's stay). Session jsonl files hold no usage figures, so they aren't read. Starting a Claude sign-in or import (GUI and CLI) first says Anthropic may suspend or ban the account and that it is at the user's own risk. TestClaudeWindowsAsked, TestParseClaudeUsage, TestClaudeUsageRunsClaudeCode, claude-usage-asked.test.cjs and claude-risk.test.cjs fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; routing-auto-reset, codex-auto-reset, quota-families, tray-usages, click-scroll, old-webkit, plugin-signin, usage-alert, usage-ledger pass.
2026-10-01 12:38:25 +08:00

60 lines
2.0 KiB
Go

package provider
import (
"testing"
"time"
)
func TestKeepLast(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
restart := func() {
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
}
restart()
defer restart()
later := time.Now().Add(time.Hour)
good := SubscriptionQuota{Provider: "claude", Name: "Claude Code", User: "A@x.com", Windows: []QuotaWindow{
{Name: "5 hours", Used: 40, ResetsAt: &later, Span: 5 * time.Hour},
{Name: "7 days · Opus", Used: 10, Model: "opus", Span: 7 * 24 * time.Hour},
}}
if q := keepLast(good, ""); q.AsOf != nil || q.Windows[0].Used != 40 {
t.Fatalf("%+v", q)
}
restart() // what was read is on disk
limited := SubscriptionQuota{Provider: "claude", Name: "Claude Code", User: "a@x.com", Windows: []QuotaWindow{},
Error: errClaudeNotAsked.Error()}
q := keepLast(limited, "")
if q.Error != "" || q.AsOf == nil || len(q.Windows) != 2 || q.Windows[0].Used != 40 || q.Windows[1].Model != "opus" || q.Windows[0].Span != 5*time.Hour {
t.Fatalf("not asked: %+v", q)
}
// the same through LoginUsage, which knows the user but not the name
q = keepLast(SubscriptionQuota{Provider: "claude", Windows: []QuotaWindow{}, Error: "Too Many Requests"}, "a@x.com")
if q.Error != "" || q.Name != "Claude Code" || q.Windows[0].Used != 40 {
t.Fatalf("login: %+v", q)
}
// a sign-in gone bad is said, not covered up
if q := keepLast(SubscriptionQuota{Provider: "claude", User: "a@x.com", Error: "Claude Code's sign-in has expired"}, ""); q.Error == "" {
t.Fatalf("expired: %+v", q)
}
// a new reading is used at once
good.Windows[0].Used = 70
keepLast(good, "")
if q := keepLast(limited, ""); q.Windows[0].Used != 70 {
t.Fatalf("newer: %+v", q)
}
// a window that has reset since starts again from nothing
past := time.Now().Add(-time.Minute)
good.Windows[0].ResetsAt = &past
keepLast(good, "")
if q := keepLast(limited, ""); q.Windows[0].Used != 0 {
t.Fatalf("reset: %+v", q)
}
}