From f55a1e88ea5948582d0e5a33efea0c5d806e1f4b Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 16 Mar 2026 04:25:26 +0200 Subject: Add Snapshot.ScopeReqs/ScopeRPM and simplify 3 callers Centralizes the provider+model map traversal and window-minutes guard that was duplicated in hexaiaction, hexaicli, and lsp. Co-Authored-By: Claude Opus 4.6 --- internal/stats/stats_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'internal/stats/stats_test.go') diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go index 45f9e2a..a9b3d22 100644 --- a/internal/stats/stats_test.go +++ b/internal/stats/stats_test.go @@ -334,3 +334,50 @@ func TestUpdate_CancelledContext(t *testing.T) { t.Fatal("expected error from cancelled context, got nil") } } + +func TestSnapshot_ScopeReqs(t *testing.T) { + snap := Snapshot{ + Providers: map[string]ProviderEntry{ + "openai": {Models: map[string]Counters{"gpt-5.0": {Reqs: 42}}}, + }, + } + if got := snap.ScopeReqs("openai", "gpt-5.0"); got != 42 { + t.Fatalf("expected 42, got %d", got) + } + if got := snap.ScopeReqs("openai", "gpt-4.1"); got != 0 { + t.Fatalf("expected 0 for missing model, got %d", got) + } + if got := snap.ScopeReqs("anthropic", "gpt-5.0"); got != 0 { + t.Fatalf("expected 0 for missing provider, got %d", got) + } +} + +func TestSnapshot_ScopeRPM(t *testing.T) { + snap := Snapshot{ + Providers: map[string]ProviderEntry{ + "openai": {Models: map[string]Counters{"gpt-5.0": {Reqs: 60}}}, + }, + Window: time.Hour, + } + rpm := snap.ScopeRPM("openai", "gpt-5.0") + if rpm != 1.0 { + t.Fatalf("expected 1.0 rpm, got %v", rpm) + } + // Missing model should return 0 + if rpm := snap.ScopeRPM("openai", "missing"); rpm != 0 { + t.Fatalf("expected 0 rpm for missing, got %v", rpm) + } +} + +func TestSnapshot_ScopeRPM_ZeroWindow(t *testing.T) { + snap := Snapshot{ + Providers: map[string]ProviderEntry{ + "openai": {Models: map[string]Counters{"gpt-5.0": {Reqs: 10}}}, + }, + Window: 0, // edge case + } + rpm := snap.ScopeRPM("openai", "gpt-5.0") + if rpm <= 0 { + t.Fatalf("expected positive rpm even with zero window, got %v", rpm) + } +} -- cgit v1.2.3