-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommand_line_test.go
More file actions
155 lines (130 loc) · 4.16 KB
/
Copy pathcommand_line_test.go
File metadata and controls
155 lines (130 loc) · 4.16 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
package main
import (
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
"testing"
)
func TestCommandLine_Input(t *testing.T) {
vtui.SetDefaultPalette()
SetDefaultF4Palette()
cl := NewCommandLine("> ")
cl.SetPosition(0, 0, 10, 0)
// Simulate typing 'f'
cl.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
Char: 'f',
})
if cl.Edit.GetText() != "f" {
t.Errorf("Expected cmdline text 'f', got '%s'", cl.Edit.GetText())
}
// Simulate Backspace
cl.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
VirtualKeyCode: vtinput.VK_BACK,
})
if len(cl.Edit.GetText()) != 0 {
t.Error("CommandLine should be empty after backspace")
}
}
func TestCommandLine_InitialFocus(t *testing.T) {
cl := NewCommandLine("> ")
if !cl.Edit.IsFocused() {
t.Error("CommandLine's underlying Edit should be focused upon creation to ensure cursor visibility")
}
if !cl.IsFocused() {
t.Error("CommandLine should be focused upon creation")
}
}
func TestCommandLine_History(t *testing.T) {
vtui.SetDefaultPalette()
SetDefaultF4Palette()
cl := NewCommandLine("> ")
// 1. Test adding history
cl.Edit.AddHistory("ls -la")
cl.Edit.AddHistory("cd /tmp")
cl.Edit.AddHistory("ls -la") // Duplicate of a previous one, but not the latest
if len(cl.Edit.History) != 3 {
t.Errorf("Expected history length 3, got %d", len(cl.Edit.History))
}
// 2. Test navigation
cl.Edit.HistoryUp() // Should be "ls -la" (index 0)
if cl.Edit.GetText() != "ls -la" {
t.Errorf("HistoryUp(1) failed: expected 'ls -la', got '%s'", cl.Edit.GetText())
}
cl.Edit.HistoryUp() // Should be "cd /tmp" (index 1)
if cl.Edit.GetText() != "cd /tmp" {
t.Errorf("HistoryUp(2) failed: expected 'cd /tmp', got '%s'", cl.Edit.GetText())
}
cl.Edit.HistoryDown() // Back to "ls -la" (index 0)
if cl.Edit.GetText() != "ls -la" {
t.Errorf("HistoryDown(1) failed: expected 'ls -la', got '%s'", cl.Edit.GetText())
}
cl.Edit.HistoryDown() // Should clear the line
if cl.Edit.GetText() != "" {
t.Errorf("HistoryDown(2) failed: expected empty string, got '%s'", cl.Edit.GetText())
}
// 3. Test duplicate prevention (consecutive)
cl.Edit.AddHistory("pwd")
cl.Edit.AddHistory("pwd")
if len(cl.Edit.History) != 4 { // Only one "pwd" should be added
t.Errorf("Duplicate history prevention failed, length: %d", len(cl.Edit.History))
}
// 4. Test reset on typing
cl.Edit.HistoryUp() // "pwd"
cl.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
Char: ' ',
})
if cl.Edit.HistoryPos != -1 {
t.Error("History browsing state should reset after typing")
}
}
func TestCommandLine_HistoryBoundaries(t *testing.T) {
cl := NewCommandLine("> ")
cl.Edit.AddHistory("cmd1")
// Go up once
cl.Edit.HistoryUp()
if cl.Edit.GetText() != "cmd1" {
t.Fatal("Setup failed")
}
// Go up again - should stay at cmd1
cl.Edit.HistoryUp()
if cl.Edit.GetText() != "cmd1" {
t.Error("HistoryUp should cap at the end of the list")
}
// Go down to clear
cl.Edit.HistoryDown()
if cl.Edit.GetText() != "" {
t.Error("HistoryDown should clear the line when at the start of history")
}
// Go down again - should stay empty and not crash
cl.Edit.HistoryDown()
if cl.Edit.HistoryPos != -1 || cl.Edit.GetText() != "" {
t.Error("HistoryDown should stay at -1 when already empty")
}
}
func TestCommandLine_AutoCompleteDisabled(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
SetDefaultF4Palette()
cl := NewCommandLine("> ")
cl.SetPosition(0, 0, 10, 0)
cl.Edit.History = []string{"ls", "long-command"}
// 1. Выключаем глобальную настройку
oldCfg := AppConfig
AppConfig.CommandLineAutoComplete = false
defer func() { AppConfig = oldCfg }()
// 2. Симулируем ввод буквы 'l'
cl.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
Char: 'l',
})
// 3. Проверяем, что меню автодополнения НЕ появилось в FrameManager
top := vtui.FrameManager.GetTopFrame()
if _, isAc := top.(*vtui.AutoCompleteMenu); isAc {
t.Error("AutoCompleteMenu was shown even though CommandLineAutoComplete is false")
}
}