Files
magpie/internal/cli/quota_reset_cli_test.go
T
yetone 22512cde53 provider, gateway: rest-cap off never rests a provider's keys after an unknown failure (Kismet Alan on Discord)
A relay or account pool fails now and then with errors that say nothing
of why (a 5xx, a dropped connection), and the next request often goes
through. Every such failure rested the key or account a minute, doubling
up to 10, and `rest-cap` could only move that cap (1 minute at least;
`off` read as `default`).

`magpie provider rest-cap <id> off` (provider.RestCapOff, restCap: -1 in
providers.json) now has a failure that says nothing of why (failOther)
rest nobody and not count toward the backoff: restAfterMarked returns a
Rest by "off" with no time left. What says why still rests as before: a
401 or 403, a model not served (restID), an account's window full, a
rate limit (magpie's own 30-minute cap; RestCapOr treats off as unset),
quota, credit, a restriction. `rest-cap <id> 30s` is turned away and names
1m and off (the first rest is a minute already). The Routing page tells a
try rested by "off" as not resting (one new string, zh/zh-TW/ja/de).
No "fail N times before switching" counter: a failed try already moves
the same request on to the next key or member, so a count would only
keep requests on a failing upstream longer.

Run:
- TestRestCapOff (gateway), TestRestCapCmd (cli), TestOwnRestCap: pass;
  each goes red with the change taken out by hand (the early return
  disabled: "#1 500: rests 59.99s by backoff"; restMinutes in place of
  restCapMinutes: "off: <nil> 0").
- routing-rest-off.test.cjs: 20/20 (chromium, webkit; 1100, 420; en zh
  zh-TW ja de); red 10/10 with the "off" case renamed.
  routing-wb-restricted 20/20. rest-try-now and routing-model-rest fail
  a varying few under load average 200-420 here, and fail more on
  origin/main 331c27703 run at the same time; neither reads "off".
- gofmt -l clean; go vet darwin (provider, gateway, cli, gui), GOOS=linux
  -tags nogui and GOOS=windows (provider, gateway, cli).
- go test ./... with pipefail, sandbox HOME: every package ok except
  agent, gateway and library, which hit the 10m timeout under load
  average ~200 (no test hung: each stack shows a different test seconds
  in). Rerun with a longer -timeout: library ok (603s), gateway ok
  (1572s) and agent ok (1457s).
Not tried: a real relay; the change is in the rest bookkeeping only.
2026-10-14 20:49:20 +08:00

95 lines
3.0 KiB
Go

package cli
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #2008: `magpie provider quota-rest <id> until 00:00+08:00` sets the time
// of day the allowance comes back, a number of minutes after it takes its
// place, and default lets both go.
func TestQuotaRestUntil(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "xk", Name: "xk", Chat: "https://example.invalid/v1", Key: "k"}); err != nil {
t.Fatal(err)
}
find := func() provider.Provider {
p, err := provider.Find("xk")
if err != nil {
t.Fatal(err)
}
return *p
}
if err := quotaRestCmd([]string{"xk", "until", "00:00+08:00"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "00:00+08:00" {
t.Fatalf("until: %q", p.QuotaResetAt)
}
if err := quotaRestCmd([]string{"xk", "until", "25:00"}); err == nil {
t.Fatal("25:00 taken")
}
if err := quotaRestCmd([]string{"xk", "6h"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "" || p.QuotaRest != 360 {
t.Fatalf("6h: %q %d", p.QuotaResetAt, p.QuotaRest)
}
if err := quotaRestCmd([]string{"xk", "at", "8:00"}); err != nil {
t.Fatal(err)
}
if err := quotaRestCmd([]string{"xk", "default"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "" || p.QuotaRest != 0 {
t.Fatalf("default: %q %d", p.QuotaResetAt, p.QuotaRest)
}
}
// #2008: `magpie provider rest-cap <id> 4h` sets where its keys' rests stop
// doubling, and default gives it back to the gateway's own.
func TestRestCapCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "xk", Name: "xk", Chat: "https://example.invalid/v1", Key: "k"}); err != nil {
t.Fatal(err)
}
cap := func() int {
p, err := provider.Find("xk")
if err != nil {
t.Fatal(err)
}
return p.RestCap
}
if err := restCapCmd([]string{"xk", "4h"}); err != nil || cap() != 240 {
t.Fatalf("4h: %v %d", err, cap())
}
if err := restCapCmd([]string{"xk", "9d"}); err == nil || cap() != 240 {
t.Fatalf("9d taken: %d", cap())
}
if err := restCapCmd([]string{"xk", "default"}); err != nil || cap() != 0 {
t.Fatalf("default: %v %d", err, cap())
}
// Kismet Alan on Discord: a relay whose unknown errors pass is set
// never to rest after one, saved and read back so; a minute is the
// shortest cap, and under it is turned away, not read as default
if err := restCapCmd([]string{"xk", "off"}); err != nil || cap() != provider.RestCapOff {
t.Fatalf("off: %v %d", err, cap())
}
if p, _ := provider.Find("xk"); !p.NoFailRest() {
t.Fatal("off not read back as no rest")
}
if err := restCapCmd([]string{"xk", "1m"}); err != nil || cap() != 1 {
t.Fatalf("1m: %v %d", err, cap())
}
if err := restCapCmd([]string{"xk", "30s"}); err == nil || cap() != 1 {
t.Fatalf("30s taken: %v %d", err, cap())
}
}