-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_value_update_test.go
More file actions
193 lines (159 loc) · 5.56 KB
/
handler_value_update_test.go
File metadata and controls
193 lines (159 loc) · 5.56 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
package devtui
import (
"fmt"
"strconv"
"strings"
"sync"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// ThreadSafePortTestHandler is a thread-safe version for race condition testing
type ThreadSafePortTestHandler struct {
mu sync.RWMutex
currentPort string
}
func (h *ThreadSafePortTestHandler) Label() string { return "Port" }
func (h *ThreadSafePortTestHandler) Editable() bool { return true }
func (h *ThreadSafePortTestHandler) Timeout() time.Duration { return 3 * time.Second }
func (h *ThreadSafePortTestHandler) Value() string {
h.mu.RLock()
defer h.mu.RUnlock()
return h.currentPort
}
func (h *ThreadSafePortTestHandler) Change(newValue string, progress chan<- string) {
portStr := strings.TrimSpace(newValue)
if portStr == "" {
if progress != nil {
progress <- "port cannot be empty"
}
return
}
port, err := strconv.Atoi(portStr)
if err != nil {
if progress != nil {
progress <- "port must be a number"
}
return
}
if port < 1 || port > 65535 {
if progress != nil {
progress <- "port must be between 1 and 65535"
}
return
}
// Thread-safe update
h.mu.Lock()
h.currentPort = portStr
h.mu.Unlock()
if progress != nil {
progress <- fmt.Sprintf("Port configured: %d", port)
}
}
// TestHandlerValueUpdateAfterEdit tests that the field displays the updated value from handler after editing
func TestHandlerValueUpdateAfterEdit(t *testing.T) {
t.Run("Field should display updated value from handler after successful edit", func(t *testing.T) {
// Setup
h := DefaultTUIForTest(func(messages ...any) {
// Test logger - do nothing
})
// Initialize viewport
h.viewport.Width = 80
h.viewport.Height = 24
// Create a new tab with our port handler
portHandler := &PortTestHandler{currentPort: "8080"}
tab := h.NewTabSection("Server", "Server configuration")
h.AddHandler(portHandler, "", tab)
// Get the test tab index (should be the last one added)
testTabIndex := len(h.TabSections) - 1
h.activeTab = testTabIndex
// Get the field
tabSection := tab.(*tabSection)
field := tabSection.FieldHandlers[0]
// Verify initial state
initialValue := field.Value()
expectedInitial := "8080"
if initialValue != expectedInitial {
t.Errorf("Expected initial value '%s', got '%s'", expectedInitial, initialValue)
}
// Realistic: User presses Enter to enter edit mode
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyEnter})
// Verify edit mode was activated
if !h.editModeActivated {
t.Error("Expected edit mode to be activated after pressing Enter")
}
// Realistic: User clears the field with multiple backspaces then types "80"
// First, simulate moving cursor to end and then backspace to clear
for i := 0; i < 5; i++ { // Clear existing "8080" (4 chars + buffer)
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyBackspace})
}
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'8'}})
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'0'}})
// User presses Enter to save the change
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyEnter})
// The critical test: field.Value() should now return the updated value from handler
expectedNewValue := "80"
actualValue := field.Value()
if actualValue != expectedNewValue {
t.Errorf("Expected field.Value() to return updated value '%s', got '%s'",
expectedNewValue, actualValue)
}
// Also verify the handler's internal state was updated
if portHandler.currentPort != expectedNewValue {
t.Errorf("Expected handler.currentPort to be '%s', got '%s'",
expectedNewValue, portHandler.currentPort)
}
// Verify edit mode is deactivated
if h.editModeActivated {
t.Error("Expected edit mode to be deactivated after pressing Enter")
}
// Verify tempEditValue is cleared
if field.tempEditValue != "" {
t.Errorf("Expected tempEditValue to be cleared after Enter, got '%s'", field.tempEditValue)
}
})
t.Run("Field should display error message when validation fails", func(t *testing.T) {
// Setup
h := DefaultTUIForTest(func(messages ...any) {
// Test logger - do nothing
})
// Initialize viewport
h.viewport.Width = 80
h.viewport.Height = 24
// Create a new tab with our port handler
portHandler := &PortTestHandler{currentPort: "8080"}
tab := h.NewTabSection("Server", "Server configuration")
h.AddHandler(portHandler, "", tab)
// Get the test tab index
testTabIndex := len(h.TabSections) - 1
h.activeTab = testTabIndex
// Get the field
tabSection := tab.(*tabSection)
field := tabSection.FieldHandlers[0]
// Realistic: User presses Enter to enter edit mode
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyEnter})
// Realistic: User clears field with backspace then types invalid value "99999"
// Clear existing text
for i := 0; i < 5; i++ { // Clear existing "8080" (4 chars + buffer)
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyBackspace})
}
// Type "99999"
for _, char := range "99999" {
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{char}})
}
originalValue := field.Value()
// User presses Enter to save the invalid change
h.handleKeyboard(tea.KeyMsg{Type: tea.KeyEnter})
// The field value should remain unchanged when validation fails
actualValue := field.Value()
if actualValue != originalValue {
t.Errorf("Expected field.Value() to remain unchanged at '%s', got '%s'",
originalValue, actualValue)
}
// Handler's internal state should also remain unchanged
if portHandler.currentPort != originalValue {
t.Errorf("Expected handler.currentPort to remain '%s', got '%s'",
originalValue, portHandler.currentPort)
}
})
}