Files
magpie/internal/provider/codex_signout_test.go
T
yetone a7b47d57ec accounts: removing the account Claude Code is signed in to switches it to another first (#4532)
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).
2026-10-31 18:18:37 +08:00

86 lines
2.7 KiB
Go

package provider
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
// Codex's only account is signed out when it is removed, as codex logout
// does (mamba on Discord); one with another saved signs Codex in to that
// one first (vincentzhang1_55530 on Discord: a Team account removed beside
// a Plus one left Codex at its sign-in screen); and ForgetAccount, which
// takes the one in use last, signs Codex out too.
func TestCodexSignOutLastAccount(t *testing.T) {
home := signIn(t)
auth := filepath.Join(home, ".codex", "auth.json")
rememberLogins(true)
var signedIn *SignedInError
// another saved: the one signed in to goes, and Codex is signed in to
// the other, its own sign-in back in auth.json
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true)
if err := ForgetLogin("codex", "work@example.com"); err != nil {
t.Fatalf("the one in use with another saved: %v", err)
}
live, ok := liveLogin("codex")
if !ok || live.User != "me@example.com" {
t.Fatalf("Codex signed in to %q (%v), want me@example.com", live.User, ok)
}
if b, _ := os.ReadFile(auth); strings.Contains(string(b), "r-work") {
t.Fatalf("auth.json still the removed one's: %s", b)
}
rememberLogins(true)
if ls := Logins("codex"); len(ls) != 1 || ls[0].User != "me@example.com" || !ls[0].Active {
t.Fatalf("listed: %+v", ls)
}
// the one left is signed out
if err := ForgetLogin("codex", "me@example.com"); err != nil {
t.Fatalf("the only account: %v", err)
}
if _, err := os.Stat(auth); !os.IsNotExist(err) {
t.Fatalf("auth.json still there: %v", err)
}
if ls := Logins("codex"); len(ls) != 0 {
t.Fatalf("still listed: %+v", ls)
}
rememberLogins(true)
if ls := Logins("codex"); len(ls) != 0 {
t.Fatalf("remembered again: %+v", ls)
}
// signed in but not saved yet: signed out all the same
codexSignIn(t, home, "new@example.com", "r-new")
if err := ForgetLogin("codex", "new@example.com"); err != nil {
t.Fatalf("unsaved: %v", err)
}
if _, err := os.Stat(auth); !os.IsNotExist(err) {
t.Fatalf("auth.json still there: %v", err)
}
// an account not signed in to and not saved is still unknown
if err := ForgetLogin("codex", "nobody@example.com"); err == nil || errors.As(err, &signedIn) {
t.Fatalf("unknown: %v", err)
}
}
func TestForgetAccountSignsCodexOut(t *testing.T) {
home := signIn(t)
rememberLogins(true)
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true)
if err := ForgetAccount("codex"); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(home, ".codex", "auth.json")); !os.IsNotExist(err) {
t.Fatalf("auth.json still there: %v", err)
}
if ls := Logins("codex"); len(ls) != 0 {
t.Fatalf("saved still: %+v", ls)
}
}