Files
magpie/internal/gui/providers_routing_test.go
T
yetone af046769fb routing: an account rate limited with quota left can go to the back of the order, so several accounts take turns (01huadalang on Discord: with several WorkBuddy accounts, one rate limited rests and then goes back to the front, so it takes everything until it is limited again) — a new opt-in setting, "Rate limited" (zh 被限流时), sits next to Routing for a provider (sink in its JSON) and for a routing group (sink on the group). It has two options: "Keeps its place" (the default, unchanged) or "Goes to the back". With "Goes to the back", an account or key that answers 429 while its subscription still has quota (failRate, not a quota or credit failure, and not a used-up allowance) rests as before. Once the rest ends it stays behind every one not rate limited since; those sunk are ordered by when they sank, so the load goes a,b,c → b,c,a → c,a,b. In a group it covers the members' accounts and keys together. The sink is kept in memory until magpie restarts or the account sinks again. It applies to every routing but In turn (which goes round already) and Manual, and is hidden there.
Where it is set:
- the provider editor's "Rate limited" row, saved with its Save (sink in /api/provider/save, kept by a Save that leaves it out);
- the Routing page's row of providers with several accounts or keys, which saves at once (/api/provider/sink);
- the group editor (/api/groups/save), with the group card tagged "Rate limited to the back".

The Routing page's trace gives a sunk account "rate limited at {time} · at the back" and a line saying why. Weighed carries `sunk` and GroupRef carries `sink`.

Tests:
- TestRateLimitedSinksToTheBack: three accounts rotate over five rate limits. Off or in turn they keep their order. A quota 429 or a full allowance doesn't sink one, and one still resting stays behind.
- TestGroupRateLimitedSinks: ordered and smart groups.
- TestProviderSaveSink.
- rate-limited-sink.test.cjs covers the editor, pool row, group editor, card tag, trace and zh, in en and zh. It fails on the old code and on the old assets.

routing-compact.test.cjs now wheels down to "Add a rule" and Save as the reader would, since the editor's new row puts them past the window.

go vet, builds (nogui, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. In webkit, the routing-*, group-*, quota-pools, panel-routing, click-scroll and settings-groups GUI tests pass, except routing-manual, routing-time and group-from-model-add, which fail the same way on origin/main. Not tried with real accounts against a vendor's 429.
2026-10-04 01:20:51 +08:00

106 lines
3.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A provider's Routing and Stays are picked in its editor and made with its
// Save (01huadalang on Discord: each click saved at once); a Save that
// doesn't carry them keeps them, where the Stays set on the Routing page
// used to be lost at the editor's next Save, which never sent it.
func TestProviderSaveRouting(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())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(action, body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", action, w.Code, w.Body)
}
}
is := func(routing, affinity string) {
t.Helper()
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
if p.Routing != routing || p.Affinity != affinity {
t.Fatalf("routing %q, affinity %q; want %q, %q", p.Routing, p.Affinity, routing, affinity)
}
}
const form = `"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","models":["sol"]`
post("save", `{`+form+`,"key":"k","new":true}`)
is("", "")
// the editor's picks, made with its Save
post("save", `{`+form+`,"from":"relay","routing":"rotate","affinity":"session"}`)
is(provider.Rotate, provider.AffinitySession)
// a Save that doesn't carry them keeps them
post("save", `{`+form+`,"from":"relay"}`)
is(provider.Rotate, provider.AffinitySession)
// set on their own, from the Routing page, and kept by the next Save
post("route", `{"id":"relay","routing":"order"}`)
post("affinity", `{"id":"relay","affinity":"turn"}`)
is(provider.Ordered, provider.AffinityTurn)
post("save", `{`+form+`,"from":"relay"}`)
is(provider.Ordered, provider.AffinityTurn)
// Smart and Auto picked are "" sent, not left out
post("save", `{`+form+`,"from":"relay","routing":"","affinity":""}`)
is("", "")
}
// Sink (01huadalang: one rate limited with quota left goes to the back)
// is picked in the editor and made with its Save, or set on its own from
// the Routing page, and a Save that doesn't carry it keeps it.
func TestProviderSaveSink(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())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(action, body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", action, w.Code, w.Body)
}
}
is := func(want bool) {
t.Helper()
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
if p.Sink != want {
t.Fatalf("sink %v, want %v", p.Sink, want)
}
if got := providerInfo(*p, nil).Sink; got != want {
t.Fatalf("listed sink %v, want %v", got, want)
}
}
const form = `"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","models":["sol"]`
post("save", `{`+form+`,"key":"k","new":true}`)
is(false)
post("save", `{`+form+`,"from":"relay","routing":"order","sink":true}`)
is(true)
post("save", `{`+form+`,"from":"relay"}`)
is(true)
post("sink", `{"id":"relay","sink":false}`)
is(false)
post("sink", `{"id":"relay","sink":true}`)
post("save", `{`+form+`,"from":"relay"}`)
is(true)
}