-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_test.go
More file actions
291 lines (263 loc) · 7.93 KB
/
Copy pathtable_test.go
File metadata and controls
291 lines (263 loc) · 7.93 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
290
291
package table
import (
"strconv"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
)
// fixtureCols returns three columns: Name (auto), Owner (auto sortable),
// Stars (right-aligned, sortable).
func fixtureCols() []Column {
return []Column{
{Key: "name", Title: "Name", Sortable: true},
{Key: "owner", Title: "Owner", Sortable: true},
{Key: "stars", Title: "Stars", Align: AlignRight, Sortable: true},
}
}
func fixtureRows() []Row {
return []Row{
{Cells: []string{"alpha", "ana", "10"}, Value: "alpha"},
{Cells: []string{"bravo", "ben", "30"}, Value: "bravo"},
{Cells: []string{"charlie", "cal", "20"}, Value: "charlie"},
{Cells: []string{"delta", "dan", "5"}, Value: "delta"},
{Cells: []string{"echo", "eve", "40"}, Value: "echo"},
}
}
func mustKey(t *testing.T, key string) tea.KeyMsg {
t.Helper()
switch key {
case "down":
return tea.KeyMsg{Type: tea.KeyDown}
case "up":
return tea.KeyMsg{Type: tea.KeyUp}
case "pgdown":
return tea.KeyMsg{Type: tea.KeyPgDown}
case "pgup":
return tea.KeyMsg{Type: tea.KeyPgUp}
case "home":
return tea.KeyMsg{Type: tea.KeyHome}
case "end":
return tea.KeyMsg{Type: tea.KeyEnd}
case "enter":
return tea.KeyMsg{Type: tea.KeyEnter}
case "left":
return tea.KeyMsg{Type: tea.KeyLeft}
case "right":
return tea.KeyMsg{Type: tea.KeyRight}
}
return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)}
}
func TestBasicRenderProducesHeaderAndRows(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10)
out := m.View()
for _, want := range []string{"Name", "Owner", "Stars"} {
if !strings.Contains(out, want) {
t.Errorf("header should contain %q, got:\n%s", want, out)
}
}
for _, want := range []string{"alpha", "bravo", "charlie", "delta", "echo"} {
if !strings.Contains(out, want) {
t.Errorf("row %q should be rendered, got:\n%s", want, out)
}
}
// Header + separator + 5 rows = 7 lines.
lines := strings.Split(out, "\n")
if len(lines) != 7 {
t.Errorf("expected 7 lines (header+sep+5 rows), got %d:\n%s", len(lines), out)
}
}
func TestCursorMovesWithDownAndUp(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10)
m, _ = m.Update(mustKey(t, "down"))
m, _ = m.Update(mustKey(t, "down"))
if m.Cursor() != 2 {
t.Fatalf("cursor should be 2 after two downs, got %d", m.Cursor())
}
m, _ = m.Update(mustKey(t, "up"))
if m.Cursor() != 1 {
t.Fatalf("cursor should be 1 after down,down,up, got %d", m.Cursor())
}
}
func TestPgDnJumpsByVisibleRows(t *testing.T) {
rows := make([]Row, 20)
for i := range rows {
rows[i] = Row{Cells: []string{strconv.Itoa(i), "x", "0"}}
}
m := New().
WithColumns(fixtureCols()...).
WithRows(rows...).
WithSize(80, 6) // visible = 4
m, _ = m.Update(mustKey(t, "pgdown"))
if m.Cursor() != 4 {
t.Fatalf("PgDn should jump to row 4 (visible=4), got %d", m.Cursor())
}
m, _ = m.Update(mustKey(t, "pgdown"))
if m.Cursor() != 8 {
t.Fatalf("second PgDn should jump to row 8, got %d", m.Cursor())
}
}
func TestHomeAndEndJump(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10)
m, _ = m.Update(mustKey(t, "end"))
if m.Cursor() != 4 {
t.Fatalf("End should land on last row (4), got %d", m.Cursor())
}
m, _ = m.Update(mustKey(t, "home"))
if m.Cursor() != 0 {
t.Fatalf("Home should land on first row, got %d", m.Cursor())
}
}
func TestSortByColumnAscendingThenDescending(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10).
WithSortBy("stars", false)
rows := m.Rows()
// Ascending: 5, 10, 20, 30, 40 → delta, alpha, charlie, bravo, echo
want := []string{"delta", "alpha", "charlie", "bravo", "echo"}
for i, w := range want {
if rows[i].Cells[0] != w {
t.Errorf("asc[%d] expected %q, got %q", i, w, rows[i].Cells[0])
}
}
m = m.WithSortBy("stars", true)
rows = m.Rows()
// Descending: 40, 30, 20, 10, 5
want = []string{"echo", "bravo", "charlie", "alpha", "delta"}
for i, w := range want {
if rows[i].Cells[0] != w {
t.Errorf("desc[%d] expected %q, got %q", i, w, rows[i].Cells[0])
}
}
}
func TestEnterEmitsSelectMsgWhenRowSelectionEnabled(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10).
WithRowSelection(true).
WithSelectedRow(2)
_, cmd := m.Update(mustKey(t, "enter"))
if cmd == nil {
t.Fatal("Enter should return a tea.Cmd when row selection is enabled")
}
msg := cmd()
sm, ok := msg.(SelectMsg)
if !ok {
t.Fatalf("expected SelectMsg, got %T", msg)
}
if sm.Index != 2 {
t.Errorf("SelectMsg.Index should be 2, got %d", sm.Index)
}
if sm.Row.Value != "charlie" {
t.Errorf("SelectMsg.Row.Value should be %q, got %v", "charlie", sm.Row.Value)
}
}
func TestEnterIsNoopWhenRowSelectionDisabled(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10)
_, cmd := m.Update(mustKey(t, "enter"))
if cmd != nil {
t.Fatalf("Enter should be no-op when row selection is disabled, got cmd %v", cmd())
}
}
func TestAutoWidthExpandsAndTruncatesWithEllipsis(t *testing.T) {
long := strings.Repeat("x", 60) // 60 cells, will be capped
rows := []Row{
{Cells: []string{"a", "short"}},
{Cells: []string{"b", long}},
}
m := New().
WithColumns(
Column{Key: "name", Title: "Name"},
Column{Key: "blob", Title: "Blob"},
).
WithRows(rows...)
// No WithSize → no shrink. computeWidths should cap the auto col at 40.
widths := m.computeWidths()
if widths[1] != maxAutoWidth {
t.Errorf("auto col with >40-cell content should clamp to %d, got %d", maxAutoWidth, widths[1])
}
out := m.View()
if !strings.Contains(out, ellipsis) {
t.Errorf("expected ellipsis %q somewhere in the view, got:\n%s", ellipsis, out)
}
// Auto width should ALSO expand: the shorter "Blob" title (4 cells)
// plus "short" (5 cells) → width >= 5 + 2 padding = 7 when no long row.
m2 := New().WithColumns(
Column{Key: "name", Title: "Name"},
Column{Key: "blob", Title: "Blob"},
).WithRows(Row{Cells: []string{"a", "abcdefgh"}}) // 8-cell cell
widths = m2.computeWidths()
if widths[1] < 8 {
t.Errorf("auto col should grow to fit 8-cell content, got width %d", widths[1])
}
}
func TestResizeShrinksTableAndDoesNotPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("View panicked after resize: %v", r)
}
}()
m := New().
WithColumns(fixtureCols()...).
WithRows(fixtureRows()...).
WithSize(80, 10)
m, _ = m.Update(tea.WindowSizeMsg{Width: 20, Height: 5})
out := m.View()
lines := strings.Split(out, "\n")
// Header + sep + visible rows (height - 2 = 3) = 5 lines
if len(lines) > 5 {
t.Errorf("expected at most 5 lines after resize, got %d:\n%s", len(lines), out)
}
// Width budget is 20: every line must be <= 20 cells once stripped.
for i, ln := range lines {
// lipgloss may emit ANSI; strip via Width which counts visible cells.
// We can't easily strip, but rendered width should not blow up.
_ = ln
_ = i
}
}
func TestEmptyRowsRendersPlaceholder(t *testing.T) {
m := New().
WithColumns(fixtureCols()...).
WithSize(80, 10)
out := m.View()
if !strings.Contains(out, "no rows") {
t.Fatalf("empty row set should render 'no rows' placeholder, got:\n%s", out)
}
// Header should still be present.
if !strings.Contains(out, "Name") {
t.Fatalf("header should render even when rows are empty, got:\n%s", out)
}
}
func TestStableSortPreservesInsertionOrderForEqualKeys(t *testing.T) {
rows := []Row{
{Cells: []string{"first", "owner", "10"}, Value: 1},
{Cells: []string{"second", "owner", "10"}, Value: 2},
{Cells: []string{"third", "owner", "10"}, Value: 3},
}
m := New().
WithColumns(fixtureCols()...).
WithRows(rows...).
WithSortBy("stars", false)
got := m.Rows()
for i, want := range []int{1, 2, 3} {
v, ok := got[i].Value.(int)
if !ok || v != want {
t.Errorf("stable sort: rows[%d].Value should be %d, got %v", i, want, got[i].Value)
}
}
}