Files
magpie/internal/gui/limit_rest_off_test.go
T
yetone c6768f06f6 providers: a provider of the user's can be set never to rest for a limit
zoecats (Discord #feedback, 10-25): a local aggregator answers "quota
exhausted" (429 insufficient_quota) for the moment it switches
upstreams, and magpie set its key aside for 30 minutes, answering every
request itself ("out of its allowance: not asked again before …").

A provider the user added now has, under the editor's Advanced fold,
"Set it aside when out of quota or rate limited" (Rests), ticked by
default, with a note. Unticked (provider.LimitRestOff, limitRestOff in
providers.json), restAfterMarked rests nothing, counts no failure and
sinks nothing for a refusal it reads as out of quota, credit or rate
limited, or any 429: by "off", and the next request is sent to it as
usual. restDrawer rests no image model of it either. A 401, a model not
served and a plain failure still rest it. The default is as before. The
editor offers it only to a provider the user added (custom); a Save that
leaves it out keeps it. A checkbox (tick), no native select; strings in
zh, zh-TW, ja and de. app.js's budget (and the core's) are raised by the
576 bytes the row adds, with no room, after the comment was cut.

Run on origin/main 75e78a5a0 plus these two commits:
- gofmt -l clean; node --check app.js, i18n.js; go vet ./... on darwin
  and GOOS=linux/windows -tags nogui: ok.
- go test -count=1 -timeout 45m ./internal/gateway/ ./internal/gui/
  ./internal/provider/ (suite lock): gateway ok 417.9s, provider ok
  114.4s, gui red only on TestCoreScriptBudget (741 over); after the
  trim and the raise, TestCoreScriptBudget, TestProviderSaveLimitRestOff
  and TestI18n* ok.
- GUI, chromium and webkit: provider-limit-rest (new; en, zh, zh-TW, ja,
  de at 1100 and 420), provider-insecure-tls, i18n-keys, fallback-more:
  128/128. Every provider*, every file opening Advanced, i18n-keys,
  routing-lifted-elsewhere, rest-try-now, rate-limited-sink (58 files):
  1053 of 1055 (on the pre-trim app.js, the trim only a comment and an
  unused class): providers-last-known webkit zh-TW (filed #3565) and
  tencent-access-key webkit en (#2733) red at load ~130; each passes
  alone in WebKit on this tree and on origin/main 5b2b3d3f5.
  After the trim: provider-limit-rest, i18n-keys, provider-insecure-tls
  124/124.
- Sandbox HOME, a dev build of this tree, fake upstream answering 429
  insufficient_quota "quota exhausted": by default the second request was
  magpie's own "not asked again before 13:17" (1 upstream hit). Saved
  limitRestOff:true through /api/provider/save and lifted the rest:
  three requests, three upstream hits, each the vendor's 429; then 429
  rate_limit_error: no rest on the key (each request still asked again
  after 1s, 2s, 4s within itself, as a lone candidate always is); the
  upstream back, 200 at once.

Broken by hand, each red, then restored:
- the check in restAfterMarked off: TestLimitRestOffKeepsAsking ("1
  rests set after the refusal, want none") and
  TestLimitRestOffRestsNothingForALimit ("rests 29m59s by credit").
- the save's keep-when-left-out branch removed: TestProviderSaveLimitRestOff
  ("a Save that left it out turned rests back on").
- app.js's save line removed: provider-limit-rest's first two tests red
  in webkit zh-TW 420.

Not tried: zoecats' own aggregator; the in-request retries on a rate
limit (1s, 2s, 4s) are left as they are, as they are not a rest.

Again after rebasing on origin/main 1b039a3c7 (which changed app.js,
client.js and gateway.go): gofmt, node --check, vet on three OSes ok;
go test ./internal/gui/ ok (the budgets set to this head's sizes);
go test -run 'TestTryNow|TestRenewed|TestLimitRestOff|TestRestCapOff|
TestOwnRestCap|Sink|Unrest|Rest' ./internal/gateway/ ok; GUI, both engines: provider-limit-rest,
i18n-keys, provider-insecure-tls, routing-lifted-elsewhere, rest-try-now,
rate-limited-sink, routing-model-rest, routing-rest-off, routing-rule-drag,
routing-rule-models, view-trackpad-coast, fallback-more: 272/272.
2026-10-25 14:00:59 +08:00

65 lines
1.9 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// zoecats (Discord, 10-25): the editor's "Set it aside when out of quota or
// rate limited" tick is saved on a provider of the user's, opens as
// saved, and is kept by a Save that leaves it out.
func TestProviderSaveLimitRestOff(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(body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
}
off := func() bool {
t.Helper()
p, err := provider.Find("agg")
if err != nil {
t.Fatal(err)
}
if info := providerInfo(*p, nil); info.LimitRestOff != p.LimitRestOff {
t.Fatalf("the editor is shown %v, saved %v", info.LimitRestOff, p.LimitRestOff)
}
return p.LimitRestOff
}
const edit = `{"id":"agg","from":"agg","name":"Aggregator","chat":"http://127.0.0.1:8317/v1","icon":"generic","models":[],"headers":{}`
post(`{"id":"agg","name":"Aggregator","key":"sk-a","chat":"http://127.0.0.1:8317/v1","icon":"generic","new":true}`)
if off() {
t.Fatal("added without it, saved never resting")
}
post(edit + `,"limitRestOff":true}`)
if !off() {
t.Fatal("unticked, still rests")
}
post(edit + `}`)
if !off() {
t.Fatal("a Save that left it out turned rests back on")
}
post(edit + `,"limitRestOff":false}`)
if off() {
t.Fatal("ticked again, still never rests")
}
post(`{"id":"agg2","name":"Aggregator 2","key":"sk-b","chat":"http://127.0.0.1:8318/v1","icon":"generic","new":true,"limitRestOff":true}`)
if p, _ := provider.Find("agg2"); p == nil || !p.LimitRestOff {
t.Fatal("added unticked, saved resting")
}
}