Files
magpie/internal/gui/group_defaults_test.go
T
yetone 6b7ee4a984 routing: every group follows a default for rate limits, slow starts and busy members unless it sets its own
jiezhengj on Discord asked to set the per-group policies once instead of
in each group. The Routing groups page's Settings card, under the list,
now has three rows: Rate limited (keeps its place / goes to the back),
Slow to start (wait / 30 s ... 4 min) and Every member busy (off / 1 min
... 4 min). They are saved in settings.json as groupSink, groupFirstToken
and groupPatience (settings.SetGroupDefaults, POST /api/groups/defaults).

A group follows the default for each value it leaves unset, and its own
value always wins:
- Sinks() is the group's Sink, or else the default unless NoSink, a new
  field for an explicit "Keeps its place".
- FirstTokenOr() is the group's own FirstToken (-1, FirstTokenWait, is an
  explicit Wait), or else the default. The gateway reads it where it
  reads a group's first-token time.
- Waits() reads the default when Patience is 0. 60 is an explicit 1 min.
Groups saved before have 0 and no NoSink, so they follow the default,
which starts at magpie's own (keep its place, wait, 60s). Nothing a user
set changes. The group editor's three rows gain a Default option, on for
a group that sets none, and its hint names the value it follows. Segments
only, with no native select.

Run on mote, at 06b2daeff rebased on origin/main 2dac9b84e:
- go test on the new tests TestGroupDefaultsUnlessItsOwn,
  TestGroupFirstTokenFollowsTheDefault, TestGroupSinkFollowsTheDefault
  and TestGroupDefaultsSaved, plus TestKeysAreWhatTheSaveWrites and the
  existing first-token and sink tests: ok.
- Broken by hand (Waits, FirstTokenOr and Sinks ignoring settings): the
  three provider/gateway tests went red.
- GUI: group-defaults (new), group-patience, group-first-token,
  group-found-off and rate-limited-sink, in Chromium and WebKit, in en,
  zh, zh-TW, ja and de, at 1100 and 420: 56/56 pass. With the old
  client.js and routing.css, group-defaults, group-patience and
  group-first-token fail 44 of 44.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...:
  clean. gofmt -l: clean.

Still running when this was pushed (owner: never hold a verified commit
behind the suite): go test -timeout 45m on internal/provider,
internal/gateway, internal/gui, internal/settings and
internal/settingsapi, then the whole GUI suite under suite.lock. Any red
they show gets its own issue.

Not tried: the real app with real groups under real rate limits. Only
the gateway tests' fakes exercise the default sink and first-token paths.

Reported-by: jiezhengj on Discord
2026-11-08 02:08:30 +08:00

73 lines
2.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The Routing groups page's Settings card sets what every group follows
// when it sets nothing of its own (jiezhengj on Discord); the page reads
// it back in /api/groups, a bad value is refused, and the groups and the
// rest of the settings are left as they were.
func TestGroupDefaultsSaved(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())
s := settings.Load()
s.ClassifyTimeout = 9
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "a", Name: "a", Key: "ka", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{ID: "own", Name: "Own", Members: []string{"a/m1"}, FirstToken: provider.FirstTokenWait}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
groupRoutes(mux)
post := func(body string) (int, groupsJSON) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/groups/defaults", strings.NewReader(body)))
var out groupsJSON
if w.Code == 200 {
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
}
return w.Code, out
}
code, got := post(`{"sink":true,"firstToken":60,"patience":-1}`)
if code != 200 || got.Defaults != (groupDefaultsJSON{Sink: true, FirstToken: 60, Patience: -1}) {
t.Fatalf("%d %+v", code, got.Defaults)
}
s = settings.Load()
if !s.GroupSink || s.GroupFirstToken != 60 || s.GroupPatience != -1 || s.ClassifyTimeout != 9 {
t.Fatalf("settings: %v %d %d %d", s.GroupSink, s.GroupFirstToken, s.GroupPatience, s.ClassifyTimeout)
}
if g, _, _ := provider.FindGroup("group/own"); g.FirstToken != provider.FirstTokenWait || g.FirstTokenOr() > 0 || !g.Sinks() || g.Waits() != 0 {
t.Fatalf("group: %+v", g)
}
if st := groupsState(); st.Defaults != (groupDefaultsJSON{Sink: true, FirstToken: 60, Patience: -1}) {
t.Fatalf("state: %+v", st.Defaults)
}
if code, _ := post(`{"patience":999}`); code == 200 {
t.Fatal("patience 999 saved")
}
if s := settings.Load(); s.GroupPatience != -1 {
t.Fatalf("after a refusal: %d", s.GroupPatience)
}
// back to magpie's own
if code, got := post(`{}`); code != 200 || got.Defaults != (groupDefaultsJSON{}) {
t.Fatalf("cleared: %d %+v", code, got.Defaults)
}
}