-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
529 lines (467 loc) · 14 KB
/
Copy pathmain_test.go
File metadata and controls
529 lines (467 loc) · 14 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
package main
import (
"bytes"
"strings"
"testing"
"time"
)
func TestParseGitStatus(t *testing.T) {
tests := []struct {
name string
input string
branch string
shortHash string
ahead string
behind string
dirty bool
}{
{
name: "clean repo on main",
input: `# branch.oid abc1234def5678
# branch.head main
# branch.ab +0 -0`,
branch: "main",
shortHash: "abc1234",
dirty: false,
},
{
name: "dirty with modified file",
input: `# branch.oid abc1234def5678
# branch.head feature
# branch.ab +0 -0
1 .M N... 100644 100644 100644 abc123 def456 file.go`,
branch: "feature",
shortHash: "abc1234",
dirty: true,
},
{
name: "ahead and behind",
input: `# branch.oid abc1234def5678
# branch.head main
# branch.ab +3 -2`,
branch: "main",
shortHash: "abc1234",
ahead: "3",
behind: "2",
},
{
name: "only ahead",
input: `# branch.oid abc1234def5678
# branch.head main
# branch.ab +5 -0`,
branch: "main",
shortHash: "abc1234",
ahead: "5",
},
{
name: "untracked file marks dirty",
input: `# branch.oid abc1234def5678
# branch.head main
? newfile.go`,
branch: "main",
shortHash: "abc1234",
dirty: true,
},
{
name: "renamed file marks dirty",
input: `# branch.oid abc1234def5678
# branch.head main
2 R. N... 100644 100644 100644 abc123 def456 R100 new.go old.go`,
branch: "main",
shortHash: "abc1234",
dirty: true,
},
{
name: "unmerged file marks dirty",
input: `# branch.oid abc1234def5678
# branch.head main
u UU N... 100644 100644 100644 100644 abc123 def456 ghi789 file.go`,
branch: "main",
shortHash: "abc1234",
dirty: true,
},
{
name: "short hex oid kept as-is",
input: `# branch.oid abc`,
shortHash: "abc",
},
{
name: "initial commit oid is ignored",
input: `# branch.oid (initial)`,
shortHash: "",
},
{
name: "non-hex oid is ignored",
input: `# branch.oid notahash`,
shortHash: "",
},
{
name: "no branch.ab line means no ahead/behind",
input: `# branch.head main`,
branch: "main",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
branch, shortHash, ahead, behind, dirty := parseGitStatus(tt.input)
if branch != tt.branch {
t.Errorf("branch = %q, want %q", branch, tt.branch)
}
if shortHash != tt.shortHash {
t.Errorf("shortHash = %q, want %q", shortHash, tt.shortHash)
}
if ahead != tt.ahead {
t.Errorf("ahead = %q, want %q", ahead, tt.ahead)
}
if behind != tt.behind {
t.Errorf("behind = %q, want %q", behind, tt.behind)
}
if dirty != tt.dirty {
t.Errorf("dirty = %v, want %v", dirty, tt.dirty)
}
})
}
}
func defaultInput() Input {
return Input{
Model: ModelInfo{DisplayName: "Claude Sonnet 4"},
ContextWindow: ContextWindow{
ContextWindowSize: 200000,
UsedPercentage: 10,
RemainingPercentage: 90,
},
}
}
var testNow = time.Unix(1738400000, 0)
func render(input Input, git GitInfo) string {
var buf bytes.Buffer
renderOutput(&buf, input, git, testNow, "/home/user")
return buf.String()
}
func TestRenderOutput(t *testing.T) {
t.Run("model line with style", func(t *testing.T) {
in := defaultInput()
in.OutputStyle = OutputStyle{Name: "concise"}
in.ContextWindow.UsedPercentage = 50
in.ContextWindow.RemainingPercentage = 50
out := render(in, GitInfo{})
if !strings.Contains(out, "Sonnet 4:concise") {
t.Errorf("expected model:style in output, got: %q", out)
}
})
t.Run("default style omitted from model line", func(t *testing.T) {
in := defaultInput()
in.OutputStyle = OutputStyle{Name: "default"}
out := render(in, GitInfo{})
if strings.Contains(out, ":default") {
t.Errorf("default style should not appear in output, got: %q", out)
}
if !strings.Contains(out, "[Sonnet 4]") {
t.Errorf("expected model without style suffix, got: %q", out)
}
})
t.Run("git line with dirty and ahead", func(t *testing.T) {
out := render(defaultInput(), GitInfo{
RepoName: "myrepo",
Branch: "main",
ShortHash: "abc1234",
Ahead: "2",
Dirty: true,
})
if !strings.Contains(out, "myrepo") {
t.Error("expected repo name in output")
}
if !strings.Contains(out, "main") {
t.Error("expected branch in output")
}
if !strings.Contains(out, "*") {
t.Error("expected dirty marker in output")
}
if !strings.Contains(out, "↑2") {
t.Error("expected ahead indicator in output")
}
})
t.Run("no git line when repo empty", func(t *testing.T) {
out := render(defaultInput(), GitInfo{})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 2 {
t.Errorf("expected 2 lines without git info, got %d", len(lines))
}
})
t.Run("git line shown without repo name", func(t *testing.T) {
out := render(defaultInput(), GitInfo{
Branch: "main",
ShortHash: "abc1234",
Dirty: true,
})
if !strings.Contains(out, "main") {
t.Error("expected branch in output")
}
if !strings.Contains(out, "*") {
t.Error("expected dirty marker in output")
}
})
t.Run("context bar percentage", func(t *testing.T) {
in := defaultInput()
in.ContextWindow.UsedPercentage = 75
in.ContextWindow.RemainingPercentage = 25
out := render(in, GitInfo{})
if !strings.Contains(out, "75%") {
t.Errorf("expected 75%% in output, got: %q", out)
}
if !strings.Contains(out, "50k free") {
t.Errorf("expected 50k free in output, got: %q", out)
}
})
t.Run("exceeds 200k warning", func(t *testing.T) {
in := defaultInput()
in.Exceeds200k = true
in.ContextWindow.UsedPercentage = 95
in.ContextWindow.RemainingPercentage = 5
out := render(in, GitInfo{})
if !strings.Contains(out, "95%!") {
t.Errorf("expected 95%%! warning in output, got: %q", out)
}
})
t.Run("cost shown when nonzero", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalCostUSD = 1.50
out := render(in, GitInfo{})
if !strings.Contains(out, "$1.50") {
t.Errorf("expected $1.50 in output, got: %q", out)
}
})
t.Run("lines added/removed", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalLinesAdded = 42
in.Cost.TotalLinesRemoved = 10
out := render(in, GitInfo{})
if !strings.Contains(out, "+42") || !strings.Contains(out, "-10") {
t.Errorf("expected +42/-10 in output, got: %q", out)
}
})
t.Run("duration hidden when zero", func(t *testing.T) {
out := render(defaultInput(), GitInfo{})
if strings.Contains(out, "0h 0m") {
t.Errorf("expected no duration at zero, got: %q", out)
}
})
t.Run("duration minutes only when under 1h", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalDurationMS = 5 * 60 * 1000
out := render(in, GitInfo{})
if !strings.Contains(out, "5m") {
t.Errorf("expected 5m in output, got: %q", out)
}
if strings.Contains(out, "0h") {
t.Errorf("expected no 0h prefix, got: %q", out)
}
})
t.Run("duration hours and minutes", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalDurationMS = 90 * 60 * 1000
out := render(in, GitInfo{})
if !strings.Contains(out, "1h 30m") {
t.Errorf("expected 1h 30m in output, got: %q", out)
}
})
t.Run("agent name shown", func(t *testing.T) {
in := defaultInput()
in.Agent = AgentInfo{Name: "myagent"}
out := render(in, GitInfo{})
if !strings.Contains(out, "@myagent") {
t.Errorf("expected @myagent in output, got: %q", out)
}
})
t.Run("worktree indicator", func(t *testing.T) {
out := render(defaultInput(), GitInfo{
RepoName: "myrepo",
Branch: "main",
IsWorktree: true,
})
if !strings.Contains(out, "wt") {
t.Errorf("expected wt indicator in output, got: %q", out)
}
})
t.Run("negative percentage does not panic", func(t *testing.T) {
in := defaultInput()
in.ContextWindow.UsedPercentage = -10
in.ContextWindow.RemainingPercentage = 110
out := render(in, GitInfo{})
if !strings.Contains(out, "Sonnet 4") {
t.Errorf("expected model in output, got: %q", out)
}
})
}
func TestRateLimits(t *testing.T) {
t.Run("rate limits shown when present", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 23.5, ResetsAt: testNow.Unix() + 3600},
SevenDay: &RateWindow{UsedPercentage: 41.2, ResetsAt: testNow.Unix() + 86400},
}
out := render(in, GitInfo{})
if !strings.Contains(out, "5h:") {
t.Errorf("expected 5h label, got: %q", out)
}
if !strings.Contains(out, "76%") { // 100 - 23.5 truncated
t.Errorf("expected 77%% remaining for 5h, got: %q", out)
}
if !strings.Contains(out, "7d:") {
t.Errorf("expected 7d label, got: %q", out)
}
if !strings.Contains(out, "59%") { // 100 - 41.2 rounded
t.Errorf("expected 59%% remaining for 7d, got: %q", out)
}
})
t.Run("rate limits hide cost", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalCostUSD = 1.50
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 10, ResetsAt: testNow.Unix() + 3600},
}
out := render(in, GitInfo{})
if strings.Contains(out, "$1.50") {
t.Errorf("cost should be hidden when rate_limits present, got: %q", out)
}
})
t.Run("cost shown without rate limits", func(t *testing.T) {
in := defaultInput()
in.Cost.TotalCostUSD = 1.50
out := render(in, GitInfo{})
if !strings.Contains(out, "$1.50") {
t.Errorf("expected cost shown without rate_limits, got: %q", out)
}
})
t.Run("reset time shown in hours", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 50, ResetsAt: testNow.Unix() + 7200}, // 2h
}
out := render(in, GitInfo{})
if !strings.Contains(out, "(2h)") {
t.Errorf("expected (2h) reset time, got: %q", out)
}
})
t.Run("reset time shown in minutes", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 50, ResetsAt: testNow.Unix() + 1800}, // 30m
}
out := render(in, GitInfo{})
if !strings.Contains(out, "(30m)") {
t.Errorf("expected (30m) reset time, got: %q", out)
}
})
t.Run("reset time shown in days", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
SevenDay: &RateWindow{UsedPercentage: 20, ResetsAt: testNow.Unix() + 3*86400}, // 3d
}
out := render(in, GitInfo{})
if !strings.Contains(out, "(3d)") {
t.Errorf("expected (3d) reset time, got: %q", out)
}
})
t.Run("low remaining shows red", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 95, ResetsAt: testNow.Unix() + 600},
}
out := render(in, GitInfo{})
if !strings.Contains(out, red) {
t.Errorf("expected red color for low remaining, got: %q", out)
}
})
t.Run("only five_hour present", func(t *testing.T) {
in := defaultInput()
in.RateLimits = &RateLimits{
FiveHour: &RateWindow{UsedPercentage: 30, ResetsAt: testNow.Unix() + 3600},
}
out := render(in, GitInfo{})
if !strings.Contains(out, "5h:") {
t.Errorf("expected 5h label, got: %q", out)
}
if strings.Contains(out, "7d:") {
t.Errorf("7d should not appear when absent, got: %q", out)
}
})
}
func TestShortenPath(t *testing.T) {
tests := []struct {
name string
dir string
homeDir string
want string
}{
{"home replaced with tilde", "/home/user/projects/foo", "/home/user", "~/projects/foo"},
{"exact home dir", "/home/user", "/home/user", "~"},
{"no home match", "/opt/app/src", "/home/user", "/opt/app/src"},
{"empty home dir", "/some/path", "", "/some/path"},
{"long path truncated at slash", "/home/user/" + strings.Repeat("a", 20) + "/" + strings.Repeat("b", 20) + "/" + strings.Repeat("c", 20), "/home/user", "…/" + strings.Repeat("b", 20) + "/" + strings.Repeat("c", 20)},
{"home prefix not partial match", "/home/username/foo", "/home/user", "/home/username/foo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shortenPath(tt.dir, tt.homeDir)
if got != tt.want {
t.Errorf("shortenPath(%q, %q) = %q, want %q", tt.dir, tt.homeDir, got, tt.want)
}
})
}
}
func TestRenderDir(t *testing.T) {
t.Run("directory shown on line 1", func(t *testing.T) {
in := defaultInput()
in.Workspace.CurrentDir = "/home/user/projects/myapp"
out := render(in, GitInfo{})
if !strings.Contains(out, "~/projects/myapp") {
t.Errorf("expected ~/projects/myapp in output, got: %q", out)
}
})
t.Run("agent before dir", func(t *testing.T) {
in := defaultInput()
in.Workspace.CurrentDir = "/home/user/projects/myapp"
in.Agent = AgentInfo{Name: "test"}
out := render(in, GitInfo{})
line1 := strings.Split(out, "\n")[0]
agentIdx := strings.Index(line1, "@test")
dirIdx := strings.Index(line1, "~/projects/myapp")
if agentIdx < 0 || dirIdx < 0 || agentIdx > dirIdx {
t.Errorf("expected agent before dir on line 1, got: %q", line1)
}
})
t.Run("no dir when empty", func(t *testing.T) {
in := defaultInput()
in.Workspace.CurrentDir = ""
out := render(in, GitInfo{})
line1 := strings.Split(out, "\n")[0]
// Should just be the model tag with no trailing pipe
pipeCount := strings.Count(line1, "|")
if pipeCount != 0 {
t.Errorf("expected no pipes without dir or agent, got: %q", line1)
}
})
}
func TestTruncateCommitMessage(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"short message unchanged", "hello", "hello"},
{"exact 70 runes unchanged", strings.Repeat("a", 70), strings.Repeat("a", 70)},
{"long ascii truncated to 70 with ellipsis", strings.Repeat("a", 75), strings.Repeat("a", 70) + "…"},
{"emoji preserved at boundary", strings.Repeat("a", 69) + "🎉", strings.Repeat("a", 69) + "🎉"},
{"emoji not split", strings.Repeat("a", 70) + "🎉", strings.Repeat("a", 70) + "…"},
{"multi-byte truncation preserves valid utf8", strings.Repeat("a", 68) + "🎉🎉🎉", strings.Repeat("a", 68) + "🎉🎉" + "…"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncateCommitMessage(tt.input)
if got != tt.want {
t.Errorf("truncateCommitMessage(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}