Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

3.5 KB
Raw Blame
package provider

import (
	"errors"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

// 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, "[email protected]", "r-work")
	rememberLogins(true)
	if err := ForgetLogin("codex", "[email protected]"); err != nil {
		t.Fatalf("the one in use with another saved: %v", err)
	}
	live, ok := liveLogin("codex")
	if !ok || live.User != "[email protected]" {
		t.Fatalf("Codex signed in to %q (%v), want [email protected]", 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 != "[email protected]" || !ls[0].Active {
		t.Fatalf("listed: %+v", ls)
	}

	// the one left is signed out
	if err := ForgetLogin("codex", "[email protected]"); 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, "[email protected]", "r-new")
	if err := ForgetLogin("codex", "[email protected]"); 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", "[email protected]"); err == nil || errors.As(err, &signedIn) {
		t.Fatalf("unknown: %v", err)
	}
}

func TestForgetAccountSignsCodexOut(t *testing.T) {
	home := signIn(t)
	rememberLogins(true)
	codexSignIn(t, home, "[email protected]", "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)
	}
}

// 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).
func TestClaudeInUseStillRefused(t *testing.T) {
	home := claudeHome(t)
	t.Setenv("CLAUDE_CONFIG_DIR", "")
	shellFakes(t)
	creds := claudeSignIn(t, home, time.Now().Add(time.Hour))
	writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "[email protected]"}})
	rememberLogins(true)
	live, ok := liveLogin("claude")
	if !ok {
		t.Fatal("no Claude Code sign-in")
	}
	var signedIn *SignedInError
	if err := ForgetLogin("claude", live.User); !errors.As(err, &signedIn) {
		t.Fatalf("claude in use: %v", err)
	}
	if _, err := os.Stat(creds); err != nil {
		t.Fatalf("credentials went: %v", err)
	}
}