Files
magpie/internal/agent/via_first_account_test.go
T
yetone 5b9530b567 agent: Claude Code's model notes name the Codex account magpie goes to first, not the one Codex is signed in to (#1957, parkavenue9639)
With the signed-in ChatGPT account paused for the others ("this
account off, the others on"), the gateway sends codex/<model> to the
first account still on, yet Claude Code's /model list still said
"<signed-in user> · via magpie". The note now names the account that
comes first in the gateway's order: provider.FirstLogin gives
inUseLogin without the allowance-room check (the note is written once,
not per request), so a kept "keep signed in to X" still names the
account at its place, and a paused own account is passed over.

Not changed: magpie still writes openai_base_url for Codex failover
when several accounts are on. That is the failover itself; the
"Only switch Codex's sign-in" tick (#1907, v0.1.1224) already keeps
magpie out of Codex's config for those who don't want it.

Run: TestViaMagpieNamesTheAccountFirstInOrder (new) fails with the
note reverted to a.User and passes with the fix;
TestCodexPausedOwnAccountPassedOver and TestKeptLoginStandsAtItsPlace
also check FirstLogin against the account the gateway served. Full
internal/agent tests, gateway/provider login tests, gofmt -l, go vet
for darwin/linux/windows (-tags nogui).
Not tried: a real Claude Code /model list with two real ChatGPT seats.
2026-10-13 17:02:05 +08:00

73 lines
2.7 KiB
Go

package agent
import (
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #1957 (parkavenue9639): Codex signed in to one account, another on in
// magpie. Claude Code's /model described every codex/gpt-* by the account
// Codex is signed in to, while magpie sent each request to the first in
// its own order: the other one when the signed-in one is paused, or when
// Codex is kept signed in to an account that isn't first. The note names
// the account a request goes to first.
func TestViaMagpieNamesTheAccountFirstInOrder(t *testing.T) {
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
auth := func(email, acct string) map[string]any {
return map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": claims(map[string]any{"email": email}),
"access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
"refresh_token": "r-" + acct, "account_id": acct}}
}
me, _ := json.Marshal(auth("me@example.com", "acct-1"))
home, _ := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5","priority":1}]}`), 0o644)
b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": true,
"seen": time.Now(), "auth": auth("spare@example.com", "acct-2")}})
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
note := func() string {
t.Helper()
for _, o := range viaMagpie("claude", "") {
if o.Value == "codex/gpt-5.5" {
return o.Note
}
}
t.Fatal("no codex/gpt-5.5 among Claude Code's models via magpie")
return ""
}
if got := note(); got != "me@example.com · via magpie" {
t.Fatalf("signed in to the first: %q", got)
}
// kept signed in to spare, me still first in the order
if err := provider.SetKeepLoginAs("codex", "spare@example.com"); err != nil {
t.Fatal(err)
}
provider.ForgetAccounts()
if got := note(); got != "me@example.com · via magpie" {
t.Fatalf("kept signed in to spare, me first: %q", got)
}
// let go, Codex is on me again; me paused, spare takes its requests
if err := provider.SetKeepLogin("codex", false); err != nil {
t.Fatal(err)
}
if err := provider.SetLoginOn("codex", "me@example.com", false); err != nil {
t.Fatal(err)
}
provider.ForgetAccounts()
if got := note(); got != "spare@example.com · via magpie" {
t.Fatalf("signed in to me, paused: %q", got)
}
}