1package views
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "github.com/bnema/archup/internal/interfaces/tui/models"
9)
10
11func TestRenderForm(t *testing.T) {
12 // Create a test form model
13 fm := models.NewFormModel()
14
15 // Render the form
16 output := RenderForm(fm, "")
17
18 // Verify key content is present
19 checks := []string{
20 "ArchUp Installer", // Title
21 "Hostname:", // Field labels
22 "Username:",
23 "↑↓ Navigate", // Instructions
24 }
25
26 for _, check := range checks {
27 if !strings.Contains(output, check) {
28 t.Errorf("Expected form output to contain '%s', but it didn't", check)
29 }
30 }
31
32 // Verify output is not empty
33 if len(output) == 0 {
34 t.Error("Form output should not be empty")
35 }
36
37 t.Logf("Form rendered successfully with %d characters", len(output))
38}
39
40func TestRenderFormWithError(t *testing.T) {
41 // Create a test form model with error
42 fm := models.NewFormModel()
43 testErr := "Test error message"
44 fm.SetError(errors.New(testErr))
45
46 // Render the form
47 output := RenderForm(fm, "")
48
49 // Verify error is displayed
50 if !strings.Contains(output, testErr) {
51 t.Errorf("Expected error message '%s' in form output", testErr)
52 }
53}
54
55func TestRenderFormWithDifferentFocus(t *testing.T) {
56 // Create a test form model
57 fm := models.NewFormModel()
58
59 // Render initial form
60 output1 := RenderForm(fm, "")
61
62 // Move focus
63 fm.FocusNext()
64
65 // Render form with different focus
66 output2 := RenderForm(fm, "")
67
68 // Both should render successfully
69 if len(output1) == 0 || len(output2) == 0 {
70 t.Error("Both renders should produce output")
71 }
72
73 // They should be different (different field is focused)
74 if output1 == output2 {
75 t.Log("Outputs are the same, but that's okay as the visual difference might not be captured")
76 }
77
78 t.Logf("Form 1: %d chars, Form 2: %d chars", len(output1), len(output2))
79}
80
81func TestFormModelCreation(t *testing.T) {
82 fm := models.NewFormModel()
83
84 // Verify initial state
85 fields := fm.GetFields()
86 if len(fields) != 7 {
87 t.Errorf("Expected 7 form fields, got %d", len(fields))
88 }
89
90 if fm.GetFocusIndex() != 0 {
91 t.Errorf("Expected initial focus index to be 0, got %d", fm.GetFocusIndex())
92 }
93
94 if fm.GetError() != nil {
95 t.Errorf("Expected no initial error, got %v", fm.GetError())
96 }
97
98 if fm.IsSubmitted() {
99 t.Error("Form should not be submitted initially")
100 }
101}
102
103func TestFormFieldInteraction(t *testing.T) {
104 fm := models.NewFormModel()
105
106 // Test focus navigation
107 initialIndex := fm.GetFocusIndex()
108 fm.FocusNext()
109 if fm.GetFocusIndex() != initialIndex+1 {
110 t.Error("FocusNext should increment focus index")
111 }
112
113 fm.FocusPrevious()
114 if fm.GetFocusIndex() != initialIndex {
115 t.Error("FocusPrevious should decrement focus index")
116 }
117
118 // Test data extraction
119 formData := fm.GetData()
120 if formData.Hostname != "" || formData.Username != "" {
121 t.Error("Initial form data should be empty")
122 }
123
124 // Set data
125 testData := models.FormData{
126 Hostname: "test-host",
127 Username: "testuser",
128 TargetDisk: "/dev/sda",
129 EncryptionType: "LUKS",
130 }
131 fm.SetData(testData)
132
133 // Verify data was set
134 retrievedData := fm.GetData()
135 if retrievedData.Hostname != testData.Hostname {
136 t.Errorf("Expected hostname '%s', got '%s'", testData.Hostname, retrievedData.Hostname)
137 }
138}
139
140func BenchmarkRenderForm(b *testing.B) {
141 fm := models.NewFormModel()
142
143 b.ResetTimer()
144 for i := 0; i < b.N; i++ {
145 _ = RenderForm(fm, "")
146 }
147}