-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputhandler_osc_test.go
More file actions
520 lines (430 loc) · 12.4 KB
/
inputhandler_osc_test.go
File metadata and controls
520 lines (430 loc) · 12.4 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
// --- OSC title tests ---
func TestOSC_SetTitle_OSC2(t *testing.T) {
t.Parallel()
h := newTestHandler()
var title string
h.OnTitleChangeEmitter.Event(func(s string) { title = s })
// OSC 2 ; <title> BEL
h.ParseString("\x1b]2;My Terminal\x07")
if title != "My Terminal" {
t.Errorf("expected title 'My Terminal', got %q", title)
}
}
func TestOSC_SetTitle_OSC0(t *testing.T) {
t.Parallel()
h := newTestHandler()
var title string
h.OnTitleChangeEmitter.Event(func(s string) { title = s })
// OSC 0 ; <title> BEL
h.ParseString("\x1b]0;Window Title\x07")
if title != "Window Title" {
t.Errorf("expected title 'Window Title', got %q", title)
}
}
func TestOSC_SetTitle_ST(t *testing.T) {
t.Parallel()
h := newTestHandler()
var title string
h.OnTitleChangeEmitter.Event(func(s string) { title = s })
// OSC 2 ; <title> ST (ESC \)
h.ParseString("\x1b]2;Title With ST\x1b\\")
if title != "Title With ST" {
t.Errorf("expected title 'Title With ST', got %q", title)
}
}
func TestOSC_SetTitle_Empty(t *testing.T) {
t.Parallel()
h := newTestHandler()
var title string
h.OnTitleChangeEmitter.Event(func(s string) { title = s })
h.ParseString("\x1b]2;\x07")
if title != "" {
t.Errorf("expected empty title, got %q", title)
}
}
// --- OSC icon name tests ---
func TestOSC1_SetIconName(t *testing.T) {
t.Parallel()
h := newTestHandler()
var iconName string
h.OnIconNameChangeEmitter.Event(func(s string) { iconName = s })
// OSC 1 ; <name> BEL
h.ParseString("\x1b]1;my-icon\x07")
if iconName != "my-icon" {
t.Errorf("expected icon name 'my-icon', got %q", iconName)
}
if h.iconName != "my-icon" {
t.Errorf("expected h.iconName 'my-icon', got %q", h.iconName)
}
}
func TestOSC1_SetIconName_ST(t *testing.T) {
t.Parallel()
h := newTestHandler()
var iconName string
h.OnIconNameChangeEmitter.Event(func(s string) { iconName = s })
// OSC 1 ; <name> ST (ESC \)
h.ParseString("\x1b]1;icon-st\x1b\\")
if iconName != "icon-st" {
t.Errorf("expected icon name 'icon-st', got %q", iconName)
}
}
func TestOSC1_SetIconName_Empty(t *testing.T) {
t.Parallel()
h := newTestHandler()
// Set a non-empty icon name first.
h.ParseString("\x1b]1;something\x07")
var iconName string
h.OnIconNameChangeEmitter.Event(func(s string) { iconName = s })
h.ParseString("\x1b]1;\x07")
if iconName != "" {
t.Errorf("expected empty icon name, got %q", iconName)
}
}
func TestOSC0_SetsTitleAndIconName(t *testing.T) {
t.Parallel()
h := newTestHandler()
var title, iconName string
h.OnTitleChangeEmitter.Event(func(s string) { title = s })
h.OnIconNameChangeEmitter.Event(func(s string) { iconName = s })
// OSC 0 should set both title and icon name.
h.ParseString("\x1b]0;both-value\x07")
if title != "both-value" {
t.Errorf("expected title 'both-value', got %q", title)
}
if iconName != "both-value" {
t.Errorf("expected icon name 'both-value', got %q", iconName)
}
}
func TestOSC2_DoesNotSetIconName(t *testing.T) {
t.Parallel()
h := newTestHandler()
iconNameChanged := false
h.OnIconNameChangeEmitter.Event(func(s string) { iconNameChanged = true })
// OSC 2 should only set title, not icon name.
h.ParseString("\x1b]2;title-only\x07")
if iconNameChanged {
t.Error("OSC 2 should not fire icon name change event")
}
}
// --- OSC color tests ---
func TestOSC4_SetIndexedColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 4 ; 1 ; #ff0000 BEL
h.ParseString("\x1b]4;1;#ff0000\x07")
type Expectation struct {
Len int
Type ColorRequestType
Index int
Color *ColorRGB
}
if len(events) == 0 {
t.Fatal("expected color events, got none")
}
got := Expectation{
Len: len(events),
Type: events[0].Type,
Index: events[0].Index,
Color: events[0].Color,
}
want := Expectation{
Len: 1,
Type: ColorRequestSet,
Index: 1,
Color: &ColorRGB{0xff, 0x00, 0x00},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestOSC4_QueryIndexedColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 4 ; 5 ; ? BEL
h.ParseString("\x1b]4;5;?\x07")
type Expectation struct {
Len int
Type ColorRequestType
Index int
}
if len(events) == 0 {
t.Fatal("expected color events, got none")
}
got := Expectation{Len: len(events), Type: events[0].Type, Index: events[0].Index}
want := Expectation{Len: 1, Type: ColorRequestReport, Index: 5}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestOSC4_MultipleColors(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 4 ; 0 ; #000000 ; 1 ; #ffffff BEL
h.ParseString("\x1b]4;0;#000000;1;#ffffff\x07")
if len(events) != 2 {
t.Fatalf("expected 2 events, got %d", len(events))
}
type Expectation struct {
Index0 int
Color0 ColorRGB
Index1 int
Color1 ColorRGB
}
got := Expectation{
Index0: events[0].Index,
Color0: *events[0].Color,
Index1: events[1].Index,
Color1: *events[1].Color,
}
want := Expectation{
Index0: 0,
Color0: ColorRGB{0, 0, 0},
Index1: 1,
Color1: ColorRGB{0xff, 0xff, 0xff},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestOSC10_SetFgColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 10 ; rgb:ff/00/ff BEL
h.ParseString("\x1b]10;rgb:ff/00/ff\x07")
type Expectation struct {
Type ColorRequestType
Index int
Color *ColorRGB
}
if len(events) == 0 {
t.Fatal("expected color events, got none")
}
got := Expectation{Type: events[0].Type, Index: events[0].Index, Color: events[0].Color}
want := Expectation{Type: ColorRequestSet, Index: int(SpecialColorForeground), Color: &ColorRGB{0xff, 0x00, 0xff}}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestOSC10_QueryFgColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]10;?\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestReport || events[0].Index != int(SpecialColorForeground) {
t.Errorf("expected REPORT for foreground, got type=%d index=%d", events[0].Type, events[0].Index)
}
}
func TestOSC11_SetBgColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]11;#00ff00\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Index != int(SpecialColorBackground) {
t.Errorf("expected background index %d, got %d", SpecialColorBackground, events[0].Index)
}
}
func TestOSC12_SetCursorColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]12;#aabbcc\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Index != int(SpecialColorCursor) {
t.Errorf("expected cursor index %d, got %d", SpecialColorCursor, events[0].Index)
}
}
func TestOSC104_RestoreIndexedColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 104 ; 5 BEL
h.ParseString("\x1b]104;5\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestRestore || events[0].Index != 5 {
t.Errorf("expected RESTORE index=5, got type=%d index=%d", events[0].Type, events[0].Index)
}
}
func TestOSC104_RestoreAll(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
// OSC 104 BEL (no params = restore all)
h.ParseString("\x1b]104;\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestRestore {
t.Errorf("expected RESTORE, got type=%d", events[0].Type)
}
}
func TestOSC110_RestoreFgColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]110;\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestRestore || events[0].Index != int(SpecialColorForeground) {
t.Errorf("expected RESTORE foreground, got type=%d index=%d", events[0].Type, events[0].Index)
}
}
func TestOSC111_RestoreBgColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]111;\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestRestore || events[0].Index != int(SpecialColorBackground) {
t.Errorf("expected RESTORE background, got type=%d index=%d", events[0].Type, events[0].Index)
}
}
func TestOSC112_RestoreCursorColor(t *testing.T) {
t.Parallel()
h := newTestHandler()
var events []ColorEvent
h.OnColorEmitter.Event(func(e []ColorEvent) { events = e })
h.ParseString("\x1b]112;\x07")
if len(events) == 0 {
t.Fatal("expected color events")
}
if events[0].Type != ColorRequestRestore || events[0].Index != int(SpecialColorCursor) {
t.Errorf("expected RESTORE cursor, got type=%d index=%d", events[0].Type, events[0].Index)
}
}
// --- Color parsing tests ---
func TestParseColor_HashRRGGBB(t *testing.T) {
t.Parallel()
got := parseColor("#ff8040")
want := &ColorRGB{0xff, 0x80, 0x40}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestParseColor_HashRGB(t *testing.T) {
t.Parallel()
got := parseColor("#f80")
want := &ColorRGB{0xf0, 0x80, 0x00}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestParseColor_RgbFormat(t *testing.T) {
t.Parallel()
got := parseColor("rgb:ff/00/80")
want := &ColorRGB{0xff, 0x00, 0x80}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestParseColor_RgbSingleDigit(t *testing.T) {
t.Parallel()
got := parseColor("rgb:f/0/8")
want := &ColorRGB{0xff, 0x00, 0x88}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestParseColor_RgbFourDigit(t *testing.T) {
t.Parallel()
got := parseColor("rgb:ffff/0000/8000")
want := &ColorRGB{0xff, 0x00, 0x80}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestParseColor_Invalid(t *testing.T) {
t.Parallel()
got := parseColor("not-a-color")
if got != nil {
t.Errorf("expected nil for invalid color, got %v", got)
}
}
func TestParseColor_Hash12Digit(t *testing.T) {
t.Parallel()
got := parseColor("#ffff00008000")
want := &ColorRGB{0xff, 0x00, 0x80}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
// --- OSC 8 hyperlink tests ---
func TestOSC8_CreateHyperlink(t *testing.T) {
t.Parallel()
h := newTestHandler()
// OSC 8 ; ; http://example.com BEL
h.ParseString("\x1b]8;;http://example.com\x07")
linkId := h.getCurrentLinkId()
if linkId == 0 {
t.Fatal("expected non-zero link ID after creating hyperlink")
}
data := h.oscLinkService.GetLinkData(linkId)
if data == nil {
t.Fatal("expected link data")
}
if data.URI != "http://example.com" {
t.Errorf("expected URI 'http://example.com', got %q", data.URI)
}
}
func TestOSC8_FinishHyperlink(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("\x1b]8;;http://example.com\x07")
if h.getCurrentLinkId() == 0 {
t.Fatal("expected link to be active")
}
// Finish: OSC 8 ; ; BEL
h.ParseString("\x1b]8;;\x07")
if h.getCurrentLinkId() != 0 {
t.Error("expected link ID to be 0 after finishing hyperlink")
}
}
func TestOSC8_HyperlinkWithId(t *testing.T) {
t.Parallel()
h := newTestHandler()
// OSC 8 ; id=mylink ; http://example.com BEL
h.ParseString("\x1b]8;id=mylink;http://example.com\x07")
linkId := h.getCurrentLinkId()
if linkId == 0 {
t.Fatal("expected non-zero link ID")
}
data := h.oscLinkService.GetLinkData(linkId)
if data == nil {
t.Fatal("expected link data")
}
if data.ID != "mylink" {
t.Errorf("expected ID 'mylink', got %q", data.ID)
}
}