summaryrefslogtreecommitdiff
path: root/internal/tmux/status_coverage_test.go
blob: 0c76ee63b4c3d1d6e3b2e5eb387570cf034a4993 (plain)
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
package tmux

import (
	"strings"
	"testing"
	"time"
)

// --- Enabled ---

func TestEnabled_DefaultTrue(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "")
	if !Enabled() {
		t.Fatal("expected Enabled() true when env is empty")
	}
}

func TestEnabled_TruthyValues(t *testing.T) {
	for _, v := range []string{"1", "true", "yes", "on", " TRUE ", " On "} {
		t.Run(v, func(t *testing.T) {
			t.Setenv("HEXAI_TMUX_STATUS", v)
			if !Enabled() {
				t.Fatalf("expected Enabled() true for %q", v)
			}
		})
	}
}

func TestEnabled_FalsyValues(t *testing.T) {
	for _, v := range []string{"0", "false", "no", "off", "random"} {
		t.Run(v, func(t *testing.T) {
			t.Setenv("HEXAI_TMUX_STATUS", v)
			if Enabled() {
				t.Fatalf("expected Enabled() false for %q", v)
			}
		})
	}
}

// --- SetUserOption (logic paths, not actual tmux calls) ---

func TestSetUserOption_DisabledByEnv(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "off")
	// Should return nil immediately when disabled
	if err := SetUserOption("hexai_status", "test"); err != nil {
		t.Fatalf("expected nil error when disabled, got %v", err)
	}
}

func TestSetUserOption_EmptyKey(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "1")
	t.Setenv("TMUX", "/tmp/tmux-1,1,1")
	old := lookPath
	t.Cleanup(func() { lookPath = old })
	lookPath = func(string) (string, error) { return "/bin/tmux", nil }
	// Empty key after trimming should return nil
	if err := SetUserOption("  @  ", "test"); err != nil {
		t.Fatalf("expected nil for empty key, got %v", err)
	}
	if err := SetUserOption("  ", "test"); err != nil {
		t.Fatalf("expected nil for blank key, got %v", err)
	}
}

// --- SetStatus (just verifies it delegates; no tmux binary needed when disabled) ---

func TestSetStatus_DisabledNoOp(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "off")
	if err := SetStatus("anything"); err != nil {
		t.Fatalf("expected nil when status disabled, got %v", err)
	}
}

// --- FormatLLMStartStatus ---

func TestFormatLLMStartStatus(t *testing.T) {
	s := FormatLLMStartStatus("openai", "gpt-4.1")
	if !strings.Contains(s, "LLM:openai:gpt-4.1") {
		t.Fatalf("missing provider:model in %q", s)
	}
	if !strings.Contains(s, "⏳") {
		t.Fatalf("missing hourglass emoji in %q", s)
	}
	// Should contain baseFGToken placeholders (pre-theme)
	if !strings.Contains(s, baseFGToken) {
		t.Fatalf("expected baseFGToken placeholder in %q", s)
	}
}

// --- humanWindow ---

func TestHumanWindow(t *testing.T) {
	tests := []struct {
		name string
		d    time.Duration
		want string
	}{
		{"zero", 0, "?"},
		{"negative", -5 * time.Minute, "?"},
		{"exact hour", time.Hour, "1h"},
		{"two hours", 2 * time.Hour, "2h"},
		{"30 minutes", 30 * time.Minute, "30m"},
		{"90 minutes", 90 * time.Minute, "90m"},
		{"45 minutes", 45 * time.Minute, "45m"},
		{"120 minutes", 120 * time.Minute, "2h"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := humanWindow(tt.d)
			if got != tt.want {
				t.Errorf("humanWindow(%v) = %q, want %q", tt.d, got, tt.want)
			}
		})
	}
}

// --- truncateStatus ---

func TestTruncateStatus(t *testing.T) {
	tests := []struct {
		name string
		s    string
		n    int
		want string
	}{
		{"zero limit", "hello", 0, ""},
		{"negative limit", "hello", -1, ""},
		{"within limit", "hi", 5, "hi"},
		{"exact limit", "hello", 5, "hello"},
		{"over limit", "hello world", 5, "hell…"},
		{"limit 1", "hello", 1, "h"},
		{"limit 2", "hello", 2, "h…"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := truncateStatus(tt.s, tt.n)
			if got != tt.want {
				t.Errorf("truncateStatus(%q, %d) = %q, want %q", tt.s, tt.n, got, tt.want)
			}
		})
	}
}

// --- strings.TrimSpace ---

func TestStringsTrim(t *testing.T) {
	tests := []struct {
		name string
		s    string
		want string
	}{
		{"empty", "", ""},
		{"no whitespace", "hello", "hello"},
		{"leading spaces", "  hello", "hello"},
		{"trailing spaces", "hello  ", "hello"},
		{"both sides", "  hello  ", "hello"},
		{"tabs", "\thello\t", "hello"},
		{"newlines", "\nhello\n", "hello"},
		{"carriage returns", "\rhello\r", "hello"},
		{"mixed whitespace", " \t\n\rhello \t\n\r", "hello"},
		{"all whitespace", "   \t\n  ", ""},
		{"internal spaces preserved", "hello world", "hello world"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := strings.TrimSpace(tt.s)
			if got != tt.want {
				t.Errorf("strings.TrimSpace(%q) = %q, want %q", tt.s, got, tt.want)
			}
		})
	}
}

// --- maxStatusLen ---

func TestMaxStatusLen(t *testing.T) {
	tests := []struct {
		name string
		env  string
		want int
	}{
		{"empty", "", 0},
		{"valid", "80", 80},
		{"negative", "-5", 0},
		{"zero", "0", 0},
		{"non-numeric", "abc", 0},
		{"whitespace padded", " 100 ", 100},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", tt.env)
			got := maxStatusLen()
			if got != tt.want {
				t.Errorf("maxStatusLen() = %d, want %d (env=%q)", got, tt.want, tt.env)
			}
		})
	}
}

// --- narrowEnabled ---

func TestNarrowEnabled(t *testing.T) {
	tests := []struct {
		name string
		env  string
		want bool
	}{
		{"empty", "", false},
		{"1", "1", true},
		{"true", "true", true},
		{"yes", "yes", true},
		{"on", "on", true},
		{"0", "0", false},
		{"false", "false", false},
		{"random", "random", false},
		{"TRUE uppercase", "TRUE", true},
		{"padded", " 1 ", true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Setenv("HEXAI_TMUX_STATUS_NARROW", tt.env)
			got := narrowEnabled()
			if got != tt.want {
				t.Errorf("narrowEnabled() = %v, want %v (env=%q)", got, tt.want, tt.env)
			}
		})
	}
}

// --- applyTheme ---

func TestApplyTheme_NoTheme(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	input := baseFGToken + "hello" + arrowUpToken + "up" + arrowDownToken + "down"
	got := applyTheme(input)
	// Should replace tokens with default fg and default arrow colors
	if strings.Contains(got, baseFGToken) {
		t.Fatalf("baseFGToken not replaced in %q", got)
	}
	if !strings.Contains(got, "#[fg=default]") {
		t.Fatalf("expected default fg in %q", got)
	}
	// No wrap, so no bg=default suffix
	if strings.HasSuffix(got, "#[fg=default,bg=default]") {
		t.Fatalf("should not wrap without theme in %q", got)
	}
}

func TestApplyTheme_PurpleTheme(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "purple")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	input := baseFGToken + "hello"
	got := applyTheme(input)
	if !strings.Contains(got, "#[fg=white") {
		t.Fatalf("expected white fg for purple theme in %q", got)
	}
	if !strings.Contains(got, "bg=magenta") {
		t.Fatalf("expected magenta bg for purple theme in %q", got)
	}
	if !strings.HasSuffix(got, "#[fg=default,bg=default]") {
		t.Fatalf("expected reset suffix for themed output in %q", got)
	}
}

func TestApplyTheme_YellowTheme(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "yellow")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	input := baseFGToken + "test" + arrowUpToken + "u" + arrowDownToken + "d"
	got := applyTheme(input)
	if !strings.Contains(got, "#[fg=black") {
		t.Fatalf("expected black fg for yellow theme in %q", got)
	}
	if !strings.Contains(got, "bg=yellow") {
		t.Fatalf("expected yellow bg in %q", got)
	}
}

func TestApplyTheme_BlueTheme(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "blue")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	input := baseFGToken + "test" + arrowUpToken + "u" + arrowDownToken + "d"
	got := applyTheme(input)
	if !strings.Contains(got, "#[fg=white") {
		t.Fatalf("expected white fg for blue theme in %q", got)
	}
	if !strings.Contains(got, "bg=blue") {
		t.Fatalf("expected blue bg in %q", got)
	}
}

func TestApplyTheme_ExplicitFGBG(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "red")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "green")
	input := baseFGToken + "test" + arrowUpToken + "u" + arrowDownToken + "d"
	got := applyTheme(input)
	if !strings.Contains(got, "#[fg=red") {
		t.Fatalf("expected red fg in %q", got)
	}
	if !strings.Contains(got, "bg=green") {
		t.Fatalf("expected green bg in %q", got)
	}
	if !strings.HasSuffix(got, "#[fg=default,bg=default]") {
		t.Fatalf("expected reset suffix in %q", got)
	}
}

func TestApplyTheme_ExplicitBGOnly(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "cyan")
	input := baseFGToken + "test"
	got := applyTheme(input)
	// When only bg is set, fg defaults to "default"
	if !strings.Contains(got, "#[fg=default") {
		t.Fatalf("expected default fg when only bg set in %q", got)
	}
	if !strings.Contains(got, "bg=cyan") {
		t.Fatalf("expected cyan bg in %q", got)
	}
}

func TestApplyTheme_NoTokensInInput(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	// Input without any tokens should pass through unchanged
	got := applyTheme("plain text")
	if got != "plain text" {
		t.Fatalf("expected unchanged output for tokenless input, got %q", got)
	}
}

func TestApplyTheme_ThemeAliases(t *testing.T) {
	// Test theme aliases that map to the same preset
	aliases := map[string]string{
		"white-on-purple":  "magenta",
		"magenta":          "magenta",
		"white-on-magenta": "magenta",
		"black-on-yellow":  "yellow",
		"black-on-gold":    "yellow",
		"white-on-blue":    "blue",
		"white-on-navy":    "blue",
	}
	for theme, expectBG := range aliases {
		t.Run(theme, func(t *testing.T) {
			t.Setenv("HEXAI_TMUX_STATUS_THEME", theme)
			t.Setenv("HEXAI_TMUX_STATUS_FG", "")
			t.Setenv("HEXAI_TMUX_STATUS_BG", "")
			got := applyTheme(baseFGToken + "x")
			if !strings.Contains(got, "bg="+expectBG) {
				t.Fatalf("theme %q: expected bg=%s in %q", theme, expectBG, got)
			}
		})
	}
}

// --- FormatGlobalStatusColored branch coverage ---

func TestFormatGlobalStatusColored_EmptyProvider(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_NARROW", "")
	t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "")
	// Empty provider should return head only (no tail)
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "", "model", 1.1, 4, time.Hour)
	if strings.Contains(s, "|") {
		t.Fatalf("expected no tail with empty provider: %q", s)
	}
}

func TestFormatGlobalStatusColored_EmptyModel(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_NARROW", "")
	t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "")
	// Empty model should return head only
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "openai", "", 1.1, 4, time.Hour)
	if strings.Contains(s, "|") {
		t.Fatalf("expected no tail with empty model: %q", s)
	}
}

func TestFormatGlobalStatusColored_WithTail(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_NARROW", "")
	t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "")
	// With valid provider and model, should include tail
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "openai", "gpt-4.1", 1.1, 4, time.Hour)
	if !strings.Contains(s, "|") || !strings.Contains(s, "openai:gpt-4.1") {
		t.Fatalf("expected tail with provider:model: %q", s)
	}
}

func TestFormatGlobalStatusColored_MaxLenTruncatesHead(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_NARROW", "")
	t.Setenv("HEXAI_TMUX_STATUS_THEME", "")
	t.Setenv("HEXAI_TMUX_STATUS_FG", "")
	t.Setenv("HEXAI_TMUX_STATUS_BG", "")
	// Set maxlen very small so even head gets truncated
	t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "5")
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "openai", "gpt-4.1", 1.1, 4, time.Hour)
	// The string contains control-char tokens; truncateStatus works on raw bytes.
	// Just verify it ends with the ellipsis character and is shorter than untruncated.
	if !strings.HasSuffix(s, "…") {
		t.Fatalf("expected truncated output ending with ellipsis, got %q", s)
	}
}

func TestFormatGlobalStatusColored_MaxLenFitsBoth(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS_NARROW", "")
	// Set maxlen very large so both head and tail fit
	t.Setenv("HEXAI_TMUX_STATUS_MAXLEN", "500")
	s := FormatGlobalStatusColored(10, 3.3, 1000, 2000, "openai", "gpt-4.1", 1.1, 4, time.Hour)
	if !strings.Contains(s, "|") || !strings.Contains(s, "openai:gpt-4.1") {
		t.Fatalf("expected full output with large maxlen: %q", s)
	}
}