-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathansi_parser_test.go
More file actions
813 lines (694 loc) · 23.2 KB
/
Copy pathansi_parser_test.go
File metadata and controls
813 lines (694 loc) · 23.2 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
package main
import (
"encoding/base64"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
"github.com/unxed/f4/piecetable"
"github.com/unxed/vtui"
)
func init() {
vtui.SetDefaultPalette()
SetDefaultF4Palette()
}
// mockPty captures writes to the PTY for testing parser responses
type mockPty struct {
mu sync.Mutex
written []byte
closed bool
}
func (m *mockPty) Write(b []byte) (int, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.written = append(m.written, b...)
return len(b), nil
}
func (m *mockPty) String() string {
m.mu.Lock()
defer m.mu.Unlock()
return string(m.written)
}
func (m *mockPty) Reset() {
m.mu.Lock()
defer m.mu.Unlock()
m.written = nil
}
func (m *mockPty) Read(b []byte) (int, error) {
for !m.closed {
time.Sleep(10 * time.Millisecond)
}
return 0, io.EOF
}
func (m *mockPty) Close() error {
m.closed = true
return nil
}
func (m *mockPty) SetSize(cols, rows int) {}
func (m *mockPty) Wait() error { return nil }
func (m *mockPty) Run(name string, args ...string) error { return nil }
func (m *mockPty) IsBusy() bool { return false }
func TestAnsiParser_CPR(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
pty := &mockPty{}
p := NewAnsiParser(tv, pty)
// 0-based coordinates in TerminalView: X=10, Y=5
tv.SetCursor(10, 5)
// Send Cursor Position Report (CPR) request
p.Process([]byte("\x1b[6n"))
// Expected response: 1-based coordinates \x1b[row;colR
expected := "\x1b[6;11R"
if string(pty.written) != expected {
t.Errorf("Expected CPR response %q, got %q", expected, string(pty.written))
}
}
func TestAnsiParser_SGR_Advanced(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Test TrueColor Foreground (38;2;R;G;B)
p.Process([]byte("\x1b[38;2;255;128;64m"))
expectedRGB := uint32(0xFF8040)
if vtui.GetRGBFore(p.Attr) != expectedRGB {
t.Errorf("TrueColor Fore: expected %06X, got %06X", expectedRGB, vtui.GetRGBFore(p.Attr))
}
if (p.Attr & vtui.IsFgRGB) == 0 {
t.Error("TrueColor Fore: IsFgRGB flag not set")
}
// 2. Test 256-color Background (48;5;Index)
p.Process([]byte("\x1b[48;5;208m"))
if vtui.GetIndexBack(p.Attr) != 208 {
t.Errorf("256-color Back: expected 208, got %d", vtui.GetIndexBack(p.Attr))
}
// 3. Test Styles: Bold (1) and Underline (4)
p.Process([]byte("\x1b[1;4m"))
if (p.Attr & vtui.ForegroundIntensity) == 0 {
t.Error("Style: Bold flag not set")
}
if (p.Attr & vtui.CommonLvbUnderscore) == 0 {
t.Error("Style: Underline flag not set")
}
// 4. Test Reset (0)
p.Process([]byte("\x1b[0m"))
if p.Attr != DefaultTermAttr {
t.Errorf("Reset: expected %v, got %v", DefaultTermAttr, p.Attr)
}
}
func TestAnsiParser_DynamicPalette(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Change Palette index 1 (ANSI Red) to Pure Purple #FF00FF
// Format: OSC 4 ; index ; color BEL
p.Process([]byte("\x1b]4;1;#FF00FF\x07"))
// 2. Set foreground to ANSI 31 (Red)
p.Process([]byte("\x1b[31m"))
gotColor := tv.Palette[vtui.GetIndexFore(p.Attr)]
if gotColor != 0xFF00FF {
t.Errorf("Dynamic Palette: expected Purple #FF00FF, got %06X", gotColor)
}
// 3. Test rgb:RR/GG/BB format (used by some versions of far2l)
// Change index 4 (ANSI Blue) to #112233
p.Process([]byte("\x1b]4;4;rgb:11/22/33\x07"))
p.Process([]byte("\x1b[34m")) // SGR 34 is ANSI Blue
gotColor = tv.Palette[vtui.GetIndexFore(p.Attr)]
if gotColor != 0x112233 {
t.Errorf("Dynamic Palette (rgb format): expected #112233, got %06X", gotColor)
}
}
func TestAnsiParser_SaveRestoreCursor_ESC(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
tv.SetCursor(15, 8)
// ESC 7 saves the cursor
p.Process([]byte("\x1b7"))
// Move away
tv.SetCursor(0, 0)
// ESC 8 restores the cursor
p.Process([]byte("\x1b8"))
if tv.CursorX != 15 || tv.CursorY != 8 {
t.Errorf("Expected cursor at (15, 8) after restore, got (%d, %d)", tv.CursorX, tv.CursorY)
}
}
func TestAnsiParser_SaveRestoreCursor_CSI(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
tv.SetCursor(22, 11)
// CSI s saves the cursor
p.Process([]byte("\x1b[s"))
// Move away
tv.SetCursor(0, 0)
// CSI u restores the cursor
p.Process([]byte("\x1b[u"))
if tv.CursorX != 22 || tv.CursorY != 11 {
t.Errorf("Expected cursor at (22, 11) after restore, got (%d, %d)", tv.CursorX, tv.CursorY)
}
}
func TestAnsiParser_StringTerminator(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Trigger APC state (Application Program Command)
p.Process([]byte("\x1b_"))
if p.State != StateAPC {
t.Fatalf("Expected state to be StateAPC, got %v", p.State)
}
// Send ESC \ (String Terminator)
p.Process([]byte("\x1b\\"))
// Parser should return to ground state
if p.State != StateGround {
t.Errorf("Expected state to return to StateGround after ST, got %v", p.State)
}
}
func TestAnsiParser_DSR_Status(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
pty := &mockPty{}
p := NewAnsiParser(tv, pty)
// Request terminal status
p.Process([]byte("\x1b[5n"))
// Expected response: "Ready, no malfunction"
expected := "\x1b[0n"
if string(pty.written) != expected {
t.Errorf("Expected DSR status response %q, got %q", expected, string(pty.written))
}
}
func TestAnsiParser_OSC4_Palette(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// ANSI Color 1 — Red. By default in f4 palette it's 0xA00000.
// Change it via OSC 4 to bright green #00FF00
// Format: ESC ] 4 ; index ; color BEL
oscSeq := "\x1b]4;1;#00FF00\x07"
p.Process([]byte(oscSeq))
if tv.Palette[1] != 0x00FF00 {
t.Errorf("OSC 4 palette update failed. Expected #00FF00, got %06X", tv.Palette[1])
}
}
func TestAnsiParser_REP_ECH(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Test REP (Repeat last char): write 'A' and repeat 5 times
p.Process([]byte("A\x1b[5b"))
line := tv.Lines[tv.CursorY]
for i := 0; i < 6; i++ {
if line[i].Char != 'A' {
t.Errorf("REP failed at pos %d: expected 'A', got %c", i, rune(line[i].Char))
}
}
// 2. Test ECH (Erase characters): erase 3 characters from position 0
tv.SetCursor(0, tv.CursorY)
p.Process([]byte("\x1b[3X"))
for i := 0; i < 3; i++ {
if line[i].Char != ' ' {
t.Errorf("ECH failed at pos %d: expected space, got %c", i, rune(line[i].Char))
}
}
}
func TestAnsiParser_SplitUTF8(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Symbol 'П' (0xD0 0x9F) sent in parts
p.Process([]byte{0xD0})
if tv.Lines[tv.CursorY][0].Char == 0xD0 {
t.Error("Parser should not put incomplete UTF-8 byte on screen")
}
p.Process([]byte{0x9F})
if tv.Lines[tv.CursorY][0].Char != 'П' {
t.Errorf("Parser failed to assemble split UTF-8: expected 'П', got %c", rune(tv.Lines[tv.CursorY][0].Char))
}
}
func TestAnsiParser_MovementAndErase(t *testing.T) {
tv := NewTerminalView(10, 5)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Test CUP (H) - Cursor Position
p.Process([]byte("\x1b[3;4H")) // 1-based, so should be 2,3
if tv.CursorY != 2 || tv.CursorX != 3 {
t.Errorf("CUP failed: expected (3,2), got (%d,%d)", tv.CursorX, tv.CursorY)
}
// 2. Test relative movements (A, B, C, D)
p.Process([]byte("\x1b[2A")) // Up 2
if tv.CursorY != 0 {
t.Errorf("CUU failed: expected Y=0, got %d", tv.CursorY)
}
p.Process([]byte("\x1b[3B")) // Down 3
if tv.CursorY != 3 {
t.Errorf("CUD failed: expected Y=3, got %d", tv.CursorY)
}
p.Process([]byte("\x1b[5C")) // Forward 5
if tv.CursorX != 8 { // 3 + 5 = 8
t.Errorf("CUF failed: expected X=8, got %d", tv.CursorX)
}
p.Process([]byte("\x1b[4D")) // Backward 4
if tv.CursorX != 4 { // 8 - 4 = 4
t.Errorf("CUB failed: expected X=4, got %d", tv.CursorX)
}
// 3. Test ED (Erase Display) and EL (Erase Line)
tv.PutChar('X', DefaultTermAttr)
p.Process([]byte("\x1b[2J")) // Erase entire screen
if tv.Lines[3][5].Char != ' ' {
t.Error("ED(2) failed to clear screen")
}
tv.SetCursor(0, 0)
// 4. Test Alternate Screen Buffer
p.Process([]byte("Main"))
p.Process([]byte("\x1b[?1049h")) // Switch to alt
if !tv.UseAltScreen {
t.Fatal("Failed to switch to alternate screen")
}
if tv.Lines[0][0].Char != 'M' {
t.Error("Main screen content was affected by alt screen switch")
}
p.Process([]byte("Alt")) // Write to alt screen
if tv.AltLines[0][0].Char != 'A' {
t.Error("Failed to write to alt screen")
}
p.Process([]byte("\x1b[?1049l")) // Switch back to main
if tv.UseAltScreen {
t.Fatal("Failed to switch back to main screen")
}
if tv.Lines[0][0].Char != 'M' {
t.Error("Main screen content was lost")
}
}
func TestAnsiParser_Win32PasteModes(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Enable modes
p.Process([]byte("\x1b[?9001h\x1b[?2004h"))
if !tv.Win32InputMode || !tv.BracketedPasteMode {
t.Error("Failed to enable Win32InputMode or BracketedPasteMode")
}
// Disable modes
p.Process([]byte("\x1b[?9001l\x1b[?2004l"))
if tv.Win32InputMode || tv.BracketedPasteMode {
t.Error("Failed to disable Win32InputMode or BracketedPasteMode")
}
}
func TestAnsiParser_AdvancedCSI(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Ensure we are at the top-left
tv.SetCursor(0, 0)
// Test Delete Characters (P)
p.Process([]byte("12345")) // Write at (0,0). Cursor moves to (5,0)
tv.SetCursor(1, 0) // Move to '2'
p.Process([]byte("\x1b[2P")) // Delete 2 characters ('2' and '3')
// Result should be "145" at index 0, 1, 2 of line 0
if tv.Lines[0][1].Char != '4' || tv.Lines[0][2].Char != '5' {
t.Errorf("Delete characters failed. Found %c (U+%04X) at [0][1]", rune(tv.Lines[0][1].Char), tv.Lines[0][1].Char)
}
// Test Insert Blank Characters (@)
tv.SetCursor(1, 0)
p.Process([]byte("\x1b[2@")) // Insert 2 blanks at pos 1
// Result should be "1 45"
if tv.Lines[0][1].Char != ' ' || tv.Lines[0][2].Char != ' ' || tv.Lines[0][3].Char != '4' {
t.Errorf("Insert blank characters failed. Found %c at [0][3]", rune(tv.Lines[0][3].Char))
}
}
func TestAnsiParser_OSC_Advanced(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Test window title OSC 2
p.Process([]byte("\x1b]2;far2l console\x07"))
if tv.Title != "far2l console" {
t.Errorf("Window title failed: expected 'far2l console', got '%s'", tv.Title)
}
}
func TestAnsiParser_SGR_IntensityPersistence(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Set Bold (Intensity)
p.Process([]byte("\x1b[1m"))
if (p.Attr & vtui.ForegroundIntensity) == 0 {
t.Fatal("Intensity flag not set")
}
// 2. Set "Bright Red" using 90-range code
// HYPOTHESIS: This should either clear the manual Intensity flag OR we must
// ensure that Flush doesn't produce double-brightening.
p.Process([]byte("\x1b[91m"))
if vtui.GetIndexFore(p.Attr) != 9 {
t.Errorf("Expected index 9, got %d", vtui.GetIndexFore(p.Attr))
}
// If Intensity flag is still there, attributesToANSI will produce "\x1b[1;38;5;9m"
// which is "Bold + Bright Red".
if (p.Attr & vtui.ForegroundIntensity) != 0 {
t.Log("Note: Intensity flag persists after 90-range SGR. Check if this causes 'dirty' colors on host.")
}
}
func TestAnsiParser_DefaultColorRestoration(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// Set some non-default colors
p.Process([]byte("\x1b[32;44m")) // Green on Blue
// Restore default foreground (39)
p.Process([]byte("\x1b[39m"))
if vtui.GetIndexFore(p.Attr) != vtui.GetIndexFore(DefaultTermAttr) {
t.Errorf("SGR 39 failed to restore default index. Expected %d, got %d",
vtui.GetIndexFore(DefaultTermAttr), vtui.GetIndexFore(p.Attr))
}
// Check if background is still blue
if vtui.GetIndexBack(p.Attr) != 4 {
t.Errorf("SGR 39 corrupted background. Expected 4, got %d", vtui.GetIndexBack(p.Attr))
}
}
func TestAnsiParser_Robustness(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Truncated CSI: should stay in StateCSI
p.Process([]byte("\x1b["))
if p.State != StateCSI {
t.Errorf("Expected state StateCSI, got %v", p.State)
}
// 2. Garbage inside CSI: should return to ground without crashing
p.Process([]byte("1;?#@")) // '@' is a valid terminator but parameters are junk
if p.State != StateGround {
t.Errorf("Expected return to StateGround after junk CSI, got %v", p.State)
}
// 3. Truncated OSC
p.Process([]byte("\x1b]"))
if p.State != StateOSC {
t.Errorf("Expected state StateOSC, got %v", p.State)
}
// 4. OSC terminated by ESC instead of BEL
p.Process([]byte("2;Title\x1b"))
// The handleOSC is called, then StateEsc is entered
if p.State != StateEsc {
t.Errorf("Expected transition from OSC to ESC, got %v", p.State)
}
if tv.Title != "Title" {
t.Error("OSC title failed with ESC terminator")
}
}
func TestAnsiParser_OSC52_Malformed(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// 1. Malformed Base64 (should not panic or crash)
// OSC 52 ; c ; <invalid_base64> BEL
p.Process([]byte("\x1b]52;c;!!!\x07"))
// 2. Incomplete OSC 52
p.Process([]byte("\x1b]52;c;"))
p.Process([]byte{0x07}) // Just BEL
// 3. Very large OSC 52 (Buffer overflow protection check)
largeSeq := "\x1b]52;c;" + strings.Repeat("A", 10000) + "\x07"
p.Process([]byte(largeSeq))
// If we are here without panic, the test is passed.
}
func TestAnsiParser_UnrecognizedCSI(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// CSI ? 999 z is unrecognized.
// The parser must consume it and return to Ground state without side effects.
p.Process([]byte("\x1b[?999z"))
if p.State != StateGround {
t.Errorf("Parser stuck in state %v after unrecognized CSI", p.State)
}
}
func TestAnsiParser_APC_Reset(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
p.CurParam.WriteString("old_garbage")
p.Process([]byte("\x1b_")) // Enter StateAPC
if p.CurParam.Len() != 0 {
t.Error("CurParam was not reset when entering APC state")
}
}
func TestAnsiParser_DECRQM(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
pty := &mockPty{}
p := NewAnsiParser(tv, pty)
// --- 1. DEC Private Modes ---
// Unknown Private Mode
p.Process([]byte("\x1b[?7777$p"))
if string(pty.written) != "\x1b[?7777;0$y" {
t.Errorf("Expected not recognized (0) for ?7777, got %q", string(pty.written))
}
pty.written = nil
// Mode 1: Application Cursor Keys
tv.ApplicationCursorKeys = false
p.Process([]byte("\x1b[?1$p"))
if string(pty.written) != "\x1b[?1;2$y" {
t.Errorf("Mode 1 Reset fail: %q", string(pty.written))
}
pty.written = nil
tv.ApplicationCursorKeys = true
p.Process([]byte("\x1b[?1$p"))
if string(pty.written) != "\x1b[?1;1$y" {
t.Errorf("Mode 1 Set fail: %q", string(pty.written))
}
pty.written = nil
// Mode 47 & 1049: Alt Screen
tv.UseAltScreen = false
p.Process([]byte("\x1b[?47$p"))
if string(pty.written) != "\x1b[?47;2$y" {
t.Errorf("Mode 47 Reset fail")
}
pty.written = nil
tv.UseAltScreen = true
p.Process([]byte("\x1b[?1049$p"))
if string(pty.written) != "\x1b[?1049;1$y" {
t.Errorf("Mode 1049 Set fail")
}
pty.written = nil
// Mode 2004: Bracketed Paste
tv.BracketedPasteMode = false
p.Process([]byte("\x1b[?2004$p"))
if string(pty.written) != "\x1b[?2004;2$y" {
t.Errorf("Mode 2004 Reset fail")
}
pty.written = nil
tv.BracketedPasteMode = true
p.Process([]byte("\x1b[?2004$p"))
if string(pty.written) != "\x1b[?2004;1$y" {
t.Errorf("Mode 2004 Set fail")
}
pty.written = nil
// Mode 9001: Win32 Input
tv.Win32InputMode = false
p.Process([]byte("\x1b[?9001$p"))
if string(pty.written) != "\x1b[?9001;2$y" {
t.Errorf("Mode 9001 Reset fail")
}
pty.written = nil
tv.Win32InputMode = true
p.Process([]byte("\x1b[?9001$p"))
if string(pty.written) != "\x1b[?9001;1$y" {
t.Errorf("Mode 9001 Set fail")
}
pty.written = nil
// --- 2. Standard Modes ---
// Standard mode (always 0/not recognized in our current implementation)
p.Process([]byte("\x1b[20$p"))
if string(pty.written) != "\x1b[20;0$y" {
t.Errorf("Expected not recognized for standard mode 20, got %q", string(pty.written))
}
pty.written = nil
// --- 3. Edge Cases & Negative Tests ---
// Wrong intermediate byte (e.g. # instead of $)
p.Process([]byte("\x1b[?1#p"))
if len(pty.written) > 0 {
t.Error("Should not respond to DECRQM with wrong intermediate byte")
}
pty.written = nil
// Missing parameters
p.Process([]byte("\x1b[$p"))
if len(pty.written) > 0 {
t.Error("Should not respond to DECRQM without parameters")
}
}
func TestAnsiParser_TechnicalCommandFilter(t *testing.T) {
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
tv.BracketedPasteMode = true // Изменим стейт, чтобы убедиться, что trailingANSI корректно отработает
// Имитация технической команды, которую генерирует f4 для Unix (set +H...).
// Обратите внимание: она содержит эхо самой команды и trailing ANSI.
techCmd := "set +H; cd '/tmp' && { printf \"\\033]133;C\\007\"; ./script.sh ; printf \"\\033]133;D\\007\"; }\r\n"
trailingANSI := "\x1b[?2004l" // Отключение bracketed paste и т.д.
p.Process([]byte(techCmd + trailingANSI))
// Парсер должен был перехватить и вырезать `techCmd`,
// поэтому на экране терминала (Active Grid) не должно быть текста скрипта 's', 'e', 't'.
if tv.Lines[tv.CursorY][0].Char == 's' {
t.Error("Technical command was leaked to the visual screen!")
}
// Убеждаемся, что trailingANSI не был утерян вместе с вырезанной командой
// и благополучно отработал, отключив режим bracketed paste.
if tv.BracketedPasteMode {
t.Error("Trailing ANSI sequence was ignored/cut off during technical command filtering!")
}
}
func TestAnsiParser_WindowsAbsoluteJumpRobustness(t *testing.T) {
// Типичный "грязный" чанк от ConPTY: очистка экрана + прыжок в середину + текст
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
// \x1b[2J (Clear) \x1b[10;5H (Jump to row 10, col 5)
chunk := "\x1b[2J\x1b[10;5HData"
p.Process([]byte(chunk))
// After writing 4 bytes "Data", X should be 4 + 4 = 8
if tv.CursorY != 9 || tv.CursorX != 8 {
t.Errorf("Absolute jump failed. Expected (8,9), got (%d,%d)", tv.CursorX, tv.CursorY)
}
if tv.Lines[9][4].Char != 'D' {
t.Errorf("Data landed in wrong place. Expected 'D' at [9][4], got '%c'", tv.Lines[9][4].Char)
}
}
func TestAnsiParser_WindowsExcision_CrossPlatform(t *testing.T) {
// Этот тест проверяет логику вырезания технических команд Windows,
// даже если тест запущен на Linux/macOS.
tv := NewTerminalView(80, 24)
defer tv.Close()
p := NewAnsiParser(tv, nil)
tests := []struct {
name string
input string
expected string
}{
{
name: "Simple CD excision",
input: "cd /d \"C:\\Windows\" & dir\r\n",
expected: "dir",
},
{
name: "Excision with prompt (screen scraping simulation)",
input: "C:\\Users\\f4>cd /d \"D:\\Data\" & echo 123\r\n",
expected: "C:\\Users\\f4>echo 123",
},
{
name: "Multiple excisions in one buffer",
input: "Prompt1>cd /d \"A\" & cmd1\r\nPrompt2>cd /d \"B\" & cmd2",
expected: "Prompt1>cmd1\nPrompt2>cmd2",
},
{
name: "Path with spaces and special chars",
input: "C:\\>cd /d \"C:\\My Folder & Stuff\" & whoami\r\n",
expected: "C:\\>whoami",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tv.ResetBuffer(80, 24)
tv.pt = piecetable.New([]byte{}) // Reset history
tv.li.Rebuild(tv.pt)
p.Process([]byte(tt.input))
logBytes := tv.GetAllLogBytes()
// Очищаем от лишних пробелов в конце строк сетки
result := strings.TrimSpace(string(logBytes))
if !strings.Contains(result, tt.expected) {
t.Errorf("Excision failed for [%s].\nExpected to contain: %q\nGot log: %q", tt.name, tt.expected, result)
}
if strings.Contains(result, "cd /d") {
t.Errorf("Excision failed for [%s]: technical 'cd' command leaked into log!", tt.name)
}
})
}
}
func TestAnsiParser_ExcisionExtra(t *testing.T) {
for _, tt := range []struct {
name string
input string
expected string
}{
{
name: "Windows background sync excision",
input: "C:\\Old>cd /d \"C:\\New\" & rem f4_sync\r\n",
expected: "",
},
{
name: "Windows technical command excision",
input: "C:\\Old>cd /d \"C:\\New\" & whoami\r\n",
expected: "C:\\Old>whoami\n",
},
{
name: "Unix background sync excision",
input: "user@host:~$ cd '/new/path' # f4_sync\r\n",
expected: "",
},
{
name: "Unix technical command excision",
input: "set +H; cd '/new/path' && { printf \"\\033]133;C\\007\"; ./'cmd' ; printf \"\\033]133;D\\007\"; }\r\n",
expected: "",
},
} {
t.Run(tt.name, func(t *testing.T) {
tv := NewTerminalView(80, 24)
parser := NewAnsiParser(tv, nil)
parser.Process([]byte(tt.input))
logBytes := tv.GetAllLogBytes()
logStr := string(logBytes)
// Normalize newlines for cross-platform comparison
logStr = strings.ReplaceAll(logStr, "\r\n", "\n")
if !strings.Contains(logStr, tt.expected) {
t.Errorf("Expected log to contain %q, but got %q", tt.expected, logStr)
}
})
}
}
type mockClipAuthManager struct {
authorized bool
}
func (m *mockClipAuthManager) Authorize(id string) int {
if m.authorized {
return 1 // Allow Once
}
return 0 // Deny
}
func TestAnsiParser_OSC52_Read_Security(t *testing.T) {
tv := NewTerminalView(80, 24)
pty := &mockPtyForTerminal{}
parser := NewAnsiParser(tv, pty)
// Test 1: Denied access
vtui.GlobalClipboardAccessManager = &mockClipAuthManager{authorized: false}
parser.Process([]byte("\x1b]52;c;?\x07"))
if pty.Len() > 0 {
t.Errorf("Expected no output when clipboard read is denied, got %q", pty.String())
}
// Test 2: Allowed access
vtui.GlobalClipboardAccessManager = &mockClipAuthManager{authorized: true}
vtui.SetClipboard("secret_data")
parser.Process([]byte("\x1b]52;c;?\x07"))
var out string
for start := time.Now(); time.Since(start) < 2*time.Second; {
out = pty.String()
if strings.Contains(out, "\x1b]52;c;") {
break
}
time.Sleep(10 * time.Millisecond)
}
if !strings.Contains(out, "\x1b]52;c;") {
t.Errorf("Expected OSC 52 reply containing clipboard data, got %q", out)
}
// Reset global state
vtui.GlobalClipboardAccessManager = nil
}
func TestAnsiParser_OSC52_Write_Success(t *testing.T) {
tv := NewTerminalView(80, 24)
parser := NewAnsiParser(tv, nil)
testStr := "Hello OSC 52"
b64 := base64.StdEncoding.EncodeToString([]byte(testStr))
// OSC 52 ; selection (c) ; data (b64) BEL
parser.Process([]byte(fmt.Sprintf("\x1b]52;c;%s\x07", b64)))
got := vtui.GetClipboard()
if got != testStr {
t.Errorf("Expected clipboard to be %q, got %q", testStr, got)
}
}