返回 DeepSeek-Reasonix
pinned_panels_test.go
根目录 / internal / cli / pinned_panels_test.go
1 package cli
2
3 import (
4 "bytes"
5 "fmt"
6 "strings"
7 "testing"
8
9 tea "charm.land/bubbletea/v2"
10 "github.com/charmbracelet/colorprofile"
11 uv "github.com/charmbracelet/ultraviolet"
12 "github.com/charmbracelet/x/ansi"
13 "github.com/charmbracelet/x/vt"
14
15 "reasonix/internal/event"
16 "reasonix/internal/i18n"
17 )
18
19 func longPanelTestTUI(t *testing.T) chatTUI {
20 t.Helper()
21 m := newTerminalScrollTUI(t, 80)
22 for i := range m.todos {
23 m.todos[i].Content = fmt.Sprintf("TODO %d %s END-%d", i, strings.Repeat("排查长路径和详细的执行步骤 ", 8), i)
24 }
25 m.input.SetValue("draft")
26 return updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 24})
27 }
28
29 func longPanelChooser() *chooser {
30 options := make([]event.AskOption, 5)
31 for i := range options {
32 options[i] = event.AskOption{Label: fmt.Sprintf("OPTION-%d", i), Description: strings.Repeat("详细说明和中文长路径 ", 20) + fmt.Sprintf(" DESC-END-%d", i)}
33 }
34 return newChooser(event.Ask{ID: "long", Questions: []event.AskQuestion{{ID: "one", Prompt: "选择后续步骤", Options: options}}})
35 }
36
37 func TestPinnedPanelsKeepFooterOnTerminal(t *testing.T) {
38 defer restoreThemeForTest(activeColorProfile, activeCLITheme)
39 defer i18n.DetectLanguage("en")
40 activeColorProfile = colorprofile.TrueColor
41 configureCLITheme("dark")
42 i18n.DetectLanguage("zh")
43 for _, method := range []ansi.Method{ansi.WcWidth, ansi.GraphemeWidth} {
44 t.Run(fmt.Sprintf("method=%d", method), func(t *testing.T) {
45 m := longPanelTestTUI(t)
46 replay := newPanelTerminalReplay(t, method)
47 for _, size := range [][2]int{{80, 24}, {60, 18}, {120, 40}, {80, 24}} {
48 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: size[0], Height: size[1]})
49 replay.check(t, m)
50 m.chooser = longPanelChooser()
51 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: size[0], Height: size[1]})
52 for range 5 {
53 replay.check(t, m)
54 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyDown})
55 }
56 m.chooser = nil
57 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: size[0], Height: size[1]})
58 replay.check(t, m)
59 }
60 })
61 }
62 }
63
64 func TestPinnedPanelScrollPreservesTextAndInput(t *testing.T) {
65 m := longPanelTestTUI(t)
66 input, cursor, transcript := m.input.Value(), m.input.Cursor(), m.viewport.YOffset()
67 var seen strings.Builder
68 for range 30 {
69 seen.WriteString(ansi.Strip(m.View().Content))
70 seen.WriteByte('\n')
71 m = updateComposerMouseTestTUI(t, m, tea.MouseWheelMsg{X: 2, Y: m.viewport.Height() + 1, Button: tea.MouseWheelDown})
72 }
73 for i := range 4 {
74 if !strings.Contains(seen.String(), fmt.Sprintf("END-%d", i)) {
75 t.Fatalf("todo %d could not be read by scrolling", i)
76 }
77 }
78 if m.input.Value() != input || *m.input.Cursor() != *cursor || m.viewport.YOffset() != transcript {
79 t.Fatal("panel scroll moved the draft, caret, or transcript")
80 }
81 if m.pinnedScroll.offset == 0 {
82 t.Fatal("long panel never scrolled")
83 }
84 before := m.pinnedScroll.offset
85 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyPgUp, Mod: tea.ModAlt})
86 if m.pinnedScroll.offset >= before || m.input.Value() != input {
87 t.Fatal("panel page-up did not scroll independently of the input")
88 }
89 before = m.pinnedScroll.offset
90 m = updateComposerMouseTestTUI(t, m, tea.MouseWheelMsg{X: 2, Y: 0, Button: tea.MouseWheelUp})
91 if m.pinnedScroll.offset != before || m.viewport.YOffset() >= transcript {
92 t.Fatal("wheel over transcript did not stay in the transcript")
93 }
94 }
95
96 func TestPinnedPanelFollowsChooserSelectionAndFreeText(t *testing.T) {
97 m := longPanelTestTUI(t)
98 m.chooser = longPanelChooser()
99 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 24})
100 for i := range 5 {
101 if !strings.Contains(ansi.Strip(m.View().Content), fmt.Sprintf("❯ %d. OPTION-%d", i+1, i)) {
102 t.Fatalf("selected option %d is off screen", i)
103 }
104 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyDown})
105 }
106 // Enter on the free-text row shrinks the panel viewport to make room for
107 // the live textarea. The same layout must position its real caret.
108 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyEnter})
109 if m.hideComposer() {
110 t.Fatal("chooser free-text input remained hidden")
111 }
112 x, y, ok := m.composerOrigin()
113 if !ok || x < 0 || y < 0 || y+m.input.Height() >= m.height {
114 t.Fatalf("composer is outside the terminal: %d,%d", x, y)
115 }
116 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: 'x', Text: "x"})
117 if m.input.Value() != "x" {
118 t.Fatal("free-text composer cannot receive input")
119 }
120 }
121
122 func TestPinnedPanelNativeScrollbackIsBoundedAndKeyboardScrollable(t *testing.T) {
123 m := longPanelTestTUI(t)
124 m.nativeScrollback = true
125 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 24})
126 view := m.View()
127 if view.AltScreen || strings.Count(view.Content, "\n")+1 > m.height {
128 t.Fatal("native scrollback bottom frame outgrew the terminal")
129 }
130 before := m.pinnedScroll.offset
131 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyPgDown, Mod: tea.ModAlt})
132 if m.pinnedScroll.offset <= before {
133 t.Fatal("native scrollback cannot scroll the long panel without a mouse")
134 }
135 }
136
137 func TestPinnedPanelRevealsSelectionAfterHeightShrink(t *testing.T) {
138 m := longPanelTestTUI(t)
139 m.chooser = longPanelChooser()
140 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 40})
141 for range 3 {
142 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyDown})
143 }
144 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 18})
145 if !strings.Contains(ansi.Strip(m.View().Content), "❯ 4. OPTION-3") {
146 t.Fatal("height-only shrink hid the selected option")
147 }
148 var seen strings.Builder
149 for range 25 {
150 seen.WriteString(ansi.Strip(m.View().Content))
151 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyPgDown, Mod: tea.ModAlt})
152 }
153 if !strings.Contains(seen.String(), "DESC-END-4") {
154 t.Fatal("long option description was discarded instead of scrollable")
155 }
156 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyUp})
157 if !strings.Contains(ansi.Strip(m.View().Content), "❯ 3. OPTION-2") {
158 t.Fatal("keyboard navigation did not reveal selection after manual scrolling")
159 }
160 }
161
162 func TestPinnedPanelFollowsElicitationFieldsAndSubmit(t *testing.T) {
163 m := longPanelTestTUI(t)
164 m.elicit = &elicitCard{message: strings.Repeat("Long question ", 30), fields: make([]elicitField, 8), values: make([]string, 8), bools: make([]bool, 8)}
165 for i := range m.elicit.fields {
166 m.elicit.fields[i] = elicitField{Label: fmt.Sprintf("FIELD-%d", i), Type: "boolean"}
167 }
168 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 18})
169 for i := range 8 {
170 if !strings.Contains(ansi.Strip(m.View().Content), fmt.Sprintf("› FIELD-%d", i)) {
171 t.Fatalf("elicitation field %d is off screen", i)
172 }
173 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyDown})
174 }
175 if !strings.Contains(ansi.Strip(m.View().Content), " ["+i18n.M.ElicitSubmit+"]") {
176 t.Fatal("elicitation Submit is off screen")
177 }
178 }
179
180 func TestPinnedPanelIgnoresTodoSelectionPrefixes(t *testing.T) {
181 for _, marker := range []string{"› ", "❯ ", "▶ "} {
182 t.Run(marker, func(t *testing.T) {
183 m := longPanelTestTUI(t)
184 // A continuation line is displayed without the todo status icon.
185 // Its content must never become the panel's selected row.
186 m.todos[0].Content = marker + "ordinary todo\n" + marker + "not selected\n" + strings.Repeat("detail\n", 30)
187 m.todos[3].Content = "ACTIVE-TASK"
188 m.todos[3].Status = "in_progress"
189 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 18})
190 p := m.pinnedPanelView()
191 if !strings.Contains(ansi.Strip(p.lines[p.focus]), "ACTIVE-TASK") || !strings.Contains(ansi.Strip(m.View().Content), "ACTIVE-TASK") {
192 t.Fatalf("todo text stole focus from the active task:\n%s", ansi.Strip(m.View().Content))
193 }
194 // Wording changes must not cancel a deliberate manual scroll.
195 m.scrollPinnedPanel(-p.offset)
196 m.todos[3].Content = "RENAMED-TASK"
197 if m.pinnedPanelView().offset != 0 {
198 t.Fatal("renaming the active task reset manual scrolling")
199 }
200 })
201 }
202 }
203
204 func TestPinnedPanelFollowsWindowedSelectionAtSameRow(t *testing.T) {
205 m := longPanelTestTUI(t)
206 m.todos = nil
207 m.quickPick = &quickPicker{title: "Models", selected: 8}
208 for i := range 24 {
209 m.quickPick.items = append(m.quickPick.items, quickPickerItem{ID: fmt.Sprint(i), Label: "Same label"})
210 }
211 m = updateComposerMouseTestTUI(t, m, tea.WindowSizeMsg{Width: 80, Height: 13})
212 before := m.pinnedPanelContent().focus
213 m.scrollPinnedPanel(-1000)
214 m = updateComposerMouseTestTUI(t, m, tea.KeyPressMsg{Code: tea.KeyDown})
215 p := m.pinnedPanelView()
216 if p.focus != before {
217 t.Fatal("fixture did not keep the selected item on the same row")
218 }
219 if !strings.Contains(ansi.Strip(p.render(m.width)), "❯ 10. Same label") {
220 t.Fatal("selection change at the same row did not restore focus after manual scrolling")
221 }
222 }
223
224 type panelTerminalReplay struct {
225 output bytes.Buffer
226 renderer *uv.TerminalRenderer
227 screen uv.ScreenBuffer
228 terminal *vt.Emulator
229 }
230
231 func newPanelTerminalReplay(t *testing.T, method ansi.Method) *panelTerminalReplay {
232 t.Helper()
233 r := &panelTerminalReplay{screen: uv.NewScreenBuffer(80, 24), terminal: vt.NewEmulator(80, 24)}
234 t.Cleanup(func() { _ = r.terminal.Close() })
235 r.screen.Method = method
236 r.renderer = uv.NewTerminalRenderer(&r.output, []string{"TERM=xterm-ghostty", "COLORTERM=truecolor"})
237 r.renderer.SetFullscreen(true)
238 r.renderer.SetColorProfile(colorprofile.TrueColor)
239 r.renderer.SetScrollOptim(true)
240 r.renderer.SetRelativeCursor(false)
241 r.renderer.SetMapNewline(false)
242 r.renderer.Resize(80, 24)
243 r.renderer.Erase()
244 _, _ = r.terminal.WriteString(ansi.SetModeAltScreenSaveCursor)
245 if method == ansi.GraphemeWidth {
246 _, _ = r.terminal.WriteString(ansi.SetModeUnicodeCore)
247 }
248 return r
249 }
250
251 func (r *panelTerminalReplay) check(t *testing.T, m chatTUI) {
252 t.Helper()
253 view := m.View()
254 if rows := strings.Count(view.Content, "\n") + 1; rows != m.height {
255 t.Fatalf("%dx%d terminal has %d frame rows", m.width, m.height, rows)
256 }
257 r.terminal.Resize(m.width, m.height)
258 r.renderer.Resize(m.width, m.height)
259 r.screen.Resize(m.width, m.height)
260 r.screen.Clear()
261 uv.NewStyledString(view.Content).Draw(r.screen, r.screen.Bounds())
262 r.renderer.Render(r.screen.RenderBuffer)
263 if view.Cursor != nil {
264 r.renderer.MoveTo(view.Cursor.X, view.Cursor.Y)
265 }
266 if err := r.renderer.Flush(); err != nil {
267 t.Fatal(err)
268 }
269 _, _ = r.terminal.Write(r.output.Bytes())
270 r.output.Reset()
271 for y := m.transcriptHeight(); y < m.height; y++ {
272 got, want := terminalRowText(m.width, y, r.terminal.CellAt), terminalRowText(m.width, y, r.screen.CellAt)
273 if got != want {
274 t.Fatalf("pinned row %d drifted:\ngot %q\nwant %q", y, got, want)
275 }
276 }
277 if !strings.Contains(terminalRowText(m.width, m.height-1, r.terminal.CellAt), "166.6K") {
278 t.Fatal("context status was displaced from the terminal")
279 }
280 }
281
281 lines GO