-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstatusline_config_test.go
More file actions
533 lines (469 loc) · 13.5 KB
/
Copy pathstatusline_config_test.go
File metadata and controls
533 lines (469 loc) · 13.5 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
package main
import (
"encoding/json"
"strings"
"testing"
"time"
)
func TestResolveStatuslineConfig_Nil(t *testing.T) {
segments, sep, opts := resolveStatuslineConfig(nil)
if len(segments) != len(defaultSegments) {
t.Errorf("got %d segments, want %d", len(segments), len(defaultSegments))
}
if sep != defaultSeparator {
t.Errorf("sep = %q, want %q", sep, defaultSeparator)
}
if opts != nil {
t.Error("expected nil options")
}
}
func TestResolveStatuslineConfig_Custom(t *testing.T) {
cfg := &StatuslineConfig{
Segments: []string{"ctx", "model"},
Separator: " | ",
SegmentOptions: map[string]SegmentOptions{
"ctx": {Emoji: "🧠"},
},
}
segments, sep, opts := resolveStatuslineConfig(cfg)
if len(segments) != 2 || segments[0] != "ctx" || segments[1] != "model" {
t.Errorf("segments = %v", segments)
}
if sep != " | " {
t.Errorf("sep = %q", sep)
}
if opts["ctx"].Emoji != "🧠" {
t.Errorf("ctx emoji = %q", opts["ctx"].Emoji)
}
}
func TestResolveStatuslineConfig_PartialOverride(t *testing.T) {
cfg := &StatuslineConfig{
Segments: []string{"model"},
}
_, sep, _ := resolveStatuslineConfig(cfg)
if sep != defaultSeparator {
t.Errorf("expected default separator when not specified, got %q", sep)
}
}
func TestSegmentEmoji_Default(t *testing.T) {
emoji := segmentEmoji("ctx", "💭", nil)
if emoji != "💭" {
t.Errorf("got %q, want 💭", emoji)
}
}
func TestSegmentEmoji_Override(t *testing.T) {
opts := map[string]SegmentOptions{"ctx": {Emoji: "🧠"}}
emoji := segmentEmoji("ctx", "💭", opts)
if emoji != "🧠" {
t.Errorf("got %q, want 🧠", emoji)
}
}
func TestSegmentLabel_Default(t *testing.T) {
label := segmentLabel("ctx", "ctx", nil)
if label != "ctx" {
t.Errorf("got %q, want ctx", label)
}
}
func TestSegmentLabel_Override(t *testing.T) {
opts := map[string]SegmentOptions{"ctx": {Label: "context"}}
label := segmentLabel("ctx", "ctx", opts)
if label != "context" {
t.Errorf("got %q, want context", label)
}
}
func makeTestInput() *StatuslineInput {
input := &StatuslineInput{}
input.Model.ID = "claude-opus-4-6"
input.ContextWindow.UsedPercentage = 33
input.ContextWindow.TotalInputTokens = 438
input.ContextWindow.TotalOutputTokens = 6518
input.Cost.TotalDurationMs = 1078708
input.Cost.TotalLinesAdded = 25
input.Cost.TotalLinesRemoved = 3
input.Cwd = "/home/user/projects/goccc"
input.Version = "2.1.83"
return input
}
func TestRenderSessionCost(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
ctx := &StatuslineContext{SessionCost: 1.50, Input: makeTestInput()}
got := renderSessionCost(ctx)
if got != "💸 $1.50 session" {
t.Errorf("got %q", got)
}
}
func TestRenderSessionCost_Zero(t *testing.T) {
ctx := &StatuslineContext{SessionCost: 0, Input: makeTestInput()}
got := renderSessionCost(ctx)
if got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderSessionCost_CustomEmoji(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
ctx := &StatuslineContext{
SessionCost: 2.00,
Input: makeTestInput(),
Options: map[string]SegmentOptions{"session_cost": {Emoji: "🤑", Label: "sess"}},
}
got := renderSessionCost(ctx)
if got != "🤑 $2.00 sess" {
t.Errorf("got %q", got)
}
}
func TestRenderTodayCost_Zero(t *testing.T) {
ctx := &StatuslineContext{TodayCost: 0, Input: makeTestInput()}
if got := renderTodayCost(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderCtx(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderCtx(ctx)
if got != "💭 33% ctx" {
t.Errorf("got %q", got)
}
}
func TestRenderModel(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderModel(ctx)
if got != "🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestRenderMCP_None(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
if got := renderMCP(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderMCP_Multiple(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput(), MCPNames: []string{"github", "jira"}}
got := renderMCP(ctx)
if !strings.Contains(got, "🔌 2 MCPs (github, jira)") {
t.Errorf("got %q", got)
}
}
func TestRender5h_Nil(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
if got := render5h(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRender5h_Present(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
input.RateLimits.FiveHour = &rateLimitWindow{
UsedPercentage: 6,
ResetsAt: time.Now().Add(3*time.Hour + 30*time.Minute).Unix(),
}
ctx := &StatuslineContext{Input: input}
got := render5h(ctx)
if !strings.Contains(got, "94%") {
t.Errorf("got %q, expected 94%%", got)
}
}
func TestRender7d_Nil(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
if got := render7d(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRender7d_Present(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
input.RateLimits.SevenDay = &rateLimitWindow{
UsedPercentage: 13,
ResetsAt: time.Now().Add(100 * time.Hour).Unix(),
}
ctx := &StatuslineContext{Input: input}
got := render7d(ctx)
if !strings.Contains(got, "87%") {
t.Errorf("got %q, expected 87%%", got)
}
if !strings.Contains(got, "/7d") {
t.Errorf("got %q, expected /7d label", got)
}
}
func TestRenderTokens(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderTokens(ctx)
if !strings.Contains(got, "📊") || !strings.Contains(got, "in") || !strings.Contains(got, "out") {
t.Errorf("got %q", got)
}
}
func TestRenderLines(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderLines(ctx)
if got != "📝 +25 -3" {
t.Errorf("got %q", got)
}
}
func TestRenderLines_BothZero(t *testing.T) {
input := makeTestInput()
input.Cost.TotalLinesAdded = 0
input.Cost.TotalLinesRemoved = 0
ctx := &StatuslineContext{Input: input}
if got := renderLines(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderDuration(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderDuration(ctx)
if !strings.Contains(got, "⏱️") || !strings.Contains(got, "17m") {
t.Errorf("got %q", got)
}
}
func TestRenderDuration_Zero(t *testing.T) {
input := makeTestInput()
input.Cost.TotalDurationMs = 0
ctx := &StatuslineContext{Input: input}
if got := renderDuration(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderCwd(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderCwd(ctx)
if got != "📁 goccc" {
t.Errorf("got %q", got)
}
}
func TestRenderCwd_FallbackToWorkspace(t *testing.T) {
input := makeTestInput()
input.Cwd = ""
input.Workspace.CurrentDir = "/opt/workspace/myproject"
ctx := &StatuslineContext{Input: input}
got := renderCwd(ctx)
if got != "📁 myproject" {
t.Errorf("got %q", got)
}
}
func TestRenderCwd_Empty(t *testing.T) {
input := makeTestInput()
input.Cwd = ""
ctx := &StatuslineContext{Input: input}
if got := renderCwd(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestRenderVersion(t *testing.T) {
ctx := &StatuslineContext{Input: makeTestInput()}
got := renderVersion(ctx)
if got != "🏷️ 2.1.83" {
t.Errorf("got %q", got)
}
}
func TestRenderVersion_Empty(t *testing.T) {
input := makeTestInput()
input.Version = ""
ctx := &StatuslineContext{Input: input}
if got := renderVersion(ctx); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestFormatStatuslineWithConfig_CustomSegments(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"ctx", "model"},
}
got := formatStatuslineWithConfig(0.50, 2.00, input, nil, "", cfg)
if got != "💭 33% ctx · 🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestFormatStatuslineWithConfig_CustomSeparator(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"ctx", "model"},
Separator: " | ",
}
got := formatStatuslineWithConfig(0, 0, input, nil, "", cfg)
if got != "💭 33% ctx | 🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestFormatStatuslineWithConfig_LineBreak(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"session_cost", "|", "ctx", "model"},
}
got := formatStatuslineWithConfig(1.00, 0, input, nil, "", cfg)
if !strings.Contains(got, "\n") {
t.Errorf("expected newline in %q", got)
}
lines := strings.Split(got, "\n")
if len(lines) != 2 {
t.Errorf("got %d lines, want 2", len(lines))
}
if lines[0] != "💸 $1.00 session" {
t.Errorf("line 0 = %q", lines[0])
}
if lines[1] != "💭 33% ctx · 🤖 Opus 4.6" {
t.Errorf("line 1 = %q", lines[1])
}
}
func TestFormatStatuslineWithConfig_UnknownSegmentIgnored(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"nonexistent", "model"},
}
got := formatStatuslineWithConfig(0, 0, input, nil, "", cfg)
if got != "🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestFormatStatuslineWithConfig_AutoHideSkipsEmptySegments(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"session_cost", "5h", "mcp", "model"},
}
got := formatStatuslineWithConfig(0, 0, input, nil, "", cfg)
if got != "🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestAssembleStatusline_ExplicitBreak(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
ctx := &StatuslineContext{
SessionCost: 1.00,
TodayCost: 5.00,
Input: input,
}
segments := []string{"session_cost", "|", "today_cost", "ctx"}
result := assembleStatusline(segments, " · ", ctx)
lines := strings.Split(result, "\n")
if len(lines) != 2 {
t.Errorf("expected 2 lines, got %d: %q", len(lines), result)
}
if lines[0] != "💸 $1.00 session" {
t.Errorf("line 0 = %q", lines[0])
}
if lines[1] != "💰 $5.00 today · 💭 33% ctx" {
t.Errorf("line 1 = %q", lines[1])
}
}
func TestAssembleStatusline_ConsecutiveBreaks(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
ctx := &StatuslineContext{SessionCost: 1.00, Input: input}
segments := []string{"session_cost", "|", "|", "model"}
result := assembleStatusline(segments, " · ", ctx)
lines := strings.Split(result, "\n")
if len(lines) != 2 {
t.Errorf("consecutive | should not produce empty lines, got %d: %q", len(lines), result)
}
}
func TestRenderBranch(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"branch", "model"},
}
got := formatStatuslineWithConfig(0, 0, input, nil, "feature/auth", cfg)
if got != "🌿 feature/auth · 🤖 Opus 4.6" {
t.Errorf("got %q", got)
}
}
func TestRenderBranch_Empty(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"branch", "model"},
}
got := formatStatuslineWithConfig(0, 0, input, nil, "", cfg)
if got != "🤖 Opus 4.6" {
t.Errorf("expected branch to be hidden when empty, got %q", got)
}
}
func TestRenderBranch_CustomEmoji(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
input := makeTestInput()
cfg := &StatuslineConfig{
Segments: []string{"branch"},
SegmentOptions: map[string]SegmentOptions{
"branch": {Emoji: "🔀"},
},
}
got := formatStatuslineWithConfig(0, 0, input, nil, "main", cfg)
if got != "🔀 main" {
t.Errorf("got %q", got)
}
}
func TestFormatRateLimitUsage_SevenDay(t *testing.T) {
noColorFlag = true
defer func() { noColorFlag = false }()
now := time.Date(2026, 3, 25, 10, 0, 0, 0, time.UTC)
resetsAt := time.Date(2026, 3, 30, 10, 0, 0, 0, time.UTC).Unix()
got := formatRateLimitUsage(13, resetsAt, now, sevenDayWindow, sevenDayLowBattery)
if !strings.Contains(got, "87%") {
t.Errorf("got %q, expected 87%%", got)
}
if !strings.Contains(got, "/7d") {
t.Errorf("got %q, expected /7d label", got)
}
}
func TestStatuslineConfigFromJSON(t *testing.T) {
raw := `{
"currency": "EUR",
"cached_rate": 0.87,
"statusline": {
"segments": ["ctx", "model"],
"separator": " | ",
"segment_options": {
"ctx": {"emoji": "🧠", "label": "context"}
}
}
}`
var cfg CurrencyConfig
if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if cfg.Statusline == nil {
t.Fatal("expected non-nil Statusline config")
}
if len(cfg.Statusline.Segments) != 2 {
t.Errorf("got %d segments", len(cfg.Statusline.Segments))
}
if cfg.Statusline.Separator != " | " {
t.Errorf("separator = %q", cfg.Statusline.Separator)
}
if cfg.Statusline.SegmentOptions["ctx"].Emoji != "🧠" {
t.Errorf("ctx emoji = %q", cfg.Statusline.SegmentOptions["ctx"].Emoji)
}
}
func TestStatuslineConfigFromJSON_Missing(t *testing.T) {
raw := `{"currency": "USD"}`
var cfg CurrencyConfig
if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if cfg.Statusline != nil {
t.Errorf("expected nil Statusline for missing key, got %+v", cfg.Statusline)
}
}