Files
magpie/internal/provider/account_plugin_user_test.go
T
yetone 3b5dccd527 accounts: a plugin account goes by its usage's name, not its id (#4416)
A Huawei Cloud CodeArts account is kept by its accountId, an IAM id
(iam::<domain>:user:<name>), and every list showed that id while its
quota card showed the masked phone the plugin's usage() names it by.
Display only: an account now reads as the name the user gave it, else
the `user` its plugin's last usage read gave (PluginUserOf: the
in-memory read, else quotas.json), else its id. The id stays the key
everything is kept by; nothing stored changes or migrates. A read that
fails keeps the last good name; nothing read yet is the id, never blank.

One rule in provider (AccountShown, AccountShownOf, AccountLabelOf),
used by every list: cards' Alias (Usage page, tray panel,
/v1/magpie/quotas, magpie quota), magpie accounts, magpie providers,
the TUI's Providers rows and quota titles, usageapi's Account filter,
the quota alert. The GUI gets it as /api/providers' accountUsers
(accountShownOf in app.js): the editor's account rows (the id in the
tooltip), the per-account proxy lines, Routing's userShown, the
Providers search, Hide accounts. app.js's account lookups share one
helper, so the core stays in its budget (1,536,561 of 1,536,586), with
no room added.

Run:
- go test ./... -timeout 45m on mote at 2f5a3f1c0 + this change: all ok
  but internal/agent TestPiModelsAskedOnTheirNativeAPI (fixed on main
  by 7474a86f1, green there; this is rebased onto it),
  internal/update TestRelaunchOpenTriedAgain (#3881, green alone x3),
  and internal/gui's real-window AppKit tests (TestPanelCloseHidesIt,
  TestMainWindowCorners, TestTrayCellClickReleasedPanel red the same
  on origin/main 99ad38b7c; #2645, #4125, #1936).
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of the
  touched packages; gofmt -l clean.
- TestPluginAccountShownByItsUsageUser (new, real plugin host and fake
  plugin): id before a read, phone after, phone after a restart from
  quotas.json, phone kept over a failed read, own name wins, id when
  nothing is known. Red on the old code.
- GUI on the Linux box (mote's Chromium segfaults on these pages on
  origin/main too, #4125): account-plugin-user (new),
  routing-account-name (extended) and account-alias, Chromium and
  WebKit, every language, 34/34; the new checks red on the old code;
  the whole suite (make test-ui on the Linux box) was still running at
  push: 417 files in, 5 reds, none on a page this changes
  (account-models' row-moved check in WebKit, 81 vs 79px, and
  agent-models-all-hidden), not yet compared with origin/main; any
  that are new get an issue once it ends.
- At the rebased head (3bce283df): go test of provider, cli, tui,
  usageapi, plugin, agent -run TestPi, gui -run
  Budget|Provider|Quota|Account: all ok. Core scripts 1,535,513 of
  1,536,586.

Broken by hand: dropping the quotas.json fallback turns the test red
at "after a restart".

Not tried: a real Huawei CodeArts account (none here). A plugin whose
windows are all model pools isn't kept in quotas.json, so after a
restart it shows its id until its first read.
2026-10-31 01:01:49 +08:00

116 lines
3.7 KiB
Go

package provider
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/testenv"
)
// #4416: a plugin keeps the vendor's id as accountId, the key the user's
// own name, proxy and gateway key are kept by (codearts: an IAM id), and
// tells the account's name in usage().user (a masked phone). The account
// is shown by the user's own name, else the plugin's, else its id: what
// was read last, kept across a restart and a failed read, never nothing.
func TestPluginAccountShownByItsUsageUser(t *testing.T) {
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
// a month's window of no model's own, so the reading is kept on disk
// as quotas.json keeps any (a pool of some models' isn't)
ws := filepath.Join(t.TempDir(), "windows.json")
if err := os.WriteFile(ws, []byte(`{"iam::01a18be16b297c5197d8569e4be6a479:user:hw059194106570983": [{"name": "Month", "used": 10, "span": 2592000}]}`), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("FAKE_WINDOWS", ws)
t.Cleanup(plugin.Settle)
forget := func() {
loginUsageCache.Lock()
loginUsageCache.m, loginUsageCache.pending = nil, nil
loginUsageCache.Unlock()
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
}
forget()
t.Cleanup(forget)
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)
}
const iam, phone = "iam::01a18be16b297c5197d8569e4be6a479:user:hw059194106570983", "180****2361"
k, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r", "access": "a", "expires": 9e15, "accountId": iam})
if err != nil {
t.Fatal(err)
}
saveLogins(t, savedLogin{Agent: "plugin:fakeco", User: iam, Home: k, On: true})
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
id := PluginID("fakeco")
shown := func(want, why string) {
t.Helper()
p, err := Find(id)
if err != nil {
t.Fatal(err)
}
if got := p.AccountShown(iam); got != want {
t.Fatalf("%s: the provider shows %q, not %q", why, got, want)
}
for _, of := range []string{id, "plugin:fakeco"} {
if got := AccountShownOf(of, iam); got != want {
t.Fatalf("%s: %s shows %q, not %q", why, of, got, want)
}
}
var keys []string
for _, v := range AccountNames() {
if strings.HasPrefix(v, id+"/") {
keys = append(keys, v)
}
}
if len(keys) != 1 || keys[0] != id+"/"+want {
t.Fatalf("%s: a gateway key's accounts read %q", why, keys)
}
alias := withAccountNames([]SubscriptionQuota{{Provider: id, User: iam}})[0].Alias
if want == iam {
want = ""
}
if alias != want {
t.Fatalf("%s: the card's alias is %q, not %q", why, alias, want)
}
}
shown(iam, "nothing read yet")
if got := LoginUsage(ctx, "plugin:fakeco")[iam]; got.Error != "" || got.User != phone {
t.Fatalf("the reading: %+v", got)
}
shown(phone, "read")
// a restart: what quotas.json kept
forget()
shown(phone, "after a restart")
// a read that failed holds the account's own id, not the phone
loginUsageCache.Lock()
loginUsageCache.m = map[string]loginUsageEntry{"plugin:fakeco/" + iam: {at: time.Now(), q: SubscriptionQuota{Provider: id, User: iam, Error: "fetch failed"}}}
loginUsageCache.Unlock()
shown(phone, "a failed read")
// the user's own name is never overridden
if err := SetAccountName(id, iam, "Work"); err != nil {
t.Fatal(err)
}
shown("Work", "named")
if err := SetAccountName(id, iam, ""); err != nil {
t.Fatal(err)
}
// nothing known: the id, never blank
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, true
lastQuotas.Unlock()
shown(iam, "nothing known")
}