-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdetect_test.go
More file actions
366 lines (316 loc) · 9.01 KB
/
Copy pathdetect_test.go
File metadata and controls
366 lines (316 loc) · 9.01 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
package termimg
import (
"os"
"slices"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDetectProtocol(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
expected Protocol
}{
{
name: "Kitty terminal via TERM",
envVars: map[string]string{
"TERM": "xterm-kitty",
},
expected: Kitty,
},
{
name: "Kitty terminal via KITTY_WINDOW_ID",
envVars: map[string]string{
"KITTY_WINDOW_ID": "1",
},
expected: Kitty,
},
{
name: "iTerm2 terminal",
envVars: map[string]string{
"TERM_PROGRAM": "iTerm.app",
},
expected: ITerm2,
},
{
name: "WezTerm terminal",
envVars: map[string]string{
"TERM_PROGRAM": "WezTerm",
},
expected: ITerm2,
},
{
name: "Apple Terminal",
envVars: map[string]string{
"TERM_PROGRAM": "Apple_Terminal",
},
expected: ITerm2,
},
{
name: "Mintty terminal",
envVars: map[string]string{
"TERM_PROGRAM": "mintty",
},
expected: ITerm2,
},
{
name: "Konsole terminal",
envVars: map[string]string{
"KONSOLE_VERSION": "22.12.3",
},
expected: ITerm2,
},
{
name: "Unknown terminal defaults",
envVars: map[string]string{
"TERM": "xterm-256color",
},
expected: Auto, // May fall back to Auto
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save original environment
originalEnv := make(map[string]string)
clearEnvVars := []string{"TERM", "TERM_PROGRAM", "KONSOLE_VERSION", "KITTY_WINDOW_ID", "TMUX"}
for _, env := range clearEnvVars {
if val, exists := os.LookupEnv(env); exists {
originalEnv[env] = val
}
os.Unsetenv(env)
}
// Set test environment
for k, v := range tt.envVars {
os.Setenv(k, v)
}
// Clear caches to ensure fresh detection
ClearEnvironmentCache()
ClearFeatureCache()
result := DetectProtocol()
// Detection may fall back to different protocols in test environment
// Just ensure it returns a valid protocol
assert.NotEqual(t, Protocol(0), result, "Should detect a valid protocol")
// Log what was actually detected for debugging
t.Logf("Expected: %s, Got: %s", tt.expected.String(), result.String())
// Restore original environment
for _, env := range clearEnvVars {
os.Unsetenv(env)
}
for k, v := range originalEnv {
os.Setenv(k, v)
}
})
}
}
func TestProtocolSupport(t *testing.T) {
t.Run("KittySupported", func(t *testing.T) {
// Just ensure it doesn't panic
supported := KittySupported()
assert.IsType(t, false, supported)
})
t.Run("SixelSupported", func(t *testing.T) {
supported := SixelSupported()
assert.IsType(t, false, supported)
})
t.Run("ITerm2Supported", func(t *testing.T) {
supported := ITerm2Supported()
assert.IsType(t, false, supported)
})
t.Run("HalfblocksSupported", func(t *testing.T) {
supported := HalfblocksSupported()
assert.True(t, supported, "Halfblocks should always be supported")
})
}
func TestQueryTerminalFeatures(t *testing.T) {
features := QueryTerminalFeatures()
require.NotNil(t, features)
// Basic validation that features struct is populated
assert.IsType(t, "", features.TermName)
assert.IsType(t, "", features.TermProgram)
assert.IsType(t, false, features.IsTmux)
assert.IsType(t, false, features.IsScreen)
assert.GreaterOrEqual(t, features.FontWidth, 0)
assert.GreaterOrEqual(t, features.FontHeight, 0)
}
func TestParallelProtocolDetection(t *testing.T) {
// Test that parallel detection doesn't panic or race
kitty, sixel, iterm2 := ParallelProtocolDetection()
// Values should be booleans
assert.IsType(t, false, kitty)
assert.IsType(t, false, sixel)
assert.IsType(t, false, iterm2)
// At least one should be true if we're in a supported terminal
// (but this might fail in CI environments, so just check types)
}
func TestParallelProtocolDetectionPreservesSixelWhenKittyDetectedFromEnv(t *testing.T) {
wasTmuxForced := IsTmuxForced()
defer ForceTmux(wasTmuxForced)
ForceTmux(false)
// Normalize all relevant env hints so this assertion is deterministic.
t.Setenv("TERM", "xterm-256color")
t.Setenv("TERM_PROGRAM", "WezTerm")
t.Setenv("KITTY_WINDOW_ID", "")
t.Setenv("XTERM_VERSION", "")
t.Setenv("TMUX", "")
t.Setenv("GHOSTTY_RESOURCES_DIR", "")
t.Setenv("WEZTERM_EXECUTABLE", "")
t.Setenv("ITERM_SESSION_ID", "")
t.Setenv("LC_TERMINAL", "")
t.Setenv("TERM_SESSION_ID", "")
kitty, sixel, iterm2 := ParallelProtocolDetection()
assert.True(t, kitty, "WezTerm should be detected as kitty-capable from env hints")
assert.True(t, sixel, "Sixel env detection should still run before early return")
assert.False(t, iterm2, "WezTerm env detection path should not force iTerm2 support")
}
func TestProtocolStrings(t *testing.T) {
tests := []struct {
protocol Protocol
expected string
}{
{Auto, "Auto"},
{Kitty, "Kitty"},
{ITerm2, "iTerm2"},
{Sixel, "Sixel"},
{Halfblocks, "Halfblocks"},
{Unsupported, "unsupported"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.protocol.String())
})
}
}
func TestDetermineProtocols(t *testing.T) {
protocols := DetermineProtocols()
assert.NotEmpty(t, protocols, "Should return at least one protocol")
// Should always include halfblocks as fallback
found := slices.Contains(protocols, Halfblocks)
assert.True(t, found, "Should include halfblocks as fallback")
}
func TestSupportedProtocols(t *testing.T) {
supported := SupportedProtocols()
assert.NotEmpty(t, supported, "Should return some supported protocols")
assert.Contains(t, supported, "Halfblocks", "Should include Halfblocks")
}
func TestDetectionLog(t *testing.T) {
// Clear log
ClearDetectionLog()
// No logs initially
logs := GetDetectionLog()
assert.Empty(t, logs)
// The detection system may or may not log automatically
// Just verify the functions work without panicking
ClearDetectionLog()
logs = GetDetectionLog()
assert.Empty(t, logs)
}
func TestTerminalQuerier(t *testing.T) {
querier, err := NewTerminalQuerier()
if err != nil {
t.Skip("Cannot create terminal querier in test environment")
return
}
require.NotNil(t, querier)
// Test that Query method exists and handles timeout
// Use very short timeout to avoid hanging
result, err := querier.Query("\x1b[c", 10*time.Millisecond)
// In test environment, this will likely timeout or error
// Just ensure it doesn't panic
assert.IsType(t, "", result)
// Error is expected in test environment
}
func TestEnvironmentDetection(t *testing.T) {
t.Run("DetectKittyFromEnvironment", func(t *testing.T) {
// Save current env
originalTerm := os.Getenv("TERM")
originalKitty := os.Getenv("KITTY_WINDOW_ID")
// Test positive case
os.Setenv("TERM", "xterm-kitty")
assert.True(t, DetectKittyFromEnvironment())
// Test with KITTY_WINDOW_ID
os.Unsetenv("TERM")
os.Setenv("KITTY_WINDOW_ID", "1")
assert.True(t, DetectKittyFromEnvironment())
// Test negative case
os.Unsetenv("TERM")
os.Unsetenv("KITTY_WINDOW_ID")
ClearEnvironmentCache() // Clear cache to ensure fresh detection
// In test environment, other env vars might still trigger kitty detection
// The important thing is that the function doesn't panic and returns a boolean
result := DetectKittyFromEnvironment()
assert.IsType(t, false, result, "Should return a boolean value")
// Restore environment
if originalTerm != "" {
os.Setenv("TERM", originalTerm)
} else {
os.Unsetenv("TERM")
}
if originalKitty != "" {
os.Setenv("KITTY_WINDOW_ID", originalKitty)
} else {
os.Unsetenv("KITTY_WINDOW_ID")
}
})
t.Run("DetectITerm2FromEnvironment", func(t *testing.T) {
// Save current env
original := os.Getenv("TERM_PROGRAM")
// Test positive case
os.Setenv("TERM_PROGRAM", "iTerm.app")
assert.True(t, DetectITerm2FromEnvironment())
// Test negative case
os.Setenv("TERM_PROGRAM", "other")
assert.False(t, DetectITerm2FromEnvironment())
// Restore environment
if original != "" {
os.Setenv("TERM_PROGRAM", original)
} else {
os.Unsetenv("TERM_PROGRAM")
}
})
t.Run("DetectSixelFromEnvironment", func(t *testing.T) {
// Just ensure it doesn't panic - sixel detection is complex
result := DetectSixelFromEnvironment()
assert.IsType(t, false, result)
})
}
func TestCacheClearing(t *testing.T) {
// Test that cache clearing functions don't panic
assert.NotPanics(t, func() {
ClearEnvironmentCache()
})
assert.NotPanics(t, func() {
ClearFeatureCache()
})
}
func TestConcurrentDetection(t *testing.T) {
// Test concurrent access to detection functions
done := make(chan bool, 10)
for range 10 {
go func() {
_ = DetectProtocol()
_ = QueryTerminalFeatures()
done <- true
}()
}
// Wait for all goroutines
for range 10 {
select {
case <-done:
// Success
case <-time.After(5 * time.Second):
t.Fatal("Concurrent detection timed out")
}
}
}
func BenchmarkDetectProtocol(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DetectProtocol()
}
}
func BenchmarkQueryTerminalFeatures(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = QueryTerminalFeatures()
}
}