-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
379 lines (358 loc) · 9.86 KB
/
main_test.go
File metadata and controls
379 lines (358 loc) · 9.86 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"testing"
)
func TestDetectAuthError(t *testing.T) {
tests := []struct {
name string
input string
wantCode AuthErrorCode
wantNil bool
}{
{
name: "token expired",
input: "Your token has expired. Please log in again.",
wantCode: AuthErrorTokenExpired,
},
{
name: "session expired",
input: "Your session expired. Re-authenticate to continue.",
wantCode: AuthErrorTokenExpired,
},
{
name: "authentication error underscore",
input: "authentication_error: invalid credentials",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "authentication failed",
input: "Authentication failed. Please try again.",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "not logged in explicit",
input: "You are not logged in. Please sign in to continue.",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "please log in",
input: "Please log in to use this feature.",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "login required",
input: "Login required to access usage metrics.",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "sign in to continue",
input: "Please sign in to continue using Claude.",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "login URL",
input: "Visit https://claude.ai/login to authenticate",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "auth URL",
input: "Go to https://anthropic.com/auth/signin to sign in",
wantCode: AuthErrorNotLoggedIn,
},
{
name: "free tier",
input: "You are on the free tier. Upgrade to Pro for more features.",
wantCode: AuthErrorNoSubscription,
},
{
name: "no subscription",
input: "No active subscription found.",
wantCode: AuthErrorNoSubscription,
},
{
name: "upgrade to pro",
input: "Upgrade to Pro to access usage metrics.",
wantCode: AuthErrorNoSubscription,
},
{
name: "setup required - let's get started",
input: "Let's get started.\n\n Choose the text style that looks best with your terminal",
wantCode: AuthErrorSetupRequired,
},
{
name: "setup required - theme selection",
input: "Choose the text style that looks best\nTo change this later, run /theme",
wantCode: AuthErrorSetupRequired,
},
{
name: "normal usage - no error",
input: "Current session: 50% used. Resets at 6am",
wantNil: true,
},
{
name: "quota data - no error",
input: "11% used\nResets 5:59pm (Europe/Berlin)",
wantNil: true,
},
{
name: "empty string - no error",
input: "",
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectAuthError(tt.input)
if tt.wantNil {
if got != nil {
t.Errorf("detectAuthError() = %v, want nil", got)
}
return
}
if got == nil {
t.Errorf("detectAuthError() = nil, want code %v", tt.wantCode)
return
}
if got.Code != tt.wantCode {
t.Errorf("detectAuthError().Code = %v, want %v", got.Code, tt.wantCode)
}
if got.Message == "" {
t.Error("detectAuthError().Message should not be empty")
}
})
}
}
func TestFormatHyprPanelAuthError(t *testing.T) {
tests := []struct {
name string
authError *AuthError
wantText string
wantAlt string
wantClass string
}{
{
name: "not logged in",
authError: &AuthError{
Code: AuthErrorNotLoggedIn,
Message: "Not logged in",
},
wantText: "Claude",
wantAlt: "not_logged_in",
wantClass: "auth_error",
},
{
name: "token expired",
authError: &AuthError{
Code: AuthErrorTokenExpired,
Message: "Token expired",
},
wantText: "Claude",
wantAlt: "token_expired",
wantClass: "auth_error",
},
{
name: "no subscription",
authError: &AuthError{
Code: AuthErrorNoSubscription,
Message: "No subscription",
},
wantText: "Claude",
wantAlt: "no_subscription",
wantClass: "auth_error",
},
{
name: "setup required",
authError: &AuthError{
Code: AuthErrorSetupRequired,
Message: "Setup required",
},
wantText: "Claude",
wantAlt: "setup_required",
wantClass: "auth_error",
},
{
name: "nil error",
authError: nil,
wantText: "--",
wantAlt: "error",
wantClass: "error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatHyprPanelAuthError(tt.authError)
if got.Text != tt.wantText {
t.Errorf("formatHyprPanelAuthError().Text = %v, want %v", got.Text, tt.wantText)
}
if got.Alt != tt.wantAlt {
t.Errorf("formatHyprPanelAuthError().Alt = %v, want %v", got.Alt, tt.wantAlt)
}
if got.Class != tt.wantClass {
t.Errorf("formatHyprPanelAuthError().Class = %v, want %v", got.Class, tt.wantClass)
}
})
}
}
func TestIsQuotaSectionMarker(t *testing.T) {
// Note: isQuotaSectionMarker expects pre-lowercased input for efficiency
tests := []struct {
name string
line string
want bool
}{
{
name: "current session marker",
line: "current session",
want: true,
},
{
name: "current week all models",
line: "current week (all models)",
want: true,
},
{
name: "current week opus",
line: "current week (opus)",
want: true,
},
{
name: "opus usage",
line: "opus usage",
want: true,
},
{
name: "sonnet usage",
line: "sonnet usage",
want: true,
},
{
name: "reset line - not a marker",
line: "resets 5d 3h",
want: false,
},
{
name: "percentage line - not a marker",
line: "50% used",
want: false,
},
{
name: "empty line - not a marker",
line: "",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isQuotaSectionMarker(tt.line)
if got != tt.want {
t.Errorf("isQuotaSectionMarker(%q) = %v, want %v", tt.line, got, tt.want)
}
})
}
}
func TestParseResetTime_StopsAtQuotaBoundary(t *testing.T) {
// This test verifies that parseResetTime stops searching when it encounters
// another quota section marker, preventing it from matching the wrong reset time.
lines := []string{
"Current session", // 0
"0% used", // 1 - startIdx
"", // 2 - no reset info for session
"Current week (all models)", // 3 - quota boundary, should stop here
"50% used", // 4
"Resets 5d 3h", // 5 - this should NOT be matched for session
}
resetText, resetTime, duration := parseResetTime(lines, 1)
// Should return empty since no reset was found before the quota boundary
if resetText != "" {
t.Errorf("parseResetTime should return empty resetText when stopped by quota boundary, got %q", resetText)
}
if resetTime != nil {
t.Errorf("parseResetTime should return nil resetTime when stopped by quota boundary, got %v", resetTime)
}
if duration != nil {
t.Errorf("parseResetTime should return nil duration when stopped by quota boundary, got %v", duration)
}
}
func TestParseResetTime_FindsResetBeforeBoundary(t *testing.T) {
// This test verifies that parseResetTime still finds reset times
// that appear before a quota boundary.
lines := []string{
"Current session", // 0
"50% used", // 1 - startIdx
"Resets 2h 30m", // 2 - reset info for session
"", // 3
"Current week (all models)", // 4 - quota boundary
"50% used", // 5
"Resets 5d 3h", // 6 - weekly reset
}
resetText, resetTime, duration := parseResetTime(lines, 1)
if resetText == "" {
t.Error("parseResetTime should find reset text before quota boundary")
}
if resetTime == nil {
t.Error("parseResetTime should find reset time before quota boundary")
}
if duration == nil {
t.Error("parseResetTime should find duration before quota boundary")
} else {
// 2h 30m = 9000 seconds
expectedSeconds := int64(2*60*60 + 30*60)
// Allow some tolerance for time passing during test
if *duration < expectedSeconds-5 || *duration > expectedSeconds+5 {
t.Errorf("parseResetTime duration = %d, want ~%d", *duration, expectedSeconds)
}
}
}
func TestParseQuotas_SessionResetNotMatchedFromWeekly(t *testing.T) {
// This test simulates the bug scenario: session at 0% with no reset time,
// followed by weekly quota with a reset time.
// The session quota should NOT get the weekly reset time.
input := `· Claude Max · user@example.com
│
│ Current session
│ 0% used
│
│ Current week (all models)
│ 50% used
│ Resets 5d 3h
│`
quotas := parseQuotas(input)
if len(quotas) < 2 {
t.Fatalf("expected at least 2 quotas, got %d", len(quotas))
}
// Find session quota
var sessionQuota *Quota
var weeklyQuota *Quota
for i := range quotas {
if quotas[i].Type == QuotaTypeSession {
sessionQuota = "as[i]
}
if quotas[i].Type == QuotaTypeWeekly {
weeklyQuota = "as[i]
}
}
if sessionQuota == nil {
t.Fatal("session quota not found")
}
if weeklyQuota == nil {
t.Fatal("weekly quota not found")
}
// Session should have 100% remaining (0% used)
if sessionQuota.PercentRemaining != 100 {
t.Errorf("session PercentRemaining = %v, want 100", sessionQuota.PercentRemaining)
}
// Session should NOT have a reset time (since there was none in its section)
if sessionQuota.TimeRemainingSeconds != nil {
t.Errorf("session TimeRemainingSeconds should be nil (no reset in section), got %v", *sessionQuota.TimeRemainingSeconds)
}
// Weekly should have the reset time
if weeklyQuota.TimeRemainingSeconds == nil {
t.Error("weekly TimeRemainingSeconds should not be nil")
} else {
// 5d 3h = 5*24*60*60 + 3*60*60 = 442800 seconds
expectedSeconds := int64(5*24*60*60 + 3*60*60)
if *weeklyQuota.TimeRemainingSeconds < expectedSeconds-5 || *weeklyQuota.TimeRemainingSeconds > expectedSeconds+5 {
t.Errorf("weekly TimeRemainingSeconds = %d, want ~%d", *weeklyQuota.TimeRemainingSeconds, expectedSeconds)
}
}
}