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 (based307aa648): - 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/maindd7442f28too (#4580). - Full suite at the earlier base76c130b29, -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.
169 lines
7.0 KiB
Go
169 lines
7.0 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A window whose reset has passed has started again, however the reading
|
|
// came: Claude Code's /usage tells a reset to the minute and can lag it,
|
|
// so a fresh run can say 100% of a session that reset a minute ago. Such a
|
|
// reading isn't spent, and Claude Code isn't moved off its account for it;
|
|
// one whose reset is still to come is.
|
|
func TestFreshClaudeReadingPastItsResetIsNotSpent(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
reset time.Time
|
|
spent bool
|
|
}{
|
|
{"reset a minute ago", time.Now().Add(-time.Minute), false},
|
|
{"reset in an hour", time.Now().Add(time.Hour), true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
at := c.reset.UTC().Format("Jan 2, 2006 at 3:04pm") + " (UTC)"
|
|
_, card := claudeUsageCards(t, "Current session: 100% used · resets "+at+"\nCurrent week (all models): 10% used · resets "+soon(3)+" at 2pm (UTC)\n", nil)
|
|
loginsMu.Lock()
|
|
ls := upsertLogin(readLogins(), savedLogin{Agent: "claude", User: "b@example.com", Plan: "max", On: true, Seen: time.Now().UTC(),
|
|
Auth: mustJSONRaw(t, map[string]any{"claudeAiOauth": map[string]any{"accessToken": "sk-ant-oat01-b", "refreshToken": "sk-ant-ort01-b",
|
|
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max", "scopes": []string{"user:inference", "user:profile"}}}),
|
|
Profile: mustJSONRaw(t, map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"})})
|
|
err := writeLogins(ls)
|
|
loginsMu.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
NoteClaudeLimits("b@example.com", []ClaudeLimit{{Kind: "five_hour", Used: .1, ResetsAt: time.Now().Add(3 * time.Hour).Unix()}})
|
|
|
|
q := card() // a fresh /usage run
|
|
if q.Error != "" || q.AsOf != nil || len(q.Windows) != 2 {
|
|
t.Fatalf("not a fresh reading: %+v", q)
|
|
}
|
|
if got := q.Windows[0].Used >= 100; got != c.spent {
|
|
t.Fatalf("the session reads %g%% (reset %v)", q.Windows[0].Used, q.Windows[0].ResetsAt)
|
|
}
|
|
if got := usedUp(q, time.Now()); got != c.spent {
|
|
t.Fatalf("used up %v, want %v: %+v", got, c.spent, q.Windows)
|
|
}
|
|
from, to, back, ok := NextLogin(context.Background(), "claude")
|
|
if ok != c.spent || c.spent && (from != "a@example.com" || to != "b@example.com" || back) {
|
|
t.Fatalf("got %q→%q back=%v ok=%v, want a move %v", from, to, back, ok, c.spent)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// NextLogin counts a window whose reset has passed as started again on
|
|
// every account it looks at — the one the agent is on, the one to go back
|
|
// to and the spares — for a fresh reading as for one kept from before
|
|
// (AsOf), Codex's as Claude's.
|
|
func TestNextLoginTakesAPassedResetAsRenewed(t *testing.T) {
|
|
past, future := time.Now().Add(-time.Minute), time.Now().Add(time.Hour)
|
|
full := func(at time.Time) []QuotaWindow {
|
|
return []QuotaWindow{{Name: "5 hours", Used: 100, ResetsAt: &at, Span: 5 * time.Hour}}
|
|
}
|
|
half := []QuotaWindow{{Name: "5 hours", Used: 50, ResetsAt: &future, Span: 5 * time.Hour}}
|
|
room := []QuotaWindow{{Name: "5 hours", Used: 10, ResetsAt: &future, Span: 5 * time.Hour}}
|
|
type move struct {
|
|
ok, back bool
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
on, spare []QuotaWindow
|
|
returns bool
|
|
asOf bool
|
|
want move
|
|
}{
|
|
{"on renewed", full(past), room, false, false, move{}},
|
|
{"on renewed, kept reading", full(past), room, false, true, move{}},
|
|
{"on still full", full(future), room, false, false, move{ok: true}},
|
|
{"spare renewed", full(future), full(past), false, false, move{ok: true}},
|
|
{"spare still full", full(future), full(future), false, false, move{}},
|
|
{"back to one renewed", half, full(past), true, false, move{ok: true, back: true}},
|
|
{"back to one still full", half, full(future), true, false, move{}},
|
|
}
|
|
for _, agent := range []string{"codex", "claude"} {
|
|
t.Run(agent, func(t *testing.T) {
|
|
var on, spare string
|
|
if agent == "codex" {
|
|
home := signIn(t) // me@example.com
|
|
rememberLogins(true)
|
|
codexSignIn(t, home, "work@example.com", "r-work")
|
|
rememberLogins(true)
|
|
on, spare = "work@example.com", "me@example.com"
|
|
} else {
|
|
home := claudeHome(t)
|
|
claudeSignIn(t, home, time.Now().Add(time.Hour))
|
|
writeFile(t, home+"/.claude.json", map[string]any{"oauthAccount": map[string]any{"emailAddress": "a@example.com", "accountUuid": "u-a"}})
|
|
rememberLogins(true)
|
|
loginsMu.Lock()
|
|
ls := upsertLogin(readLogins(), savedLogin{Agent: "claude", User: "b@example.com", Plan: "max", Seen: time.Now().UTC(),
|
|
Auth: mustJSONRaw(t, map[string]any{"claudeAiOauth": map[string]any{"accessToken": "sk-ant-oat01-b", "refreshToken": "sk-ant-ort01-b",
|
|
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max", "scopes": []string{"user:inference", "user:profile"}}}),
|
|
Profile: mustJSONRaw(t, map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"})})
|
|
err := writeLogins(ls)
|
|
loginsMu.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
on, spare = "a@example.com", "b@example.com"
|
|
}
|
|
if err := SetLoginOn(agent, spare, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Claude Code goes back to the first only In order
|
|
// (returnsToFirst)
|
|
if agent == "claude" {
|
|
if err := SetRouting(agent, Ordered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
t.Cleanup(func() { LoginUsageVia(nil) })
|
|
for _, c := range cases {
|
|
var asOf *time.Time
|
|
if c.asOf {
|
|
at := time.Now().Add(-2 * time.Hour)
|
|
asOf = &at
|
|
}
|
|
LoginUsageVia(func(context.Context, string) map[string]SubscriptionQuota {
|
|
return map[string]SubscriptionQuota{
|
|
on: {Provider: agent, User: on, Windows: c.on, AsOf: asOf},
|
|
spare: {Provider: agent, User: spare, Windows: c.spare},
|
|
}
|
|
})
|
|
r := loginReturn{}
|
|
if c.returns {
|
|
r = loginReturn{Back: spare, To: on}
|
|
}
|
|
setLoginReturn(agent, r)
|
|
from, to, back, ok := NextLogin(context.Background(), agent)
|
|
if ok != c.want.ok || back != c.want.back || ok && (from != on || to != spare) {
|
|
t.Errorf("%s: got %q→%q back=%v ok=%v, want %+v", c.name, from, to, back, ok, c.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// What stops an account, for magpie quota wait and the Codex resets, is a
|
|
// full window still to reset: one whose reset has passed doesn't stop it,
|
|
// nor say when it is back.
|
|
func TestUsedUpLeavesOutARenewedWindow(t *testing.T) {
|
|
now := time.Now()
|
|
past, future := now.Add(-time.Minute), now.Add(time.Hour)
|
|
renewed := SubscriptionQuota{Provider: "codex", Windows: []QuotaWindow{{Name: "5 hours", Used: 100, ResetsAt: &past}}}
|
|
if UsedUp(renewed, now) || !BackAt(renewed, now).IsZero() {
|
|
t.Fatalf("a window reset a minute ago stops the account: back %v", BackAt(renewed, now))
|
|
}
|
|
full := SubscriptionQuota{Provider: "codex", Windows: []QuotaWindow{
|
|
{Name: "5 hours", Used: 100, ResetsAt: &past}, {Name: "7 days", Used: 100, ResetsAt: &future},
|
|
}}
|
|
if !UsedUp(full, now) || !BackAt(full, now).Equal(future) {
|
|
t.Fatalf("a full week doesn't stop it: back %v", BackAt(full, now))
|
|
}
|
|
// read as it was, before it reset, it was used up (quota_last's holds)
|
|
if !usedUp(renewed, time.Time{}) {
|
|
t.Fatal("the reading as read isn't used up")
|
|
}
|
|
}
|