Files
magpie/internal/gui/groups_test.go
T
yetone fa5295d011 panel: a Manual routing group's model is switched from the tray panel (#2489)
Jerell.OvO asked on Discord (#general) to change a Manual group's model
from the tray's dropdown. Until now that worked only on the group's card
on the Routing page or with `magpie group pick <id> <model>`.

- provider.PickGroup(id, pick) sets a Manual group's Pick and nothing
  else. It refuses a group that doesn't route by hand, a model not in
  the group, and an unknown or hidden group.
- POST /api/groups/pick {id, pick} calls it and answers with the groups'
  state. The panel doesn't send the whole group back the way the card's
  save does, so a stale panel copy can't overwrite an edit made on the
  Routing page in the meantime.
- Tray panel, Routing tab: under today's totals, one row per Manual group
  of two or more members that is on. The row has the group's name and its
  picked member as a sess-pick button. The button opens the app's menu
  (openProtoMenu, no native select), headed "Model every request goes
  to". It lists the members with the card's notes ("fast", the provider,
  and "no provider serves it now" for one not ready), and the picked
  member is ticked. Picking the ticked one posts nothing. No new layout,
  no left border.
- The tab reads the groups again when it is shown, and loadGroups redraws
  the panel. A pick made on the Routing page or from the CLI shows here.
- New string in zh, zh-TW, ja, de.

Tests:
- TestPickGroup (provider): switches and routes to the new member, keeps
  the rest of the group, refuses the three cases and changes nothing
  then. Red with the `g.Pick = pick` line removed.
- TestGroupsPick (gui): /api/groups/pick answers with picked = the new
  member and refuses a non-member.
- panel-routing-pick.test.cjs: a 440px panel in Chromium and WebKit, in
  en, zh, zh-TW, ja and de. 10/10 pass. Red with the row left out of
  renderPanel. Screenshots were checked in de (web) and zh-TW and en
  (app).
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows: clean.
- GUI subset (panel-*, routing-*, i18n-*, header-relang,
  agent-notice-lang; 1199 cases) on 63be47717+this: 1175 pass. Each red
  was rerun file by file on this head and on a clean 63be47717 worktree.
  routing-bodies (10, also red alone on main): #2559. routing-keys-fold:
  #1612. routing-model-rest: #2115. routing-request-metrics (webkit):
  #2571. Those three are red the same on 63be47717. routing-effort-change,
  routing-flood and routing-wb-restricted pass alone on both. None of
  these open the tray panel, and the new code runs only there.

Full suite: `go test -timeout 45m ./...` under set -o pipefail with a
sandbox HOME, on 63be47717+this, load 250-300: 63 packages ok. Red:
internal/agent past 45m (#2544), internal/gateway past 45m (#2108),
TestAgentGoneInTheRetryPause/group_replan (#2543), TestIntentClassifierGroup
(#1813), TestMainWindowCorners (#1936). Each named test passes alone on
this head. A rerun of agent, gateway and gui timed out again in agent and
gateway, with TestMainWindowCorners again the only gui red. The earlier
TestPanelJoinsFullScreenSpaces red is gone after 63be47717. Rebased onto
b2764f7f9, which has six later commits that touch none of these files.
The targeted tests above were rerun there.

Rebased again onto ba132d68b (seven later commits, none touching these
files; one changes app.js). Rerun there: TestPickGroup, TestGroupsPick,
panel-routing-pick.test.cjs 10/10, gofmt -l clean.
2026-10-16 20:03:29 +08:00

194 lines
6.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"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/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)
}
}