-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_handler_test.go
More file actions
295 lines (235 loc) · 8.88 KB
/
interactive_handler_test.go
File metadata and controls
295 lines (235 loc) · 8.88 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
package devtui
import (
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
// TestInteractiveHandler is a mock handler that implements HandlerInteractive
// for testing cursor behavior, ESC handling, and multi-step wizards
type TestInteractiveHandler struct {
name string
label string
value string
waitingForUser bool
changeCalled bool
lastValue string
cancelCalled bool
log func(message ...any)
}
func NewTestInteractiveHandler(name, label, value string) *TestInteractiveHandler {
return &TestInteractiveHandler{
name: name,
label: label,
value: value,
waitingForUser: true,
log: func(...any) {},
}
}
func (h *TestInteractiveHandler) Name() string { return h.name }
func (h *TestInteractiveHandler) Label() string { return h.label }
func (h *TestInteractiveHandler) Value() string { return h.value }
func (h *TestInteractiveHandler) WaitingForUser() bool { return h.waitingForUser }
func (h *TestInteractiveHandler) SetLog(f func(...any)) { h.log = f }
func (h *TestInteractiveHandler) AlwaysShowAllLogs() bool { return true }
func (h *TestInteractiveHandler) Change(newValue string) {
h.changeCalled = true
h.lastValue = newValue
// Simulate a wizard step: after receiving input, suggest next value
if h.label == "Project Name" {
h.label = "Project Location"
h.value = "parentfolder/" + newValue
// Still waiting for user to confirm location
} else {
h.waitingForUser = false
}
}
func (h *TestInteractiveHandler) Cancel() {
h.cancelCalled = true
h.waitingForUser = false
}
// --- Bug 1 Test: Cursor should be at end after step change ---
func TestInteractiveHandler_CursorAtEndAfterStepChange(t *testing.T) {
h := DefaultTUIForTest()
tab := h.NewTabSection("WIZARD", "Test wizard")
handler := NewTestInteractiveHandler("Wizard", "Project Name", "")
addInteractiveHandlerForTest(h, tab, handler)
// Get tab section properly
tabSection := h.TabSections[len(h.TabSections)-1]
h.activeTab = tabSection.Index
h.ready = true
field := tabSection.FieldHandlers[0]
// Simulate user typing "myapp" and pressing Enter
field.tempEditValue = "myapp"
h.editModeActivated = true
tabSection.IndexActiveEditField = 0
// Simulate Enter key
msg := tea.KeyMsg{Type: tea.KeyEnter}
h.handleEditingConfigKeyboard(msg)
// BUG: After step change, cursor should be at end of new value
// The new value is "parentfolder/myapp" (18 chars as runes)
expectedCursor := len([]rune(handler.Value()))
if field.cursor != expectedCursor {
t.Errorf("Bug 1: Cursor should be at end (%d), got %d", expectedCursor, field.cursor)
}
}
// --- Bug 2 Test: ESC should always close edit mode ---
func TestInteractiveHandler_ESCAlwaysClosesEditMode(t *testing.T) {
h := DefaultTUIForTest()
tab := h.NewTabSection("WIZARD", "Test wizard")
handler := NewTestInteractiveHandler("Wizard", "Project Name", "")
addInteractiveHandlerForTest(h, tab, handler)
// Get tab section properly
tabSection := h.TabSections[len(h.TabSections)-1]
h.activeTab = tabSection.Index
h.ready = true
field := tabSection.FieldHandlers[0]
// Activate edit mode (handler is waiting for user)
h.editModeActivated = true
field.tempEditValue = "somevalue"
tabSection.IndexActiveEditField = 0
// Verify handler is still waiting for user
if !handler.WaitingForUser() {
t.Fatal("Handler should be waiting for user")
}
// Simulate ESC key
msg := tea.KeyMsg{Type: tea.KeyEsc}
h.handleEditingConfigKeyboard(msg)
// BUG: ESC should close edit mode even if WaitingForUser is true
if h.editModeActivated {
t.Error("Bug 2: ESC should close edit mode regardless of WaitingForUser")
}
}
// --- Bug 3 Test: Enter should execute and update tempEditValue ---
func TestInteractiveHandler_EnterExecutesAndUpdatesTempEditValue(t *testing.T) {
h := DefaultTUIForTest()
tab := h.NewTabSection("WIZARD", "Test wizard")
handler := NewTestInteractiveHandler("Wizard", "Project Name", "")
addInteractiveHandlerForTest(h, tab, handler)
// Get tab section properly
tabSection := h.TabSections[len(h.TabSections)-1]
h.activeTab = tabSection.Index
h.ready = true
field := tabSection.FieldHandlers[0]
// Simulate user typing "myapp"
field.tempEditValue = "myapp"
h.editModeActivated = true
tabSection.IndexActiveEditField = 0
// Simulate Enter key
msg := tea.KeyMsg{Type: tea.KeyEnter}
h.handleEditingConfigKeyboard(msg)
// Handler.Change should have been called
if !handler.changeCalled {
t.Error("Handler.Change should have been called")
}
// BUG: Since handler still wants input (step 2), edit mode should stay open
// AND tempEditValue should be updated to the new suggested value
if handler.WaitingForUser() && h.editModeActivated {
// Edit mode is open, tempEditValue should have the new value
expectedValue := handler.Value() // "parentfolder/myapp"
if field.tempEditValue != expectedValue {
t.Errorf("Bug 3: tempEditValue should be '%s', got '%s'", expectedValue, field.tempEditValue)
}
}
}
// --- Bug 4 Test: Cursor blinking should not shift text layout ---
func TestInteractiveHandler_CursorBlinkingDoesNotShiftLayout(t *testing.T) {
// Forzar perfil de color para consistencia con otros tests
lipgloss.SetColorProfile(termenv.TrueColor)
h := DefaultTUIForTest()
tab := h.NewTabSection("WIZARD", "Test wizard")
handler := NewTestInteractiveHandler("Wizard", "Project Location", "Test/myapp")
addInteractiveHandlerForTest(h, tab, handler)
// Get tab section properly
tabSection := h.TabSections[len(h.TabSections)-1]
h.activeTab = tabSection.Index
h.ready = true
h.viewport.Width = 80
h.viewport.Height = 24
field := tabSection.FieldHandlers[0]
// Activate edit mode
h.editModeActivated = true
field.tempEditValue = "Test/myapp"
field.cursor = 4 // Cursor in the middle of text
tabSection.IndexActiveEditField = 0
// Render with cursor visible
h.cursorVisible = true
renderedVisible := h.footerView()
// Render with cursor invisible (blinking off)
h.cursorVisible = false
renderedInvisible := h.footerView()
// BUG: Both renders should have the same VISUAL width
// The cursor space should always be reserved, even when invisible
// Use lipgloss.Width() to get the actual visual width (ignores ANSI codes)
visibleWidth := lipgloss.Width(renderedVisible)
invisibleWidth := lipgloss.Width(renderedInvisible)
if visibleWidth != invisibleWidth {
t.Errorf("Bug 4: Rendered visual width differs between cursor states. Visible: %d, Invisible: %d",
visibleWidth, invisibleWidth)
}
}
// --- Test: Full wizard flow including confirming suggested value ---
func TestInteractiveHandler_FullWizardFlow(t *testing.T) {
h := DefaultTUIForTest()
tab := h.NewTabSection("WIZARD", "Test wizard")
handler := NewTestInteractiveHandler("Wizard", "Project Name", "")
addInteractiveHandlerForTest(h, tab, handler)
// Get tab section properly
tabSection := h.TabSections[len(h.TabSections)-1]
h.activeTab = tabSection.Index
h.ready = true
h.viewport.Width = 80
h.viewport.Height = 24
field := tabSection.FieldHandlers[0]
// Step 1: User types "myapp" and presses Enter
field.tempEditValue = "myapp"
h.editModeActivated = true
tabSection.IndexActiveEditField = 0
msg := tea.KeyMsg{Type: tea.KeyEnter}
h.handleEditingConfigKeyboard(msg)
// After step 0 completes:
// - handler.label = "Project Location"
// - handler.value = "parentfolder/myapp"
// - handler.waitingForUser = true
// - edit mode should still be active
if !h.editModeActivated {
t.Fatal("Edit mode should still be active after step 0")
}
// Step 2: User presses Enter WITHOUT modifying the suggested value
// The tempEditValue should have been updated to the new suggested value
expectedValue := handler.Value()
if field.tempEditValue != expectedValue {
t.Errorf("tempEditValue should be '%s', got '%s'", expectedValue, field.tempEditValue)
}
// Simulate pressing Enter to confirm the suggested value
msg = tea.KeyMsg{Type: tea.KeyEnter}
h.handleEditingConfigKeyboard(msg)
// After step 1 completes:
// - handler.Change should have been called
// - handler.waitingForUser = false
// - edit mode should be closed
if handler.WaitingForUser() {
t.Error("Handler should not be waiting for user after step 1")
}
if h.editModeActivated {
t.Error("Edit mode should be closed after step 1")
}
}
// --- Helper: addInteractiveHandlerForTest ---
func addInteractiveHandlerForTest(h *DevTUI, tabAny any, handler *TestInteractiveHandler) {
ts := tabAny.(*tabSection)
ah := &anyHandler{
handlerType: handlerTypeInteractive,
nameFunc: handler.Name,
labelFunc: handler.Label,
valueFunc: handler.Value,
changeFunc: func(v string) { handler.Change(v) },
editModeFunc: handler.WaitingForUser, // This is how WaitingForUser is wired
editableFunc: func() bool { return true },
}
f := &field{
handler: ah,
parentTab: ts,
}
ts.FieldHandlers = append(ts.FieldHandlers, f)
}