10049b8062
Usage frames carry optional usage.account {harness, source, plan, as_of,
windows[{id,label,used_percent,resets_at}], unsupported}. Claude Code quota
comes from its rate_limit_event stream lines and the initialize account
(plan), shared across chats on the same account. Codex reads
account/rateLimits/read once per connection (re-read when a chat opens after
5 min) and merges account/rateLimits/updated. OpenCode exposes no quota API
and says so. The app's chat info sheet shows an Account usage section with a
progress bar and reset countdown per window, Material and Cupertino.
110 lines
4.0 KiB
Go
110 lines
4.0 KiB
Go
package codex
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func waitAccount(t *testing.T, ch <-chan protocol.ChatEventFrame) protocol.ChatEventFrame {
|
|
t.Helper()
|
|
deadline := time.After(5 * time.Second)
|
|
for {
|
|
select {
|
|
case frame := <-ch:
|
|
if frame.Kind == protocol.ChatEventUsage && frame.Usage != nil && frame.Usage.Account != nil {
|
|
return frame
|
|
}
|
|
case <-deadline:
|
|
t.Fatal("no account usage frame")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAccountRateLimitsReadOnceAndMergeSparseUpdates(t *testing.T) {
|
|
var mu sync.Mutex
|
|
reads := 0
|
|
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
|
|
if method != "account/rateLimits/read" {
|
|
t.Errorf("unexpected method %s", method)
|
|
return &rpcError{Code: -32601, Message: "unexpected"}
|
|
}
|
|
var params map[string]any
|
|
if json.Unmarshal(raw, ¶ms) != nil || params["excludeResetCreditDetails"] != true {
|
|
t.Errorf("read params = %s", raw)
|
|
}
|
|
mu.Lock()
|
|
reads++
|
|
mu.Unlock()
|
|
return json.RawMessage(modelFixture(t, "account-rate-limits-read"))
|
|
})
|
|
chat.accountReads = true
|
|
ch := chat.subscribe(fixtureThread)
|
|
read := waitAccount(t, ch)
|
|
want := []protocol.ChatQuotaWindow{
|
|
{ID: "primary", Label: "5-hour", UsedPercent: 37, ResetsAt: 1_900_000_000_000},
|
|
{ID: "secondary", Label: "Weekly", UsedPercent: 12, ResetsAt: 1_900_500_000_000},
|
|
}
|
|
account := read.Usage.Account
|
|
if account.Harness != "Codex" || account.Plan != "plus" || account.Source != "account/rateLimits" || account.AsOf <= 0 || !reflect.DeepEqual(account.Windows, want) {
|
|
t.Fatalf("read account = %+v", account)
|
|
}
|
|
if read.Usage.Scope != "" || read.Usage.TotalTokens != nil {
|
|
t.Fatalf("account-only frame invented token usage: %+v", read.Usage)
|
|
}
|
|
|
|
// The sparse update moves the 5-hour window and keeps the weekly one and
|
|
// the plan, and the token snapshot that follows carries the merged quota.
|
|
var update rpcMessage
|
|
if err := json.Unmarshal(modelFixture(t, "account-rate-limits-updated"), &update); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chat.handleNotification(update)
|
|
merged := waitAccount(t, ch).Usage.Account
|
|
if merged.Plan != "plus" || len(merged.Windows) != 2 || merged.Windows[0].UsedPercent != 41 || merged.Windows[1].UsedPercent != 12 {
|
|
t.Fatalf("merged account = %+v", merged)
|
|
}
|
|
chat.receiveUsage(json.RawMessage(`{"threadId":"` + fixtureThread + `","tokenUsage":{"total":{"totalTokens":5},"last":{}}}`))
|
|
tokens := waitAccount(t, ch).Usage
|
|
if tokens.TotalTokens == nil || *tokens.TotalTokens != 5 || tokens.Account.Windows[0].UsedPercent != 41 {
|
|
t.Fatalf("token usage = %+v", tokens)
|
|
}
|
|
|
|
// Another metered limit's update is not this account's codex quota.
|
|
chat.handleNotification(rpcMessage{Method: "account/rateLimits/updated", Params: json.RawMessage(`{"rateLimits":{"limitId":"other","primary":{"usedPercent":99,"windowDurationMins":300,"resetsAt":null}}}`)})
|
|
// A second subscriber within the interval replays the cache, no new read.
|
|
second := chat.subscribe(fixtureThread)
|
|
replayed := waitAccount(t, second).Usage
|
|
if replayed.Account.Windows[0].UsedPercent != 41 || replayed.TotalTokens == nil {
|
|
t.Fatalf("replay = %+v", replayed)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if reads != 1 {
|
|
t.Fatalf("account reads = %d, want 1", reads)
|
|
}
|
|
}
|
|
|
|
func TestAccountRateLimitsUnknownStaysHidden(t *testing.T) {
|
|
chat := usageTestChat()
|
|
ch := chat.subscribe("thread")
|
|
// A failed/absent read leaves no quota, and an update without windows adds none.
|
|
chat.receiveRateLimits(json.RawMessage(`{"rateLimits":{"limitId":null,"primary":null,"secondary":null,"planType":"unknown"}}`))
|
|
chat.receiveRateLimits(json.RawMessage(`not json`))
|
|
if len(ch) != 0 {
|
|
t.Fatalf("unknown quota published: %+v", <-ch)
|
|
}
|
|
if got := windowLabel(nil, "Primary"); got != "Primary" {
|
|
t.Fatalf("label without duration = %q", got)
|
|
}
|
|
for minutes, want := range map[int64]string{300: "5-hour", 10080: "Weekly", 1440: "Daily", 4320: "3-day", 45: "45-minute"} {
|
|
if got := windowLabel(&minutes, ""); got != want {
|
|
t.Fatalf("label(%d) = %q, want %q", minutes, got, want)
|
|
}
|
|
}
|
|
}
|