| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "math/rand/v2" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | tea "charm.land/bubbletea/v2" |
| 11 | "github.com/charmbracelet/colorprofile" |
| 12 | uv "github.com/charmbracelet/ultraviolet" |
| 13 | "github.com/charmbracelet/x/ansi" |
| 14 | "github.com/charmbracelet/x/vt" |
| 15 | |
| 16 | "reasonix/internal/control" |
| 17 | "reasonix/internal/event" |
| 18 | "reasonix/internal/i18n" |
| 19 | "reasonix/internal/provider" |
| 20 | ) |
| 21 | |
| 22 | type scrollFooterAPI struct{ control.SessionAPI } |
| 23 | |
| 24 | func (scrollFooterAPI) LastUsage() *provider.Usage { |
| 25 | return &provider.Usage{PromptTokens: 162500, TotalTokens: 166600, CacheHitTokens: 160000, CacheMissTokens: 2500, CompletionTokens: 4100} |
| 26 | } |
| 27 | |
| 28 | func (scrollFooterAPI) SessionCache() (int, int) { return 160000, 2500 } |
| 29 | |
| 30 | func (scrollFooterAPI) ContextSnapshot() (int, int) { return 166600, 1000000 } |
| 31 | |
| 32 | func newTerminalScrollTUI(t *testing.T, width int) chatTUI { |
| 33 | t.Helper() |
| 34 | m := newComposerMouseTestTUI(t, width, 30) |
| 35 | m.label = "deepseek-v4-flash" |
| 36 | m.ctrl.SetAutoApproveTools(true) |
| 37 | m.ctrl = scrollFooterAPI{m.ctrl} |
| 38 | m.todos = []event.Todo{ |
| 39 | {Content: "盘点 reverse 各实体 sprites 目录与对应的渲染绘制函数", Status: "completed"}, |
| 40 | {Content: "阅读原作实体脚本,确认贴图尺寸 / 染色 / 绘制来源", Status: "completed"}, |
| 41 | {Content: "扩展 devtools/port_assets.py:场景精灵清单 + 引用式漏洞检查", Status: "completed"}, |
| 42 | {Content: "改 render 层:draw_cores / draw_build_points 改用贴图", Status: "completed"}, |
| 43 | } |
| 44 | m = updateComposerMouseTestTUI(t, m, agentEventMsg(event.Event{Kind: event.Usage, Usage: m.ctrl.LastUsage()})) |
| 45 | // Vary wrapping and blank rows deterministically, including clusters whose |
| 46 | // terminal width can exceed the legacy wcwidth measurement at the margin. |
| 47 | rng := rand.New(rand.NewPCG(1, 2)) |
| 48 | samples := []string{ |
| 49 | "阅读原作实体脚本 · render_layer ", |
| 50 | "✅ 检查 👩🏽💻 测试 🇨🇳 路径", |
| 51 | "⚠️ ✅ ↔️ 🏳️🌈 1️⃣ é 世", |
| 52 | "some ASCII content mixed with 中文", |
| 53 | } |
| 54 | for i := range 80 { |
| 55 | text := fmt.Sprintf("%03d %s\n%s", i, strings.Repeat(samples[i%len(samples)], rng.IntN(10)+1), strings.Repeat("\n", rng.IntN(4))) |
| 56 | m = updateComposerMouseTestTUI(t, m, agentEventMsg(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: text})) |
| 57 | } |
| 58 | return m |
| 59 | } |
| 60 | |
| 61 | // A correct View is insufficient: a width disagreement can wrap the physical |
| 62 | // terminal without moving the renderer's cursor model. Later relative moves |
| 63 | // then overwrite or duplicate the pinned footer. Replay the actual output into |
| 64 | // a terminal, preserving both screens between frames, to catch that drift. |
| 65 | func TestTerminalScrollPreservesFooter(t *testing.T) { |
| 66 | defer restoreThemeForTest(activeColorProfile, activeCLITheme) |
| 67 | defer i18n.DetectLanguage("en") |
| 68 | activeColorProfile = colorprofile.TrueColor |
| 69 | configureCLITheme("dark") |
| 70 | i18n.DetectLanguage("zh") |
| 71 | for _, width := range []int{80, 120} { |
| 72 | for _, method := range []ansi.Method{ansi.WcWidth, ansi.GraphemeWidth} { |
| 73 | t.Run(fmt.Sprintf("width=%d/method=%d", width, method), func(t *testing.T) { |
| 74 | checkTerminalScrollFooter(t, width, method) |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func checkTerminalScrollFooter(t *testing.T, width int, method ansi.Method) { |
| 81 | t.Helper() |
| 82 | const height = 30 |
| 83 | m := newTerminalScrollTUI(t, width) |
| 84 | var output bytes.Buffer |
| 85 | renderer := uv.NewTerminalRenderer(&output, []string{"TERM=xterm-ghostty", "COLORTERM=truecolor"}) |
| 86 | renderer.SetColorProfile(colorprofile.TrueColor) |
| 87 | renderer.SetFullscreen(true) |
| 88 | renderer.SetScrollOptim(true) |
| 89 | renderer.SetRelativeCursor(false) |
| 90 | renderer.SetMapNewline(false) // Bubble Tea's raw PTY disables OPOST. |
| 91 | renderer.SetBackspace(true) |
| 92 | renderer.SetTabStops(width) |
| 93 | renderer.Resize(width, height) |
| 94 | renderer.Erase() |
| 95 | screen := uv.NewScreenBuffer(width, height) |
| 96 | // Match Bubble Tea's negotiated screen width method. Its renderer retains |
| 97 | // its default width method, including after a mode 2027 response. |
| 98 | screen.Method = method |
| 99 | terminal := vt.NewEmulator(width, height) |
| 100 | t.Cleanup(func() { _ = terminal.Close() }) |
| 101 | _, _ = terminal.WriteString(ansi.SetModeAltScreenSaveCursor) |
| 102 | if method == ansi.GraphemeWidth { |
| 103 | _, _ = terminal.WriteString(ansi.SetModeUnicodeCore) |
| 104 | } |
| 105 | for frame := range 161 { |
| 106 | if frame > 0 { |
| 107 | button := tea.MouseWheelUp |
| 108 | if (frame-1)%80 >= 40 { |
| 109 | button = tea.MouseWheelDown |
| 110 | } |
| 111 | m = updateComposerMouseTestTUI(t, m, tea.MouseWheelMsg{Button: button}) |
| 112 | } |
| 113 | view := m.View() |
| 114 | screen.Clear() |
| 115 | uv.NewStyledString(view.Content).Draw(screen, screen.Bounds()) |
| 116 | renderer.Render(screen.RenderBuffer) |
| 117 | if view.Cursor != nil { |
| 118 | renderer.MoveTo(view.Cursor.X, view.Cursor.Y) |
| 119 | } |
| 120 | if err := renderer.Flush(); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if _, err := terminal.Write(output.Bytes()); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | // Clusters clipped at the transcript's right margin may differ between |
| 127 | // width modes, but must never move the todo, composer, or status rows. |
| 128 | for y := m.transcriptHeight(); y < height; y++ { |
| 129 | got := terminalRowText(width, y, terminal.CellAt) |
| 130 | want := terminalRowText(width, y, screen.CellAt) |
| 131 | if got != want { |
| 132 | t.Fatalf("frame %d, pinned row %d:\ngot %q\nwant %q", frame, y, got, want) |
| 133 | } |
| 134 | } |
| 135 | output.Reset() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func terminalRowText(width, y int, cellAt func(int, int) *uv.Cell) string { |
| 140 | var text strings.Builder |
| 141 | for x := range width { |
| 142 | if cell := cellAt(x, y); cell != nil { |
| 143 | text.WriteString(cell.Content) |
| 144 | } else { |
| 145 | text.WriteByte(' ') |
| 146 | } |
| 147 | } |
| 148 | return strings.TrimRight(text.String(), " ") |
| 149 | } |
| 150 |