-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_test.go
More file actions
599 lines (540 loc) · 22.6 KB
/
Copy pathtext_test.go
File metadata and controls
599 lines (540 loc) · 22.6 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package console
import (
"strings"
"testing"
)
// TestStripANSI removes complete CSI, OSC, and ESC sequences.
func TestStripANSI(t *testing.T) {
t.Parallel()
value := "\x1b[31mred\x1b[0m " +
"\x1b]0;window title\aok " +
"\x1b]8;;https://example.test\x1b\\link\x1b]8;;\x1b\\ " +
"\x1b7saved \x1b(Bcharset"
if got, want := StripANSI(value), "red ok link saved charset"; got != want {
t.Fatalf("StripANSI() = %q, want %q", got, want)
}
}
// TestStripANSIRetainsIncompleteSequences protects malformed input from silent data loss.
func TestStripANSIRetainsIncompleteSequences(t *testing.T) {
t.Parallel()
tests := []string{
"before\x1b",
"before\x1b ",
"before\x1b[31",
"before\x1b]8;;https://example.test",
"before\x1b]title\nstill\a",
"before\x1b]title\x1b",
"before\x1bPdevice data",
"before\x1bXprivate message",
"before\x1b^privacy message",
"before\x1b_application command",
"before\x1b😀",
}
for _, value := range tests {
value := value
t.Run(value, func(t *testing.T) {
t.Parallel()
if got := StripANSI(value); got != value {
t.Fatalf("StripANSI(%q) = %q, want the malformed input unchanged", value, got)
}
})
}
}
// TestStripANSIHandlesSTTerminatedControlStrings keeps nonprinting terminal protocols out of visible text.
func TestStripANSIHandlesSTTerminatedControlStrings(t *testing.T) {
t.Parallel()
for name, introducer := range map[string]string{
"DCS": "\x1bP",
"SOS": "\x1bX",
"PM": "\x1b^",
"APC": "\x1b_",
} {
name, introducer := name, introducer
t.Run(name, func(t *testing.T) {
t.Parallel()
value := "before" + introducer + "payload\a\x1b[31mstill payload\x1b\\after"
if got, want := StripANSI(value), "beforeafter"; got != want {
t.Fatalf("StripANSI() = %q, want %q", got, want)
}
})
}
}
// TestStripANSIPreservesInvalidUTF8 verifies an ANSI utility does not rewrite unrelated malformed bytes.
func TestStripANSIPreservesInvalidUTF8(t *testing.T) {
t.Parallel()
value := string([]byte{'a', 0xff, 'b'})
if got := StripANSI(value); got != value {
t.Fatalf("StripANSI(%q) = %q, want the original bytes", value, got)
}
}
// TestVisibleWidthCountsTerminalCells covers styled, wide, combining, emoji, and tabular text.
func TestVisibleWidthCountsTerminalCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
want int
}{
{name: "ASCII", value: "plain", want: 5},
{name: "CSI styling", value: "\x1b[31mred\x1b[0m", want: 3},
{name: "OSC hyperlink with BEL", value: "\x1b]8;;https://example.test\aDocs\x1b]8;;\a", want: 4},
{name: "OSC hyperlink with ST", value: "\x1b]8;;https://example.test\x1b\\Docs\x1b]8;;\x1b\\", want: 4},
{name: "CJK", value: "界a", want: 3},
{name: "combining mark", value: "e\u0301", want: 1},
{name: "emoji ZWJ and modifier", value: "👩🏽💻", want: 2},
{name: "text presentation symbols", value: "✔✖⚠☀✈❤", want: 6},
{name: "emoji variation selectors", value: "✔️✖️⚠️☀️✈️❤️", want: 12},
{name: "default emoji presentation", value: "✅", want: 2},
{name: "text variation selector", value: "✅︎", want: 1},
{name: "regional indicator flag", value: "🇺🇳", want: 2},
{name: "unmatched ZWJ", value: "A\u200dB", want: 2},
{name: "leading unmatched ZWJ", value: "\u200dA", want: 1},
{name: "ANSI within grapheme", value: "e" + ColorRed + "\u0301" + ColorReset, want: 1},
{name: "emoji selector on text", value: "A\ufe0f", want: 1},
{name: "text selector on emoji", value: "😀\ufe0e", want: 1},
{name: "narrow supplementary symbol", value: "🜀", want: 1},
{name: "decomposed Hangul", value: "각", want: 2},
{name: "standalone emoji modifier", value: "🏻", want: 2},
{name: "modifier after non-base", value: "A🏻", want: 3},
{name: "text-default modifier base", value: "☝🏻", want: 2},
{name: "tab stop", value: "a\tb", want: 9},
{name: "control characters", value: "a\rb", want: 2},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := VisibleWidth(test.value); got != test.want {
t.Fatalf("VisibleWidth(%q) = %d, want %d", test.value, got, test.want)
}
})
}
}
// TestDisplayTokensPreserveSourceBytes verifies ANSI inside a cluster cannot alter reconstruction.
func TestDisplayTokensPreserveSourceBytes(t *testing.T) {
t.Parallel()
value := "e" + ColorRed + "\u0301" + ColorReset + "ᄀ" + StyleBold + "ᅡᆨ"
if got := joinDisplayTokens(displayTokens(value)); got != value {
t.Fatalf("joinDisplayTokens(displayTokens()) = %q, want %q", got, value)
}
}
// TestVisibleWidthUsesWidestLine verifies that line breaks reset cell positions and tab stops.
func TestVisibleWidthUsesWidestLine(t *testing.T) {
t.Parallel()
if got, want := VisibleWidth("ab\n界界\nx\ty"), 9; got != want {
t.Fatalf("VisibleWidth() = %d, want %d", got, want)
}
}
// TestTruncateUsesVisibleCells preserves complete glyphs while applying an ellipsis per line.
func TestTruncateUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "unchanged", value: "short", width: 5, want: "short"},
{name: "ASCII", value: "abcdef", width: 5, want: "abcd…"},
{name: "ellipsis only", value: "abcdef", width: 1, want: "…"},
{name: "zero width", value: "abcdef", width: 0, want: ""},
{name: "negative width", value: "abcdef", width: -1, want: ""},
{name: "CJK", value: "界界界", width: 5, want: "界界…"},
{name: "combining", value: "e\u0301clair", width: 4, want: "e\u0301cl…"},
{name: "emoji", value: "👩🏽💻 developer", width: 5, want: "👩🏽💻 d…"},
{name: "decomposed Hangul cluster", value: "각x", width: 2, want: "…"},
{name: "Indic cluster", value: "काx", width: 2, want: "…"},
{name: "each line", value: "abcdef\n界界界", width: 4, want: "abc…\n界…"},
{name: "CRLF normalization", value: "abcdef\r\nxy", width: 4, want: "abc…\nxy"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := Truncate(test.value, test.width); got != test.want {
t.Fatalf("Truncate(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestTruncateResetsSGRStyling ensures an ellipsis cannot inherit a truncated value's style.
func TestTruncateResetsSGRStyling(t *testing.T) {
t.Parallel()
value := ColorRed + "abcdef" + ColorReset
want := ColorRed + "abc" + ColorReset + "…"
if got := Truncate(value, 4); got != want {
t.Fatalf("Truncate() = %q, want %q", got, want)
}
}
// TestTruncatePreservesOSCHyperlinkText verifies OSC metadata does not affect visible truncation.
func TestTruncatePreservesOSCHyperlinkText(t *testing.T) {
t.Parallel()
open := "\x1b]8;;https://example.test\a"
close := "\x1b]8;;\a"
got := Truncate(open+"abcdef"+close, 4)
if stripped, want := StripANSI(got), "abc…"; stripped != want {
t.Fatalf("StripANSI(Truncate()) = %q, want %q", stripped, want)
}
if width := VisibleWidth(got); width != 4 {
t.Fatalf("VisibleWidth(Truncate()) = %d, want 4", width)
}
closeIndex := strings.Index(got, close)
ellipsisIndex := strings.Index(got, "…")
if closeIndex < 0 || ellipsisIndex < 0 || closeIndex > ellipsisIndex {
t.Fatalf("Truncate() did not close its OSC hyperlink before the ellipsis: %q", got)
}
}
// TestTruncateMiddleUsesVisibleCells retains useful context from both ends of every physical line.
func TestTruncateMiddleUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "unchanged", value: "short", width: 5, want: "short"},
{name: "balanced sides", value: "abcdef", width: 5, want: "ab…ef"},
{name: "prefix receives odd cell", value: "abcdef", width: 4, want: "ab…f"},
{name: "ellipsis only", value: "abcdef", width: 1, want: "…"},
{name: "zero width", value: "abcdef", width: 0, want: ""},
{name: "negative width", value: "abcdef", width: -1, want: ""},
{name: "CJK", value: "界界界", width: 5, want: "界…界"},
{name: "combining", value: "e\u0301clair", width: 4, want: "e\u0301c…r"},
{name: "emoji", value: "👩🏽💻 developer", width: 7, want: "👩🏽💻 …per"},
{name: "each line", value: "abcdef\n界界界", width: 4, want: "ab…f\n界…"},
{name: "CRLF normalization", value: "abcdef\r\nxy", width: 4, want: "ab…f\nxy"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := TruncateMiddle(test.value, test.width); got != test.want {
t.Fatalf("TruncateMiddle(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestTruncateMiddleBalancesPresentationMetadata keeps the ellipsis outside styles and hyperlinks.
func TestTruncateMiddleBalancesPresentationMetadata(t *testing.T) {
t.Parallel()
styled := ColorRed + "abcdef" + ColorReset
styledWant := ColorRed + "ab" + ColorReset + "…" + ColorRed + "f" + ColorReset
if got := TruncateMiddle(styled, 4); got != styledWant {
t.Fatalf("TruncateMiddle(styled) = %q, want %q", got, styledWant)
}
open := "\x1b]8;;https://example.test\a"
close := "\x1b]8;;\a"
linked := TruncateMiddle(open+"abcdef"+close, 4)
if got, want := StripANSI(linked), "ab…f"; got != want {
t.Fatalf("StripANSI(TruncateMiddle(linked)) = %q, want %q", got, want)
}
if !strings.Contains(linked, close+"…"+open) {
t.Fatalf("TruncateMiddle(linked) did not isolate its ellipsis: %q", linked)
}
if got, want := TruncateMiddle(ColorRed+"界界", 2), "…"; got != want {
t.Fatalf("TruncateMiddle(overwide styled text) = %q, want %q", got, want)
}
}
// TestWrapUsesVisibleCells wraps words and indivisible glyph clusters without counting ANSI bytes.
func TestWrapUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "words", value: "one two three", width: 8, want: "one two\nthree"},
{name: "word exactly fills line", value: "hello world", width: 5, want: "hello\nworld"},
{name: "trailing separator after full line", value: "hello ", width: 5, want: "hello"},
{name: "long word", value: "abcdefgh", width: 3, want: "abc\ndef\ngh"},
{name: "CJK", value: "界界界", width: 4, want: "界界\n界"},
{name: "overwide CJK", value: "界界", width: 1, want: "界\n界"},
{name: "overwide combining", value: "界\u0301界\u0301", width: 1, want: "界\u0301\n界\u0301"},
{name: "overwide variation selector", value: "✈️✈️", width: 1, want: "✈️\n✈️"},
{name: "combining", value: "e\u0301e\u0301e\u0301", width: 2, want: "e\u0301e\u0301\ne\u0301"},
{name: "emoji ZWJ", value: "👩🏽💻👩🏽💻", width: 2, want: "👩🏽💻\n👩🏽💻"},
{name: "overwide emoji ZWJ", value: "👩🏽💻👩🏽💻", width: 1, want: "👩🏽💻\n👩🏽💻"},
{name: "unmatched ZWJ", value: "A\u200dB", width: 1, want: "A\u200d\nB"},
{name: "decomposed Hangul", value: "각나", width: 2, want: "각\n나"},
{name: "Indic spacing marks", value: "काकी", width: 1, want: "का\nकी"},
{name: "ANSI within grapheme", value: "e" + ColorRed + "\u0301" + ColorReset + "x", width: 1, want: "e" + ColorRed + "\u0301" + ColorReset + "\nx"},
{name: "leading and trailing spaces", value: " alpha ", width: 10, want: "alpha"},
{name: "non-breaking space is retained", value: " alpha\u00a0 ", width: 10, want: "alpha\u00a0"},
{name: "nonpositive width", value: "alpha beta", width: 0, want: "alpha beta"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := Wrap(test.value, test.width); got != test.want {
t.Fatalf("Wrap(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestWrapPreservesExplicitLinesAndEscapes retains blank lines and complete styling metadata.
func TestWrapPreservesExplicitLinesAndEscapes(t *testing.T) {
t.Parallel()
if got, want := Wrap("a\r\n\r\nb", 3), "a\n\nb"; got != want {
t.Fatalf("Wrap() = %q, want %q", got, want)
}
styled := ColorRed + "abcdef" + ColorReset
if got, want := Wrap(styled, 3), ColorRed+"abc"+ColorReset+"\n"+ColorRed+"def"+ColorReset; got != want {
t.Fatalf("Wrap(styled) = %q, want %q", got, want)
}
styledTrailingSpace := ColorRed + "alpha " + ColorReset
if got, want := Wrap(styledTrailingSpace, 10), ColorRed+"alpha"+ColorReset; got != want {
t.Fatalf("Wrap(styled trailing space) = %q, want %q", got, want)
}
for index, line := range strings.Split(Wrap(styled, 3), "\n") {
if got := VisibleWidth(line); got != 3 {
t.Fatalf("Wrap(styled) line %d width = %d, want 3", index, got)
}
if !strings.HasSuffix(line, ColorReset) {
t.Fatalf("Wrap(styled) line %d does not end with a reset: %q", index, line)
}
}
open := "\x1b]8;;https://example.test\x1b\\"
closeST := "\x1b]8;;\x1b\\"
closeBEL := "\x1b]8;;\a"
linked := Wrap(open+"abcdef"+closeST, 3)
if got, want := StripANSI(linked), "abc\ndef"; got != want {
t.Fatalf("StripANSI(Wrap(linked)) = %q, want %q", got, want)
}
linkedLines := strings.Split(linked, "\n")
for index, line := range linkedLines {
if !strings.HasPrefix(line, open) {
t.Fatalf("Wrap(linked) line %d does not reopen its OSC hyperlink: %q", index, line)
}
if !strings.HasSuffix(line, closeBEL) && !strings.HasSuffix(line, closeST) {
t.Fatalf("Wrap(linked) line %d does not close its OSC hyperlink: %q", index, line)
}
}
if strings.Count(linked, open) != 2 || strings.Count(linked, closeBEL)+strings.Count(linked, closeST) != 2 {
t.Fatalf("Wrap(linked) did not create one bounded OSC hyperlink per line: %q", linked)
}
for name, color := range map[string]string{
"indexed black": "\x1b[38;5;0m",
"RGB black": "\x1b[38;2;0;0;0m",
} {
name, color := name, color
t.Run(name, func(t *testing.T) {
t.Parallel()
value := StyleBold + color + "abcdef" + ColorReset
want := StyleBold + color + "abc" + ColorReset + "\n" +
StyleBold + color + "def" + ColorReset
if got := Wrap(value, 3); got != want {
t.Fatalf("Wrap(extended color) = %q, want %q", got, want)
}
})
}
}
// TestPadRightUsesVisibleCells aligns plain, styled, wide, combining, tabbed, and multiline values.
func TestPadRightUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "plain", value: "ab", width: 4, want: "ab "},
{name: "already wide", value: "abcdef", width: 4, want: "abcdef"},
{name: "styled", value: ColorRed + "ab" + ColorReset, width: 4, want: ColorRed + "ab" + ColorReset + " "},
{name: "CJK", value: "界", width: 4, want: "界 "},
{name: "combining", value: "e\u0301", width: 3, want: "e\u0301 "},
{name: "tab", value: "a\t", width: 10, want: "a\t "},
{name: "multiline", value: "a\n界", width: 3, want: "a \n界 "},
{name: "CRLF normalization", value: "a\r\nb", width: 2, want: "a \nb "},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := PadRight(test.value, test.width); got != test.want {
t.Fatalf("PadRight(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestPadLeftUsesVisibleCells aligns plain, styled, wide, tabbed, and multiline values from the right.
func TestPadLeftUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "plain", value: "ab", width: 4, want: " ab"},
{name: "already wide", value: "abcdef", width: 4, want: "abcdef"},
{name: "styled", value: ColorRed + "ab" + ColorReset, width: 4, want: " " + ColorRed + "ab" + ColorReset},
{name: "CJK", value: "界", width: 4, want: " 界"},
{name: "tab expansion", value: "a\t", width: 10, want: " a "},
{name: "multiline", value: "a\n界", width: 3, want: " a\n 界"},
{name: "CRLF normalization", value: "a\r\nb", width: 2, want: " a\n b"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := PadLeft(test.value, test.width); got != test.want {
t.Fatalf("PadLeft(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestPadCenterUsesVisibleCells centers plain, styled, wide, tabbed, and multiline values deterministically.
func TestPadCenterUsesVisibleCells(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
width int
want string
}{
{name: "even padding", value: "ab", width: 6, want: " ab "},
{name: "odd padding", value: "ab", width: 5, want: " ab "},
{name: "already wide", value: "abcdef", width: 4, want: "abcdef"},
{name: "styled", value: ColorRed + "ab" + ColorReset, width: 4, want: " " + ColorRed + "ab" + ColorReset + " "},
{name: "CJK", value: "界", width: 5, want: " 界 "},
{name: "tab expansion", value: "a\t", width: 11, want: " a " + " "},
{name: "multiline", value: "a\n界", width: 4, want: " a \n 界 "},
{name: "CRLF normalization", value: "a\r\nb", width: 3, want: " a \n b "},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := PadCenter(test.value, test.width); got != test.want {
t.Fatalf("PadCenter(%q, %d) = %q, want %q", test.value, test.width, got, test.want)
}
})
}
}
// TestExpandTabsUsesLineRelativeCellStops verifies ANSI and wide glyphs affect tab expansion correctly.
func TestExpandTabsUsesLineRelativeCellStops(t *testing.T) {
t.Parallel()
value := ColorRed + "a" + ColorReset + "\tb\n界\tz"
want := ColorRed + "a" + ColorReset + " b\n界 z"
if got := ExpandTabs(value); got != want {
t.Fatalf("ExpandTabs() = %q, want %q", got, want)
}
if got := ExpandTabs("plain"); got != "plain" {
t.Fatalf("ExpandTabs() without tabs = %q, want unchanged input", got)
}
}
// TestSanitizeLayoutTextRetainsOnlyGeometrySafeTerminalSequences verifies embedded content cannot move the cursor or mutate terminal state.
func TestSanitizeLayoutTextRetainsOnlyGeometrySafeTerminalSequences(t *testing.T) {
t.Parallel()
open := "\x1b]8;;https://example.test\a"
close := "\x1b]8;;\a"
value := ColorRed + "red" + ColorReset +
"\x1b[20C" +
"\x1b]0;window title\a" +
"safe" + open + "link" + close + "\a"
want := ColorRed + "red" + ColorReset + "safe" + open + "link" + close
if got := sanitizeLayoutText(value, true); got != want {
t.Fatalf("sanitizeLayoutText() = %q, want %q", got, want)
}
if got, want := sanitizeLayoutText("a\r\nb\rc\n", true), "a\nb\nc\n"; got != want {
t.Fatalf("sanitizeLayoutText(multiline) = %q, want %q", got, want)
}
if got, want := sanitizeLayoutText("a\r\nb\rc\n", false), "a b c "; got != want {
t.Fatalf("sanitizeLayoutText(single line) = %q, want %q", got, want)
}
}
// TestSanitizeLayoutTextRejectsUnsafeTerminalMetadata prevents OSC payloads from smuggling controls.
func TestSanitizeLayoutTextRejectsUnsafeTerminalMetadata(t *testing.T) {
t.Parallel()
valid := "\x1b]8;;https://例.example/文書\x1b\\docs\x1b]8;;\x1b\\"
if got := sanitizeLayoutText(valid, true); got != valid {
t.Fatalf("sanitizeLayoutText(valid OSC 8) = %q, want %q", got, valid)
}
unsafe := []string{
"\x1b]8;;https://example.test/\x00hidden\x1b\\",
"\x1b]8;;https://example.test/\x1b[31mhidden\x1b\\",
"\x1b]8;;https://example.test/\xc2\x80hidden\x1b\\",
string([]byte{'\x1b', ']', '8', ';', ';', 'x', 0xff, '\x1b', '\\'}),
}
for _, value := range unsafe {
if got := sanitizeLayoutText(value, true); got != "" {
t.Fatalf("sanitizeLayoutText(%q) = %q, want unsafe OSC removed", value, got)
}
}
for _, value := range []string{
"\x1bPdevice control\x1b\\",
"\x1bXstart of string\x1b\\",
"\x1b^privacy message\x1b\\",
"\x1b_application command\x1b\\",
} {
if got := sanitizeLayoutText(value, true); got != "" {
t.Fatalf("sanitizeLayoutText(%q) = %q, want control string removed", value, got)
}
}
invalid := string([]byte{'a', 0x90, 0xff, 'b'})
want := string([]byte{'a', 0xff, 'b'})
if got := sanitizeLayoutText(invalid, true); got != want {
t.Fatalf("sanitizeLayoutText(invalid bytes) = %q, want %q", got, want)
}
}
// TestSingleLineLayoutTextBalancesCallerMetadata verifies labels cannot leak styles or hyperlinks into generated chrome.
func TestSingleLineLayoutTextBalancesCallerMetadata(t *testing.T) {
t.Parallel()
open := "\x1b]8;;https://example.test\a"
got := singleLineLayoutText(StyleBold + "Build\n" + open + "docs")
if stripped, want := StripANSI(got), "Build docs"; stripped != want {
t.Fatalf("StripANSI(singleLineLayoutText()) = %q, want %q", stripped, want)
}
if !strings.HasSuffix(got, ColorReset+"\x1b]8;;\a") {
t.Fatalf("singleLineLayoutText() did not close caller metadata: %q", got)
}
}
// TestIndentPrefixesEveryLine verifies hanging, blank, and trailing lines retain their structure.
func TestIndentPrefixesEveryLine(t *testing.T) {
t.Parallel()
if got, want := Indent("a\n\nb\n", "> "), "> a\n> \n> b\n> "; got != want {
t.Fatalf("Indent() = %q, want %q", got, want)
}
if got := Indent("", "> "); got != "" {
t.Fatalf("Indent(empty) = %q, want empty output", got)
}
}
// TestConsoleTextHelpersMatchPackageFunctions locks the stateless text surface to both invocation styles.
func TestConsoleTextHelpersMatchPackageFunctions(t *testing.T) {
t.Parallel()
console := New(Config{})
styled := ColorRed + "hello world" + ColorReset
if got, want := console.StripANSI(styled), StripANSI(styled); got != want {
t.Fatalf("Console.StripANSI() = %q, want %q", got, want)
}
if got, want := console.VisibleWidth(styled), VisibleWidth(styled); got != want {
t.Fatalf("Console.VisibleWidth() = %d, want %d", got, want)
}
if got, want := console.Truncate(styled, 6), Truncate(styled, 6); got != want {
t.Fatalf("Console.Truncate() = %q, want %q", got, want)
}
if got, want := console.TruncateMiddle(styled, 6), TruncateMiddle(styled, 6); got != want {
t.Fatalf("Console.TruncateMiddle() = %q, want %q", got, want)
}
if got, want := console.Wrap(styled, 5), Wrap(styled, 5); got != want {
t.Fatalf("Console.Wrap() = %q, want %q", got, want)
}
if got, want := console.PadRight(styled, 14), PadRight(styled, 14); got != want {
t.Fatalf("Console.PadRight() = %q, want %q", got, want)
}
if got, want := console.PadLeft(styled, 14), PadLeft(styled, 14); got != want {
t.Fatalf("Console.PadLeft() = %q, want %q", got, want)
}
if got, want := console.PadCenter(styled, 14), PadCenter(styled, 14); got != want {
t.Fatalf("Console.PadCenter() = %q, want %q", got, want)
}
if got, want := console.ExpandTabs("a\tb"), ExpandTabs("a\tb"); got != want {
t.Fatalf("Console.ExpandTabs() = %q, want %q", got, want)
}
if got, want := console.Indent("a\nb", "> "), Indent("a\nb", "> "); got != want {
t.Fatalf("Console.Indent() = %q, want %q", got, want)
}
}