Skip to content

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

9.4 KB
Raw Blame
package provider

import (
	"context"
	"os"
	"path/filepath"
	"testing"
	"time"
)

// Claude Code wired to magpie, its accounts served by magpie: the user logs
// out of Claude Code (it says to, over magpie's ANTHROPIC_AUTH_TOKEN), and
// the accounts saved in magpie are still served — the one it was signed in
// to first, in a config directory of its own, the others on behind it — and
// none of them is dropped from the list (StringKe on Discord: all six went).
func TestClaudeAccountsOutliveLogout(t *testing.T) {
	home := claudeHome(t)
	t.Setenv("CLAUDE_CONFIG_DIR", "")
	noAnthropic(t)
	cred := 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]"}})
	far := time.Now().Add(24 * time.Hour).UnixMilli()
	oauth := func(tok string) map[string]any {
		return map[string]any{"claudeAiOauth": map[string]any{"accessToken": tok, "refreshToken": "r-" + tok, "expiresAt": far, "subscriptionType": "max"}}
	}
	seen := time.Now().Add(-time.Hour).UTC().Truncate(time.Second)
	writeFile(t, loginsPath(), []map[string]any{
		{"agent": "claude", "user": "[email protected]", "plan": "max", "on": true, "seen": seen, "auth": oauth("tok-on")},
		{"agent": "claude", "user": "[email protected]", "plan": "pro", "seen": seen, "auth": oauth("tok-off")},
		{"agent": "claude", "user": "[email protected]", "plan": "max", "on": true, "seen": seen.Add(-time.Hour), "auth": oauth("tok-two")},
	})
	loginsMu.Lock()
	loginsSeenAt.Store(0)
	loginsMu.Unlock()
	if p, ok := find(All(), "claude"); !ok || p.Account.User != "[email protected]" {
		t.Fatalf("signed in: %v %+v", ok, p.Account)
	}

	// /logout: Claude Code's sign-in is gone
	os.Remove(cred)
	os.Remove(filepath.Join(home, ".claude.json"))
	forgetClaudeCredential()
	loginsMu.Lock()
	loginsSeenAt.Store(0)
	loginsMu.Unlock()

	// the one Claude Code held went with its /logout, which revokes the
	// refresh token it holds — magpie's copy of it too (StringKe on
	// Discord: all six had to be signed in again). The others are sign-ins
	// of their own: one on stands in, never the revoked one.
	p, ok := find(All(), "claude")
	if !ok || p.Account == nil || p.Account.User != "[email protected]" {
		t.Fatalf("logged out: %v %+v", ok, p.Account)
	}
	dir, own, err := p.Account.Token(context.Background())
	if err != nil || !own || dir != claudeAccountDir("[email protected]") {
		t.Fatalf("dir %q %v %v", dir, own, err)
	}
	if c, ok := readClaudeDir(dir); !ok || c.OAuth.RefreshToken != "r-tok-on" {
		t.Fatalf("dir credentials %+v", c.OAuth)
	}
	if _, err := os.Stat(claudeAccountDir("[email protected]")); !os.IsNotExist(err) {
		t.Fatalf("the revoked sign-in was put to use: %v", err)
	}
	var also []string
	for _, q := range p.AlsoOn() {
		also = append(also, q.Account.User)
	}
	if len(also) != 1 || also[0] != "[email protected]" {
		t.Fatalf("also on: %v", also)
	}
	ls := Logins("claude")
	if len(ls) != 4 {
		t.Fatalf("logins: %+v", ls)
	}
	for _, l := range ls {
		revoked := l.User == "[email protected]"
		if l.Active || l.On != (l.User == "[email protected]" || l.User == "[email protected]") ||
			(l.Lapsed == claudeLogoutLapse) != revoked {
			t.Fatalf("login %+v", l)
		}
	}
	if u := InUseLogin("claude"); u != "[email protected]" {
		t.Fatalf("in use %q", u)
	}
	for _, x := range savedButSignedOut() {
		if x.Agent == "claude" {
			t.Fatalf("excluded: %+v", x)
		}
	}

	// the stand-in can be paused behind another on, as Claude Code's own could
	if err := SetLoginOn("claude", "[email protected]", false); err != nil {
		t.Fatal(err)
	}
	if p, _ = find(All(), "claude"); !p.OwnPaused() {
		t.Fatal("not paused")
	}
	if u := InUseLogin("claude"); u != "[email protected]" {
		t.Fatalf("in use %q", u)
	}

	// /login again in Claude Code, to the same account: a sign-in anew,
	// no longer lapsed
	writeFile(t, cred, oauth("tok-again"))
	writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "[email protected]"}})
	ForgetAccounts()
	for _, l := range Logins("claude") {
		if l.User == "[email protected]" && (!l.Active || l.Lapsed != "") {
			t.Fatalf("signed in again: %+v", l)
		}
	}
}

// A switch in magpie takes Claude Code off one account and puts it on
// another, saving the first's sign-in as it was: nothing revoked it, and it
// isn't taken for logged out.
func TestClaudeSwitchIsNoLogout(t *testing.T) {
	home := claudeHome(t)
	t.Setenv("CLAUDE_CONFIG_DIR", "")
	noAnthropic(t)
	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]"}})
	far := time.Now().Add(24 * time.Hour).UnixMilli()
	writeFile(t, loginsPath(), []map[string]any{
		{"agent": "claude", "user": "[email protected]", "plan": "max", "seen": time.Now().Add(-time.Hour).UTC(),
			"profile": map[string]any{"emailAddress": "[email protected]"},
			"auth":    map[string]any{"claudeAiOauth": map[string]any{"accessToken": "tok-other", "refreshToken": "r-other", "expiresAt": far}}},
	})
	ForgetAccounts()
	if u := InUseLogin("claude"); u != "[email protected]" {
		t.Fatalf("in use %q", u)
	}
	if err := SwitchLogin("claude", "[email protected]"); err != nil {
		t.Fatal(err)
	}
	ForgetAccounts()
	for _, l := range Logins("claude") {
		if l.Lapsed != "" || l.Active != (l.User == "[email protected]") {
			t.Fatalf("login %+v", l)
		}
	}
}

// Logged out of Claude Code with accounts in magpie, an account signed in
// in magpie stays magpie's: Claude Code isn't signed in to it again, which
// would only bring back its warning over magpie's token.
func TestClaudeSignInWhileLoggedOut(t *testing.T) {
	home := claudeHome(t)
	t.Setenv("CLAUDE_CONFIG_DIR", "")
	noAnthropic(t)
	far := time.Now().Add(24 * time.Hour).UnixMilli()
	writeFile(t, loginsPath(), []map[string]any{
		{"agent": "claude", "user": "[email protected]", "plan": "max", "seen": time.Now().Add(-time.Hour).UTC(),
			"auth": map[string]any{"claudeAiOauth": map[string]any{"accessToken": "tok-old", "refreshToken": "r-old", "expiresAt": far}}},
	})
	fakeClaudeLogin(t, fakeClaudeAccount{email: "[email protected]", plan: "pro", refresh: "sk-ant-ort01-new"}, "", true)

	st, err := StartSignIn("claude")
	if err != nil {
		t.Fatal(err)
	}
	finishInBrowser(t, st, "the-code")
	if st = waitDone(t, st.ID); st.State != "done" || st.Using {
		t.Fatalf("state %+v", st)
	}
	if _, err := os.Stat(filepath.Join(home, ".claude", ".credentials.json")); !os.IsNotExist(err) {
		t.Fatalf("Claude Code signed in: %v", err)
	}
	forgetClaudeCredential()
	var users []string
	for _, l := range Logins("claude") {
		if l.On {
			users = append(users, l.User)
		}
	}
	if len(users) != 2 {
		t.Fatalf("on: %v", users)
	}
}

// Every saved Claude account's sign-in gone while Claude Code is signed out
// (LeeHom on Discord: two Claude Pro accounts vanished from Providers, and
// were taken for a ban): the one Claude Code held went with its sign-in,
// and the other had gone the same way before. Claude Code stays on the
// Providers list, signed out, each account on it saying to sign in again,
// and its token says so too; none is served as if it could be used.
func TestClaudeAllSignedOutStaysListed(t *testing.T) {
	home := claudeHome(t)
	t.Setenv("CLAUDE_CONFIG_DIR", "")
	noAnthropic(t)
	cred := 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]"}})
	far := time.Now().Add(24 * time.Hour).UnixMilli()
	seen := time.Now().Add(-time.Hour).UTC().Truncate(time.Second)
	writeFile(t, loginsPath(), []map[string]any{
		{"agent": "claude", "user": "[email protected]", "plan": "pro", "on": true, "seen": seen, "lapsed": claudeLogoutLapse,
			"auth": map[string]any{"claudeAiOauth": map[string]any{"accessToken": "tok-two", "refreshToken": "r-two", "expiresAt": far, "subscriptionType": "pro"}}},
	})
	loginsMu.Lock()
	loginsSeenAt.Store(0)
	loginsMu.Unlock()
	if p, ok := find(All(), "claude"); !ok || p.Account.User != "[email protected]" {
		t.Fatalf("signed in: %v %+v", ok, p.Account)
	} else if p.SignedOut() {
		t.Fatal("a signed-in Claude Code reads as signed out")
	}

	// Claude Code lets go of its sign-in: Anthropic refused it, or /logout
	os.Remove(cred)
	os.Remove(filepath.Join(home, ".claude.json"))
	forgetClaudeCredential()
	loginsMu.Lock()
	loginsSeenAt.Store(0)
	loginsMu.Unlock()

	p, ok := find(All(), "claude")
	if !ok || p.Account == nil {
		t.Fatalf("Claude Code left the Providers list with every account signed out: %v", ok)
	}
	if p.Account.User != "[email protected]" {
		t.Fatalf("listed as %q; want the one seen last", p.Account.User)
	}
	if _, _, err := p.Account.Token(context.Background()); err == nil || err.Error() != claudeLogoutLapse {
		t.Fatalf("token of a signed-out account: %v", err)
	}
	ls := Logins("claude")
	if len(ls) != 2 {
		t.Fatalf("logins: %+v", ls)
	}
	listedOn := false
	for _, l := range ls {
		if l.Lapsed != claudeLogoutLapse {
			t.Fatalf("not signed out: %+v", l)
		}
		listedOn = listedOn || l.On && l.User == "[email protected]"
	}
	// the row reads as signed out: the account it names is the one on
	if !listedOn {
		t.Fatalf("the listed account isn't on: %+v", ls)
	}
	if u := InUseLogin("claude"); u != "[email protected]" && u != "" {
		t.Fatalf("in use %q", u)
	}
	// what magpie providers, the TUI and the app's row say of it
	if !p.SignedOut() {
		t.Fatal("listed on a signed-out account, the provider doesn't say it is signed out")
	}
}