Files
magpie/internal/agent/codex_accounts_via_test.go
T
yetone 6b5181b927 agent: Codex on a relay or with several accounts in magpie offers its GPT models via magpie too (#1603, SwimmingLiu)
Codex's picker left out the models magpie serves on the Codex accounts
in it (codex/...) whenever Codex wasn't routed through magpie, on the
reasoning that going via magpie to Codex's own sign-in only adds a hop.
That holds only when Codex is on its own ChatGPT sign-in and that one
account is alone in magpie. With Codex on CC Switch's custom relay, the
accounts in magpie are not what Codex is on, and with several accounts
on, magpie routes over them; the reporter saw the GPT models direct
only, with no 经 magpie entry.

modelOptions now offers them via magpie, beside the direct ones, unless
Codex is on its own sign-in with a single account. The stopped-WSL
distro's options do the same when several accounts are on. Listing the
options writes nothing.

Run: TestCodexAccountsOfferedViaMagpie (relay + two accounts, relay +
one account, own sign-in + one account) failed before and passes after;
forcing the old branch by hand turns the third subtest red.
codex-accounts-via.test.cjs: Chromium and WebKit, en and zh, 980 and
420px, 8/8 (the API is faked; the GUI itself is unchanged, no new
strings). go test -tags nogui ./internal/agent, gofmt -l, GOOS=linux
and GOOS=windows go vet clean. Not tried with real Codex accounts.
2026-10-11 20:09:15 +08:00

99 lines
3.4 KiB
Go

package agent
import (
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #1603 (SwimmingLiu): two Codex accounts on in magpie, Codex on CC
// Switch's custom relay and not through magpie. Codex's picker had its
// GPT models direct only: the ones magpie serves on those accounts
// (codex/…) were left out as "Codex's own sign-in, one more hop". They
// are not Codex's sign-in while it is on a relay, and with several
// accounts magpie routes over them; they are offered via magpie, beside
// the direct ones. Listing them writes nothing. With Codex on its own
// ChatGPT sign-in and that one account alone in magpie, they still add
// only a hop and stay out.
func TestCodexAccountsOfferedViaMagpie(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"))
cache := []byte(`{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5","priority":1}]}`)
logins := func(on bool) {
b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": on,
"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)
}
via := func(a *Agent) (direct, magpie []Option) {
for _, o := range a.Fields[0].Options(nil) {
switch {
case o.Value == "gpt-5.5" && o.Direct != "":
direct = append(direct, o)
case strings.HasPrefix(o.Value, "codex/") && o.Direct == "" && strings.HasSuffix(o.Note, "via magpie"):
magpie = append(magpie, o)
}
}
return
}
t.Run("on a relay, two accounts", func(t *testing.T) {
home, read := codexHome(t, string(me), ccSwitchCodex)
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), cache, 0o644)
logins(true)
before := read()
direct, magpie := via(codex(home))
if len(direct) == 0 {
t.Error("the relay's own gpt-5.5 isn't offered direct")
}
if !hasValue(magpie, "codex/gpt-5.5") {
t.Errorf("no codex/gpt-5.5 via magpie: %+v", magpie)
}
if read() != before {
t.Errorf("listing the models wrote config.toml:\n%s", read())
}
})
t.Run("on a relay, one account", func(t *testing.T) {
home, _ := codexHome(t, string(me), ccSwitchCodex)
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), cache, 0o644)
logins(false)
if _, magpie := via(codex(home)); !hasValue(magpie, "codex/gpt-5.5") {
t.Errorf("the account in magpie isn't the relay Codex is on: %+v", magpie)
}
})
t.Run("on its own sign-in, one account", func(t *testing.T) {
home, _ := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), cache, 0o644)
logins(false)
if _, magpie := via(codex(home)); len(magpie) != 0 {
t.Errorf("Codex's own sign-in, again via magpie: %+v", magpie)
}
})
}
func hasValue(opts []Option, v string) bool {
for _, o := range opts {
if o.Value == v {
return true
}
}
return false
}