Files
magpie/internal/gateway/key_group_members_test.go
T
yetone 2f59545d29 gateway: a key not given a group listing alone is listed the members it may use (luckymanjc)
luckymanjc on Discord (#general, 11-05): on a second computer, through
Remote magpie, his three Codex accounts offered GPT-6-Luna alone, with
the key his Remote magpie uses held to "Codex 的全部模型" (codex/*),
GPT-6-Luna and the accounts. His screenshot of that key's picker, on the
host, offered GPT-6-Luna alone under Codex too.

Cause: a routing group listing alone (Group.Unlists, "the group alone")
keeps its members out of Catalog(), so out of /v1/models and the key
picker (/api/caller-keys/models), as the group stands in for them there.
Since d1cdce130 (11-03) that holds for subscription models again. But a
group of the user's own is never a key's unless the key names it
(kautism's rule, groupAllowed), so a key held to codex/* was given
neither the group nor its members: only the Codex models no such group
named were left, Luna here. The picker read the same Catalog(), so there
was nothing else to tick.

Fix: the group stands in only where it is given.
- provider.GroupStandIns: the models groups listing alone keep out, with
  the groups' ids (groupsKeeping, which unlistedByGroups now reads; a
  provider kept for routing groups alone is not among them).
- gateway keyListed: for a key that may use none of the groups standing
  in for a model and may use the model, the model is listed beside its
  provider's others; the list's own picks still leave one out. Every key
  list goes through it: catalogFor (/v1/models and what it feeds, the
  remote magpie's fetch included), /v1/models/{id}, the key's page,
  Codex's catalog and spawn choices. A key that names the group gets the
  group in place of its members, as before. Unrestricted callers are
  unchanged: the group alone, as the option says.
- The key picker offers those models after their provider's.
It is the same for every provider a group can list alone, not Codex
alone. Docs: reference.md, gateway-routing.md.

Tests (mote):
- TestKeyNotGivenTheGroupIsListedItsMembers (gateway): Free + Pro Codex
  sandbox accounts, a group listing sol/astra/terra alone. Unrestricted
  list: group, no members. The reporter's key (codex/*, codex/gpt-6-luna,
  both accounts): every Codex model, no group. A key of one member: that
  one. A key naming the group: group, no members. A Remote magpie on the
  reporter's key fetches every Codex model. Red on the old code (sol,
  astra, terra missing), green now.
- TestCallerKeyModelsOfferGroupKept (gui): the picker offers the kept
  models, beside their provider's. Red on the old code, green now.
- Broken by hand: dropping the "a group standing in is given" check
  turns the key-naming-the-group case red.
- go vet provider, gateway, gui; go test ./internal/provider ok;
  ./internal/gui all ok but TestMainWindowCorners (#3949, AppKit titlebar on
  mote's macOS, untouched here); ./internal/gateway -run
  "Key|Codex|Model|Remote|Group|Catalog|Spawn|Unlist|Caller" ok.

Not tried: the reporter's own host (his groups are inferred from the
screenshot: only Luna left under Codex in the picker); two real magpie
apps on two machines; real Codex accounts.
2026-11-05 18:28:38 +08:00

125 lines
4.3 KiB
Go

package gateway
import (
"context"
"encoding/json"
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// A routing group listing alone (Group.Unlists) stands in for its
// members in the lists, so its members are left out of them. A gateway
// key that may use those members and not the group is given neither:
// luckymanjc on Discord (11-05) held his Remote magpie's key to every
// Codex model ("codex/*", his three accounts), and his other computer
// was offered GPT-6-Luna alone, the one Codex model no group of his
// listed alone; the key's picker offered it alone too. The group stands
// in only for a caller that may use it: for the rest the members it may
// use are listed, on every route a list is read from, the remote
// magpie's included.
func TestKeyNotGivenTheGroupIsListedItsMembers(t *testing.T) {
codexFreeBesidePro(t)
p, err := provider.Find("codex")
if err != nil {
t.Fatal(err)
}
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{ID: "mygpt", Name: "MyGPT", Members: []string{"codex/gpt-6.1-sol", "codex/gpt-6-astra", "codex/gpt-5.6-terra"}, Unlists: true}); err != nil {
t.Fatal(err)
}
keys, secrets := newCaller(t, "Magpie")
var accts []string
for _, r := range p.AccountIDs() {
accts = append(accts, "codex/"+r.ID)
}
hold := func(models, accounts []string) {
t.Helper()
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: models}); err != nil {
t.Fatal(err)
}
if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID, Accounts: accounts}); err != nil {
t.Fatal(err)
}
}
list := func(secret string) []string {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/v1/models", nil)
if secret != "" {
req.Header.Set("Authorization", "Bearer "+secret)
}
New().Handler().ServeHTTP(rec, req)
var out struct {
Data []struct{ ID string } `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
var ids []string
for _, d := range out.Data {
ids = append(ids, d.ID)
}
return ids
}
has := func(ids []string, want ...string) bool {
for _, w := range want {
if !slices.Contains(ids, w) {
return false
}
}
return true
}
members := []string{"codex/gpt-6.1-sol", "codex/gpt-6-astra", "codex/gpt-5.6-terra"}
// the user's own list: the group in place of its members, as the
// option says
if ids := list(""); !slices.Contains(ids, "group/mygpt") || slices.ContainsFunc(members, func(m string) bool { return slices.Contains(ids, m) }) {
t.Fatalf("unrestricted: %v", ids)
}
// the reporter's key: every Codex model, Luna named too, his accounts
hold([]string{"codex/*", "codex/gpt-6-luna"}, accts)
if ids := list(secrets[0]); !has(ids, append(members, "codex/gpt-6-luna", "codex/gpt-6-sol")...) || slices.Contains(ids, "group/mygpt") {
t.Fatalf("key held to codex/*: %v", ids)
}
// a key given one member only is listed that one, not the others
hold([]string{"codex/gpt-6-astra"}, nil)
if ids := list(secrets[0]); !slices.Equal(ids, []string{"codex/gpt-6-astra"}) {
t.Fatalf("key held to one member: %v", ids)
}
// a key given the group is given it in place of its members, as the
// user's list is
hold([]string{"codex/*", "group/mygpt"}, nil)
if ids := list(secrets[0]); !slices.Contains(ids, "group/mygpt") || slices.ContainsFunc(members, func(m string) bool { return slices.Contains(ids, m) }) {
t.Fatalf("key given the group: %v", ids)
}
// the other computer: a Remote magpie on the reporter's key is
// offered every Codex model
hold([]string{"codex/*", "codex/gpt-6-luna"}, accts)
remote := httptest.NewServer(New().Handler())
t.Cleanup(remote.Close)
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Key: secrets[0], Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(remote.URL, "http://")})
if err != nil {
t.Fatal(err)
}
office, _ := provider.Find(id)
ms, err := office.Fetch(context.Background())
if err != nil {
t.Fatal(err)
}
var got []string
for _, m := range ms {
got = append(got, m.ID)
}
if !has(got, append(members, "codex/gpt-6-luna")...) {
t.Fatalf("remote magpie: %v", got)
}
}