Files
magpie/internal/gui/agent_model_order_test.go
T
yetone 273be22cac agents: Codex's /model lists its models in the order dragged on the Agents page (M3chD09, #855) — Codex lists the models by the priority magpie gives them: a ChatGPT account's own first, as the backend has them, then magpie's in the catalog's order, with no way to change it. Codex's model list (Pick on its row) now has a third view, Order: the models shown as Codex lists them, numbered, each dragged elsewhere (its grip shows on hover) or moved with Alt+↑/↓; it is saved at once (settings orderedModels, by agent and entry id), and the account's own models and magpie's go in one order. A model not in the order (one added later) goes after those that are, as it stood; the hidden stay hidden and out of the order view, hiding keeps the order, and a provider renamed renames its ids in the order too. Default order puts magpie's back. Codex gets it at once: signed in to ChatGPT, the gateway's /models gives the backend's and magpie's models priorities 1..n in that order and the list's ETag carries the order so Codex asks again; not signed in, the catalog file magpie writes for Codex (ListedFor → InOrder) is rewritten in it. Only Codex's list can be ordered; base_instructions are untouched. zh, ja, de strings. TestCodexModelListInUsersOrder (gateway: default order, an order across the account's and magpie's, the ETag changing, CatalogFor, opencode's untouched, none restoring it) and TestAgentModelOrderAPI (gui: orderable, an order read back, a new model last, hidden kept, hiding keeping it, reset, opencode refused) fail on the old code (no SetModelOrder/ModelOrder); codex-model-order.test.cjs (WebKit, en and zh: Order lists the own first and leaves the hidden out, Alt+↓ and a drag post the order, Default order posts none, nothing scrolls; a list that can't be ordered has no Order) fails on the old code (no Order). agent-models, agent-models-all-hidden, agent-models-loading, agent-model-groups, click-scroll, gui-ja and gui-de pass on WebKit. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 11:52:34 +08:00

110 lines
3.8 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Codex's model list goes in the order dragged on the Agents page (#855):
// the list says it can be ordered, an order comes back in it and stays in
// the settings, the hidden kept as they were; none puts magpie's back, and
// another agent's list can't be ordered.
func TestAgentModelOrderAPI(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("CODEX_HOME", filepath.Join(home, ".codex"))
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2", "m3"}}); err != nil {
t.Fatal(err)
}
if err := provider.SetHiddenModels("codex", []string{"relay/m2"}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
agentModelsAPI(mux)
type reply struct {
Models []agentModelJSON
Orderable bool
Ordered bool
}
call := func(method, agent, body string) (int, reply) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, "/api/agent-models/"+agent, strings.NewReader(body)))
var out reply
json.Unmarshal(w.Body.Bytes(), &out)
return w.Code, out
}
ids := func(ms []agentModelJSON) (out []string) {
for _, m := range ms {
if strings.HasPrefix(m.ID, "relay/") {
out = append(out, m.ID)
}
}
return out
}
_, got := call("GET", "codex", "")
if !got.Orderable || got.Ordered {
t.Fatalf("codex: orderable %v ordered %v", got.Orderable, got.Ordered)
}
if want := []string{"relay/m1", "relay/m2", "relay/m3"}; !slices.Equal(ids(got.Models), want) {
t.Fatalf("%v, want %v", ids(got.Models), want)
}
code, got := call("POST", "codex", `{"order":["relay/m3","relay/m1"]}`)
if code != 200 || !got.Ordered {
t.Fatalf("%d %+v", code, got)
}
if want := []string{"relay/m3", "relay/m1", "relay/m2"}; !slices.Equal(ids(got.Models), want) {
t.Errorf("ordered %v, want %v", ids(got.Models), want)
}
if !slices.Equal(provider.ModelOrder("codex"), []string{"relay/m3", "relay/m1"}) {
t.Errorf("saved %v", provider.ModelOrder("codex"))
}
if !provider.HiddenModels("codex")["relay/m2"] {
t.Error("ordering showed the hidden m2")
}
if _, got = call("GET", "codex", ""); !got.Ordered || ids(got.Models)[0] != "relay/m3" {
t.Errorf("read back %+v", got)
}
// a model added later goes after those ordered
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2", "m3", "m4"}}); err != nil {
t.Fatal(err)
}
if _, got = call("GET", "codex", ""); !slices.Equal(ids(got.Models), []string{"relay/m3", "relay/m1", "relay/m2", "relay/m4"}) {
t.Errorf("with a new model %v", ids(got.Models))
}
// hiding keeps the order
call("POST", "codex", `{"hidden":["relay/m1"]}`)
if !slices.Equal(provider.ModelOrder("codex"), []string{"relay/m3", "relay/m1"}) {
t.Errorf("hiding lost the order: %v", provider.ModelOrder("codex"))
}
// none puts magpie's back
if code, got = call("POST", "codex", `{"order":[]}`); code != 200 || got.Ordered || ids(got.Models)[0] != "relay/m1" {
t.Errorf("%d %+v", code, got)
}
if len(provider.ModelOrder("codex")) != 0 {
t.Errorf("still saved %v", provider.ModelOrder("codex"))
}
// only Codex's
if _, got = call("GET", "opencode", ""); got.Orderable {
t.Error("opencode's list says it can be ordered")
}
if code, _ = call("POST", "opencode", `{"order":["relay/m3"]}`); code == 200 {
t.Error("opencode's list took an order")
}
if len(provider.ModelOrder("opencode")) != 0 {
t.Errorf("opencode's order saved: %v", provider.ModelOrder("opencode"))
}
}