xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / input_test.go

7.7 KB
1package tui
2
3import (
4	"strings"
5	"testing"
6
7	tea "github.com/charmbracelet/bubbletea"
8	"github.com/charmbracelet/lipgloss"
9	"github.com/charmbracelet/x/exp/golden"
10)
11
12// TestInputPromptIndicators verifies each input mode has the correct indicator.
13func TestInputPromptIndicators(t *testing.T) {
14	defer setupGoldenTest(t)()
15
16	tests := []struct {
17		name      string
18		mode      InputMode
19		targetID  string
20		indicator string
21	}{
22		{"broadcast_mode", InputModeBroadcast, "", "⬧"},
23		{"message_mode", InputModeMessage, "mysis-1", "⬥"},
24		{"new_mysis_mode", InputModeNewMysis, "", "⬡"},
25		{"config_provider_mode", InputModeConfigProvider, "mysis-1", "⚙"},
26		{"config_model_mode", InputModeConfigModel, "mysis-1", "cfg"},
27	}
28
29	for _, tt := range tests {
30		t.Run(tt.name, func(t *testing.T) {
31			input := NewInputModel()
32			input.SetMode(tt.mode, tt.targetID)
33
34			prompt := input.textInput.Prompt
35			if !strings.Contains(prompt, tt.indicator) {
36				t.Errorf("prompt %q does not contain expected indicator %q", prompt, tt.indicator)
37			}
38			if input.Mode() != tt.mode {
39				t.Errorf("mode = %v, want %v", input.Mode(), tt.mode)
40			}
41			if input.TargetID() != tt.targetID {
42				t.Errorf("targetID = %q, want %q", input.TargetID(), tt.targetID)
43			}
44			if !input.IsActive() {
45				t.Error("expected input to be active")
46			}
47		})
48	}
49}
50
51// TestInputPromptNone verifies that InputModeNone clears the prompt.
52func TestInputPromptNone(t *testing.T) {
53	input := NewInputModel()
54	input.SetMode(InputModeBroadcast, "")
55	if !input.IsActive() {
56		t.Error("expected input to be active after setting broadcast mode")
57	}
58
59	input.SetMode(InputModeNone, "")
60	if input.IsActive() {
61		t.Error("expected input to be inactive after setting none mode")
62	}
63	if input.textInput.Prompt != "" {
64		t.Errorf("expected empty prompt, got %q", input.textInput.Prompt)
65	}
66	if input.textInput.Placeholder != "" {
67		t.Errorf("expected empty placeholder, got %q", input.textInput.Placeholder)
68	}
69}
70
71// TestInputPromptWidthHandling verifies prompt rendering at various widths.
72func TestInputPromptWidthHandling(t *testing.T) {
73	tests := []struct {
74		name     string
75		mode     InputMode
76		width    int
77		inputLen int
78	}{
79		{"narrow_width_empty", InputModeBroadcast, 20, 0},
80		{"narrow_width_short_text", InputModeMessage, 30, 5},
81		{"normal_width_medium_text", InputModeBroadcast, 60, 20},
82		{"wide_width_long_text", InputModeMessage, 120, 80},
83	}
84
85	for _, tt := range tests {
86		t.Run(tt.name, func(t *testing.T) {
87			input := NewInputModel()
88			input.SetMode(tt.mode, "test-target")
89			input.SetWidth(tt.width)
90
91			if tt.inputLen > 0 {
92				text := strings.Repeat("a", tt.inputLen)
93				input.textInput.SetValue(text)
94			}
95
96			output := inputStyle.Width(tt.width - 2).Render(input.textInput.View())
97			if output == "" {
98				t.Error("expected non-empty output")
99			}
100			displayWidth := lipgloss.Width(output)
101			if displayWidth == 0 {
102				t.Error("expected non-zero display width")
103			}
104		})
105	}
106}
107
108// TestInputPromptLongText verifies handling of long input text.
109func TestInputPromptLongText(t *testing.T) {
110	defer setupGoldenTest(t)()
111
112	input := NewInputModel()
113	input.SetMode(InputModeBroadcast, "")
114	input.SetWidth(60)
115
116	longText := "This is a very long message that exceeds the typical display width and should be handled gracefully by the input component without causing any rendering issues."
117	input.textInput.SetValue(longText)
118
119	output := inputStyle.Width(58).Render(input.textInput.View())
120	if !strings.Contains(output, "⬧") {
121		t.Error("expected output to contain broadcast indicator ⬧")
122	}
123	if input.Value() != longText {
124		t.Error("expected full text to be preserved in value")
125	}
126}
127
128// TestInputPromptReset verifies reset clears all state.
129func TestInputPromptReset(t *testing.T) {
130	input := NewInputModel()
131	input.SetMode(InputModeBroadcast, "target-id")
132	input.textInput.SetValue("test message")
133	input.AddToHistory("previous message")
134
135	input.Reset()
136
137	if input.Mode() != InputModeNone {
138		t.Errorf("mode = %v, want InputModeNone", input.Mode())
139	}
140	if input.TargetID() != "" {
141		t.Errorf("targetID = %q, want empty", input.TargetID())
142	}
143	if input.Value() != "" {
144		t.Errorf("value = %q, want empty", input.Value())
145	}
146	if input.IsActive() {
147		t.Error("expected input to be inactive after reset")
148	}
149	if input.historyIndex != -1 {
150		t.Errorf("historyIndex = %d, want -1", input.historyIndex)
151	}
152	if input.draft != "" {
153		t.Errorf("draft = %q, want empty", input.draft)
154	}
155}
156
157// TestInputPromptViewAlways verifies ViewAlways behavior.
158func TestInputPromptViewAlways(t *testing.T) {
159	tests := []struct {
160		name         string
161		mode         InputMode
162		sending      bool
163		sendingLabel string
164		spinnerView  string
165		width        int
166	}{
167		{"inactive_placeholder", InputModeNone, false, "", "", 60},
168		{"active_broadcast", InputModeBroadcast, false, "", "", 60},
169		{"sending_broadcast", InputModeNone, true, "Broadcasting...", "⬡", 60},
170		{"sending_message", InputModeNone, true, "Sending...", "⬢", 60},
171		{"narrow_width", InputModeNone, false, "", "", 20},
172		{"minimum_width", InputModeNone, false, "", "", 5},
173	}
174
175	for _, tt := range tests {
176		t.Run(tt.name, func(t *testing.T) {
177			input := NewInputModel()
178			if tt.mode != InputModeNone {
179				input.SetMode(tt.mode, "test-target")
180			}
181
182			output := input.ViewAlways(tt.width, tt.sending, tt.sendingLabel, tt.spinnerView)
183			if output == "" {
184				t.Error("expected non-empty output from ViewAlways")
185			}
186			if tt.sending && tt.sendingLabel != "" {
187				if !strings.Contains(output, tt.sendingLabel) {
188					t.Errorf("expected output to contain sending label %q", tt.sendingLabel)
189				}
190			}
191			if tt.mode == InputModeNone && !tt.sending && tt.width >= 40 {
192				if !strings.Contains(output, "Press") || !strings.Contains(output, "message") {
193					t.Error("expected output to contain placeholder prompt")
194				}
195			}
196		})
197	}
198}
199
200// TestInputPromptUpdate verifies input updates propagate correctly.
201func TestInputPromptUpdate(t *testing.T) {
202	input := NewInputModel()
203	input.SetMode(InputModeBroadcast, "")
204
205	keyMsg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("a")}
206	updated, _ := input.Update(keyMsg)
207
208	if updated.Value() != "a" {
209		t.Errorf("value = %q, want 'a'", updated.Value())
210	}
211	if updated.Mode() != InputModeBroadcast {
212		t.Errorf("mode = %v, want InputModeBroadcast", updated.Mode())
213	}
214}
215
216// TestInputPromptFocus verifies focus command is returned.
217func TestInputPromptFocus(t *testing.T) {
218	input := NewInputModel()
219	cmd := input.Focus()
220	if cmd == nil {
221		t.Error("expected non-nil focus command")
222	}
223}
224
225// Golden file tests for input prompts
226func TestInputPrompt(t *testing.T) {
227	defer setupGoldenTest(t)()
228
229	tests := []struct {
230		name     string
231		mode     InputMode
232		text     string
233		width    int
234		targetID string
235	}{
236		{"broadcast_empty", InputModeBroadcast, "", 60, ""},
237		{"broadcast_with_text", InputModeBroadcast, "Attack sector 7", 60, ""},
238		{"message_empty", InputModeMessage, "", 60, "mysis-alpha"},
239		{"message_with_text", InputModeMessage, "Check your cargo", 60, "mysis-beta"},
240		{"long_text", InputModeBroadcast, "This is a very long message that will exceed the normal display width and test the input component's handling of long text without wrapping issues.", 60, ""},
241		{"narrow_width", InputModeMessage, "Short", 30, "test"},
242		{"wide_width", InputModeBroadcast, "Wide terminal test message", 120, ""},
243	}
244
245	for _, tt := range tests {
246		t.Run(tt.name, func(t *testing.T) {
247			input := NewInputModel()
248			input.SetMode(tt.mode, tt.targetID)
249			input.SetWidth(tt.width)
250
251			if tt.text != "" {
252				input.textInput.SetValue(tt.text)
253			}
254
255			output := inputStyle.Width(tt.width - 2).Render(input.textInput.View())
256
257			t.Run("ANSI", func(t *testing.T) {
258				golden.RequireEqual(t, []byte(output))
259			})
260
261			t.Run("Stripped", func(t *testing.T) {
262				stripped := stripANSIForGolden(output)
263				golden.RequireEqual(t, []byte(stripped))
264			})
265		})
266	}
267}