-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_test.go
More file actions
289 lines (262 loc) · 8.58 KB
/
Copy pathselect_test.go
File metadata and controls
289 lines (262 loc) · 8.58 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
package selectinput
import (
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/truffle-dev/glyph/components/theme"
)
func opts(labels ...string) []Option {
out := make([]Option, len(labels))
for i, l := range labels {
out[i] = Option{Label: l}
}
return out
}
func sample() []Option {
return []Option{
{Label: "claude-opus-4-7", Hint: "opus", Value: "opus-4-7"},
{Label: "claude-sonnet-4-6", Hint: "sonnet", Value: "sonnet-4-6"},
{Label: "claude-haiku-4-5", Hint: "haiku", Value: "haiku-4-5"},
{Label: "gpt-5", Hint: "gpt", Value: "gpt-5"},
{Label: "gpt-4o", Hint: "gpt", Value: "gpt-4o"},
}
}
func TestNew_DefaultsAreSafe(t *testing.T) {
s := New(theme.Default)
if _, ok := s.Selected(); ok {
t.Fatal("Selected should be false on empty Select")
}
// View should not panic on an empty Select.
out := s.View()
if out == "" {
t.Fatal("View should produce some output even when empty")
}
// Tiny sizes are clamped, not honoured verbatim.
s2 := New(theme.Default).WithSize(0, 0)
if _ = s2.View(); s2.height < 1 {
t.Fatalf("height should clamp to at least 1, got %d", s2.height)
}
}
func TestWithOptions_AndCursorClamps(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithSelected(99)
o, ok := s.Selected()
if !ok {
t.Fatal("Selected should be ok on non-empty Select")
}
if s.Cursor() != len(sample())-1 {
t.Fatalf("WithSelected(99) should clamp to %d, got %d", len(sample())-1, s.Cursor())
}
if o.Label != "gpt-4o" {
t.Fatalf("expected last option label gpt-4o, got %q", o.Label)
}
s2 := New(theme.Default).WithOptions(sample()).WithSelected(-5)
if s2.Cursor() != 0 {
t.Fatalf("WithSelected(-5) should clamp to 0, got %d", s2.Cursor())
}
}
func TestUpdate_ArrowKeys(t *testing.T) {
s := New(theme.Default).WithOptions(opts("a", "b", "c"))
// Up at 0 clamps.
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyUp})
if s.Cursor() != 0 {
t.Fatalf("Up at 0 should clamp, got %d", s.Cursor())
}
// Down advances.
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyDown})
if s.Cursor() != 1 {
t.Fatalf("Down should advance to 1, got %d", s.Cursor())
}
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyDown})
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyDown}) // already at last
if s.Cursor() != 2 {
t.Fatalf("Down past end should clamp, got %d", s.Cursor())
}
// Home / End.
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyHome})
if s.Cursor() != 0 {
t.Fatalf("Home should go to 0, got %d", s.Cursor())
}
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyEnd})
if s.Cursor() != 2 {
t.Fatalf("End should go to last, got %d", s.Cursor())
}
// j/k as runes when filter is off.
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")})
if s.Cursor() != 1 {
t.Fatalf("k should step back to 1, got %d", s.Cursor())
}
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
if s.Cursor() != 2 {
t.Fatalf("j should step forward to 2, got %d", s.Cursor())
}
}
func TestUpdate_Enter_EmitsSelectMsg(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithSelected(2)
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyEnter})
if cmd == nil {
t.Fatal("Enter should produce a tea.Cmd")
}
msg := cmd()
sel, ok := msg.(SelectMsg)
if !ok {
t.Fatalf("expected SelectMsg, got %T", msg)
}
if sel.Index != 2 {
t.Fatalf("expected Index 2, got %d", sel.Index)
}
if sel.Option.Value != "haiku-4-5" {
t.Fatalf("expected Value haiku-4-5, got %q", sel.Option.Value)
}
}
func TestUpdate_Tab_AlsoCommits(t *testing.T) {
s := New(theme.Default).WithOptions(sample())
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyTab})
if cmd == nil {
t.Fatal("Tab should produce a tea.Cmd")
}
if _, ok := cmd().(SelectMsg); !ok {
t.Fatalf("Tab should emit SelectMsg, got %T", cmd())
}
}
func TestUpdate_Esc_EmitsCancelMsg(t *testing.T) {
s := New(theme.Default).WithOptions(sample())
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyEsc})
if cmd == nil {
t.Fatal("Esc should produce a tea.Cmd")
}
if _, ok := cmd().(CancelMsg); !ok {
t.Fatalf("Esc should emit CancelMsg, got %T", cmd())
}
}
func TestUpdate_Enter_NoOpOnEmpty(t *testing.T) {
s := New(theme.Default)
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyEnter})
if cmd != nil {
t.Fatalf("Enter on empty Select should be a no-op, got %v", cmd())
}
}
func TestFilter_NarrowsOptions(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithFilter(true)
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("son")})
o, ok := s.Selected()
if !ok {
t.Fatal("filtered select should still have a selection")
}
if !strings.Contains(strings.ToLower(o.Label), "sonnet") {
t.Fatalf("expected a sonnet match, got %q", o.Label)
}
// Walking down stays within filtered set (only one match here).
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyDown})
o2, _ := s.Selected()
if o2.Label != o.Label {
t.Fatalf("down with one match should stay put, got %q", o2.Label)
}
// Multi-match filter: 'gpt' returns gpt-5 + gpt-4o.
s2 := New(theme.Default).WithOptions(sample()).WithFilter(true)
s2, _ = s2.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("gpt")})
s2, _ = s2.Update(tea.KeyMsg{Type: tea.KeyDown})
o3, _ := s2.Selected()
if o3.Label != "gpt-4o" {
t.Fatalf("expected second gpt match gpt-4o, got %q", o3.Label)
}
}
func TestFilter_NoMatch_CursorStillSafe(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithFilter(true)
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("zzz")})
o, ok := s.Selected()
if ok {
t.Fatalf("expected Selected to be false on no-match, got %+v", o)
}
if o.Label != "" || o.Value != "" {
t.Fatalf("expected zero Option on no-match, got %+v", o)
}
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyEnter})
if cmd != nil {
t.Fatalf("Enter on no-match should be a no-op, got %v", cmd())
}
}
func TestFilter_Backspace_NarrowsBack(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithFilter(true)
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("zz")})
if _, ok := s.Selected(); ok {
t.Fatal("expected no match after typing zz")
}
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyBackspace})
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyBackspace})
if s.filter != "" {
t.Fatalf("expected empty filter after two backspaces, got %q", s.filter)
}
if _, ok := s.Selected(); !ok {
t.Fatal("expected a selection after clearing filter")
}
}
func TestFilter_CtrlU_ClearsFilter(t *testing.T) {
s := New(theme.Default).WithOptions(sample()).WithFilter(true)
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("son")})
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyCtrlU})
if s.filter != "" {
t.Fatalf("Ctrl-U should clear the filter, got %q", s.filter)
}
}
func TestFilter_OffIgnoresRuneTyping(t *testing.T) {
s := New(theme.Default).WithOptions(sample())
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("xyz")})
if s.filter != "" {
t.Fatalf("filter-off Select should ignore runes, got %q", s.filter)
}
}
func TestView_RendersTitleAndOptions(t *testing.T) {
s := New(theme.Default).
WithOptions(sample()).
WithSize(40, 5).
WithTitle("Pick a model")
out := s.View()
if !strings.Contains(out, "Pick a model") {
t.Errorf("View should contain the title, got %q", out)
}
for _, want := range []string{"claude-opus-4-7", "opus", "claude-sonnet-4-6"} {
if !strings.Contains(out, want) {
t.Errorf("View should include %q, got %q", want, out)
}
}
if !strings.Contains(out, "›") {
t.Errorf("View should include a cursor marker, got %q", out)
}
}
func TestUpdate_IgnoresNonKeyMessages(t *testing.T) {
s := New(theme.Default).WithOptions(sample())
s2, cmd := s.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
if s2.Cursor() != s.Cursor() {
t.Fatalf("non-key msg should not move cursor")
}
if cmd != nil {
t.Fatal("non-key msg should not produce a cmd")
}
}
func TestSelectMsg_FallsBackToLabelWhenValueEmpty(t *testing.T) {
s := New(theme.Default).WithOptions([]Option{{Label: "no-value-here"}})
_, cmd := s.Update(tea.KeyMsg{Type: tea.KeyEnter})
if cmd == nil {
t.Fatal("Enter should emit a SelectMsg")
}
sel := cmd().(SelectMsg)
if sel.Option.Value != "no-value-here" {
t.Fatalf("expected Value to fall back to Label, got %q", sel.Option.Value)
}
}
func TestView_ScrollsWithCursor(t *testing.T) {
s := New(theme.Default).
WithOptions(opts("a", "b", "c", "d", "e", "f", "g", "h")).
WithSize(20, 3)
for i := 0; i < 5; i++ {
s, _ = s.Update(tea.KeyMsg{Type: tea.KeyDown})
}
out := s.View()
// Cursor is at 5 ("f"). Window of 3 means d/e/f should be visible
// and a/b/c hidden.
for _, want := range []string{"d", "e", "f"} {
if !strings.Contains(out, want) {
t.Errorf("scrolled view should include %q, got %q", want, out)
}
}
}