Files
magpie/internal/gui/groups_test.go
T
yetone 0621e70fb9 routing: leave a provider out of the groups magpie finds
chenxiang0010 on Discord asked to keep a provider or a model out of the
auto-created routing groups. A model could already be kept out (remove
its found group; magpie keeps it removed), but a provider could only be
turned off per group or everywhere. Now Routing -> Groups, under "Find
groups on their own", has "Leave out a provider..." (app menu, saved
when it closes) and `magpie group auto skip <provider>...|none`. The
providers chosen (providers.json autoGroupSkip) join no found group; a
group the user made or changed keeps them, a found group left with one
provider goes away, and a request for it is served by AutoStandIn from
a provider still in, and agents on it are reseated (agent.Reseat). A
rename carries the id along. en, zh, zh-TW, ja, de.

Run at 383947fce (this change on origin/main 2ff75161d):
- go test -timeout 45m ./... on mote: 80 packages ok; internal/gui red
  only in its real-window AppKit tests (TestDockJoinLeadsCommandTab,
  TestPanelCloseHidesIt, TestMainWindowCorners, ...), which fail the same
  on fresh origin/main 2f65a33aa on mote (#4125, #2645).
- gofmt -l clean; go vet ./... on Windows (win-run) clean; on Linux
  (wsl-run) clean with -tags nogui (the WSL distro has no gtk4).
- GUI: group-found-skip (new) and group-found-off, all five languages,
  1100px and 390px, Chromium and WebKit on Linux (wsl-run): 24/24. On
  mote every WebKit launch times out (#4125), Chromium 12/12 there.
- The routing files the full run flagged (concurrency 6) rerun alone on
  wsl-run, here and at fresh origin/main 380032cda: click-scroll's
  WebKit case and group-morph's WebKit 420px cases fail the same on
  origin/main. group-duplicate's de case was mine: the found groups'
  new "Anbieter ausnehmen..." line made German taller than the test's
  420x1200 window, which it assumes needs no scroll; it now uses 1400
  and passes 10/10 in both engines.
- Whole GUI suite on wsl-run, concurrency 6: still running at push time
  (5092 ok, 170 not ok so far, mostly in files this change doesn't load:
  account-*, agent-*, connect-*, gui-de/gui-ja's "{agent}" placeholder
  string). They are compared against origin/main after the push.

Broken by hand: foundGroups without unskipped -> TestAutoGroupSkip and
TestGroupsSkip red. Test calls before the fix to groupCmd's args ->
TestGroupAutoSkip red.

Not tried: the real app with real providers; a model-level skip (a
model is kept out by removing its found group, as before).
2026-10-31 00:57:21 +08:00

238 lines
8.2 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The Routing view's save with another id than the group had (from)
// renames it: a found group drops its auto- prefix.
func TestGroupSaveRenames(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-6-astra"}}); err != nil {
t.Fatal(err)
}
}
mux := http.NewServeMux()
groupRoutes(mux)
post := func(body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/save", strings.NewReader(body)))
return w
}
const members = `"members":["a/gpt-6-astra","b/gpt-6-astra"]`
if w := post(`{"id":"Bad Id!","from":"auto-gpt-6-astra",` + members + `}`); w.Code < 400 {
t.Fatalf("a bad id taken: %d", w.Code)
}
if w := post(`{"id":"gpt-6-astra","from":"auto-gpt-6-astra","name":"Astra","routing":"order",` + members + `}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
g, _, ok := provider.FindGroup("group/gpt-6-astra")
if !ok || g.Name != "Astra" || g.Routing != provider.Ordered {
t.Fatalf("%v %+v", ok, g)
}
if _, _, ok := provider.FindGroup("group/auto-gpt-6-astra"); ok {
t.Fatal("auto-gpt-6-astra is back")
}
// a save without a new id stays a save
if w := post(`{"id":"gpt-6-astra","from":"gpt-6-astra","name":"Astra 2",` + members + `}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
if g, _, _ := provider.FindGroup("group/gpt-6-astra"); g.Name != "Astra 2" {
t.Fatalf("%+v", g)
}
}
// The Routing view's switch turns found groups off and on (蓝猫 on
// Discord): the state says which, and off lists none of them.
func TestGroupsFoundSwitch(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
}
mux := http.NewServeMux()
groupRoutes(mux)
found := func(body string) groupsJSON {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/found", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", body, w.Code, w.Body)
}
var st groupsJSON
if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil {
t.Fatal(err)
}
return st
}
if st := groupsState(); !st.Found || len(st.Groups) != 1 {
t.Fatalf("on: %v %+v", st.Found, st.Groups)
}
if st := found(`{"on":false}`); st.Found || len(st.Groups) != 0 || provider.AutoGroupsOn() {
t.Fatalf("off: %v %+v", st.Found, st.Groups)
}
if st := found(`{"on":true}`); !st.Found || len(st.Groups) != 1 || st.Groups[0].ID != "auto-m" {
t.Fatalf("on again: %v %+v", st.Found, st.Groups)
}
}
// groups/skip keeps providers out of the groups magpie finds
// (chenxiang0010 on Discord): the state names them, and lists every
// provider that can be, one kept out that is gone too.
func TestGroupsSkip(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b", "c"} {
if err := provider.Save(provider.Provider{ID: id, Name: "P " + id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
}
mux := http.NewServeMux()
groupRoutes(mux)
skip := func(body string) groupsJSON {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/skip", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", body, w.Code, w.Body)
}
var st groupsJSON
if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil {
t.Fatal(err)
}
return st
}
if st := groupsState(); len(st.Skip) != 0 || len(st.Skippable) != 3 || len(st.Groups) != 1 || len(st.Groups[0].Members) != 3 {
t.Fatalf("before: %+v %+v %+v", st.Skip, st.Skippable, st.Groups)
}
st := skip(`{"skip":["b","gone"]}`)
if !slices.Equal(st.Skip, []string{"b", "gone"}) || len(st.Groups) != 1 || !slices.Equal(st.Groups[0].Members, []string{"a/m", "c/m"}) {
t.Fatalf("b kept out: %+v %+v", st.Skip, st.Groups)
}
if i := slices.IndexFunc(st.Skippable, func(k skippableJSON) bool { return k.ID == "gone" }); i < 0 || st.Skippable[i].Name != "gone" {
t.Fatalf("a provider kept out and gone can't be let back in: %+v", st.Skippable)
}
if st := skip(`{"skip":[]}`); len(st.Skip) != 0 || len(st.Groups[0].Members) != 3 {
t.Fatalf("let back in: %+v %+v", st.Skip, st.Groups)
}
}
// groups/delete with ids removes them all at once (lc on Discord), or none
// when it can't remove one.
func TestGroupsDeleteSeveral(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"m", "n"}}); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"a/m", "b/n"}}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
groupRoutes(mux)
del := func(body string) (int, groupsJSON) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/delete", strings.NewReader(body)))
var st groupsJSON
json.Unmarshal(w.Body.Bytes(), &st)
return w.Code, st
}
shown := func(st groupsJSON) (out []string) {
for _, g := range st.Groups {
if !g.Hidden {
out = append(out, g.ID)
}
}
return out
}
if code, _ := del(`{"ids":["auto-m","gone"]}`); code == 200 {
t.Fatal("a group not there was removed")
}
if got := shown(groupsState()); len(got) != 3 {
t.Fatalf("a refused removal removed some: %v", got)
}
code, st := del(`{"ids":["auto-m","mine"]}`)
if code != 200 {
t.Fatalf("%d", code)
}
if got := shown(st); len(got) != 1 || got[0] != "auto-n" {
t.Fatalf("left: %v", got)
}
// one by its id alone, as the editor's Remove does
if code, st := del(`{"id":"auto-n"}`); code != 200 || len(shown(st)) != 0 {
t.Fatalf("%d %v", code, shown(st))
}
}
// The tray panel's pick (#2489) switches a Manual group's member and
// answers with the groups' state, its picked member the new one; a model
// not in the group is refused.
func TestGroupsPick(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-6-astra"}}); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{ID: "hand", Name: "Hand", Members: []string{"a/gpt-6-astra", "b/gpt-6-astra"}, Routing: provider.Manual}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
groupRoutes(mux)
post := func(body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/pick", strings.NewReader(body)))
return w
}
w := post(`{"id":"hand","pick":"b/gpt-6-astra"}`)
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var st struct {
Groups []struct{ ID, Picked string }
}
if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil {
t.Fatal(err)
}
picked := ""
for _, g := range st.Groups {
if g.ID == "hand" {
picked = g.Picked
}
}
if picked != "b/gpt-6-astra" {
t.Fatalf("picked %q in %s", picked, w.Body)
}
if w := post(`{"id":"hand","pick":"c/gpt-6-astra"}`); w.Code < 400 {
t.Fatalf("a model not in the group taken: %d", w.Code)
}
if g, _, _ := provider.FindGroup("group/hand"); g.Pick != "b/gpt-6-astra" {
t.Fatalf("%+v", g)
}
}