-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_test.go
More file actions
467 lines (396 loc) · 11.5 KB
/
filter_test.go
File metadata and controls
467 lines (396 loc) · 11.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package glyph
import (
"fmt"
"testing"
)
func TestFilterBasic(t *testing.T) {
items := []string{"alpha", "bravo", "charlie", "delta", "echo"}
f := NewFilter(&items, func(s *string) string { return *s })
t.Run("initial state has all items", func(t *testing.T) {
if f.Len() != 5 {
t.Fatalf("expected 5 items, got %d", f.Len())
}
if f.Active() {
t.Error("should not be active with no query")
}
})
t.Run("filter narrows results", func(t *testing.T) {
f.Update("av")
if !f.Active() {
t.Error("should be active with query")
}
if f.Len() != 1 {
t.Fatalf("expected 1 match for 'av', got %d", f.Len())
}
if *f.Items[0] != "bravo" {
t.Errorf("expected bravo, got %s", *f.Items[0])
}
})
t.Run("original maps back to source", func(t *testing.T) {
orig := f.Original(0)
if orig == nil {
t.Fatal("Original returned nil")
}
if *orig != "bravo" {
t.Errorf("expected bravo, got %s", *orig)
}
if f.OriginalIndex(0) != 1 {
t.Errorf("expected original index 1, got %d", f.OriginalIndex(0))
}
})
t.Run("reset restores all items", func(t *testing.T) {
f.Reset()
if f.Len() != 5 {
t.Fatalf("expected 5 items after reset, got %d", f.Len())
}
if f.Active() {
t.Error("should not be active after reset")
}
})
t.Run("empty query resets", func(t *testing.T) {
f.Update("av")
f.Update("")
if f.Len() != 5 {
t.Fatalf("expected 5 items after empty query, got %d", f.Len())
}
})
t.Run("no matches returns empty", func(t *testing.T) {
f.Update("zzz")
if f.Len() != 0 {
t.Errorf("expected 0 matches, got %d", f.Len())
}
})
t.Run("same query is no-op", func(t *testing.T) {
f.Update("zzz") // already set
if f.Query() != "zzz" {
t.Errorf("expected query 'zzz', got %q", f.Query())
}
})
}
func TestFilterStruct(t *testing.T) {
type profile struct {
name string
service string
}
items := []profile{
{"heap-2024-01-01", "api-gateway"},
{"goroutine-2024-01-01", "api-gateway"},
{"heap-2024-01-02", "auth-service"},
{"cpu-2024-01-01", "payment-service"},
}
f := NewFilter(&items, func(p *profile) string { return p.name + " " + p.service })
t.Run("filter by service", func(t *testing.T) {
f.Update("gateway")
if f.Len() != 2 {
t.Fatalf("expected 2 matches, got %d", f.Len())
}
})
t.Run("filter by name and service", func(t *testing.T) {
f.Update("heap auth")
if f.Len() != 1 {
t.Fatalf("expected 1 match, got %d", f.Len())
}
if f.Items[0].name != "heap-2024-01-02" {
t.Errorf("expected heap-2024-01-02, got %s", f.Items[0].name)
}
})
t.Run("original points into source slice", func(t *testing.T) {
f.Update("payment")
if f.Len() != 1 {
t.Fatalf("expected 1 match, got %d", f.Len())
}
orig := f.Original(0)
if orig == nil {
t.Fatal("Original returned nil")
}
// verify it's a pointer into the original slice
if &items[3] != orig {
t.Error("Original should return pointer into source slice")
}
})
}
func TestFilterItemsReflectSourceMutations(t *testing.T) {
type task struct {
name string
status string
}
items := []task{
{"deploy", "pending"},
{"build", "pending"},
{"test", "pending"},
}
f := NewFilter(&items, func(t *task) string { return t.name + " " + t.status })
// no filter active — all items visible
f.Reset()
if f.Len() != 3 {
t.Fatalf("expected 3 items, got %d", f.Len())
}
// mutate the source in-place
items[0].status = "running"
items[2].status = "done"
// Items should reflect the mutation WITHOUT calling Refresh()
if f.Items[0].status != "running" {
t.Errorf("expected Items[0].status='running' (live from source), got %q", f.Items[0].status)
}
if f.Items[2].status != "done" {
t.Errorf("expected Items[2].status='done' (live from source), got %q", f.Items[2].status)
}
// also verify with an active filter
f.Update("deploy")
if f.Len() != 1 {
t.Fatalf("expected 1 match, got %d", f.Len())
}
// mutate again after filtering
items[0].status = "complete"
if f.Items[0].status != "complete" {
t.Errorf("expected filtered Items[0].status='complete' (live from source), got %q", f.Items[0].status)
}
}
func TestFilterOriginalBounds(t *testing.T) {
items := []string{"a", "b", "c"}
f := NewFilter(&items, func(s *string) string { return *s })
if f.Original(-1) != nil {
t.Error("negative index should return nil")
}
if f.Original(100) != nil {
t.Error("out of bounds index should return nil")
}
if f.OriginalIndex(-1) != -1 {
t.Error("negative index should return -1")
}
if f.OriginalIndex(100) != -1 {
t.Error("out of bounds index should return -1")
}
}
func TestFilterRanking(t *testing.T) {
items := []string{
"xyzabcxyz", // abc scattered/embedded
"abc", // exact match, should rank highest
"xxabcxxxxx", // abc present but longer
}
f := NewFilter(&items, func(s *string) string { return *s })
f.Update("abc")
if f.Len() != 3 {
t.Fatalf("expected 3 matches, got %d", f.Len())
}
// best match should be "abc" (shortest, exact)
if *f.Items[0] != "abc" {
t.Errorf("expected 'abc' as top result, got %q", *f.Items[0])
}
}
func TestFilterSourceChanges(t *testing.T) {
items := []string{"alpha", "bravo"}
f := NewFilter(&items, func(s *string) string { return *s })
// add items to source
items = append(items, "charlie")
f.Reset()
if f.Len() != 2 {
// f.source still points to old slice header since items was reassigned
// this is expected, source is a *[]T so we need to update through the pointer
}
// proper way: mutate through pointer
items2 := []string{"alpha", "bravo"}
f2 := NewFilter(&items2, func(s *string) string { return *s })
items2 = append(items2, "charlie")
f2.Reset()
// items2 may or may not have reallocated, but f2.source points at items2
if f2.Len() != len(items2) {
t.Errorf("expected %d items, got %d", len(items2), f2.Len())
}
}
func TestFilterListClampsSelectionOnSync(t *testing.T) {
items := []string{
"Go", "Rust", "Python", "JavaScript", "TypeScript",
"Ruby", "Java", "C", "C++", "C#",
}
fl := FilterList(&items, func(s *string) string { return *s })
// navigate down several times (simulating j presses)
fl.list.SetIndex(7)
if fl.list.Index() != 7 {
t.Fatalf("expected index 7, got %d", fl.list.Index())
}
// simulate typing "o", triggers onChange which calls sync()
fl.input.SetValue("o")
fl.sync()
if fl.Filter().Len() != 2 {
t.Fatalf("expected 2 filtered items, got %d", fl.Filter().Len())
}
// selection must be clamped to last valid index
if fl.list.Index() != 1 {
t.Errorf("expected selection clamped to 1, got %d", fl.list.Index())
}
if sel := fl.Selected(); sel == nil {
t.Error("Selected() returned nil after clamp")
}
}
func TestFilterListClampsToZeroOnEmpty(t *testing.T) {
items := []string{"Go", "Rust", "Python"}
fl := FilterList(&items, func(s *string) string { return *s })
fl.list.SetIndex(2)
fl.input.SetValue("zzz")
fl.sync()
if fl.Filter().Len() != 0 {
t.Fatalf("expected 0 filtered items, got %d", fl.Filter().Len())
}
if fl.list.Index() != 0 {
t.Errorf("expected selection 0 on empty list, got %d", fl.list.Index())
}
}
func TestFilterListCompilesAsTemplateTree(t *testing.T) {
items := []string{"a", "b", "c"}
fl := FilterList(&items, func(s *string) string { return *s }).
Placeholder("search...").
MaxVisible(10).
Render(func(s *string) any { return Text(s) })
tmpl := Build(VBox(fl))
if len(tmpl.pendingBindings) < 2 {
t.Errorf("expected at least 2 nav bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingTIB == nil {
t.Error("expected text input binding to be set")
}
}
func TestFilterListSelectedMapsToOriginal(t *testing.T) {
items := []string{"Go", "Rust", "Python", "JavaScript"}
fl := FilterList(&items, func(s *string) string { return *s })
fl.input.SetValue("o")
fl.sync()
// should have Go and Python
if fl.Filter().Len() != 2 {
t.Fatalf("expected 2 items, got %d", fl.Filter().Len())
}
// select second filtered item (Python)
fl.list.SetIndex(1)
sel := fl.Selected()
if sel == nil {
t.Fatal("Selected() returned nil")
}
if *sel != "Python" {
t.Errorf("expected Python, got %s", *sel)
}
// verify it maps back to the original slice
idx := fl.SelectedIndex()
if idx != 2 {
t.Errorf("expected original index 2, got %d", idx)
}
}
func TestStreamWriterWrite(t *testing.T) {
var items []string
fl := FilterList(&items, func(s *string) string { return *s }).
Render(func(s *string) any { return Text(s) })
renders := 0
w := fl.Stream(func() { renders++ })
w.Write("alpha")
w.Write("bravo")
w.WriteAll([]string{"charlie", "delta"})
w.Close()
if fl.Filter().Len() != 4 {
t.Fatalf("expected 4 items, got %d", fl.Filter().Len())
}
if renders < 3 {
t.Errorf("expected at least 3 render calls, got %d", renders)
}
}
func TestStreamWriterWithFilter(t *testing.T) {
var items []string
fl := FilterList(&items, func(s *string) string { return *s }).
Render(func(s *string) any { return Text(s) })
// apply filter before streaming
fl.input.SetValue("o")
fl.sync()
w := fl.Stream(func() {})
w.Write("Go") // matches "o"
w.Write("Rust") // no match
w.Write("Python") // matches "o"
w.Write("Odin") // matches "o"
w.Close()
if fl.Filter().Len() != 3 {
t.Fatalf("expected 3 filtered items, got %d", fl.Filter().Len())
}
if len(items) != 4 {
t.Fatalf("expected 4 total items in source, got %d", len(items))
}
}
func TestStreamLifecycle(t *testing.T) {
var items []string
fl := FilterList(&items, func(s *string) string { return *s }).
Render(func(s *string) any { return Text(s) })
if fl.streaming() {
t.Fatal("should not be streaming before Stream called")
}
w := fl.Stream(func() {})
if !fl.streaming() {
t.Fatal("expected streaming=true after Stream called")
}
w.Write("item")
w.Close()
if fl.streaming() {
t.Error("expected streaming=false after Close")
}
// Close is idempotent
w.Close()
if fl.streaming() {
t.Error("expected streaming=false after second Close")
}
}
func TestStreamCounterUpdates(t *testing.T) {
var items []string
fl := FilterList(&items, func(s *string) string { return *s }).
Render(func(s *string) any { return Text(s) })
fl.input.SetValue("a")
fl.sync()
w := fl.Stream(func() {})
w.Write("alpha") // matches "a"
w.Write("bravo") // matches "a"
w.Write("echo") // no match
if fl.counterMatch != 2 {
t.Errorf("expected counterMatch=2, got %d", fl.counterMatch)
}
if fl.counterTotal != 3 {
t.Errorf("expected counterTotal=3, got %d", fl.counterTotal)
}
w.Close()
}
func BenchmarkFilterUpdate(b *testing.B) {
items := make([]string, 1000)
for i := range items {
items[i] = "prefix/service-name/instance-id/heap/profile_" + string(rune('a'+i%26)) + ".pb.gz"
}
f := NewFilter(&items, func(s *string) string { return *s })
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.lastQuery = "" // force re-filter
f.Update("heap service")
}
}
func BenchmarkStreamWriter(b *testing.B) {
for _, size := range []int{100, 1000, 5000, 10000} {
prebuilt := make([]string, size)
for i := range prebuilt {
prebuilt[i] = fmt.Sprintf("item-%d-service-name", i)
}
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
var items []string
fl := FilterList(&items, func(s *string) string { return *s }).
Render(func(s *string) any { return Text(s) })
fl.input.SetValue("a")
fl.sync()
// warm internal slices to steady-state capacity
w := fl.Stream(func() {})
w.WriteAll(prebuilt)
w.Close()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// reset source to baseline
items = items[:0]
fl.refresh()
w := fl.Stream(func() {})
for j := 0; j < size; j++ {
w.Write(prebuilt[j])
}
w.Close()
}
})
}
}