StringKe (#4532): removing the signed-in Claude account was refused with SignedInError, while Codex's switched away first. ForgetLogin now does the same for Claude Code: nextOnForget picks another account (on first, then one whose sign-in holds), SwitchLogin signs Claude Code in to it, and only then is this one forgotten. If that switch fails, nothing is removed and the error says why. nextOnForget leaves out accounts Claude Code or Codex can't be signed in to (switchable: not a setup-token account, not a Codex account held in another folder). Claude Code is never signed out: when the account is the only one it can be signed in to, it is kept, with SignedInError{Only} (GUI code signed_in_only), which says to add another account first or sign out in Claude Code. The GUI gives the signed-in Claude row a Remove when another account it can use exists, and no Remove or Sign out otherwise. Claude Code's own credential is never deleted; forgetClaudeDir removes only the forgotten account's own folder and its folder's Keychain item, and claudeHolders' check of other records holding the same token is unchanged. Tests: TestForgetLiveClaudeSwitchesFirst, TestForgetOnlyClaudeIsKept (replacing TestClaudeInUseStillRefused, which held the old refusal); codex-signout.test.cjs gets three Claude cases (Remove with another; only + setup-token: no Remove, reason in zh/zh-TW/ja/de; the signed_in_only refusal in en/zh). Run (mote, sandbox HOME through the test helpers): - go test ./internal/provider ./internal/cli, vet and gofmt at the stack head: in the next commit's body (one tree, same run). - Broken by hand, each red: Claude not switched first (TestForgetLiveClaudeSwitchesFirst), Only left false, a setup-token account counted as another, nextOnForget unfiltered (TestForgetOnlyClaudeIsKept). - GUI on Linux (WSL magpie-desktop), Chromium and WebKit: codex-signout, own-signin-remove, saved-signedout-remove, login-import, claude-signin, account-alias and i18n-keys all pass but i18n-keys' "a plugin's words are its own", red on main (#4524). On mote, codex-signout passed in Chromium; its WebKit launches were killed with mote at load ~25 under other sessions' suites (#4125), so WebKit's record is Linux's. CLI in a sandbox HOME (WSL, file credentials): forget of the signed-in account by its name switched Claude Code to the other (.claude.json then says max@x.com); forget of the only one was refused with the message above and left it signed in. Not tried: a real Claude Code switch on removal with real accounts, or in the CLI against a live ~/.claude (sandbox only).
87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// StringKe (#4532): removing the account Claude Code is signed in to was
|
|
// refused while Codex's switched first. Claude Code is signed in to the
|
|
// next account first now, and the removed one's sign-in is left nowhere:
|
|
// not in Claude Code, and not in another record.
|
|
func TestForgetLiveClaudeSwitchesFirst(t *testing.T) {
|
|
_, _ = claudeThree(t)
|
|
live, ok := liveLogin("claude")
|
|
if !ok || claudeEmail(live.User) != "pro@x.com" {
|
|
t.Fatalf("live %+v", live)
|
|
}
|
|
gone := live
|
|
if err := ForgetLogin("claude", live.User); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forgetAccountCaches()
|
|
now, ok := liveLogin("claude")
|
|
if !ok || claudeEmail(now.User) == "pro@x.com" {
|
|
t.Fatalf("Claude Code still on %+v", now)
|
|
}
|
|
if c, ok := parseClaudeCredentials(now.Auth); !ok || c.OAuth.RefreshToken != "r-"+claudeEmail(now.User) {
|
|
t.Fatalf("Claude Code's sign-in isn't %s's: %+v", now.User, c)
|
|
}
|
|
var users []string
|
|
for _, l := range Logins("claude") {
|
|
users = append(users, l.User)
|
|
}
|
|
if len(users) != 2 || strings.Contains(strings.Join(users, ","), "pro@x.com") {
|
|
t.Fatalf("saved %v", users)
|
|
}
|
|
if _, others := claudeHolders(readLogins(), gone); len(others) != 0 {
|
|
t.Fatalf("the removed sign-in is kept in %+v", others)
|
|
}
|
|
}
|
|
|
|
// Claude Code's account in use is never signed out by a removal, even its
|
|
// only one: its sign-in may be in the keychain, and magpie serves its
|
|
// saved accounts while it is signed out (claudeStandIn). The refusal says
|
|
// it is the only one, not "sign it in to another first", and an account
|
|
// added by setup-token, which Claude Code can't be signed in to, doesn't
|
|
// count as another.
|
|
func TestForgetOnlyClaudeIsKept(t *testing.T) {
|
|
home := claudeHome(t)
|
|
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
|
shellFakes(t)
|
|
creds := claudeSignIn(t, home, time.Now().Add(time.Hour))
|
|
writeFile(t, home+"/.claude.json", map[string]any{"oauthAccount": map[string]any{"emailAddress": "cc@example.com"}})
|
|
rememberLogins(true)
|
|
live, ok := liveLogin("claude")
|
|
if !ok {
|
|
t.Fatal("no Claude Code sign-in")
|
|
}
|
|
loginsMu.Lock()
|
|
err := writeLogins(upsertLogin(readLogins(), savedLogin{Agent: "claude", User: "ci@x.com", On: true, Auth: []byte(`{"setupToken":"sk-ant-oat01-x"}`)}))
|
|
loginsMu.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var signedIn *SignedInError
|
|
err = ForgetLogin("claude", live.User)
|
|
if !errors.As(err, &signedIn) || !signedIn.Only || !strings.Contains(err.Error(), "only account") {
|
|
t.Fatalf("claude in use: %v", err)
|
|
}
|
|
if _, err := os.Stat(creds); err != nil {
|
|
t.Fatalf("credentials went: %v", err)
|
|
}
|
|
if now, ok := liveLogin("claude"); !ok || now.User != live.User {
|
|
t.Fatalf("Claude Code moved to %+v", now)
|
|
}
|
|
found := false
|
|
for _, l := range Logins("claude") {
|
|
found = found || l.User == live.User
|
|
}
|
|
if !found {
|
|
t.Fatal("the account was forgotten")
|
|
}
|
|
}
|