-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.go
More file actions
795 lines (721 loc) · 23.8 KB
/
parser_test.go
File metadata and controls
795 lines (721 loc) · 23.8 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
// makeRecordWithBranch builds a JSONL line for an assistant message with an optional git branch.
func makeRecordWithBranch(requestID, model, timestamp string, input, output, cacheRead, cacheWrite5m, cacheWrite1h int, gitBranch string) string {
type cacheCreation struct {
Ephemeral5m int `json:"ephemeral_5m_input_tokens"`
Ephemeral1h int `json:"ephemeral_1h_input_tokens"`
}
type usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheReadInputTokens int `json:"cache_read_input_tokens"`
CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
CacheCreation *cacheCreation `json:"cache_creation,omitempty"`
}
type message struct {
Model string `json:"model"`
Role string `json:"role"`
Usage usage `json:"usage"`
}
type record struct {
Type string `json:"type"`
RequestID string `json:"requestId"`
Timestamp string `json:"timestamp"`
GitBranch string `json:"gitBranch,omitempty"`
Message message `json:"message"`
}
var cc *cacheCreation
if cacheWrite5m > 0 || cacheWrite1h > 0 {
cc = &cacheCreation{Ephemeral5m: cacheWrite5m, Ephemeral1h: cacheWrite1h}
}
rec := record{
Type: "assistant", RequestID: requestID, Timestamp: timestamp, GitBranch: gitBranch,
Message: message{Model: model, Role: "assistant", Usage: usage{
InputTokens: input, OutputTokens: output,
CacheReadInputTokens: cacheRead,
CacheCreationInputTokens: cacheWrite5m + cacheWrite1h,
CacheCreation: cc,
}},
}
b, _ := json.Marshal(rec)
return string(b)
}
// makeRecord builds a JSONL line for an assistant message (no branch).
func makeRecord(requestID, model, timestamp string, input, output, cacheRead, cacheWrite5m, cacheWrite1h int) string {
return makeRecordWithBranch(requestID, model, timestamp, input, output, cacheRead, cacheWrite5m, cacheWrite1h, "")
}
func makeRecordWithSpeed(requestID, model, timestamp string, input, output int, speed string) string {
type usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
Speed string `json:"speed,omitempty"`
}
type message struct {
Model string `json:"model"`
Role string `json:"role"`
Usage usage `json:"usage"`
}
type record struct {
Type string `json:"type"`
RequestID string `json:"requestId"`
Timestamp string `json:"timestamp"`
Message message `json:"message"`
}
rec := record{
Type: "assistant", RequestID: requestID, Timestamp: timestamp,
Message: message{Model: model, Role: "assistant", Usage: usage{
InputTokens: input, OutputTokens: output, Speed: speed,
}},
}
b, _ := json.Marshal(rec)
return string(b)
}
func makeUserRecord(timestamp string) string {
return fmt.Sprintf(`{"type":"user","message":{"role":"user","content":"hello"},"timestamp":%q}`, timestamp)
}
func localMidnight() time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
}
func ts(daysAgo int, hour int) string {
t := localMidnight().AddDate(0, 0, -daysAgo).Add(time.Duration(hour) * time.Hour)
return t.Format(time.RFC3339)
}
func dateStr(daysAgo int) string {
return localMidnight().AddDate(0, 0, -daysAgo).Format("2006-01-02")
}
// setupProject creates a project dir with JSONL lines and returns the base dir.
func setupProject(t *testing.T, projectName string, lines []string) string {
t.Helper()
base := t.TempDir()
return addProject(t, base, projectName, lines)
}
// addProject adds a project's JSONL lines to an existing base dir.
func addProject(t *testing.T, base, projectName string, lines []string) string {
t.Helper()
projDir := filepath.Join(base, "projects", projectName)
if err := os.MkdirAll(projDir, 0755); err != nil {
t.Fatal(err)
}
content := ""
for _, l := range lines {
content += l + "\n"
}
if err := os.WriteFile(filepath.Join(projDir, "session.jsonl"), []byte(content), 0644); err != nil {
t.Fatal(err)
}
return base
}
// --- Deduplication ---
func TestDeduplicate_StreamingUpdates(t *testing.T) {
// Same requestID appears twice (streaming) — last value wins
lines := []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 10, 500, 200, 0),
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 500, 200, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 deduped record, got %d", data.TotalRecords)
}
opus := data.ModelUsage["claude-opus-4-6"]
if opus == nil {
t.Fatal("expected opus bucket")
return
}
if opus.OutputTokens != 50 {
t.Errorf("expected output_tokens=50 (last value wins), got %d", opus.OutputTokens)
}
}
func TestDeduplicate_DifferentRequests(t *testing.T) {
lines := []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
makeRecord("req_002", "claude-opus-4-6", ts(0, 11), 200, 100, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 2 {
t.Errorf("expected 2 records, got %d", data.TotalRecords)
}
opus := data.ModelUsage["claude-opus-4-6"]
if opus.InputTokens != 300 {
t.Errorf("expected aggregated input=300, got %d", opus.InputTokens)
}
if opus.OutputTokens != 150 {
t.Errorf("expected aggregated output=150, got %d", opus.OutputTokens)
}
}
func TestDeduplicate_SameRequestAcrossProjects(t *testing.T) {
// Same requestID in two projects — last write wins (map semantics)
base := setupProject(t, "project-a", []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
addProject(t, base, "project-b", []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
// Same requestID should deduplicate to 1 record
if data.TotalRecords != 1 {
t.Errorf("expected 1 deduped record across projects, got %d", data.TotalRecords)
}
}
// --- Model Aggregation ---
func TestModelAggregation_MultipleModels(t *testing.T) {
lines := []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 500, 200, 0),
makeRecord("req_002", "claude-sonnet-4-6", ts(0, 11), 80, 30, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if len(data.ModelUsage) != 2 {
t.Errorf("expected 2 models, got %d", len(data.ModelUsage))
}
opus := data.ModelUsage["claude-opus-4-6"]
if opus == nil || opus.Requests != 1 {
t.Error("expected 1 opus request")
}
sonnet := data.ModelUsage["claude-sonnet-4-6"]
if sonnet == nil || sonnet.Requests != 1 {
t.Error("expected 1 sonnet request")
}
}
// --- Project Filter ---
func TestProjectFilter_MatchesSubstring(t *testing.T) {
base := setupProject(t, "my-cool-project", []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
addProject(t, base, "other-project", []string{
makeRecord("req_002", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
data, err := parseLogs(base, 0, "cool")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record matching 'cool', got %d", data.TotalRecords)
}
}
func TestProjectFilter_CaseInsensitive(t *testing.T) {
base := setupProject(t, "MyProject", []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
data, err := parseLogs(base, 0, "myproject")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected case-insensitive match, got %d records", data.TotalRecords)
}
}
func TestProjectFilter_NoMatch(t *testing.T) {
base := setupProject(t, "test-project", []string{
makeRecord("req_001", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
data, err := parseLogs(base, 0, "nonexistent")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 0 {
t.Errorf("expected 0 records with filter, got %d", data.TotalRecords)
}
}
// --- Days Filter ---
func TestDaysFilter_ExcludesOldRecords(t *testing.T) {
lines := []string{
makeRecord("req_today", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
makeRecord("req_yesterday", "claude-opus-4-6", ts(1, 10), 100, 50, 0, 0, 0),
makeRecord("req_old", "claude-opus-4-6", ts(5, 10), 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 2, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 2 {
t.Errorf("expected 2 records (today + yesterday), got %d", data.TotalRecords)
}
}
func TestDaysFilter_OneDayMeansToday(t *testing.T) {
lines := []string{
makeRecord("req_today", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
makeRecord("req_yesterday", "claude-opus-4-6", ts(1, 10), 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 1, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record (today only), got %d", data.TotalRecords)
}
}
func TestDaysFilter_BoundaryExactCutoff(t *testing.T) {
// A record at 00:00:00 on the cutoff day should be included
cutoffDay := localMidnight().AddDate(0, 0, -2)
atMidnight := cutoffDay.Format(time.RFC3339)
beforeMidnight := cutoffDay.Add(-1 * time.Second).Format(time.RFC3339)
lines := []string{
makeRecord("req_at_cutoff", "claude-opus-4-6", atMidnight, 100, 50, 0, 0, 0),
makeRecord("req_before_cutoff", "claude-opus-4-6", beforeMidnight, 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
// days=3: today, yesterday, 2 days ago
data, err := parseLogs(base, 3, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record (at cutoff included, before cutoff excluded), got %d", data.TotalRecords)
}
}
func TestDaysFilter_ZeroMeansAllTime(t *testing.T) {
lines := []string{
makeRecord("req_ancient", "claude-opus-4-6", "2020-01-01T00:00:00Z", 100, 50, 0, 0, 0),
makeRecord("req_today", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 2 {
t.Errorf("expected 2 records with days=0, got %d", data.TotalRecords)
}
}
func TestDaysFilter_ExcludesEmptyTimestamp(t *testing.T) {
lines := []string{
makeRecord("req_today", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
// Record without timestamp
`{"type":"assistant","requestId":"req_notime","message":{"model":"claude-opus-4-6","role":"assistant","usage":{"input_tokens":100,"output_tokens":50}}}`,
}
base := setupProject(t, "test-project", lines)
// With days filter, empty-timestamp records should be excluded
data, err := parseLogs(base, 1, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record (empty timestamp excluded with days filter), got %d", data.TotalRecords)
}
// Without days filter, empty-timestamp records should be included
data, err = parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 2 {
t.Errorf("expected 2 records (empty timestamp included without days filter), got %d", data.TotalRecords)
}
}
func TestDaysFilter_CountsCalendarDays(t *testing.T) {
// Build one record per day for the last 10 days
var lines []string
for i := 0; i < 10; i++ {
lines = append(lines, makeRecord(
fmt.Sprintf("req_%d", i), "claude-opus-4-6", ts(i, 12), 100, 50, 0, 0, 0,
))
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 7, "")
if err != nil {
t.Fatal(err)
}
// days=7 should include today + 6 previous days = 7 records (days 0-6)
if data.TotalRecords != 7 {
t.Errorf("expected 7 records for days=7, got %d", data.TotalRecords)
}
// Verify the daily breakdown has exactly 7 dates
if len(data.DailyUsage) != 7 {
t.Errorf("expected 7 dates in daily breakdown, got %d", len(data.DailyUsage))
}
}
// --- Daily and Project Aggregation ---
func TestDailyAggregation(t *testing.T) {
lines := []string{
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
makeRecord("req_2", "claude-opus-4-6", ts(0, 14), 200, 100, 0, 0, 0),
makeRecord("req_3", "claude-opus-4-6", ts(1, 10), 300, 150, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
today := dateStr(0)
yesterday := dateStr(1)
todayBucket := data.DailyUsage[today]["claude-opus-4-6"]
if todayBucket == nil {
t.Fatalf("no bucket for today %s", today)
return
}
if todayBucket.Requests != 2 {
t.Errorf("expected 2 requests today, got %d", todayBucket.Requests)
}
if todayBucket.InputTokens != 300 {
t.Errorf("expected 300 input tokens today, got %d", todayBucket.InputTokens)
}
yesterdayBucket := data.DailyUsage[yesterday]["claude-opus-4-6"]
if yesterdayBucket == nil {
t.Fatalf("no bucket for yesterday %s", yesterday)
return
}
if yesterdayBucket.Requests != 1 {
t.Errorf("expected 1 request yesterday, got %d", yesterdayBucket.Requests)
}
}
func TestProjectAggregation(t *testing.T) {
base := setupProject(t, "project-alpha", []string{
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
makeRecord("req_2", "claude-sonnet-4-6", ts(0, 11), 80, 30, 0, 0, 0),
})
addProject(t, base, "project-beta", []string{
makeRecord("req_3", "claude-opus-4-6", ts(0, 10), 200, 100, 0, 0, 0),
})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
alpha := data.ProjectUsage["project-alpha"]
if alpha == nil {
t.Fatal("expected project-alpha in results")
}
if len(alpha) != 2 {
t.Errorf("expected 2 models in project-alpha, got %d", len(alpha))
}
beta := data.ProjectUsage["project-beta"]
if beta == nil {
t.Fatal("expected project-beta in results")
}
if beta["claude-opus-4-6"].InputTokens != 200 {
t.Errorf("expected 200 input for project-beta opus, got %d", beta["claude-opus-4-6"].InputTokens)
}
}
// --- Cache Token Handling ---
func TestCacheTokenAggregation(t *testing.T) {
lines := []string{
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 500, 200, 100),
makeRecord("req_2", "claude-opus-4-6", ts(0, 11), 100, 50, 300, 0, 150),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
opus := data.ModelUsage["claude-opus-4-6"]
if opus.CacheRead != 800 {
t.Errorf("expected cache_read=800, got %d", opus.CacheRead)
}
// req_1: 5m=200, 1h=100; req_2: 5m=0, 1h=150
if opus.CacheWrite5m != 200 {
t.Errorf("expected cache_write_5m=200, got %d", opus.CacheWrite5m)
}
if opus.CacheWrite1h != 250 {
t.Errorf("expected cache_write_1h=250, got %d", opus.CacheWrite1h)
}
if opus.TotalCacheWrite() != 450 {
t.Errorf("expected total_cache_write=450, got %d", opus.TotalCacheWrite())
}
}
func TestCacheTokenFallback_FlatField(t *testing.T) {
// When cache_creation is nil, cache_creation_input_tokens defaults to 1h
line := `{"type":"assistant","requestId":"req_flat","timestamp":` +
fmt.Sprintf("%q", ts(0, 10)) +
`,"message":{"model":"claude-opus-4-6","role":"assistant","usage":{"input_tokens":100,"output_tokens":50,"cache_read_input_tokens":0,"cache_creation_input_tokens":500}}}`
base := setupProject(t, "test-project", []string{line})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
opus := data.ModelUsage["claude-opus-4-6"]
if opus.CacheWrite5m != 0 {
t.Errorf("expected cache_write_5m=0, got %d", opus.CacheWrite5m)
}
if opus.CacheWrite1h != 500 {
t.Errorf("expected cache_write_1h=500, got %d", opus.CacheWrite1h)
}
}
// --- Edge Cases ---
func TestSkipsNonAssistantRecords(t *testing.T) {
lines := []string{
makeUserRecord(ts(0, 10)),
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record (user record skipped), got %d", data.TotalRecords)
}
}
func TestSkipsMalformedJSON(t *testing.T) {
lines := []string{
`{"type":"assistant", this is not valid json}`,
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 valid record, got %d", data.TotalRecords)
}
if data.ParseErrors != 1 {
t.Errorf("expected 1 parse error, got %d", data.ParseErrors)
}
}
func TestEmptyProject(t *testing.T) {
base := setupProject(t, "empty-project", []string{})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 0 {
t.Errorf("expected 0 records, got %d", data.TotalRecords)
}
}
func TestSkipsSyntheticErrorEntries(t *testing.T) {
lines := []string{
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
`{"type":"assistant","requestId":"req_err","timestamp":"` + ts(0, 11) + `","message":{"model":"<synthetic>","role":"assistant","content":[{"type":"text","text":"Claude AI usage limit reached|1755295200"}],"usage":{"input_tokens":0,"output_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0}},"isApiErrorMessage":true}`,
}
base := setupProject(t, "test-project", lines)
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if data.TotalRecords != 1 {
t.Errorf("expected 1 record (synthetic skipped), got %d", data.TotalRecords)
}
if _, ok := data.ModelUsage["<synthetic>"]; ok {
t.Error("synthetic model should not appear in results")
}
}
func TestNoProjectsDir(t *testing.T) {
base := t.TempDir()
_, err := parseLogs(base, 0, "")
if err == nil {
t.Error("expected error when projects dir doesn't exist")
}
}
// --- Project Best Match ---
func TestResolveProjectSlug(t *testing.T) {
base := t.TempDir()
projectsDir := filepath.Join(base, "projects")
for _, d := range []string{"foo-backend", "foo-frontend", "foo", "bar-app"} {
if err := os.MkdirAll(filepath.Join(projectsDir, d), 0755); err != nil {
t.Fatal(err)
}
}
tests := []struct {
name, filter, expected string
}{
{"exact match wins", "foo", "foo"},
{"shortest substring", "bar", "bar-app"},
{"no match", "zzz", ""},
{"empty filter", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveProjectSlug(projectsDir, tt.filter)
if got != tt.expected {
t.Errorf("resolveProjectSlug(%q) = %q, want %q", tt.filter, got, tt.expected)
}
})
}
}
// --- Branch Aggregation ---
func TestBranchAggregation(t *testing.T) {
base := setupProject(t, "project-alpha", []string{
makeRecordWithBranch("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0, "main"),
makeRecordWithBranch("req_2", "claude-opus-4-6", ts(0, 11), 200, 100, 0, 0, 0, "feature/auth"),
makeRecordWithBranch("req_3", "claude-sonnet-4-6", ts(0, 12), 80, 30, 0, 0, 0, "main"),
})
addProject(t, base, "project-beta", []string{
makeRecordWithBranch("req_4", "claude-opus-4-6", ts(0, 13), 150, 75, 0, 0, 0, "main"),
})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
alpha := data.BranchUsage["project-alpha"]
if alpha == nil {
t.Fatal("missing project-alpha in BranchUsage")
}
if len(alpha) != 2 {
t.Errorf("expected 2 branches in project-alpha, got %d", len(alpha))
}
if alpha["main"]["claude-opus-4-6"].Requests != 1 {
t.Error("wrong main opus count in project-alpha")
}
if alpha["main"]["claude-sonnet-4-6"].Requests != 1 {
t.Error("wrong main sonnet count in project-alpha")
}
if alpha["feature/auth"]["claude-opus-4-6"].Requests != 1 {
t.Error("wrong feature/auth count in project-alpha")
}
beta := data.BranchUsage["project-beta"]
if beta == nil {
t.Fatal("missing project-beta in BranchUsage")
}
if beta["main"]["claude-opus-4-6"].Requests != 1 {
t.Error("wrong main count in project-beta")
}
}
func TestBranchAggregation_NoBranch(t *testing.T) {
base := setupProject(t, "test-project", []string{
makeRecord("req_1", "claude-opus-4-6", ts(0, 10), 100, 50, 0, 0, 0),
})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
proj := data.BranchUsage["test-project"]
if proj == nil {
t.Fatal("missing project in BranchUsage")
}
if _, ok := proj["(no branch)"]; !ok {
t.Error("expected (no branch) bucket for records without gitBranch")
}
if proj["(no branch)"]["claude-opus-4-6"].Requests != 1 {
t.Error("wrong (no branch) request count")
}
}
func TestFastModeSeparateBucket(t *testing.T) {
withEmbeddedPricing(t)
base := setupProject(t, "test-project", []string{
makeRecordWithSpeed("req_1", "claude-opus-4-6", ts(0, 10), 100_000, 50_000, "standard"),
makeRecordWithSpeed("req_2", "claude-opus-4-6", ts(0, 11), 100_000, 50_000, "fast"),
makeRecordWithSpeed("req_3", "claude-opus-4-6", ts(0, 12), 100_000, 50_000, ""),
})
data, err := parseLogs(base, 0, "")
if err != nil {
t.Fatal(err)
}
if len(data.ModelUsage) != 2 {
t.Errorf("expected 2 model buckets (standard + fast), got %d", len(data.ModelUsage))
}
standard, ok := data.ModelUsage["claude-opus-4-6"]
if !ok || standard == nil {
t.Fatal("missing standard bucket")
}
if standard.Requests != 2 {
t.Errorf("expected 2 standard requests, got %d", standard.Requests)
}
fast, ok := data.ModelUsage["claude-opus-4-6:fast"]
if !ok || fast == nil {
t.Fatal("missing fast bucket")
}
if fast.Requests != 1 {
t.Errorf("expected 1 fast request, got %d", fast.Requests)
}
// Verify fast costs 6x more than standard for same tokens
if fast.Cost == 0 {
t.Error("fast cost should not be zero")
}
ratio := fast.Cost / (standard.Cost / 2) // per-request comparison
if ratio < 5.9 || ratio > 6.1 {
t.Errorf("expected ~6x ratio, got %.2f", ratio)
}
}
func TestFastModeDisplayName(t *testing.T) {
withEmbeddedPricing(t)
got := shortModel("claude-opus-4-6:fast")
if got != "Opus 4.6 ⚡" {
t.Errorf("shortModel for fast = %q, want %q", got, "Opus 4.6 ⚡")
}
}
func TestTotals(t *testing.T) {
result := &ParseResult{
ModelUsage: map[string]*Bucket{
"claude-opus-4-6": {
InputTokens: 1000, OutputTokens: 500,
CacheRead: 200, CacheWrite5m: 100, CacheWrite1h: 50,
Cost: 1.5, Requests: 2,
},
"claude-haiku-4-5-20251001": {
InputTokens: 800, OutputTokens: 300,
CacheRead: 100, CacheWrite5m: 60, CacheWrite1h: 40,
Cost: 0.5, Requests: 3,
},
},
}
totals := result.Totals()
assertInt(t, "totals.Input", totals.Input, 1800)
assertInt(t, "totals.Output", totals.Output, 800)
assertInt(t, "totals.CacheR", totals.CacheR, 300)
assertInt(t, "totals.CacheW5m", totals.CacheW5m, 160)
assertInt(t, "totals.CacheW1h", totals.CacheW1h, 90)
assertInt(t, "totals.CacheW", totals.CacheW, 250)
assertInt(t, "totals.Requests", totals.Requests, 5)
assertCost(t, "totals.Cost", totals.Cost, 2.0)
}
func TestDateRange(t *testing.T) {
tests := []struct {
name string
dates []string
wantFrom, wantTo string
}{
{
name: "multiple dates",
dates: []string{"2026-02-18", "2026-02-20", "2026-02-19"},
wantFrom: "2026-02-18", wantTo: "2026-02-20",
},
{
name: "single date",
dates: []string{"2026-02-19"},
wantFrom: "2026-02-19", wantTo: "2026-02-19",
},
{
name: "unknown dates excluded",
dates: []string{"unknown", "2026-02-18", "2026-02-20"},
wantFrom: "2026-02-18", wantTo: "2026-02-20",
},
{
name: "only unknown",
dates: []string{"unknown"},
wantFrom: "", wantTo: "",
},
{
name: "empty",
dates: nil,
wantFrom: "", wantTo: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &ParseResult{DailyUsage: make(map[string]map[string]*Bucket)}
for _, d := range tt.dates {
r.DailyUsage[d] = map[string]*Bucket{"model": {}}
}
from, to := r.DateRange()
if from != tt.wantFrom {
t.Errorf("from = %q, want %q", from, tt.wantFrom)
}
if to != tt.wantTo {
t.Errorf("to = %q, want %q", to, tt.wantTo)
}
})
}
}