Files
magpie/internal/provider/switch_sticky_test.go
T
yetone 41b5c0f19a accounts: Claude Code stays on the account it moved to till that is spent too (hxy920830 on X)
hxy920830 on X wants Claude Code to use one account to 98%, move to the
next and use that one to 98%, not jump back the moment the first resets.
No setting gave that. NextLogin (#408) signed the agent back in to the
first account as soon as it was under 90% again, under every routing.
KeepLogin pins one account, In order goes back too, and switch only
went back as well.

Where the sign-in is what spends the accounts (Claude Code, whose own
requests never pass the gateway, and Codex under switch only), the
sign-in now stays on the account it moved to until that one is spent
or fails, then moves on to the next ticked account with room. It goes
back to the first only where the user said the first comes first:
routing In order, or KeepLogin. Codex through the gateway keeps going
back, because Smart routing sends its requests to the first account
(#209) and the sign-in follows it. This is returnsToFirst in
codex_switch.go. The return record is still written, so turning In
order on later goes back as before. An old record is ignored otherwise,
and its "First again" label (logins.go) is hidden. No user setting is
overridden: In order and KeepLogin keep their meaning.

GUI: the Routing note for Claude Code (and Codex under switch only) now
says it stays on the account till that one is 98% used too. The two
switch-only tooltips no longer promise going back. New and changed
strings are in zh, zh-TW, ja and de. app.js lost a 3-line comment so
the core script budget holds without a raise (core 1,531,108 of
1,533,105).

Run on mote at 94cd5edde (base d307aa648):
- go test -tags nogui -count=1 ./internal/provider/: ok. New test
  TestSignInStaysOnTheAccountMovedTo (claude, claude switch only, codex
  switch only stay; claude in order goes back).
- TestCoreScriptBudget and the gui package's budget tests: ok.
- go vet -tags nogui ./internal/provider/ ./internal/gui/ with GOOS
  darwin, linux and windows: clean. gofmt -l: clean.
- GUI, Chromium and WebKit: account-return, switch-only,
  claude-switch-only, i18n-keys, keep-login, routing-signin, gui-zh-tw,
  gui-de, gui-ja. 116/117 and 64/65 passed. The one red is gui-ja's
  "{agent} three times" string, red on origin/main dd7442f28 too (#4580).
- Full suite at the earlier base 76c130b29, -timeout 45m: provider ok.
  Reds, all already filed: TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514), TestCodexListTagNotKeptOverAFreshFile (#4216),
  TestUnroutedRequestShownThenAnswered (#4200). TestCoreScriptBudget
  failed by 41 bytes, fixed above. internal/gui hit its 45m timeout in
  TestUpdateInATerminalRestartsTheAppWithoutCuttingIt.

Broken by hand: with returnsToFirst forced to true,
TestSignInStaysOnTheAccountMovedTo (all three staying cases) and
TestCodexSwitchOnlyStillSwitchesTheSignIn went red. With the note forced
back to the "back to the first" text, the new account-return GUI test
went red in Chromium en and zh.

Not tried: a real Claude account moving and its first renewing (needs
two real windows to run out), runs on the Windows and Linux boxes, and
the full GUI suite at this head.
2026-11-01 05:23:58 +08:00

84 lines
2.6 KiB
Go

package provider
import (
"context"
"strings"
"testing"
)
// hxy920830 on X: "use an account to 98%, switch, then keep using the
// next account to 98% — not switch right back as soon as the other
// account resets". Where the sign-in alone spends the accounts (Claude
// Code; Codex whose sign-in alone magpie switches), magpie stays on the
// account it moved to till that is spent too, and the one it moved off
// isn't said to come first again. In order still goes back to the first,
// as its note says; Codex through magpie goes back too
// (TestCodexSwitchedBackWhenItHasRoom), its sign-in following Smart.
func TestSignInStaysOnTheAccountMovedTo(t *testing.T) {
for _, c := range []struct {
name, agent string
set func(agent string) error
back bool
}{
{"claude", "claude", nil, false},
{"claude switch only", "claude", func(a string) error { return SetSwitchOnly(a, true) }, false},
{"codex switch only", "codex", func(a string) error { return SetSwitchOnly(a, true) }, false},
{"claude in order", "claude", func(a string) error { return SetRouting(a, Ordered) }, true},
} {
t.Run(c.name, func(t *testing.T) {
u := threeAccounts(t, c.agent)
first := u[2] // signed in to it now
if c.set != nil {
if err := c.set(c.agent); err != nil {
t.Fatal(err)
}
}
t.Cleanup(func() { LoginUsageVia(nil) })
used := map[string]float64{first: 100, u[0]: 10, u[1]: 0}
switched := func() string {
t.Helper()
LoginUsageVia(usedAt(c.agent, used))
to, err := SwitchWhenSpent(context.Background(), c.agent)
if err != nil {
t.Fatal(err)
}
return to
}
returns := func(user string) bool {
for _, l := range Logins(c.agent) {
if strings.EqualFold(l.User, user) {
return l.Returns
}
}
t.Fatalf("no %s", user)
return false
}
a := switched()
if !strings.EqualFold(a, u[0]) && !strings.EqualFold(a, u[1]) {
t.Fatalf("the spent first moved to %q", a)
}
if returns(first) != c.back {
t.Fatalf("the first said to come first again: %v, want %v", returns(first), c.back)
}
// the first's window renews while a is half used
used[first], used[a] = 0, 50
to := switched()
if c.back {
if !strings.EqualFold(to, first) {
t.Fatalf("in order: moved to %q, want back to %s", to, first)
}
return
}
if to != "" {
t.Fatalf("moved off %s at 50%% to %s as the first renewed", a, to)
}
// a is spent in turn: on to the next with room, the first
// among them in their order
used[a] = 99
if to := switched(); to == "" || strings.EqualFold(to, a) {
t.Fatalf("a spent: moved to %q", to)
}
})
}
}