-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmacro_test.go
More file actions
513 lines (429 loc) · 15 KB
/
Copy pathmacro_test.go
File metadata and controls
513 lines (429 loc) · 15 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
)
func TestMacroRecordingAndPlayback(t *testing.T) {
tmpFile := "test_macros.ini"
defer os.Remove(tmpFile)
mgr := NewMacroManager(tmpFile)
// Trigger recording start (Ctrl+.)
ctrlDot := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD,
ControlKeyState: vtinput.LeftCtrlPressed,
}
if !mgr.Filter(ctrlDot) {
t.Fatal("Ctrl+. should be filtered and start recording")
}
if !mgr.Recording {
t.Fatal("Manager should be in recording state")
}
// Send a normal key 'A'
keyA := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_A,
Char: 'a',
}
mgr.Filter(keyA)
if len(mgr.Buffer) != 1 {
t.Fatalf("Expected 1 event in buffer, got %d", len(mgr.Buffer))
}
// Stop recording
mgr.Filter(ctrlDot)
if mgr.Recording {
t.Fatal("Manager should stop recording")
}
// Simulate Assign Frame capturing Ctrl+F1
ctrlF1 := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_F1,
ControlKeyState: vtinput.LeftCtrlPressed,
}
assignFrame := NewMacroAssignFrame(mgr)
assignFrame.ProcessKey(ctrlF1)
if _, ok := mgr.Macros[KeyStr(vtinput.VK_F1, vtinput.LeftCtrlPressed)]; !ok {
t.Fatal("Macro should be saved with Ctrl+F1 key")
}
// Test reloading from file
mgr2 := NewMacroManager(tmpFile)
if _, ok := mgr2.Macros[KeyStr(vtinput.VK_F1, vtinput.LeftCtrlPressed)]; !ok {
t.Fatal("Macro was not correctly loaded from INI file")
}
}
func TestKeyNormalization(t *testing.T) {
// Check that Left and Right Ctrl give same key
k1 := KeyStr(vtinput.VK_A, vtinput.LeftCtrlPressed)
k2 := KeyStr(vtinput.VK_A, vtinput.RightCtrlPressed)
if k1 != k2 {
t.Errorf("Normalization failed: %s != %s", k1, k2)
}
// Check Ctrl+Shift combination
k3 := KeyStr(vtinput.VK_B, vtinput.LeftCtrlPressed|vtinput.ShiftPressed)
if !strings.Contains(k3, ":18") { // 0x08 (Ctrl) | 0x10 (Shift) = 0x18
t.Errorf("Complex normalization failed: %s", k3)
}
}
func TestMacroPlaybackLogic(t *testing.T) {
mgr := NewMacroManager("unused.ini")
// Create macro: print "hi" on F2 press
f2Key := KeyStr(vtinput.VK_F2, 0)
macroSeq := []*vtinput.InputEvent{
{Type: vtinput.KeyEventType, KeyDown: true, Char: 'h', VirtualKeyCode: vtinput.VK_H},
{Type: vtinput.KeyEventType, KeyDown: true, Char: 'i', VirtualKeyCode: vtinput.VK_I},
}
mgr.Macros[f2Key] = macroSeq
// Simulate F2 press
pressF2 := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_F2,
}
// Hack for test: intercepting InjectEvents by replacing global FrameManager is not easy,
// but we can check that Filter returned true (event consumed to be replaced by macro)
if !mgr.Filter(pressF2) {
t.Error("Filter should return true when triggering a macro")
}
}
func TestMacro_FilterTriggerSwallowing_Order(t *testing.T) {
mgr := NewMacroManager("unused.ini")
mgr.Recording = true
mgr.Buffer = make([]*vtinput.InputEvent, 0)
// Trigger stop recording via Ctrl+.
stopEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
Char: '.', ControlKeyState: vtinput.LeftCtrlPressed,
}
res := mgr.Filter(stopEvent)
if !res {
t.Error("Filter should swallow the stop trigger even if recording is active")
}
if len(mgr.Buffer) != 0 {
t.Errorf("Stop trigger should NOT be added to macro buffer, but buffer size is %d", len(mgr.Buffer))
}
}
func TestMacro_TriggerSwallowing(t *testing.T) {
mgr := NewMacroManager("unused.ini")
// 1. Start recording via Ctrl+. (using Char for compatibility)
startEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
Char: '.', ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(startEvent)
if !mgr.Recording {
t.Fatal("Should be recording")
}
// 2. Type 'A'
mgr.Filter(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, Char: 'a', VirtualKeyCode: vtinput.VK_A})
// 3. Stop recording via Ctrl+.
stopEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
Char: '.', ControlKeyState: vtinput.LeftCtrlPressed,
}
res := mgr.Filter(stopEvent)
if !res {
t.Error("Stop trigger should be consumed (return true)")
}
if mgr.Recording {
t.Error("Should have stopped recording")
}
// 4. Verify buffer: should ONLY contain 'a', NOT the trigger dot
if len(mgr.Buffer) != 1 || mgr.Buffer[0].Char != 'a' {
t.Errorf("Macro buffer polluted or incomplete. Items: %d", len(mgr.Buffer))
}
}
func TestMacro_AssignRobustness(t *testing.T) {
// Clean manager for testing
mgr := &MacroManager{Macros: make(map[string][]*vtinput.InputEvent)}
mgr.Buffer = []*vtinput.InputEvent{{Char: 'x', KeyDown: true}}
f := NewMacroAssignFrame(mgr)
// 1. Standalone modifiers should be ignored (dialog stays open)
f.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_SHIFT})
if f.Done {
t.Error("Assign dialog should ignore standalone Shift")
}
// 2. Esc SHOULD now cancel the dialog without assignment
f.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_ESCAPE})
if !f.Done {
t.Error("Assign dialog should close after pressing Esc")
}
escKey := KeyStr(vtinput.VK_ESCAPE, 0)
if _, ok := mgr.Macros[escKey]; ok {
t.Error("Esc should cancel, not assign a macro")
}
// 3. Test Alt+X assignment
f.Done = false
mgr.Buffer = []*vtinput.InputEvent{{Char: 'y', KeyDown: true}}
f.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
VirtualKeyCode: vtinput.VK_X, ControlKeyState: vtinput.LeftAltPressed,
})
altXKey := KeyStr(vtinput.VK_X, vtinput.LeftAltPressed)
if _, ok := mgr.Macros[altXKey]; !ok {
t.Error("Macro failed to assign to Alt+X")
}
}
func TestMacro_KeyUpConsumption(t *testing.T) {
mgr := NewMacroManager("unused.ini")
// Start recording
ctrlDot := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD, ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(ctrlDot)
// Release trigger (KeyUp)
ctrlDotUp := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: false,
VirtualKeyCode: vtinput.VK_OEM_PERIOD, ControlKeyState: vtinput.LeftCtrlPressed,
}
if !mgr.Filter(ctrlDotUp) {
t.Error("KeyUp for Ctrl+. should be consumed by the filter")
}
// Normal key release during recording should NOT be added to buffer
keyAUp := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: false,
VirtualKeyCode: vtinput.VK_A, Char: 'a',
}
mgr.Filter(keyAUp)
if len(mgr.Buffer) != 0 {
t.Errorf("KeyUp should not be recorded in macro buffer, got length %d", len(mgr.Buffer))
}
}
func TestMacro_CancelEsc(t *testing.T) {
tmpPath := filepath.Join(os.TempDir(), "esc.ini")
os.Remove(tmpPath)
defer os.Remove(tmpPath)
mgr := NewMacroManager(tmpPath)
mgr.Recording = true
mgr.Buffer = []*vtinput.InputEvent{{Char: 'h', KeyDown: true}}
assign := NewMacroAssignFrame(mgr)
escEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
VirtualKeyCode: vtinput.VK_ESCAPE,
}
assign.ProcessKey(escEvent)
key := KeyStr(vtinput.VK_ESCAPE, 0)
if _, ok := mgr.Macros[key]; ok {
t.Error("Esc should cancel, not assign a macro")
}
if !assign.Done {
t.Error("Assign frame should be Done after cancellation")
}
}
func TestMacro_Clear(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "clear_macros.ini")
mgr := NewMacroManager(tmpFile)
// 1. Assign a macro first
key := KeyStr(vtinput.VK_F3, 0)
mgr.Macros[key] = []*vtinput.InputEvent{
{Type: vtinput.KeyEventType, KeyDown: true, Char: 'x'},
}
mgr.Save()
// 2. Simulate empty recording and assigning to F3 (to clear it)
mgr.Buffer = nil // Empty recording
assignFrame := NewMacroAssignFrame(mgr)
assignFrame.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_F3,
})
// 3. Verify it is deleted from active map
if _, ok := mgr.Macros[key]; ok {
t.Error("Macro should be completely deleted from map when assigned an empty buffer")
}
// 4. Verify it is deleted from saved file
mgr2 := NewMacroManager(tmpFile)
if _, ok := mgr2.Macros[key]; ok {
t.Error("Cleared macro should not persist in the saved INI file")
}
}
func TestMacro_CharTrigger(t *testing.T) {
mgr := NewMacroManager("unused.ini")
// Test trigger using Char instead of VK (for terminals that map dot differently)
event := &vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true,
Char: '.', VirtualKeyCode: 0, ControlKeyState: vtinput.LeftCtrlPressed,
}
if !mgr.Filter(event) {
t.Error("Macro recording should start via Char '.' detection")
}
if !mgr.Recording {
t.Error("Manager failed to enter recording state via Char trigger")
}
}
func TestMacro_AssignFrame_Structure(t *testing.T) {
mgr := &MacroManager{Macros: make(map[string][]*vtinput.InputEvent)}
f := NewMacroAssignFrame(mgr)
// Check that it's a proper window with a child (the prompt text)
if len(f.GetChildren()) == 0 {
t.Error("MacroAssignFrame should have at least one child (prompt)")
}
// Validate Layout
vtui.AssertLayout(t, f)
// Verify focus logic: it should NOT allow Tab to cycle away
// because any key (including Tab) must be captured as a macro.
tabEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_TAB,
}
mgr.Buffer = []*vtinput.InputEvent{{Char: 't', KeyDown: true}}
handled := f.ProcessKey(tabEvent)
if !handled {
t.Error("MacroAssignFrame should handle (capture) Tab key")
}
if !f.IsDone() {
t.Error("MacroAssignFrame should close after capturing a key")
}
// Verify that macro was assigned to Tab
tabKey := KeyStr(vtinput.VK_TAB, 0)
if _, ok := mgr.Macros[tabKey]; !ok {
t.Error("Macro failed to assign to Tab key")
}
}
func TestMacroKeyStrDistinguishesEnhancedKeys(t *testing.T) {
// Standard Delete has the EnhancedKey modifier in modern protocols
delKeyStr := KeyStr(vtinput.VK_DELETE, vtinput.EnhancedKey)
// Numpad Delete (NumDel) does not have the EnhancedKey modifier
numDelKeyStr := KeyStr(vtinput.VK_DELETE, 0)
if delKeyStr == numDelKeyStr {
t.Errorf("Expected different KeyStr representations for standard Del (%q) and NumDel (%q), but they are identical", delKeyStr, numDelKeyStr)
}
}
func TestMacroIgnoresStandaloneModifiers(t *testing.T) {
mgr := NewMacroManager("")
mgr.Recording = true
mgr.Buffer = nil
// Simulate pressing Ctrl, then Shift, then a letter 'A', then releasing them
events := []*vtinput.InputEvent{
{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_CONTROL},
{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_SHIFT},
{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_A, Char: 'A'},
{Type: vtinput.KeyEventType, KeyDown: false, VirtualKeyCode: vtinput.VK_A, Char: 'A'},
{Type: vtinput.KeyEventType, KeyDown: false, VirtualKeyCode: vtinput.VK_SHIFT},
{Type: vtinput.KeyEventType, KeyDown: false, VirtualKeyCode: vtinput.VK_CONTROL},
}
for _, ev := range events {
mgr.Filter(ev)
}
if len(mgr.Buffer) != 1 {
t.Errorf("Expected exactly 1 event in macro buffer, but got %d", len(mgr.Buffer))
} else if mgr.Buffer[0].VirtualKeyCode != vtinput.VK_A {
t.Errorf("Expected recorded event to be VK_A, but got %v", vtinput.VKString(mgr.Buffer[0].VirtualKeyCode))
}
}
func TestMacroClearRecordingIsEmpty(t *testing.T) {
mgr := NewMacroManager("")
// Start recording
startEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD,
Char: '.',
ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(startEvent)
if !mgr.Recording {
t.Error("Expected MacroManager to be in Recording state")
}
// Pressing Ctrl key before pressing '.' to stop recording
ctrlDown := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_CONTROL,
}
mgr.Filter(ctrlDown)
// Pressing '.' to stop recording
stopEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD,
Char: '.',
ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(stopEvent)
if mgr.Recording {
t.Error("Expected MacroManager to stop Recording")
}
if len(mgr.Buffer) != 0 {
t.Errorf("Expected macro buffer to be empty for immediate stop recording, but got %d items", len(mgr.Buffer))
}
}
func TestMacroClearResetsExisting(t *testing.T) {
mgr := NewMacroManager("")
mgr.Macros = map[string][]*vtinput.InputEvent{
"C:0": {{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_F3}},
}
scr := vtui.NewSilentScreenBuf()
scr.AllocBuf(80, 25)
vtui.FrameManager.Init(scr)
// 1. Начинаем запись макроса
startEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD,
Char: '.',
ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(startEvent)
if !mgr.Recording {
t.Error("Expected MacroManager to be in Recording state")
}
// 2. Останавливаем запись (буфер пуст)
stopEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_OEM_PERIOD,
Char: '.',
ControlKeyState: vtinput.LeftCtrlPressed,
}
mgr.Filter(stopEvent)
if mgr.Recording {
t.Error("Expected MacroManager to stop Recording")
}
// Выполняем все накопившиеся асинхронные задачи, пока не включится нужный режим
timeout := time.After(1 * time.Second)
WaitLoop:
for !mgr.Assigning {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
case <-timeout:
t.Fatal("Timeout waiting for Assigning state")
break WaitLoop
}
}
if !mgr.Assigning {
t.Error("Expected MacroManager to be in Assigning state")
}
// Пытаемся нажать 'Clear' (VK_CLEAR = 0x0C)
clearEvent := &vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_CLEAR,
}
// Фильтр НЕ должен поглотить событие воспроизведением старого макроса,
// так как активен режим назначения (Assigning == true)
consumed := mgr.Filter(clearEvent)
if consumed {
t.Error("Expected Filter to not consume VK_CLEAR while Assigning is active")
}
// Симулируем обработку нажатия диалогом
frame := NewMacroAssignFrame(mgr)
frame.ProcessKey(clearEvent)
// После обработки флаг назначения должен сброситься, а макрос удалиться
if mgr.Assigning {
t.Error("Expected Assigning state to be cleared after key processing")
}
if _, exists := mgr.Macros["C:0"]; exists {
t.Error("Expected macro on C:0 to be deleted")
}
}