Files
magpie/internal/provider/served_test.go
T
yetone 5d7c7d2808 quota: each subscription account, plan and key says when it last answered, and the latest is marked last (#570: quota --json / GET /v1/magpie/quotas 增加每个订阅、plan、key 最近一次应答请求的时间) — under routing Smart nobody is first for good, so a menu-bar widget had nothing to tell the one in use by: a Quota's asOf is when its reading was taken, accounts --json covers signed-in Codex and Claude accounts only, and /v1/magpie/route wants a session. Each Quota in magpie quota --json and GET /v1/magpie/quotas now has lastServedAt (RFC3339, left out when it hasn't answered) and last: true on the latest (each of a key's plan and balance, which are one key). The gateway notes each answer where servedCandidate counts it (provider.NoteServed): an account by its agent and user, a key by its provider and key id, matched to a card by its user, or by the key's name or mask, or, for a card with no user, by any of its provider's. It is kept in served.json beside providers.json, entries older than 30 days dropped, since magpie quota runs in a process of its own and computes the report itself (provider.QuotaReport) rather than asking the gateway: both read the file, so they say the same, and a restart keeps it. TestQuotasLastServedAt (gateway: a request answered by one key, its balance card has lastServedAt and last, the other key's neither) and TestQuotaJSONLastServedAt (the CLI's --json, fake CLIs and keychain on PATH) fail without the change; TestQuotasLastServed covers the matching (account by user, key by name and by mask, a card with no user, the tie, read back from the file alone). README tells the fields. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-02 22:16:17 +08:00

62 lines
2.5 KiB
Go

package provider
import (
"encoding/json"
"os"
"strings"
"testing"
"time"
)
// Each quota is told when its account, plan or key last answered (#570):
// an account by its user, a key by the name or mask its card has, a card
// with no user by any of its provider's; the latest is marked last.
func TestQuotasLastServed(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := Save(Provider{ID: "dk", Name: "DK", Key: "sk-first-key-0001", KeyName: "work", Chat: "http://127.0.0.1:1/v1",
Keys: []KeyAccount{{Key: "sk-other-key-0002"}}}); err != nil {
t.Fatal(err)
}
now := time.Now().Truncate(time.Second)
NoteServed(Provider{ID: "codex", Account: &Account{Agent: "codex", User: "A@x.com"}}, now.Add(-time.Hour))
NoteServed(Provider{ID: "dk", Key: "sk-other-key-0002"}, now.Add(-time.Minute))
NoteServed(Provider{ID: "solo", Key: "sk-solo"}, now.Add(-2*time.Hour))
// the CLI is a process of its own: what it is told is read from the file
servedAt.Lock()
servedAt.m = map[string]time.Time{}
servedAt.Unlock()
qs := withServed([]Quota{
{Provider: "codex", Kind: "subscription", User: "a@x.com"},
{Provider: "codex", Kind: "subscription", User: "b@x.com"},
{Provider: "dk", Kind: "plan", User: "work"},
{Provider: "dk", Kind: "plan", User: Mask("sk-other-key-0002")},
{Provider: "dk", Kind: "balance"},
{Provider: "solo", Kind: "balance"},
{Provider: "none", Kind: "balance"},
}, LastServed())
want := []*time.Time{ptr(now.Add(-time.Hour)), nil, nil, ptr(now.Add(-time.Minute)), ptr(now.Add(-time.Minute)), ptr(now.Add(-2 * time.Hour)), nil}
for i, q := range qs {
if (q.LastServedAt == nil) != (want[i] == nil) || q.LastServedAt != nil && !q.LastServedAt.Equal(*want[i]) {
t.Errorf("%d %s %s %q: lastServedAt %v, want %v", i, q.Provider, q.Kind, q.User, q.LastServedAt, want[i])
}
if last := i == 3 || i == 4; q.Last != last {
t.Errorf("%d %s %q: last %v", i, q.Provider, q.User, q.Last)
}
}
b, _ := json.Marshal(qs[0])
if !strings.Contains(string(b), `"lastServedAt":"`+now.Add(-time.Hour).UTC().Format(time.RFC3339)+`"`) || strings.Contains(string(b), `"last"`) {
t.Errorf("json %s", b)
}
if b, _ := json.Marshal(qs[1]); strings.Contains(string(b), "lastServedAt") || strings.Contains(string(b), `"last"`) {
t.Errorf("never served: %s", b)
}
if _, err := os.Stat(servedPath()); err != nil {
t.Fatal("not kept on disk:", err)
}
}
func ptr(t time.Time) *time.Time { return &t }