|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + tea "github.com/charmbracelet/bubbletea" |
| 8 | +) |
| 9 | + |
| 10 | +// TestUpDownScrollTranscript verifies that ↑/↓ scroll the conversation when |
| 11 | +// the textarea cursor is at the corresponding edge of the input. |
| 12 | +func TestUpDownScrollTranscript(t *testing.T) { |
| 13 | + m := newTestModel() |
| 14 | + |
| 15 | + // Build a tall, markdown-heavy transcript so the viewport can scroll. |
| 16 | + md := "# Heading\n\nThis is **bold** and *italic*.\n\n```go\nfunc main() {}\n```\n\n- one\n- two\n- three\n\n" + strings.Repeat("More text. ", 30) |
| 17 | + for i := 0; i < 4; i++ { |
| 18 | + m.msgs = append(m.msgs, message{role: roleUser, content: "prompt"}) |
| 19 | + m.msgs = append(m.msgs, message{role: roleAsst, content: md, rendered: m.render(md)}) |
| 20 | + } |
| 21 | + m.refresh() |
| 22 | + |
| 23 | + if m.vp.TotalLineCount() <= m.vp.Height { |
| 24 | + t.Fatal("test transcript should be taller than the viewport") |
| 25 | + } |
| 26 | + if !m.vp.AtBottom() { |
| 27 | + t.Fatal("transcript should start pinned to the bottom") |
| 28 | + } |
| 29 | + bottom := m.vp.YOffset |
| 30 | + |
| 31 | + // With an empty single-line input, pressing Up should scroll the viewport. |
| 32 | + m.Update(key("up")) |
| 33 | + if m.vp.YOffset >= bottom { |
| 34 | + t.Errorf("up did not scroll transcript up: yoffset=%d, was=%d", m.vp.YOffset, bottom) |
| 35 | + } |
| 36 | + |
| 37 | + // And Down should return to the bottom. |
| 38 | + m.Update(key("down")) |
| 39 | + if !m.vp.AtBottom() { |
| 40 | + t.Errorf("down did not return transcript to bottom: yoffset=%d", m.vp.YOffset) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// TestUpDownEditMultiLine verifies that ↑/↓ still edit a multi-line input |
| 45 | +// when the cursor is not at the corresponding edge. |
| 46 | +func TestUpDownEditMultiLine(t *testing.T) { |
| 47 | + m := newTestModel() |
| 48 | + |
| 49 | + // Two-line input with the cursor on the first line. |
| 50 | + m.ta.Focus() |
| 51 | + m.ta.SetValue("line one\nline two") |
| 52 | + m.ta.CursorStart() |
| 53 | + m.ta.CursorUp() |
| 54 | + if m.ta.Line() != 0 { |
| 55 | + t.Fatalf("expected cursor on line 0, got %d", m.ta.Line()) |
| 56 | + } |
| 57 | + |
| 58 | + // Up from the first line should scroll, but with no transcript there is |
| 59 | + // nowhere to scroll; the viewport stays at top and the key is consumed. |
| 60 | + before := m.vp.YOffset |
| 61 | + m.Update(key("up")) |
| 62 | + if m.vp.YOffset != before { |
| 63 | + t.Errorf("up scrolled an empty transcript unexpectedly") |
| 64 | + } |
| 65 | + |
| 66 | + // Down from the first line of a two-line input should move the cursor down, |
| 67 | + // not scroll the transcript. |
| 68 | + m.Update(key("down")) |
| 69 | + if m.ta.Line() != 1 { |
| 70 | + t.Errorf("down should move cursor to line 1, got line %d", m.ta.Line()) |
| 71 | + } |
| 72 | + |
| 73 | + // Another down from the last line should scroll (empty transcript: no-op). |
| 74 | + m.Update(key("down")) |
| 75 | + if m.ta.Line() != 1 { |
| 76 | + t.Errorf("down from last line changed cursor unexpectedly to %d", m.ta.Line()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestMouseWheelScrollsTranscript verifies that wheel events scroll the |
| 81 | +// transcript once mouse reporting is enabled by the program. |
| 82 | +func TestMouseWheelScrollsTranscript(t *testing.T) { |
| 83 | + m := newTestModel() |
| 84 | + |
| 85 | + md := "# Section\n\n" + strings.Repeat("Paragraph. ", 40) |
| 86 | + for i := 0; i < 5; i++ { |
| 87 | + m.msgs = append(m.msgs, message{role: roleUser, content: "q"}) |
| 88 | + m.msgs = append(m.msgs, message{role: roleAsst, content: md, rendered: m.render(md)}) |
| 89 | + } |
| 90 | + m.refresh() |
| 91 | + |
| 92 | + if !m.vp.AtBottom() { |
| 93 | + t.Fatal("transcript should start at the bottom") |
| 94 | + } |
| 95 | + bottom := m.vp.YOffset |
| 96 | + |
| 97 | + _, _ = m.Update(tea.MouseMsg{Action: tea.MouseActionPress, Button: tea.MouseButtonWheelUp}) |
| 98 | + if m.vp.YOffset >= bottom { |
| 99 | + t.Errorf("mouse wheel up did not scroll transcript up: yoffset=%d, was=%d", m.vp.YOffset, bottom) |
| 100 | + } |
| 101 | + |
| 102 | + _, _ = m.Update(tea.MouseMsg{Action: tea.MouseActionPress, Button: tea.MouseButtonWheelDown}) |
| 103 | + if !m.vp.AtBottom() { |
| 104 | + t.Errorf("mouse wheel down did not return to bottom: yoffset=%d", m.vp.YOffset) |
| 105 | + } |
| 106 | +} |
0 commit comments