Files
magpie/internal/budget/total_test.go
T
yetone ab5824aeea gateway keys: a limit in total never resets by the period, only by Reset usage (Cyrus_4433 on X)
The Limit window menu gains "in total" (period "total", CLI: gateway-key limit <id> total). It counts from when it is set or last reset and ends a century off (access.Limit.Window), so only Reset usage starts it again; its 429 says it stays refused until reset or raised, the row reads "$0.00 / $300 · 总额" with no reset time, and a remote key's window is "Tokens/Spend in total" with no reset. Old settings files read the same (new period only). i18n in zh, zh-TW, ja, de; reference.md and gateway-routing.md say it.

Run: TestLimitTotal and TestTotalLimitNeverResetsByThePeriod, red on the old code (Valid refuses "total"); gateway-key-total.test.cjs red without the keys.js change (5 of 5). Not tried: a real key through a live gateway; full suite follows after the tag.

Run (before this push):
- gofmt -l clean; go vet darwin, linux (nogui), windows clean.
- go test: internal/access, internal/budget, internal/cli, internal/provider ok.
- Go tests for the total period are red on the old code (Valid refuses "total"); the GUI test is 0 of 5 languages passing without the keys.js change.
- gateway-key-total.test.cjs and i18n-keys: 9 pass in chromium and in webkit (zh, zh-TW, ja, de, en; 500px width; no native select).
- Full GUI suite on mica, chromium: 46 reds in files this change does not touch (accounts, Pi/OpenCode Go, panel sheet, check-in, Aside provider, API key theme, allowed addresses); none is a key-limit test. Webkit pass still running; reds are to be compared with origin/main and filed after.
- TestCoreScriptBudget is red on origin/main 2d12e8274 by 7 bytes (1,533,809 > 1,533,802); this change touches no counted core script (#5918).
Not tried: a real key through a live gateway; the full Go suite (started after the tag).
2026-11-11 20:35:13 +08:00

90 lines
2.7 KiB
Go

package budget
import (
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/usage"
)
// A "total" limit (@Cyrus_4433 on X) never resets by the clock: what a
// key spent 100 days ago still counts against it, where a month limit has
// forgotten it, and only the key's Reset usage starts it again.
func TestTotalLimitNeverResetsByThePeriod(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
now := time.Now().Round(0)
long := now.AddDate(0, 0, -100)
// the limits are set 120 days ago, so a total one counts from then
access.Now = func() time.Time { return now.AddDate(0, 0, -120) }
t.Cleanup(func() { access.Now = time.Now })
newKey := func(name, period string) access.Key {
t.Helper()
if _, err := access.Update("add-key", access.Change{Name: name}); err != nil {
t.Fatal(err)
}
ks, _ := access.List()
for _, k := range ks {
if k.Name == name {
if _, err := access.Update("limit-key", access.Change{Key: k.ID, Limit: &access.Limit{Period: period, Tokens: 1000}}); err != nil {
t.Fatal(err)
}
}
}
ks, _ = access.List()
for _, k := range ks {
if k.Name == name {
return k
}
}
t.Fatal("no key")
return access.Key{}
}
refused := func(k access.Key) *Refusal {
t.Helper()
who := access.Identity{KeyID: k.ID, KeyName: k.Name, Limit: k.Limit, More: k.More}
_, r := Reserve(who, -1, "", now)
return r
}
for _, name := range []string{"Total", "Month"} {
period := strings.ToLower(name)
k := newKey(name, period)
Append(usage.Record{Time: long, Provider: "p", Model: "m", Input: 1000, Status: 200, CallerKeyID: k.ID})
r := refused(k)
if period == "month" && r != nil {
t.Fatalf("a month limit still counts a call of 100 days ago: %v", r)
}
if period == "total" {
if r == nil {
t.Fatal("a total limit forgot a call of 100 days ago")
}
if !strings.Contains(r.Error(), "total limit") || strings.Contains(r.Error(), "resets at") {
t.Errorf("refusal: %s", r.Error())
}
if r.RetryAfter(now) < 365*24*3600 {
t.Errorf("retry after %ds", r.RetryAfter(now))
}
// Reset usage counts it again from now
access.Now = func() time.Time { return now.Add(-time.Minute) }
if _, err := access.Update("reset-limit-key", access.Change{Key: k.ID}); err != nil {
t.Fatal(err)
}
ks, _ := access.List()
for _, x := range ks {
if x.ID == k.ID {
k = x
}
}
if r := refused(k); r != nil {
t.Fatalf("still refused after Reset usage: %v", r)
}
}
}
}