Files
magpie/internal/provider/grouppick_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

83 lines
2.3 KiB
Go

package provider
import (
"strings"
"testing"
)
// A Manual group switched from the tray panel (#2489) sends every request
// to the member picked, from the next request on, and keeps the rest of
// itself; a group that doesn't route by hand, a model not in it, or a
// group magpie hasn't are refused and change nothing.
func TestPickGroup(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := Save(Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
t.Fatal(err)
}
}
if err := SaveGroup(Group{Name: "Hand", Members: []string{"a/x", "b/x"}, Routing: Manual, Patience: 45}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{Name: "Order", Members: []string{"a/x", "b/x"}, Routing: Ordered}); err != nil {
t.Fatal(err)
}
routes := func(id string) string {
t.Helper()
_, ms, ok := FindGroup("group/" + id)
if !ok {
return "-"
}
var out []string
for _, m := range ms {
out = append(out, m.Provider.ID+"/"+m.Model)
}
return strings.Join(out, " ")
}
if s := routes("hand"); s != "a/x" {
t.Fatalf("before: %s", s)
}
if err := PickGroup("hand", "b/x"); err != nil {
t.Fatal(err)
}
if s := routes("hand"); s != "b/x" {
t.Fatalf("picked b/x, routes to %s", s)
}
g := mustGroup(t, "hand")
if g.Routing != Manual || g.Name != "Hand" || len(g.Members) != 2 || g.Patience != 45 {
t.Fatalf("the rest of the group changed: %+v", g)
}
for _, c := range []struct{ id, pick, want string }{
{"order", "b/x", "Manual"},
{"hand", "c/x", "not in"},
{"nope", "a/x", "no group"},
} {
if err := PickGroup(c.id, c.pick); err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("PickGroup(%s, %s) = %v, want %q", c.id, c.pick, err, c.want)
}
}
if s := routes("hand"); s != "b/x" {
t.Fatalf("a refused pick moved it: %s", s)
}
if s := routes("order"); s != "a/x b/x" {
t.Fatalf("a refused pick changed the ordered group: %s", s)
}
}
func mustGroup(t *testing.T, id string) Group {
t.Helper()
f, err := read()
if err != nil {
t.Fatal(err)
}
for _, g := range f.Groups {
if g.ID == id {
return g
}
}
t.Fatalf("no group %s", id)
return Group{}
}