xonecas.io
62159
Sy

symb / files

symb / internal / tui / modal / modal_test.go

3.9 KB
1package modal
2
3import (
4	"testing"
5
6	tea "charm.land/bubbletea/v2"
7)
8
9var testColors = Colors{Fg: "#ccc", Bg: "#1a1a2e", Dim: "#666", SelFg: "#fff", SelBg: "#444", Border: "#555"}
10
11func fruits(query string) []Item {
12	all := []Item{
13		{Name: "apple"},
14		{Name: "banana"},
15		{Name: "cherry"},
16	}
17	if query == "" {
18		return all
19	}
20	var out []Item
21	for _, it := range all {
22		for _, r := range query {
23			for _, c := range it.Name {
24				if r == c {
25					out = append(out, it)
26					goto next
27				}
28			}
29		}
30	next:
31	}
32	return out
33}
34
35func key(ch rune) tea.KeyPressMsg {
36	return tea.KeyPressMsg{Code: ch, Text: string(ch)}
37}
38
39func special(name string) tea.KeyPressMsg {
40	switch name {
41	case "enter":
42		return tea.KeyPressMsg{Code: tea.KeyEnter}
43	case "esc":
44		return tea.KeyPressMsg{Code: tea.KeyEscape}
45	case "up":
46		return tea.KeyPressMsg{Code: tea.KeyUp}
47	case "down":
48		return tea.KeyPressMsg{Code: tea.KeyDown}
49	case "backspace":
50		return tea.KeyPressMsg{Code: tea.KeyBackspace}
51	default:
52		return tea.KeyPressMsg{}
53	}
54}
55
56func TestEscapeCloses(t *testing.T) {
57	m := New(fruits, "> ", testColors)
58	a, _ := m.HandleMsg(special("esc"))
59	if _, ok := a.(ActionClose); !ok {
60		t.Fatalf("expected ActionClose, got %T", a)
61	}
62}
63
64func TestEnterSelectsFirst(t *testing.T) {
65	m := New(fruits, "> ", testColors)
66	a, _ := m.HandleMsg(special("enter"))
67	sel, ok := a.(ActionSelect)
68	if !ok {
69		t.Fatalf("expected ActionSelect, got %T", a)
70	}
71	if sel.Item.Name != "apple" {
72		t.Fatalf("expected apple, got %s", sel.Item.Name)
73	}
74}
75
76func TestDownThenEnterSelectsHighlighted(t *testing.T) {
77	m := New(fruits, "> ", testColors)
78	m.HandleMsg(special("down")) // enter list, selected=0
79	m.HandleMsg(special("down")) // selected=1
80	a, _ := m.HandleMsg(special("enter"))
81	sel, ok := a.(ActionSelect)
82	if !ok {
83		t.Fatalf("expected ActionSelect, got %T", a)
84	}
85	if sel.Item.Name != "banana" {
86		t.Fatalf("expected banana, got %s", sel.Item.Name)
87	}
88}
89
90func TestUpFromTopReturnsFocusToInput(t *testing.T) {
91	m := New(fruits, "> ", testColors)
92	m.HandleMsg(special("down")) // enter list
93	if !m.inList {
94		t.Fatal("expected inList=true")
95	}
96	m.HandleMsg(special("up")) // back to input
97	if m.inList {
98		t.Fatal("expected inList=false")
99	}
100}
101
102func TestTypingProducesDebounceCmd(t *testing.T) {
103	m := New(fruits, "> ", testColors)
104	_, cmd := m.HandleMsg(key('a'))
105	if cmd == nil {
106		t.Fatal("expected debounce cmd")
107	}
108	if string(m.input) != "a" {
109		t.Fatalf("expected input 'a', got %q", string(m.input))
110	}
111}
112
113func TestDebounceFiresSearch(t *testing.T) {
114	called := false
115	searchFn := func(q string) []Item {
116		if q == "x" {
117			called = true
118		}
119		return nil
120	}
121	m := New(searchFn, "> ", testColors)
122	// Type 'x'.
123	m.HandleMsg(key('x'))
124	seq := m.seq
125	// Fire matching debounce.
126	_, cmd := m.HandleMsg(debounceMsg{seq: seq})
127	if cmd != nil {
128		cmd() // execute the search command
129	}
130	if !called {
131		t.Fatal("expected search to be called")
132	}
133}
134
135func TestStaleDebounceIgnored(t *testing.T) {
136	callCount := 0
137	searchFn := func(q string) []Item {
138		if q != "" {
139			callCount++
140		}
141		return nil
142	}
143	m := New(searchFn, "> ", testColors)
144	m.HandleMsg(key('a'))
145	staleSeq := m.seq
146	m.HandleMsg(key('b')) // bumps seq
147	// Fire stale debounce — should be ignored.
148	m.HandleMsg(debounceMsg{seq: staleSeq})
149	if callCount != 0 {
150		t.Fatalf("expected 0 search calls for stale debounce, got %d", callCount)
151	}
152}
153
154func TestBackspaceRemovesChar(t *testing.T) {
155	m := New(fruits, "> ", testColors)
156	m.HandleMsg(key('a'))
157	m.HandleMsg(key('b'))
158	m.HandleMsg(special("backspace"))
159	if string(m.input) != "a" {
160		t.Fatalf("expected 'a', got %q", string(m.input))
161	}
162}
163
164func TestViewRenders(t *testing.T) {
165	m := New(fruits, "> ", testColors)
166	v := m.View(100, 40)
167	if v == "" {
168		t.Fatal("expected non-empty view")
169	}
170}
171
172func TestEmptyResultsEnterNoAction(t *testing.T) {
173	m := New(func(string) []Item { return nil }, "> ", testColors)
174	a, _ := m.HandleMsg(special("enter"))
175	if a != nil {
176		t.Fatalf("expected nil action, got %T", a)
177	}
178}