Files
magpie/internal/provider/account_names_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

81 lines
2.7 KiB
Go

package provider
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/testenv"
)
// #1515: a plugin's signed-in account (ZCode's, through
// opencode-zcode-auth) takes a name of the user's own. It is magpie's,
// in providers.json: plugin-auth.json and logins.json are not written.
func TestAccountNameOfAPluginAccount(t *testing.T) {
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
t.Cleanup(plugin.Settle)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
k, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r", "access": "a", "expires": 9e15, "accountId": "31****36@qq.com"})
if err != nil {
t.Fatal(err)
}
saveLogins(t, savedLogin{Agent: "plugin:fakeco", User: "31****36@qq.com", Home: k, On: true})
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
id := PluginID("fakeco")
read := func(p string) []byte { b, _ := os.ReadFile(p); return b }
auth, logins := read(plugin.AuthPath()), read(loginsPath())
if len(auth) == 0 || len(logins) == 0 {
t.Fatalf("no sign-in to keep: auth %d bytes, logins %d", len(auth), len(logins))
}
if err := SetAccountName(id, "31****36@QQ.com", " 某某·GLM\n高级版 "); err != nil {
t.Fatal(err)
}
if got := AccountNameOf(id, "31****36@qq.com"); got != "某某·GLM 高级版" {
t.Fatalf("name %q", got)
}
if !bytes.Equal(read(plugin.AuthPath()), auth) || !bytes.Equal(read(loginsPath()), logins) {
t.Fatal("naming an account wrote to its sign-in")
}
// on its card, as every surface reads it
qs := withAccountNames([]SubscriptionQuota{{Provider: id, User: "31****36@qq.com"}})
if qs[0].Alias != "某某·GLM 高级版" {
t.Fatalf("card alias %q", qs[0].Alias)
}
for _, bad := range []struct{ ref, name string }{
{"nobody@qq.com", "x"},
{"31****36@qq.com", strings.Repeat("名", MaxAccountName+1)},
} {
if err := SetAccountName(id, bad.ref, bad.name); err == nil {
t.Fatalf("%q named %q", bad.ref, bad.name)
}
}
// a key provider's keys are named in their own rows
if err := Save(Provider{ID: "zhipu-team", Name: "某某·GLM高级版", Chat: "https://open.bigmodel.cn/api/coding/paas/v4", Key: "k", KeyName: "张三"}); err != nil {
t.Fatal(err)
}
if err := SetAccountName("zhipu-team", "张三", "x"); err == nil {
t.Fatal("a key provider took an account's name")
}
if err := SetAccountName(id, "31****36@qq.com", ""); err != nil {
t.Fatal(err)
}
if got := AccountNameOf(id, "31****36@qq.com"); got != "" {
t.Fatalf("cleared name %q", got)
}
}