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

71 lines
2.7 KiB
Go

package provider
import (
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// What a group sets wins; what it leaves unset follows the default for
// every group in settings (jiezhengj on Discord), and with that unset too,
// magpie's own: wait for a first token, keep one's place, a minute's
// patience.
func TestGroupDefaultsUnlessItsOwn(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
none := Group{}
if none.Waits() != PatienceDefault*time.Second || none.FirstTokenOr() != 0 || none.Sinks() {
t.Fatalf("magpie's own: %s %d %v", none.Waits(), none.FirstTokenOr(), none.Sinks())
}
if err := settings.SetGroupDefaults(true, 45, 200); err != nil {
t.Fatal(err)
}
if none.Waits() != 200*time.Second || none.FirstTokenOr() != 45 || !none.Sinks() {
t.Fatalf("default: %s %d %v", none.Waits(), none.FirstTokenOr(), none.Sinks())
}
own := Group{Patience: PatienceDefault, FirstToken: FirstTokenWait, NoSink: true}
if own.Waits() != PatienceDefault*time.Second || own.FirstTokenOr() > 0 || own.Sinks() {
t.Fatalf("own: %s %d %v", own.Waits(), own.FirstTokenOr(), own.Sinks())
}
if err := settings.SetGroupDefaults(false, 0, PatienceOff); err != nil {
t.Fatal(err)
}
if none.Waits() != 0 {
t.Fatalf("default off: %s", none.Waits())
}
if g := (Group{Patience: 120, Sink: true, FirstToken: 30}); g.Waits() != 120*time.Second || !g.Sinks() || g.FirstTokenOr() != 30 {
t.Fatalf("own over off: %s %v %d", g.Waits(), g.Sinks(), g.FirstTokenOr())
}
// a value no setting can hold is refused, and the file kept
for _, bad := range [][2]int{{-2, 0}, {0, -2}, {0, PatienceLongest + 1}} {
if err := settings.SetGroupDefaults(false, bad[0], bad[1]); err == nil {
t.Fatalf("%v saved", bad)
}
}
if none.Waits() != 0 {
t.Fatalf("after refusals: %s", none.Waits())
}
// Wait, a minute and Keeps its place are kept as the group's own
if err := Save(Provider{ID: "a", Name: "a", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{Name: "Own", Members: []string{"a/x"}, Routing: Ordered, FirstToken: FirstTokenWait, Patience: PatienceDefault, NoSink: true}); err != nil {
t.Fatal(err)
}
g, _, _ := FindGroup("group/own")
if g.FirstToken != FirstTokenWait || g.Patience != PatienceDefault || !g.NoSink {
t.Fatalf("saved: %+v", g)
}
// Sink and NoSink both: Sink
if err := SaveGroup(Group{ID: "own", Name: "Own", Members: []string{"a/x"}, Routing: Ordered, Sink: true, NoSink: true}); err != nil {
t.Fatal(err)
}
if g, _, _ := FindGroup("group/own"); !g.Sink || g.NoSink {
t.Fatalf("both: %+v", g)
}
}