Files
magpie/internal/provider/quota_read_at_test.go
T
yetone 6892a28685 quota: each allowance says when it was read, a cached one keeps its own time, and magpie quota history tells the windows' readings over time (#802, margbug01) — a subscription's card had no time unless its reading had failed, and a Claude account's allowance, which is what Claude Code last told (kept until it answers again or runs /usage), was shown on every refresh as if just read and written into the history again at each one's time. A reading now carries ReadAt: the time it was made (readNow, for every subscription and account read), and a Claude account's the time Claude Code told it (claudeReadAt). The usage card says "Updated 40 minutes ago" from it, a reading standing in for a failed one still says "As of …" in amber, a failed one says neither. The history (#651) keeps a point at its ReadAt, so a reading shown again is no new point. magpie quota and magpie accounts add "read 40m ago" to a line read more than 2 minutes before, and --json and GET /v1/magpie/quotas carry readAt. magpie quota history [<provider|account>…] [--days N] [--json] tells each account's windows over time (UTC, kept 45 days, a point only where it changed, the runs of a window apart by their reset), the last eight a window in text with ↻ where it started again, every one with --json, as GET /v1/magpie/quotas/history answers. On the card, hovering a window's key under the curve lists its latest readings with their times; no control is added to the card (the curves are kept uncluttered). Strings in zh, ja, de. Request-log correlation between readings isn't attempted. TestClaudeKeptReadingKeepsItsTime (a kept Claude reading shown three times ten minutes apart is one point at its own time; fails on the old code), TestQuotaHistoryCmd, and quota-read-at.test.cjs (WebKit, en and zh; fails on the old code). quota history run against a copy of the owner's real quota-history.json lists their Claude, Codex, Command Code, Copilot, Cursor and Devin windows. quota-curve, quota-pools, quota-families, quota-credits, quota-count-wrap, claude-usage-asked, panel-usage and balance-* pass on WebKit. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn, TestUserPath, TestClaudeSignedOut and TestCursorSignInJoinsAWrappedLink failed under load and pass alone).
2026-10-05 00:14:25 +08:00

60 lines
2.4 KiB
Go

package provider
import (
"context"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
// A Claude account's allowance is what Claude Code last told: shown again
// on each refresh, it says when it was told (ReadAt), and the history
// keeps it at that time, once, rather than as a new reading each time it
// is shown (#802).
func TestClaudeKeptReadingKeepsItsTime(t *testing.T) {
testenv.SetHome(t, t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
told := time.Now().Add(-40 * time.Minute).Truncate(time.Second)
reset := time.Now().Add(3 * time.Hour).Truncate(time.Second)
claudeUsage.Lock()
was := claudeUsage.m
claudeUsage.m = map[string]claudeUsageEntry{"kept@example.com": {at: told, heard: told,
ws: []QuotaWindow{{Name: "5 hours", Used: 30, ResetsAt: &reset}}}}
claudeUsage.Unlock()
t.Cleanup(func() { claudeUsage.Lock(); claudeUsage.m = was; claudeUsage.Unlock() })
saved := Login{Agent: "claude", User: "Kept@example.com"}
var q SubscriptionQuota
for i := range 3 {
q = readNow(loginQuota(context.Background(), saved))
q.User = saved.User
if q.Error != "" || len(q.Windows) != 1 {
t.Fatalf("saved account's card = %+v", q)
}
if q.ReadAt == nil || !q.ReadAt.Equal(told) {
t.Fatalf("read at %v, want when Claude Code told it, %v", q.ReadAt, told)
}
noteQuotaHistory([]SubscriptionQuota{q}, time.Now().Add(time.Duration(i)*10*time.Minute))
}
pts := lineOf(t, QuotaHistories(told.Add(-time.Hour), "", ""), "kept@example.com", "5 hours")
if len(pts) != 1 || !pts[0].At.Equal(told.UTC()) || pts[0].Left != 70 {
t.Fatalf("history = %+v, want one point at %v", pts, told)
}
if r := quotaReport([]SubscriptionQuota{q}, nil, nil, time.Now()); len(r) != 1 || r[0].ReadAt == nil || !r[0].ReadAt.Equal(told) {
t.Fatalf("report = %+v", r)
}
// a reading just made is stamped now; a failed one or one kept from
// before isn't
if q := readNow(SubscriptionQuota{Provider: "codex", Windows: []QuotaWindow{{Name: "5 hours", Used: 1}}}); q.ReadAt == nil || time.Since(*q.ReadAt) > time.Minute {
t.Errorf("a new reading: %+v", q)
}
if q := readNow(SubscriptionQuota{Provider: "codex", Error: "401"}); q.ReadAt != nil {
t.Errorf("a failed reading was stamped: %+v", q)
}
if q := readNow(SubscriptionQuota{Provider: "codex", AsOf: &told, Windows: []QuotaWindow{{Name: "5 hours"}}}); q.ReadAt != nil {
t.Errorf("a kept reading was stamped: %+v", q)
}
}