Files
magpie/internal/gui/agent_models_test.go
T
yetone 3df75514b9 agents: each agent's own model list — under an agent's name on the Agents page, "Showing 5 / 32 models" (or "All 41 models") opens a list to take models out of that agent's picker one by one and put them back (@ChongkaiX on X: 在 magpie 里选哪些模型出现在 Codex 的模型列表里) — until now an agent's list could only be narrowed by whole providers or families (Visible). settings.HiddenModels holds, by agent, the catalog entries taken out, after Visible: a deny list, so a new model is shown. provider.CatalogFor drops them, so the gateway's /models, the agents' files and pickers and Codex's catalog all follow in one place, and catalog.Touched rewrites the agents' files; Codex's own ChatGPT models are dropped from the backend's list too (CodexNativeHidden), and the list's ETag and models_cache tag carry them (CodexListTag) so Codex asks again. The model an agent is set to is marked 在用 and never taken out, on the server as well. The list groups routing groups first, then providers with their logos and on/total, a long group folded; search, 全部/已显示, a group's 全部显示/全部隐藏 on hover, 恢复全部显示; a click on a row applies at once, Esc or a click elsewhere closes it, and nothing scrolls. A renamed provider's hidden models follow it. Tested with agent-models.test.cjs in Chromium and WebKit, English and Chinese; TestHiddenModels (provider), TestCodexModelListHidesOnAgentsPage (gateway) and TestAgentModelsAPI (gui) in sandbox HOMEs. click-scroll, panel-fold, old-webkit, model-pick, menu-scroll, add-button pass; GOOS=linux go vet, linux and windows builds, go test -tags nogui ./... pass. Not tried in the real Wails window or against a real Codex.
2026-09-30 12:44:59 +08:00

78 lines
2.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The Agents page's model list for an agent: every model it may be shown,
// the one it's set to marked and never taken out.
func TestAgentModelsAPI(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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)
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte("model = \"magpie/relay/m1\"\n"), 0o600)
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)
}
mux := http.NewServeMux()
agentModelsAPI(mux)
call := func(method, body string) (out struct {
Models []agentModelJSON
Count *modelCountJSON
}) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, "/api/agent-models/codex", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s %d %s", method, w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &out)
return out
}
find := func(ms []agentModelJSON, id string) agentModelJSON {
for _, m := range ms {
if m.ID == id {
return m
}
}
t.Fatalf("no %s in %+v", id, ms)
return agentModelJSON{}
}
got := call("GET", "")
if m := find(got.Models, "relay/m1"); !m.InUse || m.Hidden || m.Group != "Relay" {
t.Fatalf("%+v", m)
}
if m := find(got.Models, "relay/m2"); m.InUse || m.Hidden {
t.Fatalf("%+v", m)
}
// the one in use stays whatever is asked
got = call("POST", `{"hidden":["relay/m1","relay/m2"]}`)
if saved := settings.Load().HiddenModels["codex"]; !slices.Equal(saved, []string{"relay/m2"}) {
t.Fatalf("saved %v", saved)
}
if !find(got.Models, "relay/m2").Hidden || find(got.Models, "relay/m1").Hidden ||
got.Count == nil || got.Count.Shown != got.Count.Listed-1 {
t.Fatalf("%+v %+v", got.Models, got.Count)
}
if c := modelCount("codex"); *c != *got.Count {
t.Fatalf("count %+v, answered %+v", c, got.Count)
}
got = call("POST", `{"hidden":[]}`)
if got.Count.Shown != got.Count.Listed {
t.Fatalf("%+v", got.Count)
}
}