Files
magpie/internal/provider/group_test.go
T
yetone fe405b23e2 gateway: a Codex on another computer reads magpie's models with a gateway key from GET /v1/codex/models, in Codex's own catalog shape, as its model_catalog_url; /v1/models stays OpenAI's list (#1281, jskorlol)
Codex decodes a model_catalog_url as ModelsResponse {models:[ModelInfo]}
(codex-rs model-provider/src/models_endpoint.rs), which /v1/models' OpenAI
list isn't, and /backend-api/codex/models relays to the ChatGPT backend,
which answers a gateway key (replaced by magpie's token) with 401. The new
route lists the models magpie shows Codex, as codexcat writes them for
model_catalog_json, kept to the key's models (keyAllowed, as /v1/models),
the ChatGPT account's own included (provider.CodexCatalog). Codex reads at
most 1 MiB of such a catalog (MAX_MODEL_CATALOG_BYTES); entries past it are
left out and counted in X-Magpie-Left-Out rather than failing the list.

Verified:
- TestCodexCatalogForAGatewayKey through lanGuard(Handler) from a LAN
  address: required ModelInfo fields, unique slugs, held key sees only its
  models, no key / wrong key 401, /v1/models unchanged. Without the route it
  fails "404 ... magpie serves /v1/chat/completions ..."; with keyAllowed
  removed it fails "held key: [plan/m1 spare/m2]".
- TestCodexCatalogFitsCodexsLimit: 300 models -> 127 in 1047147 bytes, 173
  left out. Fails 404 without the route.
- Real Codex CLI 0.161.0 (npm) and 0.160.1 in sandbox CODEX_HOMEs against a
  sandbox `magpie serve` (0.0.0.0:3591, LAN on, two gateway keys) over
  10.0.0.111 with a fake chat upstream: models_cache.json holds the catalog
  (held key: fake/fake-coder only; free key: both), no "Unknown model"
  fallback warning, codex exec answered PONG-1281 through /v1/responses,
  also with the reporter's requires_openai_auth = true. 0.160.1 needs
  [features] api_key_model_discovery = true.
- go test -tags nogui ./internal/gateway ./internal/provider in a temp HOME:
  ok. Full ./...: 44 packages ok (internal/agent had timed out once
  under load; alone it passes in 45s). go vet; GOOS=linux and
  GOOS=windows vet of gateway+provider and nogui builds.
Not verified: Codex's TUI /model picker driven by hand.
2026-10-08 18:46:28 +08:00

106 lines
3.9 KiB
Go

package provider
import (
"maps"
"testing"
)
// Vendors spell the same model differently; a group magpie finds joins
// them all.
func TestAutoGroupsSameModel(t *testing.T) {
for in, want := range map[string]string{
"claude-opus-5-5": "claude-opus-5-5",
"anthropic/claude-opus-5.5": "claude-opus-5-5",
"Claude-Opus-5.5": "claude-opus-5-5",
"claude-opus-5-5-20260801": "claude-opus-5-5",
"anthropic/claude-opus-5.5:batch": "claude-opus-5-5:batch",
"gpt-5.1-codex": "gpt-5-1-codex",
"v1.beta": "v1.beta",
} {
if got := sameModel(in); got != want {
t.Errorf("sameModel(%q) = %q, want %q", in, got, want)
}
}
e := func(p, m, name string) Entry {
return Entry{ID: p + "/" + m, Model: m, Name: name, Provider: Provider{ID: p}}
}
gs := autoGroups([]Entry{
e("openrouter", "anthropic/claude-opus-5.5", "anthropic/claude-opus-5.5"),
e("copilot", "claude-opus-5.5", "claude-opus-5.5"),
e("claude", "claude-opus-5-5", "Claude Opus 5.5"),
}, nil)
if len(gs) != 1 || gs[0].ID != "auto-claude-opus-5-5" || len(gs[0].Members) != 3 || gs[0].Name != "Claude Opus 5.5" {
t.Fatalf("groups: %+v", gs)
}
}
// A group takes images when one of its members does: the gateway gives an
// image to the members that see it (#756: an auto group of DeepSeek V4.1
// Flash with one text-only member turned every image away).
func TestGroupImages(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", "")
no, yes := false, true
e := func(p, m string, images bool, in *bool) Entry {
return Entry{ID: p + "/" + m, Model: m, Name: m, Provider: Provider{ID: p}, Images: images, ImageInput: in}
}
gs := groupEntries([]Entry{
e("a", "kimi-k3", true, nil), e("b", "kimi-k3", true, nil),
e("a", "glm-5.3", true, nil), e("b", "glm-5.3", false, nil),
e("a", "deepseek-v4.1-flash", false, &no), e("b", "deepseek-v4.1-flash", true, &yes),
e("a", "glm-5.2", false, &no), e("b", "glm-5.2", false, &no),
})
got := map[string]bool{}
for _, g := range gs {
got[g.ID] = g.Images
}
want := map[string]bool{
GroupPrefix + "auto-kimi-k3": true, GroupPrefix + "auto-glm-5-3": true,
GroupPrefix + "auto-deepseek-v4-1-flash": true, GroupPrefix + "auto-glm-5-2": false,
}
if !maps.Equal(got, want) {
t.Errorf("%v", got)
}
}
func TestGroupImagesFromAMemberThatSees(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", "")
yes := true
entries := []Entry{
{ID: "a/m", Model: "m", Provider: Provider{ID: "a"}, Images: true, ImageInput: &yes},
{ID: "b/m", Model: "m", Provider: Provider{ID: "b"}, Images: true},
}
groups := groupEntries(entries)
if len(groups) != 1 || !groups[0].Images || groups[0].ImageInput == nil || !*groups[0].ImageInput {
t.Fatalf("group with a member that sees lost images: %+v", groups)
}
}
// A group whose first member is a ChatGPT account's model is still magpie's
// to list to a signed-in Codex: the backend lists the model, not the group.
func TestCodexListedKeepsGroups(t *testing.T) {
acct := Provider{ID: "codex-me", Account: &Account{Agent: "codex"}}
ms := codexListed([]Entry{
{ID: GroupPrefix + "fast", Model: "gpt-5.5", Name: "fast", Provider: acct, Group: "fast"},
{ID: "codex-me/gpt-5.5", Model: "gpt-5.5", Provider: acct},
{ID: "or/glm-5.3", Model: "glm-5.3", Provider: Provider{ID: "or"}},
}, func(id string) []Member {
return []Member{{Provider: Provider{ID: "or"}, Model: "glm-5.3"}, {Provider: acct, Model: "gpt-5.5"}}
}, false)
var ids []string
for _, m := range ms {
ids = append(ids, m.ID)
}
if len(ids) != 2 || ids[0] != GroupPrefix+"fast" || ids[1] != "or/glm-5.3" {
t.Errorf("listed %v", ids)
}
// Codex offers /fast on the group, a ChatGPT GPT model being in it
if !ms[0].Fast || ms[1].Fast {
t.Errorf("fast: group %v, model %v", ms[0].Fast, ms[1].Fast)
}
}