-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal_test.go
More file actions
447 lines (425 loc) · 10.3 KB
/
Copy pathterminal_test.go
File metadata and controls
447 lines (425 loc) · 10.3 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
package main
import (
"fmt"
"strings"
"testing"
)
func TestSkipANSI(t *testing.T) {
tests := []struct {
name string
s string
i int
expected int
}{
{
name: "no escape at position",
s: "hello",
i: 0,
expected: 0,
},
{
name: "CSI color code",
s: "\x1b[32mgreen",
i: 0,
expected: 5, // \x1b [ 3 2 m
},
{
name: "CSI reset",
s: "\x1b[0m",
i: 0,
expected: 4,
},
{
name: "CSI 8-bit color",
s: "\x1b[38;5;123mtext",
i: 0,
expected: 11,
},
{
name: "OSC hyperlink",
s: "\x1b]8;;https://example.com\x1b\\link text\x1b]8;;\x1b\\",
i: 0,
expected: 26, // \x1b]8;;https://example.com\x1b\\
},
{
name: "escape in middle of string",
s: "hi \x1b[31mred",
i: 3,
expected: 5,
},
{
name: "not at escape position",
s: "hi \x1b[31mred",
i: 1,
expected: 0,
},
{
name: "at end of string",
s: "hi",
i: 1,
expected: 0,
},
{
name: "strikeout",
s: "\x1b[9mstrikethrough",
i: 0,
expected: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := skipANSI(tt.s, tt.i)
if got != tt.expected {
t.Errorf("skipANSI(%q, %d) = %d, want %d", tt.s, tt.i, got, tt.expected)
}
})
}
}
func TestStripANSI(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "no escapes",
input: "hello world",
expected: "hello world",
},
{
name: "single color",
input: "\x1b[32mgreen\x1b[0m",
expected: "green",
},
{
name: "multiple colors",
input: "\x1b[31mred\x1b[0m and \x1b[34mblue\x1b[0m",
expected: "red and blue",
},
{
name: "hyperlink",
input: "\x1b]8;;https://example.com\x1b\\click here\x1b]8;;\x1b\\",
expected: "click here",
},
{
name: "color inside hyperlink",
input: "\x1b[36m\x1b]8;;https://x.com\x1b\\text\x1b]8;;\x1b\\\x1b[0m",
expected: "text",
},
{
name: "8-bit color",
input: "\x1b[38;5;42mcolored\x1b[0m",
expected: "colored",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "strikeout and color",
input: "\x1b[31m\x1b[9mdeleted\x1b[0m",
expected: "deleted",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripANSI(tt.input)
if got != tt.expected {
t.Errorf("stripANSI(%q) = %q, want %q", tt.input, got, tt.expected)
}
})
}
}
func TestWidth(t *testing.T) {
tests := []struct {
name string
input string
expected int
}{
{
name: "plain ASCII",
input: "hello",
expected: 5,
},
{
name: "with color codes",
input: "\x1b[32mgreen\x1b[0m",
expected: 5,
},
{
name: "with hyperlink",
input: "\x1b]8;;https://example.com\x1b\\click\x1b]8;;\x1b\\",
expected: 5,
},
{
name: "empty string",
input: "",
expected: 0,
},
{
name: "wide characters (CJK)",
input: "日本語",
expected: 6,
},
{
name: "mixed ASCII and wide",
input: "hi日本",
expected: 6,
},
{
name: "colored wide characters",
input: "\x1b[31m日本\x1b[0m",
expected: 4,
},
{
name: "diff graph with colors",
input: fmt.Sprintf("%s++%s--%s", GREEN, RED, RESET),
expected: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := width(tt.input)
if got != tt.expected {
t.Errorf("width(%q) = %d, want %d", tt.input, got, tt.expected)
}
})
}
}
func TestTruncateToWidth(t *testing.T) {
tests := []struct {
name string
input string
maxWidth int
expected string // expected visible text (after stripping ANSI)
}{
{
name: "no truncation needed",
input: "hello",
maxWidth: 10,
expected: "hello",
},
{
name: "truncate plain text",
input: "hello world",
maxWidth: 5,
expected: "hello",
},
{
name: "truncate at zero",
input: "hello",
maxWidth: 0,
expected: "",
},
{
name: "preserve ANSI color when truncating",
input: "\x1b[32mgreen text\x1b[0m",
maxWidth: 5,
expected: "green",
},
{
name: "ANSI codes don't count toward width",
input: "\x1b[32mhi\x1b[0m",
maxWidth: 2,
expected: "hi",
},
{
name: "hyperlink preserved when text fits",
input: "\x1b]8;;https://example.com\x1b\\hey\x1b]8;;\x1b\\",
maxWidth: 3,
expected: "hey",
},
{
name: "hyperlink text truncated",
input: "\x1b]8;;https://example.com\x1b\\hello\x1b]8;;\x1b\\",
maxWidth: 3,
expected: "hel",
},
{
name: "wide characters truncated at boundary",
input: "日本語",
maxWidth: 4,
expected: "日本",
},
{
name: "wide char doesn't fit partially",
input: "日本語",
maxWidth: 3,
expected: "日", // Can't fit half of 本
},
{
name: "colored diff graph truncated",
input: fmt.Sprintf("%s+++%s---%s", GREEN, RED, RESET),
maxWidth: 4,
expected: "+++-",
},
{
name: "date string no truncation",
input: "2024-10-01",
maxWidth: 10,
expected: "2024-10-01",
},
{
name: "date string truncated by 1",
input: "2024-10-01",
maxWidth: 9,
expected: "2024-10-0",
},
{
name: "nerdfont icon with color preserved",
input: "\x1b[32m\uf040\x1b[0m ",
maxWidth: 2,
expected: "\uf040 ",
},
{
name: "nerdfont icon with color truncated to 1",
input: "\x1b[32m\uf040\x1b[0m ",
maxWidth: 1,
expected: "\uf040",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncateToWidth(tt.input, tt.maxWidth)
gotVisible := stripANSI(got)
if gotVisible != tt.expected {
t.Errorf("truncateToWidth(%q, %d) visible = %q, want %q\n raw output: %q",
tt.input, tt.maxWidth, gotVisible, tt.expected, got)
}
// Verify width invariant: visible width must not exceed maxWidth
gotWidth := width(got)
if gotWidth > tt.maxWidth {
t.Errorf("truncateToWidth(%q, %d) produced width %d, exceeds max",
tt.input, tt.maxWidth, gotWidth)
}
})
}
}
func TestTruncateToWidthPreservesANSI(t *testing.T) {
// Verify that ANSI codes pass through (not stripped) when text fits.
// Trailing ANSI codes (like RESET) after the last visible character must
// be preserved to avoid color bleeding into subsequent output.
input := "\x1b[32mgreen\x1b[0m"
got := truncateToWidth(input, 5)
gotVisible := stripANSI(got)
if gotVisible != "green" {
t.Errorf("truncateToWidth visible text = %q, want %q", gotVisible, "green")
}
if !strings.Contains(got, "\x1b[32m") {
t.Errorf("truncateToWidth should preserve leading ANSI: %q", got)
}
// Trailing RESET must be preserved
if !strings.Contains(got, "\x1b[0m") {
t.Errorf("truncateToWidth should preserve trailing RESET: %q", got)
}
// Verify color code is kept even when text is truncated
got = truncateToWidth(input, 3)
if !strings.Contains(got, "\x1b[32m") {
t.Errorf("truncateToWidth should keep leading ANSI in truncated output: %q", got)
}
if width(got) != 3 {
t.Errorf("truncateToWidth visible width = %d, want 3", width(got))
}
// Verify diff graph pattern: GREEN+++RED--RESET doesn't lose RESET
diffGraph := "\x1b[32m+++\x1b[31m--\x1b[0m"
got = truncateToWidth(diffGraph, 5)
if !strings.HasSuffix(got, "\x1b[0m") {
t.Errorf("truncateToWidth should preserve trailing RESET in diff graph: %q", got)
}
if width(got) != 5 {
t.Errorf("truncateToWidth diff graph visible width = %d, want 5", width(got))
}
}
func TestHashToColor(t *testing.T) {
tests := []struct {
name string
hash string
}{
{
name: "empty hash returns cyan",
hash: "",
},
{
name: "same hash returns same color",
hash: "abc123",
},
{
name: "different hash",
hash: "def456",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
color := hashToColor(tt.hash)
// Empty hash should return CYAN
if tt.hash == "" {
if color != CYAN {
t.Errorf("hashToColor(\"\") = %q, expected CYAN (%q)", color, CYAN)
}
return
}
// Non-empty hash should return a valid 8-bit color code
if !strings.HasPrefix(color, "\x1b[38;5;") || !strings.HasSuffix(color, "m") {
t.Errorf("hashToColor(%q) = %q, expected 8-bit color format", tt.hash, color)
}
// Same hash should always return same color (deterministic)
color2 := hashToColor(tt.hash)
if color != color2 {
t.Errorf("hashToColor not deterministic: first=%q, second=%q", color, color2)
}
})
}
// Different hashes should (likely) produce different colors
color1 := hashToColor("55b998eae87118427808144927549a39e821c0fb")
color2 := hashToColor("dd59602e5a8af4da0429237c2f2d547ee1bdc800")
if color1 == color2 {
t.Errorf("Different hashes produced same color: both got %q", color1)
}
}
func TestLinkify(t *testing.T) {
tests := []struct {
name string
msg string
github string
hash string
expected string
}{
{
name: "no issue references",
msg: "Some message",
github: "https://github.com/a/b",
hash: "123abc",
expected: link("https://github.com/a/b/commit/123abc", "Some message"),
},
{
name: "one issue reference",
msg: "fixes issue (#17)",
github: "https://github.com/a/b",
hash: "123abc",
expected: link("https://github.com/a/b/commit/123abc", "fixes issue (") +
link("https://github.com/a/b/pull/17", fmt.Sprintf("%s%s%s", BLUE, "#17", RESET)) +
link("https://github.com/a/b/commit/123abc", ")"),
},
{
name: "two issue references",
msg: "fixes (#17) closes (#99)",
github: "https://github.com/a/b",
hash: "123abc",
expected: link("https://github.com/a/b/commit/123abc", "fixes (") +
link("https://github.com/a/b/pull/17", fmt.Sprintf("%s%s%s", BLUE, "#17", RESET)) +
link("https://github.com/a/b/commit/123abc", ") closes (") +
link("https://github.com/a/b/pull/99", fmt.Sprintf("%s%s%s", BLUE, "#99", RESET)) +
link("https://github.com/a/b/commit/123abc", ")"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := linkify(tt.msg, tt.github, tt.hash)
if got != tt.expected {
t.Errorf("linkify(%q, %q, %q):\n got: %q\n want: %q",
tt.msg, tt.github, tt.hash, got, tt.expected)
}
})
}
}