From 186453e0093580e686c78fdddd9dbe9e21638117 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 16:55:33 +0800 Subject: [PATCH] provider: a plan key's card says when it was read (jiabubu) jiabubu (Discord #feedback): on Usage -> Overview the Kimi Code card showed no "Updated ..." line while every other provider's card did. The GUI's readWhen shows it only when a quota carries readAt, and PlanQuotas' key plan cards (Kimi Code, OpenCode Go, GLM Coding Plan, MiniMax, Ollama, Command Code) and StepFun's, Volcengine's and Tencent's never set it. Each now goes through readNow, which stamps a reading that worked and has windows or a balance, as the other subscriptions' cards already did. A failed read is left alone. Test: TestPlanKeyCardSaysWhenRead (sandbox HOME, a fake Kimi /coding/v1/usages, a kimi-code-cn key) wants the card's ReadAt between the call's start and end. Run on mote at this tree: - go vet ./internal/provider: darwin, GOOS=linux -tags nogui, windows - go test ./internal/provider: ok (184s) - go test -run 'Quota|Usage|Plan|Balance' ./internal/gui ./internal/cli: ok - gofmt -l: clean Broken by hand: without readNow on the plan path the test is red ("readAt , asOf "); green with it. Not tried: a real Kimi Code key; no GUI test, since the page already draws readAt when the API gives it and the omission was the API's; Windows/Linux runs. --- internal/provider/plan_read_at_test.go | 60 ++++++++++++++++++++++++++ internal/provider/planquota.go | 13 ++++-- 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 internal/provider/plan_read_at_test.go diff --git a/internal/provider/plan_read_at_test.go b/internal/provider/plan_read_at_test.go new file mode 100644 index 000000000..c08af0bcc --- /dev/null +++ b/internal/provider/plan_read_at_test.go @@ -0,0 +1,60 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/agentenv" +) + +// jiabubu (Discord #feedback): on Usage → Overview the Kimi Code card +// said nothing of when it was read, while every other card said "Updated +// 3 minutes ago". A plan key's card (PlanQuotas: Kimi Code, OpenCode Go, +// a GLM Coding Plan, MiniMax, Ollama) was handed out with no readAt, so +// the page's readWhen had nothing to show. One read just now says now. +func TestPlanKeyCardSaysWhenRead(t *testing.T) { + isolate(t) + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache")) + t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share")) + t.Setenv("PATH", h) + for _, v := range agentenv.Vars { + t.Setenv(v, "") + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Host")+r.URL.Path != "api.kimi.com/coding/v1/usages" { + t.Errorf("asked %s%s", r.Header.Get("X-Host"), r.URL.Path) + w.WriteHeader(http.StatusNotFound) + return + } + w.Write([]byte(`{"usage":{"limit":"100","used":"12","resetTime":"2026-11-10T00:00:00Z"}, + "limits":[{"window":{"duration":300,"timeUnit":"TIME_UNIT_MINUTE"},"detail":{"limit":"100","used":"11","resetTime":"2026-11-04T11:25:00Z"}}]}`)) + })) + defer srv.Close() + old := http.DefaultClient.Transport + http.DefaultClient.Transport = rewrite{srv} + t.Cleanup(func() { http.DefaultClient.Transport = old }) + if err := Save(FromPresetKey(t, "kimi-code-cn", "sk-kimi-cn")); err != nil { + t.Fatal(err) + } + before := time.Now() + var q *SubscriptionQuota + for _, c := range PlanQuotas(context.Background()) { + if c.Provider == "kimi-code-cn" { + q = &c + } + } + if q == nil || q.Error != "" || len(q.Windows) == 0 { + t.Fatalf("Kimi Code's card: %+v", q) + } + if q.ReadAt == nil || q.ReadAt.Before(before) || q.ReadAt.After(time.Now()) { + t.Fatalf("Kimi Code's card says no time it was read: readAt %v, asOf %v", q.ReadAt, q.AsOf) + } +} diff --git a/internal/provider/planquota.go b/internal/provider/planquota.go index 038780b49..7459f984b 100644 --- a/internal/provider/planquota.go +++ b/internal/provider/planquota.go @@ -818,7 +818,9 @@ func PlanQuotas(ctx context.Context) []SubscriptionQuota { q.Resets = <-resets } } - q = keepReading(ctx, q, tag) + // read just now: the card says when (jiabubu: Kimi Code's + // said nothing while every other card did) + q = keepReading(ctx, readNow(q), tag) got[i] = &q }() } @@ -837,9 +839,12 @@ func PlanQuotas(ctx context.Context) []SubscriptionQuota { out = append(out, *q) } } - out = append(out, <-stepfun...) - out = append(out, <-volc...) - out = append(out, <-tencent...) + // StepFun's, Volcengine's and Tencent's, read just now too + for _, qs := range [][]SubscriptionQuota{<-stepfun, <-volc, <-tencent} { + for _, q := range qs { + out = append(out, readNow(q)) + } + } if ctx.Err() == nil { noteQuotaHistory(out, time.Now()) c.Lock()