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)
126 lines
5.2 KiB
Go
126 lines
5.2 KiB
Go
package cli
|
||
|
||
import (
|
||
"encoding/base64"
|
||
"encoding/json"
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// magpie provider account-cap caps a subscription account at a share of
|
||
// each usage window (70, 70%), off lifts it; a share out of 1–99, an
|
||
// account the subscription hasn't, or a provider with keys is an error.
|
||
func TestAccountCapCmd(t *testing.T) {
|
||
home := t.TempDir()
|
||
t.Setenv("HOME", home)
|
||
t.Setenv("USERPROFILE", home)
|
||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||
t.Setenv("DSH_HOME", "")
|
||
jwt := func(m map[string]any) string {
|
||
b, _ := json.Marshal(m)
|
||
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
|
||
}
|
||
auth, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
|
||
"id_token": jwt(map[string]any{"email": "me@example.com"}),
|
||
"access_token": jwt(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
|
||
"refresh_token": "r", "account_id": "acct-1"}})
|
||
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
|
||
if err := os.WriteFile(filepath.Join(home, ".codex", "auth.json"), auth, 0o600); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
provider.ForgetAccounts()
|
||
t.Cleanup(provider.ForgetAccounts)
|
||
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "Me@Example.com", "70%"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if c := provider.AccountCapOf("codex", "me@example.com"); c != 70 {
|
||
t.Fatalf("cap %d", c)
|
||
}
|
||
// shown, for one account or all
|
||
for _, args := range [][]string{{"provider", "account-cap", "codex"}, {"provider", "account-cap", "codex", "me@example.com"}} {
|
||
if err := providerCmd(args); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
for _, bad := range []string{"0.5", "150", "lots"} {
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", bad}); err == nil {
|
||
t.Fatalf("%s taken", bad)
|
||
}
|
||
}
|
||
if c := provider.AccountCapOf("codex", "me@example.com"); c != 70 {
|
||
t.Fatalf("a refused share changed the cap to %d", c)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "nobody@example.com", "50"}); err == nil {
|
||
t.Fatal("an account it hasn't was capped")
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "off"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if c := provider.AccountCapOf("codex", "me@example.com"); c != 0 {
|
||
t.Fatalf("off left %d", c)
|
||
}
|
||
// a window's own (willz on Discord): the five hours at 50%, the week
|
||
// with none, then back to the account's
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window", "5 Hours", "50%"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window=Weekly", "none"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
c := provider.AccountCapsOf("codex", "me@example.com")
|
||
if five, _ := c.Of("5 hours"); five != 50 || c.Windows["weekly"] != 100 || c.All != 0 {
|
||
t.Fatalf("window caps %+v", c)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window", "5 hours"}); err != nil {
|
||
t.Fatal(err) // shown
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window", "5 hours", "lots"}); err == nil {
|
||
t.Fatal("a window's share of lots taken")
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "--window", "5 hours"}); err == nil {
|
||
t.Fatal("a window with no account taken")
|
||
}
|
||
for _, w := range []string{"5 hours", "weekly"} {
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window", w, "default"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
if c := provider.AccountCapsOf("codex", "me@example.com"); c.Windows != nil {
|
||
t.Fatalf("default left %+v", c)
|
||
}
|
||
// a fixed amount of a window's unit (9won_25273 on Discord): 300
|
||
// credits a day, then off; a bad amount, or one with no window, refused
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window", "Daily credits", "--amount", "1,000"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if c := provider.AccountCapsOf("codex", "me@example.com"); c.Amounts["daily credits"] != 1000 {
|
||
t.Fatalf("amount %+v", c)
|
||
}
|
||
for _, bad := range [][]string{{"--window", "Daily credits", "--amount", "-3"}, {"--window", "Daily credits", "--amount", "lots"}, {"--amount", "300"}, {"--window", "Daily credits", "--amount", "300", "50"}} {
|
||
if err := providerCmd(append([]string{"provider", "account-cap", "codex", "me@example.com"}, bad...)); err == nil {
|
||
t.Errorf("%q taken", bad)
|
||
}
|
||
}
|
||
if c := provider.AccountCapsOf("codex", "me@example.com"); c.Amounts["daily credits"] != 1000 {
|
||
t.Fatalf("a refused amount changed it: %+v", c)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "codex", "me@example.com", "--window=Daily credits", "--amount=off"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if c := provider.AccountCapsOf("codex", "me@example.com"); c.Amounts != nil {
|
||
t.Fatalf("off left %+v", c)
|
||
}
|
||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", Chat: "http://127.0.0.1:1/v1"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-cap", "relay", "k1", "50"}); err == nil {
|
||
t.Fatal("a key provider was capped")
|
||
}
|
||
}
|