Files
magpie/internal/agent/claude_through_magpie_test.go
T
yetone dabb5722f8 claude: Claude Code routed through magpie stays signed in where the user put it when that account is spent
oreoft on Discord has two Claude accounts: A, the main one, whose sign-in
carries their claude.ai connectors (MCP), and B, a quota account. A's
allowance was used up. They set A first and signed in, B second, and
"跑着跑着" B came first again and became Claude Code's sign-in, and the
MCP broke. They set it back more than ten times.

Cause: the used-up switch (#208). KeepOnAnAccountWithRoom looks at
Claude Code's sign-in every minute or five; A is spent, so NextLogin
returns B and SwitchWhenSpent rewrites Claude Code's live credential to
B. The GUI's list puts the account in use first (loginsInOrder), so B
"moves to first". Each time the user signed A in again, the next look
moved it back.

Decision: when Claude Code's settings send its requests to magpie's
gateway (ANTHROPIC_BASE_URL is the gateway, whether it signs in with
magpie's key or keeps its claude.ai sign-in), NextLogin never moves its
sign-in off a spent account. The gateway already routes every request
to an account with room (it skips spent ones on its own), so moving the
sign-in served nothing and only took away what the user signed in as.
This keeps the user's choice and needs no setting.

Still switching, unchanged:
- not routed (Claude Code asks Anthropic as its own sign-in): the switch
  is the only thing that keeps it working (#208);
- "Only switch Claude Code's sign-in" (SwitchOnly): the gateway serves
  none of those accounts, so switching is all magpie is asked to do;
- the move back to the first account / the kept account
  (returnsToFirst, KeepLoginAs), which go toward the user's choice;
- `magpie accounts switch claude next` and the GUI's own switch.
- Codex is left as it is: the Codex app stops sending once its own
  account is out, so it needs the sign-in moved.

ClaudeThroughMagpie is a provider hook the agent package sets
(claudeRouted, the same check claudeIn's "routed" uses), like
CodexAppRoute.

Run (all on mote via mote-run):
- new TestClaudeRoutedThroughMagpieKeepsItsSignIn (sandbox HOME: A
  spent, B with room; routed: three looks, no switch, credential still
  A's; SwitchOnly: switches to B; not routed: switches to B) with
  TestClaudeSwitchedWhenSpent, TestClaudeSwitchOnly*, TestKeepLoginAs*,
  TestNextLogin*, TestClaudeSwitch*, TestNextInOrder, TestCodexDrain*:
  ok. New TestClaudeThroughMagpieReadsItsSettings (agent): ok.
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build: ok.
Broken by hand: with the NextLogin gate turned off ("if false && ..."),
TestClaudeRoutedThroughMagpieKeepsItsSignIn FAILs at line 80, "Claude
Code routed through magpie was signed in to b@example.com".
Not tried: real Claude accounts and a real Claude Code with claude.ai
connectors; Windows and Linux boxes. Full suite runs after the push.
A sign-in already moved to B stays on B until the user switches back
once.
2026-11-05 06:43:51 +08:00

43 lines
1.5 KiB
Go

package agent
import (
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// provider.ClaudeThroughMagpie, which keeps Claude Code's sign-in where the
// user put it while its requests go through the gateway (oreoft on
// Discord), is told by Claude Code's own settings.json: its
// ANTHROPIC_BASE_URL at the gateway, whether Claude Code sends magpie's key
// or its claude.ai sign-in there; not at Anthropic's or with none.
func TestClaudeThroughMagpieReadsItsSettings(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("CLAUDE_CONFIG_DIR", "")
path := filepath.Join(home, ".claude", "settings.json")
if provider.ClaudeThroughMagpie() {
t.Fatal("no settings.json, and Claude Code counted routed through magpie")
}
for _, c := range []struct {
settings string
want bool
}{
{`{"env": {"ANTHROPIC_BASE_URL": "` + gateway.URL() + `", "ANTHROPIC_AUTH_TOKEN": "` + gateway.Token + `"}}`, true},
// its claude.ai sign-in kept: the connectors work, and the
// requests still go through magpie
{`{"env": {"ANTHROPIC_BASE_URL": "` + gateway.URL() + `", "ANTHROPIC_AUTH_TOKEN": ""}}`, true},
{`{"env": {"ANTHROPIC_BASE_URL": "https://api.anthropic.com"}}`, false},
{`{"model": "opus"}`, false},
} {
writeFile(t, path, c.settings)
if got := provider.ClaudeThroughMagpie(); got != c.want {
t.Errorf("%s: routed through magpie %v, want %v", c.settings, got, c.want)
}
}
}