@@ -0,0 +1,602 @@
7+ "github.com/charmbracelet/glamour/v2/ansi"
8+ uv "github.com/charmbracelet/ultraviolet"
11+func TestRenderHelper(t *testing.T) {
13+ NewStringItem("1", "Item 1"),
14+ NewStringItem("2", "Item 2"),
15+ NewStringItem("3", "Item 3"),
24+ if len(output) == 0 {
25+ t.Error("expected non-empty output from Render()")
28+ // Check that output contains the items
29+ if !strings.Contains(output, "Item 1") {
30+ t.Error("expected output to contain 'Item 1'")
32+ if !strings.Contains(output, "Item 2") {
33+ t.Error("expected output to contain 'Item 2'")
35+ if !strings.Contains(output, "Item 3") {
36+ t.Error("expected output to contain 'Item 3'")
40+func TestRenderWithScrolling(t *testing.T) {
42+ NewStringItem("1", "Item 1"),
43+ NewStringItem("2", "Item 2"),
44+ NewStringItem("3", "Item 3"),
45+ NewStringItem("4", "Item 4"),
46+ NewStringItem("5", "Item 5"),
50+ l.SetSize(80, 2) // Small viewport
52+ // Initial render should show first 2 items
54+ if !strings.Contains(output, "Item 1") {
55+ t.Error("expected output to contain 'Item 1'")
57+ if !strings.Contains(output, "Item 2") {
58+ t.Error("expected output to contain 'Item 2'")
60+ if strings.Contains(output, "Item 3") {
61+ t.Error("expected output to NOT contain 'Item 3' in initial view")
64+ // Scroll down and render
68+ // Now should show items 3 and 4
69+ if strings.Contains(output, "Item 1") {
70+ t.Error("expected output to NOT contain 'Item 1' after scrolling")
72+ if !strings.Contains(output, "Item 3") {
73+ t.Error("expected output to contain 'Item 3' after scrolling")
75+ if !strings.Contains(output, "Item 4") {
76+ t.Error("expected output to contain 'Item 4' after scrolling")
80+func TestRenderEmptyList(t *testing.T) {
86+ t.Errorf("expected empty output for empty list, got: %q", output)
90+func TestRenderVsDrawConsistency(t *testing.T) {
92+ NewStringItem("1", "Item 1"),
93+ NewStringItem("2", "Item 2"),
99+ // Render using Render() method
100+ renderOutput := l.Render()
102+ // Render using Draw() method
103+ screen := uv.NewScreenBuffer(80, 10)
104+ area := uv.Rect(0, 0, 80, 10)
105+ l.Draw(&screen, area)
106+ drawOutput := screen.Render()
108+ // Trim any trailing whitespace for comparison
109+ renderOutput = strings.TrimRight(renderOutput, "\n")
110+ drawOutput = strings.TrimRight(drawOutput, "\n")
112+ // Both methods should produce the same output
113+ if renderOutput != drawOutput {
114+ t.Errorf("Render() and Draw() produced different outputs:\nRender():\n%q\n\nDraw():\n%q",
115+ renderOutput, drawOutput)
119+func BenchmarkRender(b *testing.B) {
120+ items := make([]Item, 100)
121+ for i := range items {
122+ items[i] = NewStringItem(string(rune(i)), "Item content here")
127+ l.Render() // Prime the buffer
130+ for i := 0; i < b.N; i++ {
135+func BenchmarkRenderWithScrolling(b *testing.B) {
136+ items := make([]Item, 1000)
137+ for i := range items {
138+ items[i] = NewStringItem(string(rune(i)), "Item content here")
143+ l.Render() // Prime the buffer
146+ for i := 0; i < b.N; i++ {
152+func TestStringItemCache(t *testing.T) {
153+ item := NewStringItem("1", "Test content")
155+ // First draw at width 80 should populate cache
156+ screen1 := uv.NewScreenBuffer(80, 5)
157+ area1 := uv.Rect(0, 0, 80, 5)
158+ item.Draw(&screen1, area1)
160+ if len(item.cache) != 1 {
161+ t.Errorf("expected cache to have 1 entry after first draw, got %d", len(item.cache))
163+ if _, ok := item.cache[80]; !ok {
164+ t.Error("expected cache to have entry for width 80")
167+ // Second draw at same width should reuse cache
168+ screen2 := uv.NewScreenBuffer(80, 5)
169+ area2 := uv.Rect(0, 0, 80, 5)
170+ item.Draw(&screen2, area2)
172+ if len(item.cache) != 1 {
173+ t.Errorf("expected cache to still have 1 entry after second draw, got %d", len(item.cache))
176+ // Draw at different width should add to cache
177+ screen3 := uv.NewScreenBuffer(40, 5)
178+ area3 := uv.Rect(0, 0, 40, 5)
179+ item.Draw(&screen3, area3)
181+ if len(item.cache) != 2 {
182+ t.Errorf("expected cache to have 2 entries after draw at different width, got %d", len(item.cache))
184+ if _, ok := item.cache[40]; !ok {
185+ t.Error("expected cache to have entry for width 40")
189+func TestWrappingItemHeight(t *testing.T) {
190+ // Short text that fits in one line
191+ item1 := NewWrappingStringItem("1", "Short")
192+ if h := item1.Height(80); h != 1 {
193+ t.Errorf("expected height 1 for short text, got %d", h)
196+ // Long text that wraps
197+ longText := "This is a very long line that will definitely wrap when constrained to a narrow width"
198+ item2 := NewWrappingStringItem("2", longText)
200+ // At width 80, should be fewer lines than width 20
201+ height80 := item2.Height(80)
202+ height20 := item2.Height(20)
204+ if height20 <= height80 {
205+ t.Errorf("expected more lines at narrow width (20: %d lines) than wide width (80: %d lines)",
209+ // Non-wrapping version should always be 1 line
210+ item3 := NewStringItem("3", longText)
211+ if h := item3.Height(20); h != 1 {
212+ t.Errorf("expected height 1 for non-wrapping item, got %d", h)
216+func TestMarkdownItemBasic(t *testing.T) {
217+ markdown := "# Hello\n\nThis is a **test**."
218+ item := NewMarkdownItem("1", markdown)
220+ if item.ID() != "1" {
221+ t.Errorf("expected ID '1', got '%s'", item.ID())
224+ // Test that height is calculated
225+ height := item.Height(80)
227+ t.Errorf("expected height >= 1, got %d", height)
231+ screen := uv.NewScreenBuffer(80, 10)
232+ area := uv.Rect(0, 0, 80, 10)
233+ item.Draw(&screen, area)
235+ // Should not panic and should render something
236+ rendered := screen.Render()
237+ if len(rendered) == 0 {
238+ t.Error("expected non-empty rendered output")
242+func TestMarkdownItemCache(t *testing.T) {
243+ markdown := "# Test\n\nSome content."
244+ item := NewMarkdownItem("1", markdown)
246+ // First render at width 80 should populate cache
247+ height1 := item.Height(80)
248+ if len(item.cache) != 1 {
249+ t.Errorf("expected cache to have 1 entry after first render, got %d", len(item.cache))
252+ // Second render at same width should reuse cache
253+ height2 := item.Height(80)
254+ if height1 != height2 {
255+ t.Errorf("expected consistent height, got %d then %d", height1, height2)
257+ if len(item.cache) != 1 {
258+ t.Errorf("expected cache to still have 1 entry, got %d", len(item.cache))
261+ // Render at different width should add to cache
263+ if len(item.cache) != 2 {
264+ t.Errorf("expected cache to have 2 entries after different width, got %d", len(item.cache))
268+func TestMarkdownItemMaxCacheWidth(t *testing.T) {
269+ markdown := "# Test\n\nSome content."
270+ item := NewMarkdownItem("1", markdown).WithMaxWidth(50)
272+ // Render at width 40 (below limit) - should cache at width 40
274+ if len(item.cache) != 1 {
275+ t.Errorf("expected cache to have 1 entry for width 40, got %d", len(item.cache))
278+ // Render at width 80 (above limit) - should cap to 50 and cache
280+ // Cache should have width 50 entry (capped from 80)
281+ if len(item.cache) != 2 {
282+ t.Errorf("expected cache to have 2 entries (40 and 50), got %d", len(item.cache))
284+ if _, ok := item.cache[50]; !ok {
285+ t.Error("expected cache to have entry for width 50 (capped from 80)")
288+ // Render at width 100 (also above limit) - should reuse cached width 50
289+ _ = item.Height(100)
290+ if len(item.cache) != 2 {
291+ t.Errorf("expected cache to still have 2 entries (reusing 50), got %d", len(item.cache))
295+func TestMarkdownItemWithStyleConfig(t *testing.T) {
296+ markdown := "# Styled\n\nContent with **bold** text."
298+ // Create a custom style config
299+ styleConfig := ansi.StyleConfig{
300+ Document: ansi.StyleBlock{
305+ item := NewMarkdownItem("1", markdown).WithStyleConfig(styleConfig)
307+ // Render should use the custom style
308+ height := item.Height(80)
310+ t.Errorf("expected height >= 1, got %d", height)
313+ // Draw should work without panic
314+ screen := uv.NewScreenBuffer(80, 10)
315+ area := uv.Rect(0, 0, 80, 10)
316+ item.Draw(&screen, area)
318+ rendered := screen.Render()
319+ if len(rendered) == 0 {
320+ t.Error("expected non-empty rendered output with custom style")
324+func TestMarkdownItemInList(t *testing.T) {
326+ NewMarkdownItem("1", "# First\n\nMarkdown item."),
327+ NewMarkdownItem("2", "# Second\n\nAnother item."),
328+ NewStringItem("3", "Regular string item"),
334+ // Should render without error
335+ output := l.Render()
336+ if len(output) == 0 {
337+ t.Error("expected non-empty output from list with markdown items")
340+ // Should contain content from markdown items
341+ if !strings.Contains(output, "First") {
342+ t.Error("expected output to contain 'First'")
344+ if !strings.Contains(output, "Second") {
345+ t.Error("expected output to contain 'Second'")
347+ if !strings.Contains(output, "Regular string item") {
348+ t.Error("expected output to contain 'Regular string item'")
352+func TestMarkdownItemHeightWithWidth(t *testing.T) {
353+ // Test that widths are capped to maxWidth
354+ markdown := "This is a paragraph with some text."
356+ item := NewMarkdownItem("1", markdown).WithMaxWidth(50)
358+ // At width 30 (below limit), should cache and render at width 30
359+ height30 := item.Height(30)
361+ t.Errorf("expected height >= 1, got %d", height30)
364+ // At width 100 (above maxWidth), should cap to 50 and cache
365+ height100 := item.Height(100)
367+ t.Errorf("expected height >= 1, got %d", height100)
370+ // Both should be cached (width 30 and capped width 50)
371+ if len(item.cache) != 2 {
372+ t.Errorf("expected cache to have 2 entries (30 and 50), got %d", len(item.cache))
374+ if _, ok := item.cache[30]; !ok {
375+ t.Error("expected cache to have entry for width 30")
377+ if _, ok := item.cache[50]; !ok {
378+ t.Error("expected cache to have entry for width 50 (capped from 100)")
382+func BenchmarkMarkdownItemRender(b *testing.B) {
383+ markdown := "# Heading\n\nThis is a paragraph with **bold** and *italic* text.\n\n- Item 1\n- Item 2\n- Item 3"
384+ item := NewMarkdownItem("1", markdown)
387+ screen := uv.NewScreenBuffer(80, 10)
388+ area := uv.Rect(0, 0, 80, 10)
389+ item.Draw(&screen, area)
392+ for i := 0; i < b.N; i++ {
393+ screen := uv.NewScreenBuffer(80, 10)
394+ area := uv.Rect(0, 0, 80, 10)
395+ item.Draw(&screen, area)
399+func BenchmarkMarkdownItemUncached(b *testing.B) {
400+ markdown := "# Heading\n\nThis is a paragraph with **bold** and *italic* text.\n\n- Item 1\n- Item 2\n- Item 3"
403+ for i := 0; i < b.N; i++ {
404+ item := NewMarkdownItem("1", markdown)
405+ screen := uv.NewScreenBuffer(80, 10)
406+ area := uv.Rect(0, 0, 80, 10)
407+ item.Draw(&screen, area)
411+func TestSpacerItem(t *testing.T) {
412+ spacer := NewSpacerItem("spacer1", 3)
415+ if spacer.ID() != "spacer1" {
416+ t.Errorf("expected ID 'spacer1', got %q", spacer.ID())
420+ if h := spacer.Height(80); h != 3 {
421+ t.Errorf("expected height 3, got %d", h)
424+ // Height should be constant regardless of width
425+ if h := spacer.Height(20); h != 3 {
426+ t.Errorf("expected height 3 for width 20, got %d", h)
429+ // Draw should not produce any visible content
430+ screen := uv.NewScreenBuffer(20, 3)
431+ area := uv.Rect(0, 0, 20, 3)
432+ spacer.Draw(&screen, area)
434+ output := screen.Render()
435+ // Should be empty (just spaces)
436+ for _, line := range strings.Split(output, "\n") {
437+ trimmed := strings.TrimSpace(line)
439+ t.Errorf("expected empty spacer output, got: %q", line)
444+func TestSpacerItemInList(t *testing.T) {
445+ // Create a list with items separated by spacers
447+ NewStringItem("1", "Item 1"),
448+ NewSpacerItem("spacer1", 1),
449+ NewStringItem("2", "Item 2"),
450+ NewSpacerItem("spacer2", 2),
451+ NewStringItem("3", "Item 3"),
457+ output := l.Render()
459+ // Should contain all three items
460+ if !strings.Contains(output, "Item 1") {
461+ t.Error("expected output to contain 'Item 1'")
463+ if !strings.Contains(output, "Item 2") {
464+ t.Error("expected output to contain 'Item 2'")
466+ if !strings.Contains(output, "Item 3") {
467+ t.Error("expected output to contain 'Item 3'")
470+ // Total height should be: 1 (item1) + 1 (spacer1) + 1 (item2) + 2 (spacer2) + 1 (item3) = 6
472+ if l.TotalHeight() != expectedHeight {
473+ t.Errorf("expected total height %d, got %d", expectedHeight, l.TotalHeight())
477+func TestSpacerItemNavigation(t *testing.T) {
478+ // Spacers should not be selectable (they're not focusable)
480+ NewStringItem("1", "Item 1"),
481+ NewSpacerItem("spacer1", 1),
482+ NewStringItem("2", "Item 2"),
488+ // Select first item
489+ l.SetSelectedIndex(0)
490+ if l.SelectedIndex() != 0 {
491+ t.Errorf("expected selected index 0, got %d", l.SelectedIndex())
494+ // Can select the spacer (it's a valid item, just not focusable)
495+ l.SetSelectedIndex(1)
496+ if l.SelectedIndex() != 1 {
497+ t.Errorf("expected selected index 1, got %d", l.SelectedIndex())
500+ // Can select item after spacer
501+ l.SetSelectedIndex(2)
502+ if l.SelectedIndex() != 2 {
503+ t.Errorf("expected selected index 2, got %d", l.SelectedIndex())
507+// Helper function to create a pointer to uint
508+func uintPtr(v uint) *uint {
512+func TestListDoesNotEatLastLine(t *testing.T) {
513+ // Create items that exactly fill the viewport
515+ NewStringItem("1", "Line 1"),
516+ NewStringItem("2", "Line 2"),
517+ NewStringItem("3", "Line 3"),
518+ NewStringItem("4", "Line 4"),
519+ NewStringItem("5", "Line 5"),
522+ // Create list with height exactly matching content (5 lines, no gaps)
527+ output := l.Render()
529+ // Count actual lines in output
530+ lines := strings.Split(strings.TrimRight(output, "\r\n"), "\r\n")
531+ actualLineCount := 0
532+ for _, line := range lines {
533+ if strings.TrimSpace(line) != "" {
538+ // All 5 items should be visible
539+ if !strings.Contains(output, "Line 1") {
540+ t.Error("expected output to contain 'Line 1'")
542+ if !strings.Contains(output, "Line 2") {
543+ t.Error("expected output to contain 'Line 2'")
545+ if !strings.Contains(output, "Line 3") {
546+ t.Error("expected output to contain 'Line 3'")
548+ if !strings.Contains(output, "Line 4") {
549+ t.Error("expected output to contain 'Line 4'")
551+ if !strings.Contains(output, "Line 5") {
552+ t.Error("expected output to contain 'Line 5'")
555+ if actualLineCount != 5 {
556+ t.Errorf("expected 5 lines with content, got %d", actualLineCount)
560+func TestListWithScrollDoesNotEatLastLine(t *testing.T) {
561+ // Create more items than viewport height
563+ NewStringItem("1", "Item 1"),
564+ NewStringItem("2", "Item 2"),
565+ NewStringItem("3", "Item 3"),
566+ NewStringItem("4", "Item 4"),
567+ NewStringItem("5", "Item 5"),
568+ NewStringItem("6", "Item 6"),
569+ NewStringItem("7", "Item 7"),
572+ // Viewport shows 3 items at a time
576+ // Need to render first to build the buffer and calculate total height
579+ // Now scroll to bottom
582+ output := l.Render()
584+ t.Logf("Output:\n%s", output)
585+ t.Logf("Offset: %d, Total height: %d", l.offset, l.TotalHeight())
587+ // Should show last 3 items: 5, 6, 7
588+ if !strings.Contains(output, "Item 5") {
589+ t.Error("expected output to contain 'Item 5'")
591+ if !strings.Contains(output, "Item 6") {
592+ t.Error("expected output to contain 'Item 6'")
594+ if !strings.Contains(output, "Item 7") {
595+ t.Error("expected output to contain 'Item 7'")
598+ // Should not show earlier items
599+ if strings.Contains(output, "Item 1") {
600+ t.Error("expected output to NOT contain 'Item 1' when scrolled to bottom")