Files
magpie/internal/provider/group_order_test.go
T
yetone 855e7985f2 routing: the groups are put in an order of the user's, as the Agents page's rows are, and /v1/models lists them so (#779)
The Routing page's group list followed the order the groups were made in, found ones after, so a group used every day couldn't be put first, though /v1/models lists the groups in that order and agents' pickers (Claude Desktop's 3P list among them) show it and some clients probe its first model. A group row's logos are now its handle, as an agent row's logo is (#57): dragged (dragRows, the Agents page's sorter) it moves the row, clicked, or the row right-clicked, it opens Move up and Move down (openRowMenu, with the Agents menu's arrows, now MOVE_UP/MOVE_DOWN), and Alt+↑/↓ moves it from the keyboard, which stays on it. The grip in the row's margin is drawn only while the row is under the pointer, dragged, or its handle is focused or its menu open, as on the Agents page. The list is drawn in its new order at once and put back if the save fails; a later move wins over an earlier one still saving.

The order is kept in providers.json as groupOrder (provider.SetGroupOrder, POST /api/groups/arrange), beside the providers' own order (#499), and applied in groupsIn, which every list of the groups is made from: the Routing page, Catalog and so /v1/models, magpie group ls and the TUI's routing page. A found group, which isn't stored until changed, can be put among the user's this way, which reordering the groups array couldn't do. A group not named (one made or found after) goes last; a renamed one keeps its place. Backup and sync carry the order as they do the providers' (MirrorGroupOrder).

TestGroupOrder (Groups and the catalog follow SetGroupOrder, providers.json has it, a new group last, a rename in place, bad orders refused, a synced order first) fails on the old code. group-arrange.test.cjs (WebKit, en and zh): the grip hidden with the pointer away and shown on the hovered row only, in its margin; right-click Move down with Move up off on the first; Alt+↓ and Alt+↑ with focus kept; the handle's click opens the menu, not the editor; a drag by the grip to the top; each posting every group's id with the removed one last; no click moving the page. It fails on the old code. agent-grip, click-scroll, account-arrange, usage-arrange, provider-order and the group-*/routing-* GUI tests pass in WebKit, but for group-from-model-add's 600x420 cases, which fail on main as well. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 19:27:51 +08:00

99 lines
3.0 KiB
Go

package provider
import (
"encoding/json"
"os"
"slices"
"strings"
"testing"
)
// #779: the routing groups can be put in an order of the user's, a found
// one among them, and every list of them follows it: Groups (the Routing
// page, the CLI, the TUI) and the catalog, which /v1/models is. It is kept
// in providers.json; a group made after goes last, a renamed one keeps its
// place, and sync brings the other computer's order first.
func TestGroupOrder(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := Save(Provider{ID: id, Name: id, Key: "sk-" + id, Chat: "https://" + id + ".example.com/v1", Models: []string{"m", "x-" + id}}); err != nil {
t.Fatal(err)
}
}
for _, g := range []Group{{ID: "one", Members: []string{"a/x-a"}}, {ID: "two", Members: []string{"b/x-b"}}} {
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
}
found := AutoGroupID("m")
groups := func() []string {
var out []string
for _, g := range Groups() {
out = append(out, g.ID)
}
return out
}
// the groups at the head of the catalog, as /v1/models lists them
catalogGroups := func() []string {
var out []string
for _, e := range Catalog() {
if id, ok := strings.CutPrefix(e.ID, GroupPrefix); ok {
out = append(out, id)
}
}
return out
}
if got := groups(); !slices.Equal(got, []string{"one", "two", found}) {
t.Fatalf("as made: %v", got)
}
if err := SetGroupOrder([]string{found, "two"}); err != nil {
t.Fatal(err)
}
want := []string{found, "two", "one"}
if got := groups(); !slices.Equal(got, want) {
t.Fatalf("Groups after SetGroupOrder(%s, two): %v", found, got)
}
if got := catalogGroups(); !slices.Equal(got, want) {
t.Fatalf("catalog after SetGroupOrder(%s, two): %v", found, got)
}
if e := Catalog(); len(e) == 0 || e[0].ID != GroupPrefix+found {
t.Fatalf("the catalog's first model isn't %s", GroupPrefix+found)
}
b, err := os.ReadFile(Path())
if err != nil {
t.Fatal(err)
}
var f struct {
GroupOrder []string `json:"groupOrder"`
}
if err := json.Unmarshal(b, &f); err != nil || !slices.Equal(f.GroupOrder, want) {
t.Fatalf("providers.json's groupOrder: %v (%v)", f.GroupOrder, err)
}
if err := SaveGroup(Group{ID: "three", Members: []string{"a/m"}}); err != nil {
t.Fatal(err)
}
if got := groups(); !slices.Equal(got, []string{found, "two", "one", "three"}) {
t.Fatalf("three made: %v", got)
}
if err := RenameGroup("two", "deux"); err != nil {
t.Fatal(err)
}
if got := groups(); !slices.Equal(got, []string{found, "deux", "one", "three"}) {
t.Fatalf("two renamed: %v", got)
}
for _, bad := range [][]string{{"one", "one"}, {"nope"}} {
if err := SetGroupOrder(bad); err == nil {
t.Fatalf("SetGroupOrder(%v) taken", bad)
}
}
if err := MirrorGroupOrder([]string{"three", "one"}); err != nil {
t.Fatal(err)
}
if got := groups(); !slices.Equal(got, []string{"three", "one", found, "deux"}) {
t.Fatalf("mirrored (three, one): %v", got)
}
}