Files
magpie/internal/cli/quota_rest_cli_test.go
T
yetone ff930886a2 gateway: keys all out of their allowance are answered from their rests with a Retry-After, not asked again on every request, and a key listed twice is asked once (#1772)
A fleet on four Ollama Cloud keys, every one out of its weekly allowance:
each new request asked every key again (the first twice) and answered
429 with no Retry-After, so an agent that waits as told asked again at
once, and the vendor saw four refused requests a turn.

What changed:
- The first key twice: the reporter's providers.json named the first key
  in "keys" as well as "key". KeysOn now lists a key once.
- When every key left for a request rests out of its allowance (a 429 or
  402 that failure() calls quota or credit, keys only), the request is
  answered from the rest without asking: the vendor's own words, "not
  asked again before HH:MM", and Retry-After until the first of them is
  back (quotaHeld, soonestBack). Accounts are left out, so autoReset
  can still spend a reset for them; a 403 or 400 worded as quota is
  asked as before.
- The last key out of its allowance now rests as the ones before it do
  (before, the last one left was never rested), and a 429 the vendor gave
  no Retry-After for gets the soonest rest's end as one.
- A provider can set how long its keys rest when the vendor says nothing
  of when the allowance is back: quotaRest (minutes) in providers.json,
  or `magpie provider quota-rest <id> 7d`; default stays 15 minutes, at
  most 8 days. A time the vendor names (resets_at, Retry-After, a
  window) still wins. A GUI save keeps it.

What was run:
- TestAllRestingAnswersFromRest, TestFirstKeyListedTwiceAskedOnce and
  TestOwnQuotaRest fail on the old code (no Retry-After, keys re-asked;
  "dead-1 dead-1 dead-2 ok-3", the reporter's log); they pass now, with
  TestQuotaHeldOnlyForKeysOutOfAllowance, TestDeadChainWalksEachOnce and
  TestRestMinutes. The mock answers with the reporter's literal 429 body.
- go test -tags nogui ./internal/provider ./internal/gui pass. A gateway
  package run at load ~60 hit its 20m timeout mid-suite with only
  TestMaxRPM* red; those pass 8/8 alone here and on origin/main
  17c12621b (timing under load, filed as #1791).

Not verified: no run against real Ollama Cloud keys; quotaRest has no
field in the provider editor (CLI and providers.json only).
2026-10-12 22:42:46 +08:00

27 lines
817 B
Go

package cli
import (
"testing"
"time"
)
// #1772: `magpie provider quota-rest <id> 7d` takes a rest as a person says
// it, and says it back the same way.
func TestRestMinutes(t *testing.T) {
for in, want := range map[string]int{"default": 0, "off": 0, "0": 0, "90": 90, "30m": 30, "6h": 360, "7d": 7 * 24 * 60, "1h30m": 90} {
if got, err := restMinutes(in); err != nil || got != want {
t.Errorf("%q: %d %v, want %d", in, got, err, want)
}
}
for _, in := range []string{"soon", "30s", "d"} {
if _, err := restMinutes(in); err == nil {
t.Errorf("%q taken", in)
}
}
for d, want := range map[time.Duration]string{7 * 24 * time.Hour: "7 days", 6 * time.Hour: "6 hours", 90 * time.Minute: "90 minutes"} {
if got := restWords(d); got != want {
t.Errorf("%v: %q, want %q", d, got, want)
}
}
}