-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat_test.go
More file actions
252 lines (230 loc) · 6.49 KB
/
format_test.go
File metadata and controls
252 lines (230 loc) · 6.49 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
package main
import (
"testing"
"time"
)
func TestFmtTokens(t *testing.T) {
tests := []struct {
input int
expected string
}{
{0, "0"},
{500, "500"},
{1_500, "1.5K"},
{1_500_000, "1.5M"},
{1_500_000_000, "1.5B"},
}
for _, tt := range tests {
got := fmtTokens(tt.input)
if got != tt.expected {
t.Errorf("fmtTokens(%d) = %q, want %q", tt.input, got, tt.expected)
}
}
}
func TestFmtCost(t *testing.T) {
tests := []struct {
input float64
expected string
}{
{0.0001, "$0.00"},
{0.5, "$0.50"},
{1.0, "$1.00"},
{52.18, "$52.18"},
}
for _, tt := range tests {
got := fmtCost(tt.input)
if got != tt.expected {
t.Errorf("fmtCost(%f) = %q, want %q", tt.input, got, tt.expected)
}
}
}
func TestShortProject(t *testing.T) {
tests := []struct {
input string
expected string
}{
// macOS paths: strip -Users-<user>- prefix
{"-Users-john-repos-myproject", "repos/myproject"},
{"-Users-john-repos-org-subdir", "repos/org-subdir"},
// Linux paths: strip -home-<user>- prefix
{"-home-john-repos-myproject", "repos/myproject"},
// No prefix to strip — first hyphen becomes /
{"simple-project", "simple/project"},
// Double hyphens collapsed
{"a--b", "a/b"},
// Long names preserved
{"a-very-long-project-name-that-exceeds-the-forty-character-limit", "a/very-long-project-name-that-exceeds-the-forty-character-limit"},
// Empty slug returns original
{"", ""},
}
for _, tt := range tests {
got := shortProject(tt.input)
if got != tt.expected {
t.Errorf("shortProject(%q) = %q, want %q", tt.input, got, tt.expected)
}
}
}
func TestWrapName(t *testing.T) {
tests := []struct {
name string
chunk int
expected []string
}{
{"short", 10, []string{"short"}},
{"abcdefghij", 10, []string{"abcdefghij"}},
{"abcdefghijklmnopqrst", 10, []string{"abcdefghij", "klmnopqrst"}},
{"abcdefghijklmnopqrstuvwxyz12345", 10, []string{"abcdefghij", "klmnopqrst", "uvwxyz1234", "5"}},
{"", 10, nil},
}
for _, tt := range tests {
got := wrapName(tt.name, tt.chunk)
if len(got) != len(tt.expected) {
t.Errorf("wrapName(%q, %d) len = %d, want %d", tt.name, tt.chunk, len(got), len(tt.expected))
continue
}
for i := range got {
if got[i] != tt.expected[i] {
t.Errorf("wrapName(%q, %d)[%d] = %q, want %q", tt.name, tt.chunk, i, got[i], tt.expected[i])
}
}
}
}
func TestFmtDuration(t *testing.T) {
tests := []struct {
input time.Duration
expected string
}{
{500 * time.Microsecond, "500µs"},
{999 * time.Microsecond, "999µs"},
{1 * time.Millisecond, "1ms"},
{150 * time.Millisecond, "150ms"},
{1500 * time.Millisecond, "1500ms"},
}
for _, tt := range tests {
got := fmtDuration(tt.input)
if got != tt.expected {
t.Errorf("fmtDuration(%v) = %q, want %q", tt.input, got, tt.expected)
}
}
}
func TestColorizeCustomThresholds(t *testing.T) {
origNoColor := noColorFlag
noColorFlag = false
origWarn := costThresholdYellow
origAlert := costThresholdRed
origCurrency := activeCurrency
defer func() {
noColorFlag = origNoColor
costThresholdYellow = origWarn
costThresholdRed = origAlert
activeCurrency = origCurrency
}()
costThresholdYellow = 10.0
costThresholdRed = 20.0
if colorize("test", 5.0) != "test" {
t.Error("below warn threshold should not colorize")
}
if colorize("test", 15.0) == "test" {
t.Error("between warn and alert should colorize (yellow)")
}
if colorize("test", 25.0) == "test" {
t.Error("above alert should colorize (red)")
}
// With currency active, colorize converts USD cost before comparing
activeCurrency.Rate = 0.92
costThresholdYellow = 25.0
costThresholdRed = 50.0
if colorize("test", 20.0) != "test" { // 20 * 0.92 = 18.4 < 25
t.Error("€18.40 should be plain (below €25 warn)")
}
if colorize("test", 30.0) == "test" { // 30 * 0.92 = 27.6, between 25-50
t.Error("€27.60 should be yellow")
}
if colorize("test", 60.0) == "test" { // 60 * 0.92 = 55.2 > 50
t.Error("€55.20 should be red")
}
}
func TestColorizeDefaultThresholds(t *testing.T) {
origNoColor := noColorFlag
noColorFlag = false
defer func() { noColorFlag = origNoColor }()
if colorize("test", 20.0) != "test" {
t.Error("$20 should be plain with default $25 warn threshold")
}
}
func TestTotalCacheWrite(t *testing.T) {
b := &Bucket{CacheWrite5m: 100, CacheWrite1h: 200}
if got := b.TotalCacheWrite(); got != 300 {
t.Errorf("TotalCacheWrite() = %d, want 300", got)
}
}
func TestAggregateMonthly(t *testing.T) {
daily := map[string]map[string]*Bucket{
"2025-01-15": {
"claude-sonnet": {InputTokens: 100, OutputTokens: 50, Requests: 1, Cost: 1.0},
},
"2025-01-20": {
"claude-sonnet": {InputTokens: 200, OutputTokens: 100, Requests: 2, Cost: 2.0},
"claude-opus": {InputTokens: 300, OutputTokens: 150, Requests: 1, Cost: 5.0},
},
"2025-02-05": {
"claude-sonnet": {InputTokens: 400, OutputTokens: 200, Requests: 3, Cost: 3.0},
},
}
monthly := aggregateMonthly(daily)
tests := []struct {
name string
month string
model string
wantReqs int
wantCost float64
}{
{"jan sonnet combined", "2025-01", "claude-sonnet", 3, 3.0},
{"jan opus", "2025-01", "claude-opus", 1, 5.0},
{"feb sonnet", "2025-02", "claude-sonnet", 3, 3.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, ok := monthly[tt.month][tt.model]
if !ok {
t.Fatalf("missing bucket for %s / %s", tt.month, tt.model)
}
if b.Requests != tt.wantReqs {
t.Errorf("requests = %d, want %d", b.Requests, tt.wantReqs)
}
if b.Cost != tt.wantCost {
t.Errorf("cost = %f, want %f", b.Cost, tt.wantCost)
}
})
}
if len(monthly) != 2 {
t.Errorf("expected 2 months, got %d", len(monthly))
}
}
func TestAggregateMonthlyTokens(t *testing.T) {
daily := map[string]map[string]*Bucket{
"2025-03-01": {
"model-a": {InputTokens: 100, OutputTokens: 50, CacheRead: 10, CacheWrite5m: 5, CacheWrite1h: 15, Requests: 1, Cost: 1.0},
},
"2025-03-15": {
"model-a": {InputTokens: 200, OutputTokens: 100, CacheRead: 20, CacheWrite5m: 10, CacheWrite1h: 30, Requests: 2, Cost: 2.0},
},
}
monthly := aggregateMonthly(daily)
b := monthly["2025-03"]["model-a"]
if b.InputTokens != 300 {
t.Errorf("InputTokens = %d, want 300", b.InputTokens)
}
if b.OutputTokens != 150 {
t.Errorf("OutputTokens = %d, want 150", b.OutputTokens)
}
if b.CacheRead != 30 {
t.Errorf("CacheRead = %d, want 30", b.CacheRead)
}
if b.CacheWrite5m != 15 {
t.Errorf("CacheWrite5m = %d, want 15", b.CacheWrite5m)
}
if b.CacheWrite1h != 45 {
t.Errorf("CacheWrite1h = %d, want 45", b.CacheWrite1h)
}
}