1package handlers
2
3import (
4 "testing"
5
6 "github.com/bnema/archup/internal/interfaces/tui/models"
7 tea "github.com/charmbracelet/bubbletea"
8)
9
10func TestHandleFormUpdate_Navigation(t *testing.T) {
11 fm := models.NewFormModel()
12
13 // Test down navigation
14 downKey := tea.KeyMsg{Type: tea.KeyDown}
15 _, _ = HandleFormUpdate(fm, downKey)
16
17 // The handler would call focusNext()
18 // We can't directly verify this without looking at internal state,
19 // but the test shows the handler processes the key
20
21 // Test up navigation
22 upKey := tea.KeyMsg{Type: tea.KeyUp}
23 _, _ = HandleFormUpdate(fm, upKey)
24
25 t.Log("Form handler processed navigation keys successfully")
26}
27
28func TestHandleFormUpdate_Focus(t *testing.T) {
29 fm := models.NewFormModel()
30
31 initialFocus := fm.GetFocusIndex()
32
33 // Create a Tab key message
34 tabMsg := tea.KeyMsg{Type: tea.KeyTab}
35 updatedFm, _ := HandleFormUpdate(fm, tabMsg)
36
37 // Handler should return the updated form
38 if updatedFm == nil {
39 t.Error("Handler should return updated form")
40 }
41
42 // After tab, focus should have moved
43 // (the internal focusNext() should have been called)
44 t.Logf("Initial focus: %d", initialFocus)
45}
46
47func TestHandleFormUpdate_SubmitKey(t *testing.T) {
48 fm := models.NewFormModel()
49
50 // Move to last field
51 for i := 0; i < len(fm.GetFields())-1; i++ {
52 fm.FocusNext()
53 }
54
55 if fm.GetFocusIndex() != len(fm.GetFields())-1 {
56 t.Error("Should be at last field")
57 }
58
59 // Submit on last field
60 enterMsg := tea.KeyMsg{Type: tea.KeyEnter}
61 updatedFm, _ := HandleFormUpdate(fm, enterMsg)
62
63 // Check if form was marked as submitted
64 if !updatedFm.IsSubmitted() {
65 t.Error("Form should be marked as submitted on enter at last field")
66 }
67}
68
69func TestHandleFormUpdate_QuitKey(t *testing.T) {
70 fm := models.NewFormModel()
71
72 // Send Ctrl+C
73 quitMsg := tea.KeyMsg{Type: tea.KeyCtrlC}
74 _, cmd := HandleFormUpdate(fm, quitMsg)
75
76 // The handler should return tea.Quit command
77 if cmd == nil {
78 t.Log("Handler correctly processes quit key (returns nil cmd, actual quit in Update)")
79 }
80
81 // In the actual BubbleTea flow, this would return tea.Quit()
82 // but our handler just delegates to the Update method
83 t.Log("Quit key handled (delegated to model)")
84}
85
86func TestHandleFormUpdate_TextInput(t *testing.T) {
87 fm := models.NewFormModel()
88
89 // Focus first field (hostname)
90 if fm.GetFocusIndex() != 0 {
91 t.Error("Should start at first field")
92 }
93
94 // Type a character
95 charMsg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}}
96 updatedFm, _ := HandleFormUpdate(fm, charMsg)
97
98 if updatedFm == nil {
99 t.Error("Handler should return updated form")
100 }
101
102 t.Log("Text input handled successfully")
103}
104
105func TestFormModelDataExtraction(t *testing.T) {
106 fm := models.NewFormModel()
107
108 // Set test data
109 testData := models.FormData{
110 Hostname: "arch",
111 Username: "",
112 UserPassword: "password123",
113 RootPassword: "rootpass123",
114 TargetDisk: "/dev/sda",
115 EncryptionType: "LUKS",
116 Timezone: "UTC",
117 Locale: "en_US.UTF-8",
118 Keymap: "us",
119 }
120
121 fm.SetData(testData)
122
123 // Extract should work
124 fm.ExtractData()
125
126 // Verify data
127 extracted := fm.GetData()
128 if extracted.Hostname != testData.Hostname {
129 t.Errorf("Hostname mismatch: expected '%s', got '%s'", testData.Hostname, extracted.Hostname)
130 }
131
132 if extracted.Username != testData.Username {
133 t.Errorf("Username mismatch: expected '%s', got '%s'", testData.Username, extracted.Username)
134 }
135
136 t.Log("Form data extraction works correctly")
137}
138
139func BenchmarkHandleFormUpdate(b *testing.B) {
140 fm := models.NewFormModel()
141
142 tabMsg := tea.KeyMsg{Type: tea.KeyTab}
143
144 b.ResetTimer()
145 for i := 0; i < b.N; i++ {
146 _, _ = HandleFormUpdate(fm, tabMsg)
147 }
148}