A window magpie reads with a total and a unit (a day's 1000 credits, a
plan's requests) can now be capped at an amount of that unit, not only
a share: 9won_25273 asked to stop an account at 300 of its day's credits.
- AccountWindowAmounts keeps it per account and window (capId). An
amount and a window's own share replace each other.
- WindowCaps.limitShare turns it into a share of the total for the mark
and NearCap. CapHeld and capReached hold the account once the window's
count reaches the amount (past), and rest it as the percentage cap
does: the next account takes the request. cappedError and the trace
say "has used 300 credits ... past its cap of 300".
- A reading with no total ignores the amount; the account's share
applies.
- GUI: the window meter's menu has one more item, Amount..., typed in
place (provider/windowcap with amount). It shows only on a window
with a unit and total. Strings in zh, zh-TW, ja, de.
- CLI: magpie provider account-cap <id> <account> --window <name>
--amount <n|off>.
Usage outside magpie counts: every cap reads the vendor's own count, so
what the account spent in the vendor's app or on another machine counts
from the next reading on. magpie can only hold back its own requests.
Run, at this head on Linux (wsl-run, magpie-desktop):
- gofmt -l: clean; go vet ./... for linux (-tags nogui) and windows: ok (darwin
vet needs cgo, so it runs with the full suite on mote afterwards)
- go test -run 'Lang|ToolLoop|LoopGuard|Cap|Amount' ./internal/gateway
./internal/provider ./internal/cli: ok (TestWindowAmountCapHeld,
TestSetWindowAmount, TestWindowAmountHoldsTheAccount, the CLI cases)
- GUI window-cap, window-cap-pool, account-cap in chromium and webkit,
420 and 1100 wide, en zh zh-TW ja de: 78 of 78 in each engine. An
earlier webkit run lost the amount test's page once ("Target page,
context or browser has been closed"); it passed in 2 more full-file
runs and 60 of 60 alone
- i18n-keys: only "a plugin's words are its own" red, words that aren't
this change's (#5332, #5426, #5482)
Broken by hand: limitShare ignoring amounts (of < 0): TestWindowAmountCapHeld
and TestWindowAmountHoldsTheAccount go red.
Not tried: the full go test ./... at this head (queued on mote, runs
after); a real account at an amount cap.
iFlytek: 9won's 讯飞 overshoot isn't covered. Loomy's daily window sends
used as a count and amount as what is left (plugins#889); AStudio's
wallets tell a balance only, which no cap can hold (#5770).
Reported-by: 9won_25273 (Discord)
78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// 9won_25273 on Discord: an account capped at 300 of a day's 1000 credits
|
|
// is held once the vendor's count of the day reaches 300, and the request
|
|
// goes to the next account; under it, it is used; every account at its
|
|
// cap is refused, named with the count and the amount.
|
|
func TestWindowAmountHoldsTheAccount(t *testing.T) {
|
|
codexSignedIn(t, "spare@example.com")
|
|
b := newResetBackend(t)
|
|
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, u := range []string{"me@example.com", "spare@example.com"} {
|
|
if err := provider.SetWindowAmount("codex", u, []string{"Daily credits"}, 300); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
try := func(count map[string]float64) (int, string, string, *Server) {
|
|
t.Helper()
|
|
old := allowances
|
|
t.Cleanup(func() { allowances = old })
|
|
day := time.Now().Add(5 * time.Hour)
|
|
allowances = func(string) map[string]provider.Allowance {
|
|
out := map[string]provider.Allowance{}
|
|
for u, n := range count {
|
|
out[u] = provider.Allowance{{Name: "Daily credits", Used: n / 10, Amount: n, Of: 1000, Unit: "credits", Resets: day, Span: 24 * time.Hour}}
|
|
}
|
|
return out
|
|
}
|
|
b.mu.Lock()
|
|
b.tried = nil
|
|
b.mu.Unlock()
|
|
srv := New()
|
|
code, body := resetPost(t, srv)
|
|
tried, _ := b.seen()
|
|
return code, body, tried, srv
|
|
}
|
|
|
|
code, body, tried, srv := try(map[string]float64{"me@example.com": 320, "spare@example.com": 40})
|
|
if code != 200 || tried != "acct-2" {
|
|
t.Fatalf("320 of 300 credits: %d %s tried %s", code, body, tried)
|
|
}
|
|
var left []string
|
|
for _, r := range srv.Trace(t.Context(), 0, 0).Routes {
|
|
for _, w := range r.Left {
|
|
if w.Capped > 0 && !w.Barred {
|
|
left = append(left, fmt.Sprintf("%s %g/%g %s", w.Who, w.CapCount, w.CapAmount, w.CapUnit))
|
|
}
|
|
}
|
|
}
|
|
if len(left) != 1 || left[0] != "me@example.com 320/300 credits" {
|
|
t.Fatalf("traced as capped: %v", left)
|
|
}
|
|
|
|
if code, body, tried, _ := try(map[string]float64{"me@example.com": 299, "spare@example.com": 40}); code != 200 || tried != "acct-1" {
|
|
t.Fatalf("299 of 300 credits: %d %s tried %s", code, body, tried)
|
|
}
|
|
|
|
code, body, tried, _ = try(map[string]float64{"me@example.com": 300, "spare@example.com": 412.5})
|
|
if code != 429 || tried != "" {
|
|
t.Fatalf("both at their amount: %d tried %q", code, tried)
|
|
}
|
|
for _, want := range []string{"usage cap reached", "(me@example.com) has used 300 credits", "(spare@example.com) has used 412.5 credits", "past its cap of 300"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("no %q in %s", want, body)
|
|
}
|
|
}
|
|
}
|