Files
magpie/internal/provider/allowance_await_test.go
T
yetone 690bc352f2 gui, provider: a Codex reset spent on Usage shows at once in the Providers editor and in routing (#1491, isdou)
isdou spent a reset on the Usage page: Usage showed the windows at 100%
left and 2 resets, while the Providers editor still said the week was
used up with 3 resets, and Routing said the account's allowance was
unknown and sent requests on to the next account.

Two reasons, one per page:
- The Providers editor reads each account's windows itself
  (loginUsageOf, /api/login/usage) and keeps the reading a minute in
  the page. askReset reloaded Usage's quotas only. It now drops the
  editor's Codex reading too, and re-renders an open editor.
- UseCodexReset forgets the account's allowance (renewedNow), so until
  the next background reading routing had no windows for it: weighed as
  not known, behind the others, and a reading already out, begun before
  the reset, is dropped for it too. The codex-reset handler now waits
  (up to 10s) for provider.AwaitAllowance, which has the accounts read
  again and asks once more when a reading begun before the reset lands,
  so by the time the page hears "reset", routing knows the windows
  started again, and /api/login/usage has the new reading cached.

Tests:
- provider TestResetAllowanceKnownAtOnce: a fake reader, the reset
  spent with no reading out and with one out (held in the fake until
  after renewedNow). Red with AwaitAllowance a no-op (not known), and
  red with it asking one reading only (the in-flight case).
- reset-providers.test.cjs: Providers editor opened (3 resets), reset
  on Usage, editor opened again: says 2. Red on origin/main's app.js
  ("still says 3 resets", login/usage not asked again) in all four;
  passes in Chromium and WebKit, en and zh, 420 and 1000px.

Run: go test -tags nogui ./internal/provider/ ./internal/gui/ (ok),
-race -count=20 on the new provider test, gofmt -l clean.
2026-10-10 20:00:38 +08:00

105 lines
3.2 KiB
Go

package provider
import (
"context"
"sync/atomic"
"testing"
"time"
)
// A reset spent from the Usage page is known to routing by the time the
// page hears it was spent (#1491, isdou): the account's windows read again
// as started over, not left out of the allowances, where routing counted it
// "not known" and sent requests to the next account, and the Routing page
// said so, until a later request's reading came back. That holds too when a
// reading begun before the reset is still out: it can't count the reset, so
// another is asked for.
func TestResetAllowanceKnownAtOnce(t *testing.T) {
signIn(t)
week := func(used float64) map[string]SubscriptionQuota {
resets := time.Now().Add(100 * time.Hour)
return map[string]SubscriptionQuota{
"me@example.com": {Windows: []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: used, ResetsAt: &resets}}},
"other@example.com": {Windows: []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: 40, ResetsAt: &resets}}},
}
}
var spent atomic.Bool
hold, entered := make(chan struct{}), make(chan struct{})
var held atomic.Bool
LoginUsageVia(func(context.Context, string) map[string]SubscriptionQuota {
if held.CompareAndSwap(true, false) {
// the reading out when the reset is spent: it read the
// windows before
close(entered)
<-hold
return week(100)
}
if spent.Load() {
return week(0)
}
return week(100)
})
clear := func() {
usedCache.Lock()
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
usedCache.stale, usedCache.renewed, usedCache.seen = nil, nil, nil
usedCache.Unlock()
}
clear()
t.Cleanup(func() { LoginUsageVia(nil); clear() })
oldWait := firstWait
firstWait = 5 * time.Second
t.Cleanup(func() { firstWait = oldWait })
// the allowances routing has now, without asking for more
used := func() (float64, bool) {
usedCache.Lock()
a, ok := usedCache.m["codex"]["me@example.com"]
usedCache.Unlock()
if !ok || len(a) == 0 {
return 0, false
}
return a[0].Used, true
}
// a reading still out when the test ends is waited for
t.Cleanup(func() {
usedCache.Lock()
done := usedCache.loading["codex"]
usedCache.Unlock()
if done != nil {
<-done
}
})
Allowances("codex")
if u, ok := used(); !ok || u != 100 {
t.Fatalf("before the reset: %v %v", u, ok)
}
for _, inFlight := range []bool{false, true} {
spent.Store(false)
forgetAllowance("codex", "other@example.com") // read again at the next ask
if inFlight {
held.Store(true)
Allowances("codex") // a reading goes out, and waits
<-entered
}
// what UseCodexReset does once the vendor says the windows started
// again
spent.Store(true)
StaleAllowance("codex", "me@example.com")
renewedNow("codex", "me@example.com")
if inFlight {
close(hold)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
known := AwaitAllowance(ctx, "codex", "Me@example.com")
cancel()
if !known {
t.Fatalf("in flight %v: the account isn't known once the reset is spent", inFlight)
}
if u, ok := used(); !ok || u != 0 {
t.Fatalf("in flight %v: routing reads %v used (known %v), want 0", inFlight, u, ok)
}
}
}