feat: show current-cycle token totals

This commit is contained in:
2026-08-14 11:07:46 +08:00
parent 1ec9001387
commit bf37999833
12 changed files with 254 additions and 19 deletions
+66
View File
@@ -71,6 +71,72 @@ func TestDashboardSerializesNilListsAsEmptyArrays(t *testing.T) {
}
}
func TestCurrentTokenCycleUsesLongestWindowAndFiltersDailyUsage(t *testing.T) {
now := time.Date(2026, time.August, 14, 12, 0, 0, 0, time.UTC)
reset := time.Date(2026, time.August, 15, 0, 0, 0, 0, time.UTC)
cycle := currentTokenCycle(
[]LimitBucket{
{
LimitID: "codex",
WindowType: "primary",
WindowDurationMinutes: 300,
ResetsAt: now.Add(5 * time.Hour).Unix(),
},
{
LimitID: "codex",
WindowType: "secondary",
WindowDurationMinutes: 7 * 24 * 60,
ResetsAt: reset.Unix(),
},
},
[]UsagePoint{
{Date: "2026-08-07", TotalTokens: 50},
{Date: "2026-08-08", TotalTokens: 100},
{Date: "2026-08-10", TotalTokens: 200},
{Date: "2026-08-14", TotalTokens: 300},
{Date: "2026-08-15", TotalTokens: 400},
},
now.Unix(),
)
if cycle == nil {
t.Fatal("currentTokenCycle() returned nil")
}
if cycle.WindowType != "secondary" || cycle.WindowDurationMinutes != 7*24*60 {
t.Fatalf("cycle window = %#v; want the seven-day secondary window", cycle)
}
if cycle.StartedAt != time.Date(2026, time.August, 8, 0, 0, 0, 0, time.UTC).Unix() {
t.Fatalf("cycle start = %d; want 2026-08-08", cycle.StartedAt)
}
if cycle.ResetsAt != reset.Unix() || cycle.TotalTokens != 600 {
t.Fatalf("cycle = %#v; want reset %d and 600 tokens", cycle, reset.Unix())
}
}
func TestCurrentTokenCycleRequiresAValidFutureResetWindow(t *testing.T) {
cycle := currentTokenCycle(
[]LimitBucket{{WindowDurationMinutes: 0, ResetsAt: 1}},
[]UsagePoint{{Date: "2026-08-14", TotalTokens: 100}},
time.Date(2026, time.August, 14, 12, 0, 0, 0, time.UTC).Unix(),
)
if cycle != nil {
t.Fatalf("currentTokenCycle() = %#v; want nil", cycle)
}
}
func TestFlattenLimitReadsAppServerWindowDuration(t *testing.T) {
var limit rawLimit
if err := json.Unmarshal([]byte(`{
"limitId":"codex",
"primary":{"usedPercent":20,"windowDurationMins":10080,"resetsAt":1787197043}
}`), &limit); err != nil {
t.Fatal(err)
}
flattened := flattenLimit(limit)
if len(flattened) != 1 || flattened[0].WindowDurationMinutes != 10080 {
t.Fatalf("flattened limit = %#v; want a 10080-minute window", flattened)
}
}
type fakeCodexClient struct {
mu sync.Mutex
connected bool