Files
magpie/internal/gui/trayusage_alias_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

38 lines
1.4 KiB
Go

package gui
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #1515: the menu bar's tooltip names an account by the name the user gave
// it (or the key plan it stands in for), not only the card's name; the
// account in use is named once, by that name.
func TestTrayNamesTheAccountByItsAlias(t *testing.T) {
now := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
at := now.Add(2 * time.Hour)
q := provider.SubscriptionQuota{Provider: "zcode-plugin", Name: "ZCode", User: "31****36@qq.com", Alias: "某某·GLM高级版",
Windows: []provider.QuotaWindow{{Name: "5 hours", Used: 12, ResetsAt: &at, Span: 5 * time.Hour}}}
if _, tip := trayUsageText(q, now, false); !strings.HasPrefix(tip, "ZCode · 某某·GLM高级版\n") {
t.Fatalf("tip %q", tip)
}
q.Error = "BigModel API keys: not JSON"
if _, tip := trayUsageText(q, now, false); !strings.HasPrefix(tip, "ZCode · 某某·GLM高级版: ") {
t.Fatalf("error tip %q", tip)
}
other := q
other.User, other.Alias = "b@qq.com", ""
trayInUseAccount = func(string) string { return "31****36@qq.com" }
t.Cleanup(func() { trayInUseAccount = provider.InUseLogin })
c, ok := trayInUseCard([]provider.SubscriptionQuota{other, q}, "zcode-plugin", now)
if !ok {
t.Fatal("no card")
}
if _, tip := trayUsageText(c, now, false); !strings.HasPrefix(tip, "ZCode · 某某·GLM高级版: ") {
t.Fatalf("in use tip %q", tip)
}
}