Files
magpie/internal/provider/account_window_test.go
T
yetone 6256d9956a gateway keys: a limit can follow a subscription account's own window (ljayic on X)
A key's limit gains the period "account": it names one subscription
account ("<provider>/<user>") and one of its windows (5h, Weekly, Monthly,
...), and starts and resets when that account's window does, at the reset
time in the account's last good reading (quotas.json), never asked of the
vendor on the request's path (provider.AccountWindow).

When the window can't be read (no reading yet, no such window, or its reset
has passed with no reading since), the limit is "reset unknown": it is
neither reset nor lifted. It counts from the latest time the window can't
have begun before (the reset that passed, else when the limit was set), so
it counts at least the window's calls; a refused call gets Retry-After 300
and a 429 saying the window can't be read now, and the accounts are read
again in the background at most once a minute.

GUI: Gateway keys -> a key's limit -> Window -> "An account's window", then
an app-menu picker of the accounts' windows as read (GET
/api/caller-keys/account-windows); the row reads "$x / $y · 5h of
codex/...", and an unknown reset reads "resets with the account's window".
CLI: magpie gateway-key limit <id> account --account codex/me@example.com
--window 5h --tokens 2m. i18n zh, zh-TW, ja, de. reference.md and
gateway-routing.md say it, with the fallback.

Run (at this tree, base ec8125e68, rebased onto 1ac137e10 which touches
none of these files):
- gofmt -l clean; go vet darwin, linux (-tags nogui), windows clean (mica).
- go test -count=1: internal/access, internal/budget, internal/provider,
  internal/cli, internal/gui ok (mica). TestEveryRouteHasACommandOrIsNamed
  was red until the new route was named (magpie quota).
- New: TestAccountWindowLimit (budget), TestAccountWindowFromLastReading and
  TestReadAccountWindowsOnceAMinute (provider), TestParseAccountWindowLimit
  (cli), TestCallerKeyAccountWindowsRoute (gui). Broken by hand: the unknown
  branch made to guess a 5h window from now -> TestAccountWindowLimit red
  ("let through on a window not read").
- GUI: gateway-key-account-window.test.cjs, chromium + webkit, en zh zh-TW
  ja de, 500px, no native select: 10 of 10 (red without the keys.js and
  i18n change). Every GUI file that loads the caller-keys fixture or the
  Gateway keys page, plus i18n*: 1064 tests, 1046 pass, 18 red, the same
  18 red on origin/main ec8125e68 alone: api-key-theme (6, #6444),
  gateway-key-arrange 420px (10, #6268), text-size webkit (a subtest and its test, #6561).
Not tried: a real Codex/Claude account's window through a live gateway;
the full GUI suite (mote and mica each had a full suite running).
2026-11-15 03:50:46 +08:00

89 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package provider
import (
"sync/atomic"
"testing"
"time"
)
// A key's limit that follows an account's window (ljayic on X) finds it
// in the account's last good reading, read again from disk after a
// restart: when it began and when it resets. Past its reset, or with no
// reading, it is unknown, never a window guessed, and the accounts are
// asked to be read again without waiting.
func TestAccountWindowFromLastReading(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
restart := func() {
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
}
restart()
defer restart()
asked := 0
was := readAccountWindows
readAccountWindows = func(time.Time) { asked++ }
defer func() { readAccountWindows = was }()
now := time.Now().Round(time.Second)
reset := now.Add(2 * time.Hour)
week := now.Add(3 * 24 * time.Hour)
keepLast(SubscriptionQuota{Provider: "codex", Name: "Codex", User: "Me@example.com", Windows: []QuotaWindow{
{Name: "5h", Used: 40, ResetsAt: &reset, Span: 5 * time.Hour},
{Name: "Weekly", Used: 10, ResetSecs: int64(week.Sub(now) / time.Second)},
{Name: "Credits", Display: "12", Text: true},
}}, "")
restart() // from disk, as after a restart: the span isn't in the window's json
start, end, known := AccountWindow("codex/me@example.com", "5H", now)
if !known || !end.Equal(reset) || !start.Equal(reset.Add(-5*time.Hour)) {
t.Fatalf("5h: %v – %v known %v", start, end, known)
}
start, end, known = AccountWindow("codex/me@example.com", "weekly", now)
if !known || end.Sub(week).Abs() > 2*time.Second || end.Sub(start) != 7*24*time.Hour {
t.Fatalf("weekly: %v – %v known %v", start, end, known)
}
if asked != 0 {
t.Fatalf("read again with every window known: %d", asked)
}
// past the reset with no reading since: unknown, from the reset that
// passed, which no later window begins before
start, end, known = AccountWindow("codex/me@example.com", "5h", reset.Add(time.Minute))
if known || !end.IsZero() || !start.Equal(reset) {
t.Fatalf("over: %v – %v known %v", start, end, known)
}
// a window with no clock, an account with no reading, a window it hasn't
for _, c := range [][2]string{{"codex/me@example.com", "Credits"}, {"codex/other@example.com", "5h"}, {"codex/me@example.com", "Monthly"}, {"codex", "5h"}} {
if start, end, known := AccountWindow(c[0], c[1], now); known || !start.IsZero() || !end.IsZero() {
t.Fatalf("%v: %v – %v known %v", c, start, end, known)
}
}
if asked == 0 {
t.Fatal("an unknown window wasn't read again")
}
}
// readAccountWindows reads the accounts again at most once a minute,
// however many requests find a window unknown.
func TestReadAccountWindowsOnceAMinute(t *testing.T) {
var reads atomic.Int32
was := readAccounts
readAccounts = func() { reads.Add(1) }
defer func() { readAccounts = was }()
accountWindowsAsked.Store(0)
now := time.Now()
for range 5 {
readAccountWindows(now)
}
readAccountWindows(now.Add(30 * time.Second))
readAccountWindows(now.Add(61 * time.Second))
deadline := time.Now().Add(10 * time.Second)
for reads.Load() < 2 && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
if n := reads.Load(); n != 2 {
t.Fatalf("%d reads, want 2", n)
}
}