Files
magpie/internal/provider/quotas_dedupe_test.go
T
yetone cd93bbec0e provider, gui: a signed-in account can be given a name of the user's own, and a GLM seat's account card keeps the key plan's name and no longer flickers back to two cards (#1515, aindijrncom)
The same GLM Coding Plan team seat was added twice: as a key provider the
user had named, and as a ZCode plugin account. The Usage page kept only the
account's card, which read "31****36@qq.com" or just "ZCode", and whenever
either read failed ("BigModel API keys: not JSON") or a window had no reset
yet, the key's card came back beside it.

Seat (the flicker): the old check returned "different" on any error. It
now returns same, different or unknown (seatMatch). A match is remembered in
seats.json beside quotas.json; while the remembered account is signed in an
unknown check keeps the key card hidden, windows that disagree forget it,
and a signed-out account brings the key card back (seatCards, used by
quotas, Allotted and CachedCards). A failed read is "unknown", never "not
the same".

Names: providers.json gains accountNames (by the account in lower case,
like accountCaps), set by provider.SetAccountName. They are magpie's own
and never written into the vendor's or a plugin's sign-in. Built-in and
plugin accounts both take one; key providers are refused. Each card carries
Alias (the user's name, else the seat) and Seat (the key provider's name,
and the key's when it has several).
- GUI: the editor's account row shows the name with the address beside
  it, or the address with "≡ <key plan>"; a click or Enter renames it in
  place (posted once to provider/accountname; Escape leaves it, empty
  clears it). The name stays selectable text: dragging across it renames
  nothing.
  The Usage page's cards and the tray panel name the account the same way.
  Hide accounts hides the name too. Strings in zh, zh-TW, ja, de.
- The tray tooltip says "ZCode · <name>".
- CLI: `magpie accounts alias <agent> <email> [<name>|--clear]`.
  `magpie accounts` and `magpie quota` show the name; quota also matches
  on it.
- /v1/magpie/quotas carries alias and seat.

Run:
- account_seats_test TestSeatKeptThroughAFailedRead fails on the old code
  ("a failed read brought the key's card back") and passes now.
  TestAccountNameOfAPluginAccount uses the fake plugin and checks that
  plugin-auth.json and logins.json are byte-for-byte unchanged.
  TestAccountsAlias checks that Codex's auth.json is unchanged.
  TestTrayNamesTheAccountByItsAlias.
- account-alias.test.cjs: Chromium and WebKit; en, zh, zh-TW, ja, de;
  900 and 360px; 20/20 pass. It covers no scroll on a click, keyboard,
  selectable text, no select, no stripe, no sideways scroll and masking.
  With the post-once guard removed it goes red. account-arrange (text
  selection) passes in both engines.
- Sandbox HOME, built binary, vendors behind a dead proxy:
  - `magpie accounts alias codex Me@Example.com 某某·GLM高级版`, then
    `magpie accounts` and `magpie quota` showed the name.
  - An unknown account was refused.
  - `magpie web` on spare ports: POST /api/provider/accountname saved
    "Team seat" (whitespace folded), and an unknown account got an error.
  - Gateway /v1/magpie/quotas and /api/usage/quotas carried the alias.
  - auth.json's sha was unchanged throughout.
- go test -tags nogui ./... (temp HOME, load ~200): all ok except
  TestSetAnswersWhileDevinAndProvidersHang (15.5s under load), which
  passes alone 3/3 at ~250ms.
- gofmt -l; GOOS=linux and GOOS=windows go vet on internal/provider,
  internal/gui and the root package.
- The whole GUI suite in Chromium and WebKit, each failed file rerun
  alone. These are red on the parent commit 472b36cec as well:
  mobile-web (9), usage-heatmap (11), old-webkit (Safari 15 :has and
  :focus-visible rules), usage-alert on WebKit (2), and routing-proxy,
  which flakes 2 of 3 runs on the base. Every other file passes alone.
Not run: a real GLM key plus a real ZCode account. The seat match against
real BigModel/ZCode answers is covered only by the fake-upstream tests.
2026-10-11 03:42:35 +08:00

139 lines
6.1 KiB
Go

package provider
import (
"reflect"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// A reset shared by unrelated vendors is not evidence of a shared account.
func TestUnrelatedPlanWithSameResetStays(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
reset := time.Date(2026, 10, 12, 0, 0, 0, 0, time.UTC)
windows := []QuotaWindow{{Name: "Weekly", Span: 7 * 24 * time.Hour, ResetsAt: &reset}}
for _, pair := range [][2]string{
{"minimax", "codex"},
{"zhipu", "claude"},
{"zai", "codex"},
{"kimi-code", "zcode"},
{"minimax-cn", "zcode-plugin"},
} {
t.Run(pair[0]+"/"+pair[1], func(t *testing.T) {
plan := SubscriptionQuota{Provider: pair[0], User: "plan-key", Windows: windows}
plan.glmPlan = pair[0] == "zhipu" || pair[0] == "zai"
sub := SubscriptionQuota{Provider: pair[1], User: "other@example.com", Windows: windows}
got := notShown([]SubscriptionQuota{plan}, []SubscriptionQuota{sub})
if !reflect.DeepEqual(got, []SubscriptionQuota{plan}) {
t.Fatalf("unrelated plan disappeared: %+v", got)
}
})
}
}
// A Zhipu plan read with a key of the account ZCode is signed in to is
// shown once, on ZCode's card; another account's plan stays.
func TestPlanOfASignedInAccountShownOnce(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
at := func(h int) *time.Time {
x := time.UnixMilli(1790000000000).Add(time.Duration(h) * time.Hour)
return &x
}
five, week := 5*time.Hour, 7*24*time.Hour
zcode := SubscriptionQuota{Provider: "zcode", Windows: []QuotaWindow{
{Name: "5 hours", Span: five, Used: 1, ResetsAt: at(2)},
{Name: "Weekly", Span: week, Used: 32, ResetsAt: at(90)},
}}
same := SubscriptionQuota{Provider: "zhipu", glmPlan: true, Plan: "Max", Windows: []QuotaWindow{
{Name: "5 hours", Span: five, Used: 4, ResetsAt: at(2)},
{Name: "7 days", Span: week, Used: 32, ResetsAt: at(90)},
{Name: "MCP · Month", Aside: true, ResetsAt: at(300)},
}}
other := SubscriptionQuota{Provider: "zai", glmPlan: true, Plan: "Pro", Windows: []QuotaWindow{
{Name: "5 hours", Span: five, ResetsAt: at(3)},
{Name: "7 days", Span: week, ResetsAt: at(90)},
}}
unknown := SubscriptionQuota{Provider: "zai", glmPlan: true, Plan: "Lite", Windows: []QuotaWindow{{Name: "5 hours", Span: five}}}
got := notShown([]SubscriptionQuota{same, other, unknown}, []SubscriptionQuota{zcode})
if len(got) != 2 || got[0].Provider != "zai" || got[1].Plan != "Lite" {
t.Fatalf("%+v", got)
}
// ZCode not answering: still the seat they were found to be, on
// ZCode's card (#1515; TestSeatKeptThroughAFailedRead)
zcode.Error = "timeout"
if got := notShown([]SubscriptionQuota{same}, []SubscriptionQuota{zcode}); len(got) != 0 {
t.Fatalf("%+v", got)
}
}
// Moved ZCode keeps its id; an independently installed plugin has the
// -plugin suffix. Both carry the same windows through quotaOfPlugin.
func TestGLMPlanMatchesZCodePlugin(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
reset := time.Date(2026, 10, 12, 0, 0, 0, 0, time.UTC)
for _, id := range []string{"zcode", "zcode-plugin"} {
t.Run(id, func(t *testing.T) {
sub := quotaOfPlugin(SubscriptionQuota{Provider: id, User: "me@example.com"}, plugin.Usage{
Windows: []plugin.UsageWindow{{Name: "Weekly", Span: 7 * 24 * 60 * 60, ResetsAt: reset.Format(time.RFC3339)}},
})
for _, provider := range []string{"zhipu", "zai", "custom-glm"} {
plan := SubscriptionQuota{Provider: provider, glmPlan: true, User: "work key",
Windows: []QuotaWindow{{Span: 7 * 24 * time.Hour, ResetsAt: &reset}}}
if got := notShown([]SubscriptionQuota{plan}, []SubscriptionQuota{sub}); len(got) != 0 {
t.Errorf("%s: duplicate plan remains: %+v", provider, got)
}
}
})
}
}
// Leaving a plan out doesn't touch the list it was left out of, which is
// PlanQuotas' cache.
func TestPlanLeftOutOfACopy(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
r := time.Now()
w := []QuotaWindow{{Span: time.Hour, ResetsAt: &r}}
plans := []SubscriptionQuota{{Provider: "a", glmPlan: true, Windows: w}, {Provider: "b"}}
if got := notShown(plans, []SubscriptionQuota{{Provider: "zcode", Windows: w}}); len(got) != 1 || got[0].Provider != "b" {
t.Fatalf("filtered cards: %+v", got)
}
if plans[0].Provider != "a" || plans[1].Provider != "b" {
t.Fatalf("%+v", plans)
}
}
// magpie quota, its --json and the gateway show a pool's own windows in
// place of the models' drawing on it (the shapes TestAntigravityQuotaPools
// records): one span per pool window, the pool in its name.
func TestQuotaReportPoolsAntigravity(t *testing.T) {
now := time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC)
reset := now.Add(48 * time.Hour)
five, week := 5*time.Hour, 7*24*time.Hour
antigravity := SubscriptionQuota{Provider: "antigravity", Name: "Antigravity", User: "u@x.com",
Windows: []QuotaWindow{
{Name: "Gemini 3 Flash", Model: "gemini-3-flash", Family: "Gemini", Pool: "Gemini", Used: 85, ResetsAt: &reset},
{Name: "Gemini 3.1 Pro (High)", Model: "gemini-3.1-pro-high", Family: "Gemini", Pool: "Gemini", Used: 85, ResetsAt: &reset},
{Name: "Claude Opus 4.6 (Thinking)", Model: "claude-opus-4-6-thinking", Family: "Claude", Pool: "Claude & GPT", Used: 90},
{Name: "GPT-OSS 120B (Medium)", Model: "gpt-oss-120b-medium", Family: "GPT-OSS", Pool: "Claude & GPT", Used: 90},
{Name: "7 days", Pool: "Gemini", Span: week, Aside: true, Used: 85, ResetsAt: &reset},
{Name: "5 hours", Pool: "Gemini", Span: five, Aside: true, Used: 95, ResetsAt: &reset},
{Name: "7 days", Pool: "Claude & GPT", Span: week, Aside: true, Used: 90, ResetsAt: &reset},
{Name: "5 hours", Pool: "Claude & GPT", Span: five, Aside: true, Used: 90, ResetsAt: &reset},
}}
got := quotaReport([]SubscriptionQuota{antigravity}, nil, nil, now)
var names []string
for _, w := range got[0].Windows {
names = append(names, w.Name)
}
if strings.Join(names, ",") != "Gemini · 7 days,Gemini · 5 hours,Claude & GPT · 7 days,Claude & GPT · 5 hours" {
t.Fatalf("windows %v", names)
}
// the pool's own use, label and reset reach the span
w := got[0].Windows[0]
if w.Used != 85 || w.Pool != "Gemini" || w.Remaining != 15 || w.ResetsAt == nil || !w.ResetsAt.Equal(reset) {
t.Fatalf("span %+v", w)
}
}