-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimit_test.go
More file actions
349 lines (295 loc) · 8.67 KB
/
Copy pathratelimit_test.go
File metadata and controls
349 lines (295 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package tok
import (
"strings"
"sync"
"testing"
"time"
)
func TestRecordIncreasesCounters(t *testing.T) {
tracker := NewUsageTracker()
tracker.Record(1000, 0.05, "openai", "gpt-4")
summary := tracker.GetUsage()
if summary.HourlyTokens != 1000 {
t.Errorf("expected hourly tokens 1000, got %d", summary.HourlyTokens)
}
if summary.DailyTokens != 1000 {
t.Errorf("expected daily tokens 1000, got %d", summary.DailyTokens)
}
if summary.SessionTokens != 1000 {
t.Errorf("expected session tokens 1000, got %d", summary.SessionTokens)
}
if summary.DailyCostUSD != 0.05 {
t.Errorf("expected daily cost 0.05, got %f", summary.DailyCostUSD)
}
}
func TestCanProceedReturnsFalseWhenHourlyLimitHit(t *testing.T) {
tracker := NewUsageTracker()
tracker.HourlyLimit = 1000
tracker.Record(1000, 0.01, "openai", "gpt-4")
ok, reason := tracker.CanProceed()
if ok {
t.Error("expected CanProceed to return false when hourly limit reached")
}
if !strings.Contains(reason, "hourly") {
t.Errorf("expected reason to mention hourly, got: %s", reason)
}
}
func TestCanProceedReturnsFalseWhenDailyLimitHit(t *testing.T) {
tracker := NewUsageTracker()
tracker.DailyLimit = 5000
tracker.HourlyLimit = 100000 // high so it doesn't trigger first
tracker.Record(5000, 0.01, "openai", "gpt-4")
ok, reason := tracker.CanProceed()
if ok {
t.Error("expected CanProceed to return false when daily limit reached")
}
if !strings.Contains(reason, "daily token") {
t.Errorf("expected reason to mention daily token, got: %s", reason)
}
}
func TestCanProceedReturnsFalseWhenSessionLimitHit(t *testing.T) {
tracker := NewUsageTracker()
tracker.SessionLimit = 2000
tracker.HourlyLimit = 100000
tracker.DailyLimit = 100000
tracker.Record(2000, 0.01, "openai", "gpt-4")
ok, reason := tracker.CanProceed()
if ok {
t.Error("expected CanProceed to return false when session limit reached")
}
if !strings.Contains(reason, "session") {
t.Errorf("expected reason to mention session, got: %s", reason)
}
}
func TestCanProceedReturnsFalseWhenCostLimitHit(t *testing.T) {
tracker := NewUsageTracker()
tracker.CostLimitUSD = 1.00
tracker.HourlyLimit = 10000000
tracker.DailyLimit = 10000000
tracker.SessionLimit = 10000000
tracker.Record(100, 1.00, "openai", "gpt-4")
ok, reason := tracker.CanProceed()
if ok {
t.Error("expected CanProceed to return false when cost limit reached")
}
if !strings.Contains(reason, "cost") {
t.Errorf("expected reason to mention cost, got: %s", reason)
}
}
func TestHourlyWindowExpiry(t *testing.T) {
tracker := NewUsageTracker()
tracker.HourlyLimit = 1000
// Inject an old entry directly
tracker.mu.Lock()
tracker.hourlyUsage = append(tracker.hourlyUsage, UsageEntry{
Tokens: 900,
CostUSD: 0.01,
Timestamp: time.Now().Add(-2 * time.Hour),
Provider: "openai",
Model: "gpt-4",
})
tracker.mu.Unlock()
// After pruning, the old entry should be gone
tracker.PruneOld()
summary := tracker.GetUsage()
if summary.HourlyTokens != 0 {
t.Errorf("expected hourly tokens 0 after expiry, got %d", summary.HourlyTokens)
}
}
func TestDailyWindowExpiry(t *testing.T) {
tracker := NewUsageTracker()
tracker.DailyLimit = 5000
// Inject an old entry directly
tracker.mu.Lock()
tracker.dailyUsage = append(tracker.dailyUsage, UsageEntry{
Tokens: 4000,
CostUSD: 0.50,
Timestamp: time.Now().Add(-25 * time.Hour),
Provider: "openai",
Model: "gpt-4",
})
tracker.mu.Unlock()
tracker.PruneOld()
summary := tracker.GetUsage()
if summary.DailyTokens != 0 {
t.Errorf("expected daily tokens 0 after expiry, got %d", summary.DailyTokens)
}
}
func TestAlertGenerationAtThresholds(t *testing.T) {
tracker := NewUsageTracker()
tracker.HourlyLimit = 100
// 50% - warning
tracker.Record(50, 0.01, "openai", "gpt-4")
found := false
for _, a := range tracker.Alerts {
if a.Level == "warning" && a.Threshold == 50 {
found = true
}
}
if !found {
t.Error("expected warning alert at 50% threshold")
}
// 80% - critical
tracker.Record(30, 0.01, "openai", "gpt-4")
found = false
for _, a := range tracker.Alerts {
if a.Level == "critical" && a.Threshold == 80 {
found = true
}
}
if !found {
t.Error("expected critical alert at 80% threshold")
}
// 100% - limit_reached
tracker.Record(20, 0.01, "openai", "gpt-4")
found = false
for _, a := range tracker.Alerts {
if a.Level == "limit_reached" && a.Threshold == 100 {
found = true
}
}
if !found {
t.Error("expected limit_reached alert at 100% threshold")
}
}
func TestNoDuplicateAlerts(t *testing.T) {
tracker := NewUsageTracker()
tracker.HourlyLimit = 100
// Record multiple times past 50%
tracker.Record(55, 0.01, "openai", "gpt-4")
tracker.Record(5, 0.01, "openai", "gpt-4")
tracker.Record(5, 0.01, "openai", "gpt-4")
// Count how many warning-level alerts with threshold 50 exist for hourly
count := 0
for _, a := range tracker.Alerts {
if a.Threshold == 50 && a.Level == "warning" && strings.Contains(a.Message, "hourly") {
count++
}
}
if count > 1 {
t.Errorf("expected at most 1 warning alert for hourly 50%%, got %d", count)
}
}
func TestFormatUsageBarEmpty(t *testing.T) {
bar := FormatUsageBar(0, 16)
if !strings.Contains(bar, "0%") {
t.Errorf("expected 0%% in bar, got: %s", bar)
}
// Should have all empty blocks
if !strings.Contains(bar, "░░░░░░░░░░░░░░░░") {
t.Errorf("expected all empty blocks, got: %s", bar)
}
}
func TestFormatUsageBarFull(t *testing.T) {
bar := FormatUsageBar(100, 16)
if !strings.Contains(bar, "100%") {
t.Errorf("expected 100%% in bar, got: %s", bar)
}
if !strings.Contains(bar, "████████████████") {
t.Errorf("expected all filled blocks, got: %s", bar)
}
}
func TestFormatUsageBarHalf(t *testing.T) {
bar := FormatUsageBar(50, 16)
if !strings.Contains(bar, "50%") {
t.Errorf("expected 50%% in bar, got: %s", bar)
}
}
func TestFormatSummaryOutput(t *testing.T) {
tracker := NewUsageTracker()
tracker.Record(45000, 2.40, "openai", "gpt-4")
output := tracker.FormatSummary()
if !strings.Contains(output, "Token Usage:") {
t.Error("expected output to contain 'Token Usage:'")
}
if !strings.Contains(output, "Hourly:") {
t.Error("expected output to contain 'Hourly:'")
}
if !strings.Contains(output, "Daily:") {
t.Error("expected output to contain 'Daily:'")
}
if !strings.Contains(output, "Session:") {
t.Error("expected output to contain 'Session:'")
}
if !strings.Contains(output, "Cost:") {
t.Error("expected output to contain 'Cost:'")
}
if !strings.Contains(output, "$2.40") {
t.Errorf("expected output to contain '$2.40', got:\n%s", output)
}
}
func TestEstimateRemaining(t *testing.T) {
tracker := NewUsageTracker()
tracker.HourlyLimit = 10000
tracker.DailyLimit = 100000
tracker.SessionLimit = 50000
tracker.Record(5000, 0.10, "openai", "gpt-4")
// Hourly remaining: 10000-5000=5000, daily: 100000-5000=95000, session: 50000-5000=45000
// Min remaining is 5000 (hourly), so 5000/1000 = 5
est := tracker.EstimateRemaining(1000)
if est != 5 {
t.Errorf("expected estimate 5, got %d", est)
}
}
func TestEstimateRemainingZeroTokens(t *testing.T) {
tracker := NewUsageTracker()
est := tracker.EstimateRemaining(0)
if est != 0 {
t.Errorf("expected 0 for zero tokensPerRequest, got %d", est)
}
}
func TestConcurrentRecordCalls(t *testing.T) {
tracker := NewUsageTracker()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tracker.Record(100, 0.01, "openai", "gpt-4")
}()
}
wg.Wait()
summary := tracker.GetUsage()
if summary.SessionTokens != 10000 {
t.Errorf("expected session tokens 10000 after concurrent records, got %d", summary.SessionTokens)
}
}
func TestResetClearsSession(t *testing.T) {
tracker := NewUsageTracker()
tracker.Record(5000, 0.50, "openai", "gpt-4")
tracker.Reset()
summary := tracker.GetUsage()
if summary.SessionTokens != 0 {
t.Errorf("expected session tokens 0 after reset, got %d", summary.SessionTokens)
}
if len(tracker.Alerts) != 0 {
t.Errorf("expected alerts cleared after reset, got %d", len(tracker.Alerts))
}
}
func TestCanProceedReturnsTrueUnderLimits(t *testing.T) {
tracker := NewUsageTracker()
tracker.Record(100, 0.01, "openai", "gpt-4")
ok, reason := tracker.CanProceed()
if !ok {
t.Errorf("expected CanProceed to return true under limits, got reason: %s", reason)
}
}
func TestFormatNumber(t *testing.T) {
tests := []struct {
input int
expected string
}{
{0, "0"},
{999, "999"},
{1000, "1,000"},
{45000, "45,000"},
{1000000, "1,000,000"},
{200000, "200,000"},
}
for _, tc := range tests {
got := formatNumber(tc.input)
if got != tc.expected {
t.Errorf("formatNumber(%d) = %s, want %s", tc.input, got, tc.expected)
}
}
}