Files
magpie/internal/gateway/quotas_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

73 lines
2.0 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// GET /v1/magpie/quotas tells when each key last answered a request through
// the gateway, and marks the latest last (#570); one that never did has
// neither.
func TestQuotasLastServedAt(t *testing.T) {
fresh(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/balance" {
io.WriteString(w, `{"balance":"7"}`)
return
}
servedBy(provider.Chat, "sol")(w, r)
}))
t.Cleanup(up.Close)
for _, id := range []string{"qa", "qb"} {
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k-" + id, Models: []string{"sol"},
Chat: up.URL + "/v1", BalanceURL: up.URL + "/balance", BalancePath: "balance"}); err != nil {
t.Fatal(err)
}
}
provider.ForgetBalances()
s := New()
before := time.Now().Add(-time.Second)
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"qa/sol","messages":[{"role":"user","content":"hi"}]}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
rec = httptest.NewRecorder()
r := httptest.NewRequest("GET", "/v1/magpie/quotas", nil)
r.RemoteAddr = "127.0.0.1:5000"
s.Handler().ServeHTTP(rec, r)
var out struct {
Data []map[string]any `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatal(err, rec.Body)
}
seen := 0
for _, q := range out.Data {
switch q["provider"] {
case "qa":
seen++
s, _ := q["lastServedAt"].(string)
at, err := time.Parse(time.RFC3339, s)
if err != nil || at.Before(before) || q["last"] != true {
t.Errorf("served key: %v (%v)", q, err)
}
case "qb":
seen++
if _, ok := q["lastServedAt"]; ok || q["last"] != nil {
t.Errorf("a key that never answered: %v", q)
}
}
}
if seen != 2 {
t.Fatalf("quotas %s", rec.Body)
}
}