A free tier's "You've reached today's free-model token quota" says nothing of when it is back, so it rested 15 minutes (or the provider's own quota-rest), and was asked again all day. A vendor Retry-After over an hour was cut to the hour, and that hour then stood over the user's own longer rest. - Provider.QuotaResetAt: "08:00" on this computer's clock, or "00:00+08:00" on the vendor's. `magpie provider quota-rest <id> until 08:00` sets it; a number of minutes set after it replaces it, and default clears both. normalize drops one that isn't a time of day. It is kept by the editor's save and the account copy. - restAfterMarked's quota branch rests until its next occurrence (OwnQuotaRest). A time the vendor names, or a window's end, still stands over it; a Retry-After stands unless it was cut to longestWait while the user's own rest is longer. - Defaults are unchanged: nothing set, nothing differs. Run: go test -tags nogui ./internal/provider ./internal/gateway ./internal/cli ./internal/gui (temp HOME). Broken by hand, each fails: OwnQuotaRest replaced by QuotaRestFor (TestQuotaRestsUntilTheTimeOfDaySet, 3 asserts), the Retry-After guard removed (the 7200 case), the GUI keep removed (TestSaveKeepsQuotaResetAt), default not clearing it (TestQuotaRestUntil). Not tried against a real free-tier vendor.
65 lines
2.4 KiB
Go
65 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// #2008 (Gaudi-XIX): a free tier's daily allowance, refused as xkiro
|
|
// refuses it with no word of when it is back, rests the key until the time
|
|
// of day the user said it comes back, not 15 minutes, nor an hour when the
|
|
// vendor's Retry-After asked for longer than magpie trusts. A Retry-After
|
|
// shorter than that still stands over it.
|
|
func TestQuotaRestsUntilTheTimeOfDaySet(t *testing.T) {
|
|
deadChain(t, "dead-a")
|
|
srv := New()
|
|
daily := []byte(`{"error":{"message":"You've reached today's free-model token quota","type":"rate_limit_error"}}`)
|
|
at := time.Now().UTC().Add(3 * time.Hour).Truncate(time.Minute)
|
|
if err := provider.SetQuotaResetAt("mockdead", at.Format("15:04")+"Z"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := provider.Find("mockdead")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := candidate{p: *p, model: "glm-5.3-flash", rest: "mockdead#x"}
|
|
near := func(r Rest, want time.Time, by string) {
|
|
t.Helper()
|
|
if r.Why != failQuota || r.By != by || r.Until.Before(want.Add(-time.Second)) || r.Until.After(want.Add(time.Second)) {
|
|
t.Errorf("rests %s by %q until %v, want by %q until %v", r.Why, r.By, r.Until, by, want)
|
|
}
|
|
}
|
|
near(srv.restAfter(c, 429, http.Header{}, daily), at, "quota")
|
|
// a Retry-After of two hours, cut to the hour magpie trusts: the
|
|
// user's three hours stand
|
|
near(srv.restAfter(c, 429, http.Header{"Retry-After": {"7200"}}, daily), at, "quota")
|
|
// two minutes is the vendor's word, and stands
|
|
now := time.Now()
|
|
r := srv.restAfter(c, 429, http.Header{"Retry-After": {"120"}}, daily)
|
|
near(r, now.Add(2*time.Minute), "retry-after")
|
|
|
|
// a time already gone by today is tomorrow's
|
|
past := time.Now().UTC().Add(-2 * time.Hour).Truncate(time.Minute)
|
|
if err := provider.SetQuotaResetAt("mockdead", past.Format("15:04")+"+00:00"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = provider.Find("mockdead")
|
|
c.p = *p
|
|
near(srv.restAfter(c, 429, http.Header{}, daily), past.Add(24*time.Hour), "quota")
|
|
|
|
// a number of minutes set after it is the one that counts
|
|
if err := provider.SetQuotaRest("mockdead", 90); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = provider.Find("mockdead")
|
|
c.p = *p
|
|
if p.QuotaResetAt != "" {
|
|
t.Fatalf("a rest of minutes kept the time of day %q", p.QuotaResetAt)
|
|
}
|
|
now = time.Now()
|
|
near(srv.restAfter(c, 429, http.Header{}, daily), now.Add(90*time.Minute), "quota")
|
|
}
|