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.
48 lines
1.8 KiB
Go
48 lines
1.8 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestChatUsageUnknownAndReportedZeroAreDistinct(t *testing.T) {
|
|
unknown, _ := json.Marshal(ChatUsage{})
|
|
if strings.Contains(string(unknown), "total_tokens") || strings.Contains(string(unknown), "cost_usd") {
|
|
t.Fatal("unknown metrics became zero")
|
|
}
|
|
zeroTokens := int64(0)
|
|
zeroCost := float64(0)
|
|
known, _ := json.Marshal(ChatUsage{Scope: "session", TotalTokens: &zeroTokens, CostUSD: &zeroCost})
|
|
for _, want := range []string{`"total_tokens":0`, `"cost_usd":0`, `"scope":"session"`} {
|
|
if !strings.Contains(string(known), want) {
|
|
t.Fatalf("missing %s in %s", want, known)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChatAccountUsageWireShapeAndClone(t *testing.T) {
|
|
if data, _ := json.Marshal(ChatUsage{}); strings.Contains(string(data), "account") {
|
|
t.Fatalf("unknown account quota was serialized: %s", data)
|
|
}
|
|
account := &ChatAccountUsage{Harness: "Codex", Source: "account/rateLimits", Plan: "plus", AsOf: 1700000000000,
|
|
Windows: []ChatQuotaWindow{{ID: "primary", Label: "5-hour", UsedPercent: 0, ResetsAt: 1700000600000}}}
|
|
data, _ := json.Marshal(ChatUsage{Account: account})
|
|
want := `{"account":{"harness":"Codex","source":"account/rateLimits","plan":"plus","as_of":1700000000000,"windows":[{"id":"primary","label":"5-hour","used_percent":0,"resets_at":1700000600000}]}}`
|
|
if string(data) != want {
|
|
t.Fatalf("wire = %s\nwant %s", data, want)
|
|
}
|
|
copy := account.Clone()
|
|
copy.Windows[0].UsedPercent = 50
|
|
if account.Windows[0].UsedPercent != 0 {
|
|
t.Fatal("clone shares windows")
|
|
}
|
|
if (*ChatAccountUsage)(nil).Clone() != nil {
|
|
t.Fatal("nil clone")
|
|
}
|
|
unsupported, _ := json.Marshal(ChatAccountUsage{Harness: "OpenCode", Unsupported: true})
|
|
if string(unsupported) != `{"harness":"OpenCode","unsupported":true}` {
|
|
t.Fatalf("unsupported wire = %s", unsupported)
|
|
}
|
|
}
|