-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathtscreen_test.go
More file actions
1133 lines (1037 loc) · 29.7 KB
/
Copy pathtscreen_test.go
File metadata and controls
1133 lines (1037 loc) · 29.7 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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tcell
import (
"bytes"
"runtime"
"strings"
"testing"
"time"
"github.com/gdamore/tcell/v3/tty"
"github.com/gdamore/tcell/v3/vt"
)
// This just offers some very basic tests that do not require a full mock.
// drainInput just does a very primitive sleep to allow input to drain.
// We need this because otherwise the application will close too soon before
// consuming characters from input, including sequences that are returned in
// response to queries.
func drainInput() {
time.Sleep(time.Millisecond * 30)
}
// TestInitScreen just tries to initialize the default screen.
// It requires a working tty.
func TestInitScreen(t *testing.T) {
s, err := NewTerminfoScreen()
if err != nil {
t.Skip("failed to get screen", err)
}
if err := s.Init(); err != nil {
t.Skip("failed to initialize screen", err)
}
defer s.Fini()
if s.CharacterSet() != "UTF-8" {
t.Fatalf("Character Set (%v) not UTF-8", s.CharacterSet())
}
drainInput()
}
type spyTty struct {
vt.MockTerm
writes bytes.Buffer
}
func (t *spyTty) Write(b []byte) (int, error) {
_, _ = t.writes.Write(b)
return t.MockTerm.Write(b)
}
func (t *spyTty) Output() string {
return t.writes.String()
}
func TestOptAltScreenDisable(t *testing.T) {
t.Setenv("TCELL_ALTSCREEN", "")
tty := &spyTty{MockTerm: vt.NewMockTerm()}
s, err := NewTerminfoScreenFromTty(tty, OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
s.Fini()
out := tty.Output()
if strings.Contains(out, enterCA) {
t.Fatalf("alternate screen enter escape was emitted")
}
if strings.Contains(out, exitCA) {
t.Fatalf("alternate screen exit escape was emitted")
}
}
func TestOptAltScreenDefault(t *testing.T) {
t.Setenv("TCELL_ALTSCREEN", "")
tty := &spyTty{MockTerm: vt.NewMockTerm()}
s, err := NewTerminfoScreenFromTty(tty)
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
s.Fini()
out := tty.Output()
if !strings.Contains(out, enterCA) {
t.Fatalf("alternate screen enter escape was not emitted")
}
if !strings.Contains(out, exitCA) {
t.Fatalf("alternate screen exit escape was not emitted")
}
}
func TestFiniPreventsSetStyleMutation(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt)
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
before := StyleDefault.Foreground(ColorRed)
after := StyleDefault.Foreground(ColorBlue)
scr.SetStyle(before)
scr.Fini()
scr.SetStyle(after)
ts.Lock()
defer ts.Unlock()
if !ts.fini {
t.Fatal("screen was not marked finished")
}
if ts.style != before {
t.Fatal("SetStyle mutated the screen after Fini")
}
}
func TestFiniPreventsFurtherTerminalMutation(t *testing.T) {
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})}
scr, err := NewTerminfoScreenFromTty(tty)
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
scr.ShowCursor(1, 1)
scr.SetCursorStyle(CursorStyleSteadyBlock, ColorRed)
scr.EnableMouse()
scr.EnablePaste()
scr.EnableFocus()
scr.Fini()
beforeOutput := tty.Output()
ts.Lock()
beforeCursorX, beforeCursorY := ts.cursorx, ts.cursory
beforeCursorStyle, beforeCursorColor := ts.cursorStyle, ts.cursorColor
beforeMouseFlags := ts.mouseFlags
beforePasteEnabled, beforeFocusEnabled := ts.pasteEnabled, ts.focusEnabled
ts.Unlock()
scr.ShowCursor(2, 1)
scr.SetCursorStyle(CursorStyleBlinkingBar, ColorBlue)
scr.EnableMouse(MouseMotionEvents)
scr.DisableMouse()
scr.EnablePaste()
scr.DisablePaste()
scr.EnableFocus()
scr.DisableFocus()
scr.SetSize(20, 10)
if err := scr.Suspend(); err != nil {
t.Fatalf("Suspend after Fini failed: %v", err)
}
if err := scr.Resume(); err != nil {
t.Fatalf("Resume after Fini failed: %v", err)
}
if err := scr.Beep(); err != nil {
t.Fatalf("Beep after Fini failed: %v", err)
}
scr.SetTitle("after")
scr.SetClipboard([]byte("after"))
scr.GetClipboard()
scr.ShowNotification("after", "after")
if got := tty.Output(); got != beforeOutput {
t.Fatal("terminal output changed after Fini")
}
ts.Lock()
defer ts.Unlock()
if ts.cursorx != beforeCursorX || ts.cursory != beforeCursorY {
t.Fatal("cursor position changed after Fini")
}
if ts.cursorStyle != beforeCursorStyle || ts.cursorColor != beforeCursorColor {
t.Fatal("cursor style changed after Fini")
}
if ts.mouseFlags != beforeMouseFlags {
t.Fatal("mouse flags changed after Fini")
}
if ts.pasteEnabled != beforePasteEnabled {
t.Fatal("paste state changed after Fini")
}
if ts.focusEnabled != beforeFocusEnabled {
t.Fatal("focus state changed after Fini")
}
}
func TestOptAdvancedKeys(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptAdvancedKeys(true))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if !ts.advancedKeys {
t.Fatal("advanced keys option was not applied")
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer scr.Fini()
if !ts.input.advanced {
t.Fatal("advanced keys option was not propagated to input parser")
}
}
func TestOptKeyboardProtocol(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptKeyboardProtocol(KittyKeyboard))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if !ts.forceKbd || ts.forcedKbd != KittyKeyboard {
t.Fatalf("forced keyboard protocol = (%v, %v), want (true, %v)", ts.forceKbd, ts.forcedKbd, KittyKeyboard)
}
}
func TestOptNegotiation(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptNegotiation(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if ts.negotiate {
t.Fatal("negotiation option was not applied")
}
}
func TestSetSizeTakesScreenLock(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
ts := &tScreen{tty: mt, w: 8, h: 2}
done := make(chan struct{})
ts.Lock()
go func() {
ts.SetSize(8, 2)
close(done)
}()
select {
case <-done:
ts.Unlock()
t.Fatal("SetSize returned while the screen lock was held")
case <-time.After(10 * time.Millisecond):
}
ts.Unlock()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("SetSize did not return after the screen lock was released")
}
}
func TestOptControlStringLimit(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptControlStringLimit(4096))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if ts.controlStringLimit != 4096 {
t.Fatalf("control string limit = %d, want %d", ts.controlStringLimit, 4096)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer scr.Fini()
if ts.input.controlStringMax != 4096 {
t.Fatalf("input parser control string limit = %d, want %d", ts.input.controlStringMax, 4096)
}
}
func TestOptControlStringLimitUnlimited(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptControlStringLimit(0))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
bs, ok := scr.(*baseScreen)
if !ok {
t.Fatalf("expected *baseScreen, got %T", scr)
}
ts, ok := bs.screenImpl.(*tScreen)
if !ok {
t.Fatalf("expected *tScreen, got %T", bs.screenImpl)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer scr.Fini()
payload := bytes.Repeat([]byte{'x'}, defaultControlStringLimit+1)
ts.input.ScanUTF8(append([]byte("\x1b]"), payload...))
if ts.input.state != istOsc {
t.Fatalf("parser state = %v, want %v", ts.input.state, istOsc)
}
if !bytes.Equal(ts.input.strBuf, payload) {
t.Fatalf("string buffer length = %d, want %d", len(ts.input.strBuf), len(payload))
}
}
func TestOptSanitizeContent(t *testing.T) {
t.Run("disabled by default", func(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt)
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer scr.Fini()
scr.PutStr(0, 0, "\x1bA\x07B")
if got, _, _ := scr.Get(0, 0); !strings.Contains(got, "\x1b") {
t.Fatalf("expected control bytes to remain when sanitizer is disabled, got %q", got)
}
if got, _, _ := scr.Get(1, 0); !strings.Contains(got, "\x07") {
t.Fatalf("expected control bytes to remain when sanitizer is disabled, got %q", got)
}
})
t.Run("enabled", func(t *testing.T) {
mt := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 2})
scr, err := NewTerminfoScreenFromTty(mt, OptSanitizeContent(true))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := scr.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer scr.Fini()
scr.PutStr(0, 0, "\x1bA\x07B")
if got, _, _ := scr.Get(0, 0); got != "A" {
t.Fatalf("unexpected sanitized cell content at 0,0: %q", got)
}
if got, _, _ := scr.Get(1, 0); got != "B" {
t.Fatalf("unexpected sanitized cell content at 1,0: %q", got)
}
})
}
func TestNewScreenSanitizeContentOption(t *testing.T) {
scr, err := NewScreen(OptSanitizeContent(true))
if err != nil {
t.Skipf("failed to get screen: %v", err)
}
if err := scr.Init(); err != nil {
t.Skipf("failed to initialize screen: %v", err)
}
defer scr.Fini()
scr.PutStr(0, 0, "\x1bA\x07B")
if got, _, _ := scr.Get(0, 0); got != "A" {
t.Fatalf("unexpected sanitized cell content at 0,0: %q", got)
}
if got, _, _ := scr.Get(1, 0); got != "B" {
t.Fatalf("unexpected sanitized cell content at 1,0: %q", got)
}
}
func TestNewScreenShimScreen(t *testing.T) {
_, scr := NewMockScreen(t)
ShimScreen(scr)
got, err := NewScreen()
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if got != scr {
t.Fatalf("unexpected shimmed screen: got %T, want %T", got, scr)
}
}
func TestUnlockRegionRedrawsUntouchedBlankCell(t *testing.T) {
mt, scr := NewMockScreen(t, vt.MockOptSize{X: 1, Y: 1})
defer scr.Fini()
mt.Backend().Put(vt.Coord{X: 0, Y: 0}, vt.Cell{C: "X", S: vt.BaseStyle, W: 1})
scr.LockRegion(0, 0, 1, 1, true)
scr.LockRegion(0, 0, 1, 1, false)
scr.Show()
if got := mt.GetCell(vt.Coord{X: 0, Y: 0}); got.C != " " || got.W != 1 {
t.Fatalf("unlocking an untouched blank cell should repaint a space, got %#v", got)
}
}
func TestOSC8ControlsAreStrippedFromOutput(t *testing.T) {
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
style := StyleDefault.
Url("http://exa\x07mple.com/\x1b\\path").
UrlId("id\x00\x1f\x7f\x80\x9fend")
s.PutStrStyled(0, 0, "X", style)
s.Show()
out := tty.Output()
const prefix = "\x1b]8;id=idend;"
_, link, ok := strings.Cut(out, prefix)
if !ok {
t.Fatalf("missing OSC 8 link open sequence in output: %q", out)
}
link, _, ok = strings.Cut(link, "\x1b\\")
if !ok {
t.Fatalf("missing OSC 8 terminator in output: %q", out)
}
if link != "http://example.com/\\path" {
t.Fatalf("unexpected emitted URL payload: %q", link)
}
if _, afterLink, ok := strings.Cut(out, "X"); !ok || !strings.Contains(afterLink, "\x1b]8;;\x1b\\") {
t.Fatalf("missing OSC 8 link close sequence after linked content: %q", out)
}
for i := 0; i < len(link); i++ {
c := link[i]
if c <= 0x1f || c == 0x7f || (c >= 0x80 && c <= 0x9f) {
t.Fatalf("control characters survived in emitted URL payload: %q", link)
}
}
}
func TestOSC8IdWithoutUrlDoesNotEmitClose(t *testing.T) {
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
tty.writes.Reset()
s.PutStrStyled(0, 0, "X", StyleDefault.UrlId("orphan"))
s.Show()
if out := tty.Output(); strings.Contains(out, "\x1b]8;;\x1b\\") {
t.Fatalf("unexpected OSC 8 close sequence for id-only style: %q", out)
}
}
func TestKeyboardProtocol(t *testing.T) {
tests := []struct {
name string
setup func(*tScreen)
want KeyProtocol
}{
{
name: "Legacy",
want: LegacyKeyboard,
},
{
name: "Xterm",
setup: func(s *tScreen) {
s.haveXTermKbd = true
},
want: XTermKeyboard,
},
{
name: "Kitty",
setup: func(s *tScreen) {
s.haveKittyKbd = true
},
want: KittyKeyboard,
},
{
name: "Win32",
setup: func(s *tScreen) {
s.haveWin32Kbd = true
},
want: Win32Keyboard,
},
{
name: "KittyBeforeXterm",
setup: func(s *tScreen) {
s.haveKittyKbd = true
s.haveXTermKbd = true
},
want: KittyKeyboard,
},
{
name: "Win32BeforeKittyAndXterm",
setup: func(s *tScreen) {
s.haveWin32Kbd = true
s.haveKittyKbd = true
s.haveXTermKbd = true
},
want: Win32Keyboard,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &tScreen{}
if tt.setup != nil {
tt.setup(s)
}
if got := s.KeyboardProtocol(); got != tt.want {
t.Fatalf("KeyboardProtocol() = %v, want %v", got, tt.want)
}
})
}
}
func TestApplyKnownTerminalProfile(t *testing.T) {
tests := []struct {
name string
goos string
termProgram string
wantKnown bool
wantInitted bool
wantMouse bool
wantMouseSgr bool
wantKittyKbd bool
wantWin32Kbd bool
}{
{
name: "AppleTerminal",
goos: "darwin",
termProgram: "Apple_Terminal",
wantKnown: true,
wantMouse: true,
wantMouseSgr: true,
},
{
name: "LocalWindowsWezTerm",
goos: "windows",
termProgram: "WezTerm",
wantKnown: true,
wantInitted: true,
wantMouse: true,
wantMouseSgr: true,
wantWin32Kbd: true,
},
{
name: "LocalUnixWezTerm",
goos: "linux",
termProgram: "WezTerm",
wantKnown: true,
wantInitted: true,
wantMouse: true,
wantMouseSgr: true,
wantKittyKbd: true,
},
{
name: "UnknownTerminal",
goos: "linux",
termProgram: "Other",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &tScreen{}
if got := s.applyKnownTerminalProfile(tt.goos, tt.termProgram); got != tt.wantKnown {
t.Fatalf("applyKnownTerminalProfile() = %v, want %v", got, tt.wantKnown)
}
if s.initted != tt.wantInitted {
t.Fatalf("initted = %v, want %v", s.initted, tt.wantInitted)
}
if s.haveMouse != tt.wantMouse {
t.Fatalf("haveMouse = %v, want %v", s.haveMouse, tt.wantMouse)
}
if s.haveMouseSgr != tt.wantMouseSgr {
t.Fatalf("haveMouseSgr = %v, want %v", s.haveMouseSgr, tt.wantMouseSgr)
}
if s.haveKittyKbd != tt.wantKittyKbd {
t.Fatalf("haveKittyKbd = %v, want %v", s.haveKittyKbd, tt.wantKittyKbd)
}
if s.haveWin32Kbd != tt.wantWin32Kbd {
t.Fatalf("haveWin32Kbd = %v, want %v", s.haveWin32Kbd, tt.wantWin32Kbd)
}
})
}
}
func TestAppleTerminalProfileSkipsStartupQueries(t *testing.T) {
t.Setenv("TERM_PROGRAM", "Apple_Terminal")
t.Setenv("TERM_PROGRAM_VERSION", "999")
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
out := tty.Output()
for _, seq := range []string{
vt.PmResizeReports.Query(),
vt.PmMouseButton.Query(),
vt.PmMouseSgr.Query(),
vt.PmWin32Input.Query(),
queryKittyKbd,
queryXTermKbd,
requestExtAttr,
} {
if strings.Contains(out, seq) {
t.Fatalf("Apple Terminal profile emitted startup query %q", seq)
}
}
if !strings.Contains(out, requestPrimaryDA) {
t.Fatal("Apple Terminal profile did not emit primary DA")
}
name, version := s.Terminal()
if name != "Terminal.app" || version != "999" {
t.Fatalf("Terminal() = %q, %q, want %q, %q", name, version, "Terminal.app", "999")
}
}
func TestKeyboardProbePolicy(t *testing.T) {
tests := []struct {
name string
goos string
wantWindowSizeQuery bool
wantXTermQuery bool
}{
{
name: "Unix",
goos: "linux",
wantWindowSizeQuery: true,
wantXTermQuery: true,
},
{
name: "Windows",
goos: "windows",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := useVTWindowSizeQuery(tt.goos); got != tt.wantWindowSizeQuery {
t.Fatalf("useVTWindowSizeQuery() = %v, want %v", got, tt.wantWindowSizeQuery)
}
if got := useXTermKeyboardQuery(tt.goos); got != tt.wantXTermQuery {
t.Fatalf("useXTermKeyboardQuery() = %v, want %v", got, tt.wantXTermQuery)
}
})
}
}
func TestKeyboardProtocolHelpers(t *testing.T) {
tests := []struct {
name string
value string
want KeyProtocol
wantValid bool
}{
{name: "legacy", value: "legacy", want: LegacyKeyboard, wantValid: true},
{name: "kitty", value: "kitty", want: KittyKeyboard, wantValid: true},
{name: "win32", value: "win32", want: Win32Keyboard, wantValid: true},
{name: "xterm", value: "xterm", want: XTermKeyboard, wantValid: true},
{name: "invalid", value: "bogus"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseKeyboardProtocol(tt.value)
if got != tt.want || ok != tt.wantValid {
t.Fatalf("parseKeyboardProtocol(%q) = (%v, %v), want (%v, %v)", tt.value, got, ok, tt.want, tt.wantValid)
}
})
}
if validKeyboardProtocol(KeyProtocol(99)) {
t.Fatal("invalid keyboard protocol was accepted")
}
}
func TestForceKeyboardProtocol(t *testing.T) {
s := &tScreen{
haveKittyKbd: true,
haveWin32Kbd: true,
haveXTermKbd: true,
}
if s.forceKeyboardProtocol(KeyProtocol(99)) {
t.Fatal("invalid keyboard protocol was forced")
}
if s.forceKbd {
t.Fatal("invalid keyboard protocol changed override state")
}
if !s.forceKeyboardProtocol(XTermKeyboard) {
t.Fatal("valid keyboard protocol was not forced")
}
s.applyKeyboardProtocolOverride()
if s.haveKittyKbd || s.haveWin32Kbd || !s.haveXTermKbd {
t.Fatalf("forced XTerm protocol state = kitty:%v win32:%v xterm:%v", s.haveKittyKbd, s.haveWin32Kbd, s.haveXTermKbd)
}
}
func TestApplyEnvironmentOverrides(t *testing.T) {
tests := []struct {
name string
keyboardProtocol string
negotiate string
mouse string
startForceKbd bool
startForcedKbd KeyProtocol
startNegotiate bool
wantForceKbd bool
wantForcedKbd KeyProtocol
wantNegotiate bool
wantMouseOff bool
}{
{
name: "defaults",
startNegotiate: true,
wantNegotiate: true,
wantForcedKbd: LegacyKeyboard,
},
{
name: "force all",
keyboardProtocol: "win32",
negotiate: "disable",
mouse: "disable",
startNegotiate: true,
wantForceKbd: true,
wantForcedKbd: Win32Keyboard,
wantMouseOff: true,
},
{
name: "auto resets options",
keyboardProtocol: "auto",
negotiate: "auto",
startForceKbd: true,
startForcedKbd: KittyKeyboard,
wantNegotiate: true,
wantForcedKbd: KittyKeyboard,
},
{
name: "invalid leaves options",
keyboardProtocol: "bogus",
negotiate: "bogus",
startForceKbd: true,
startForcedKbd: XTermKeyboard,
startNegotiate: false,
wantForceKbd: true,
wantForcedKbd: XTermKeyboard,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TCELL_KEYBOARD_PROTOCOL", tt.keyboardProtocol)
t.Setenv("TCELL_NEGOTIATE", tt.negotiate)
t.Setenv("TCELL_MOUSE", tt.mouse)
s := &tScreen{
forceKbd: tt.startForceKbd,
forcedKbd: tt.startForcedKbd,
negotiate: tt.startNegotiate,
}
s.applyEnvironmentOverrides()
if s.forceKbd != tt.wantForceKbd {
t.Fatalf("forceKbd = %v, want %v", s.forceKbd, tt.wantForceKbd)
}
if s.forcedKbd != tt.wantForcedKbd {
t.Fatalf("forcedKbd = %v, want %v", s.forcedKbd, tt.wantForcedKbd)
}
if s.negotiate != tt.wantNegotiate {
t.Fatalf("negotiate = %v, want %v", s.negotiate, tt.wantNegotiate)
}
if s.mouseDisabled != tt.wantMouseOff {
t.Fatalf("mouseDisabled = %v, want %v", s.mouseDisabled, tt.wantMouseOff)
}
})
}
}
func TestForcedKeyboardProtocolSkipsKeyboardQueries(t *testing.T) {
t.Setenv("TERM_PROGRAM", "")
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptKeyboardProtocol(KittyKeyboard), OptAdvancedKeys(true), OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
out := tty.Output()
for _, seq := range []string{vt.PmWin32Input.Query(), queryKittyKbd, queryXTermKbd} {
if strings.Contains(out, seq) {
t.Fatalf("forced keyboard protocol emitted competing query %q", seq)
}
}
for _, seq := range []string{vt.PmResizeReports.Query(), vt.PmMouseButton.Query(), vt.PmMouseSgr.Query(), requestExtAttr} {
if !strings.Contains(out, seq) {
t.Fatalf("forced keyboard protocol suppressed unrelated query %q", seq)
}
}
if !strings.Contains(out, enableKittyKbdAdv) {
t.Fatalf("forced Kitty protocol did not emit advanced enable sequence")
}
}
func TestNegotiationDisabledSkipsStartupQueries(t *testing.T) {
t.Setenv("TERM_PROGRAM", "")
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptNegotiation(false), OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
out := tty.Output()
for _, seq := range []string{
requestWindowSize,
vt.PmResizeReports.Query(),
vt.PmMouseButton.Query(),
vt.PmMouseSgr.Query(),
vt.PmWin32Input.Query(),
queryKittyKbd,
queryXTermKbd,
requestExtAttr,
requestPrimaryDA,
} {
if strings.Contains(out, seq) {
t.Fatalf("disabled negotiation emitted startup query %q", seq)
}
}
}
func TestMouseDisabledPreventsEnablement(t *testing.T) {
t.Setenv("TCELL_MOUSE", "disable")
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptNegotiation(false), OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
s.EnableMouse()
out := tty.Output()
for _, seq := range []string{vt.PmMouseButton.Enable(), vt.PmMouseDrag.Enable(), vt.PmMouseMotion.Enable(), vt.PmMouseSgr.Enable()} {
if strings.Contains(out, seq) {
t.Fatalf("disabled mouse emitted enable sequence %q", seq)
}
}
}
func TestMouseWithoutSgrPreventsEnablement(t *testing.T) {
tty := &spyTty{MockTerm: vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 5})}
s, err := NewTerminfoScreenFromTty(tty, OptNegotiation(false), OptAltScreen(false))
if err != nil {
t.Fatalf("failed to get screen: %v", err)
}
if err := s.Init(); err != nil {
t.Fatalf("failed to initialize screen: %v", err)
}
defer s.Fini()
ts := s.(*baseScreen).screenImpl.(*tScreen)
ts.haveMouse = true
ts.haveMouseSgr = false
s.EnableMouse()
out := tty.Output()
for _, seq := range []string{vt.PmMouseButton.Enable(), vt.PmMouseDrag.Enable(), vt.PmMouseMotion.Enable(), vt.PmMouseSgr.Enable()} {
if strings.Contains(out, seq) {
t.Fatalf("mouse without SGR support emitted enable sequence %q", seq)
}
}
}
func TestProcessInitQKeyboardProtocol(t *testing.T) {
tests := []struct {
name string
evs []Event
want KeyProtocol
}{
{
name: "Legacy",
want: LegacyKeyboard,
},
{
name: "Xterm",
evs: []Event{&eventXTermKbdMode{Mode: XtermKbdModeExt}},
want: XTermKeyboard,
},
{
name: "Kitty",
evs: []Event{&eventKittyKbdMode{Mode: KittyKbdModeBase}},
want: KittyKeyboard,
},
{
name: "Win32",
evs: []Event{&eventPrivateMode{Mode: vt.PmWin32Input, Status: vt.ModeOff}},
want: Win32Keyboard,
},
{
name: "KittyPreferredOverXterm",
evs: []Event{
&eventXTermKbdMode{Mode: XtermKbdModeExt},
&eventKittyKbdMode{Mode: KittyKbdModeBase},
},
want: KittyKeyboard,
},
{
name: "Win32PreferredOverKittyAndXterm",
evs: []Event{
&eventXTermKbdMode{Mode: XtermKbdModeExt},
&eventKittyKbdMode{Mode: KittyKbdModeBase},
&eventPrivateMode{Mode: vt.PmWin32Input, Status: vt.ModeOff},
},
want: Win32Keyboard,
},
{