返回 DeepSeek-Reasonix
terminal_wrap_test.go
根目录 / internal / cli / terminal_wrap_test.go
1 package cli
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7
8 uv "github.com/charmbracelet/ultraviolet"
9 "github.com/charmbracelet/x/ansi"
10 )
11
12 func TestTerminalWrappingPreservesGraphemes(t *testing.T) {
13 for _, width := range []int{10, 40, 80, 120} {
14 for _, cluster := range []string{"1️⃣", "#️⃣", "*️⃣", "é", "👩🏽‍💻", "🇨🇳", "中文", "⚠️✅↔️🏳️‍🌈1️⃣é世"} {
15 t.Run(fmt.Sprintf("%d/%s", width, cluster), func(t *testing.T) {
16 plain := strings.Repeat(cluster+"X", 50)
17 styled := ansi.SetHyperlink("https://example.com") + "\x1b[31m" + plain + "\x1b[m" + ansi.ResetHyperlink()
18 for name, wrap := range map[string]func(string, int) string{
19 "transcript": wrapTranscript, "scrollback": clampWidth, "status": wrapStatusLine,
20 } {
21 t.Run(name, func(t *testing.T) {
22 wrapped := wrap(styled, width)
23 var restored strings.Builder
24 for row := range strings.SplitSeq(wrapped, "\n") {
25 if got := visibleWidth(row); got > width {
26 t.Fatalf("row occupies %d columns, limit %d: %q", got, width, row)
27 }
28 if ansi.Strip(row) != "" {
29 restored.WriteString(strings.TrimRight(ansi.Strip(row), " "))
30 }
31 // A width assertion alone can miss a split grapheme. Compare
32 // the actual decoded cells with every character in the row.
33 screen := uv.NewScreenBuffer(width, 1)
34 screen.Method = ansi.GraphemeWidth
35 uv.NewStyledString(row).Draw(screen, screen.Bounds())
36 if got, want := terminalRowText(width, 0, screen.CellAt), strings.TrimRight(ansi.Strip(row), " "); got != want {
37 t.Fatalf("screen lost content: got %q, want %q", got, want)
38 }
39 }
40 if restored.String() != plain {
41 t.Fatalf("wrapped text lost or altered a cluster:\n%q\nwant %q", restored.String(), plain)
42 }
43 })
44 }
45 })
46 }
47 }
48 }
49
50 func TestWrappedTranscriptBalancesStyleAndLinkAcrossKeycaps(t *testing.T) {
51 text := strings.Repeat("1️⃣X", 50)
52 wrapped := wrapTranscript(ansi.SetHyperlink("https://example.com")+"\x1b[31m"+text+"\x1b[m"+ansi.ResetHyperlink(), 40)
53 for row := range strings.SplitSeq(wrapped, "\n") {
54 screen := uv.NewScreenBuffer(40, 1)
55 screen.Method = ansi.GraphemeWidth
56 uv.NewStyledString(row).Draw(screen, screen.Bounds())
57 for x := range 40 {
58 cell := screen.CellAt(x, 0)
59 if cell == nil || cell.Content == "" || cell.Content == " " {
60 continue
61 }
62 if cell.Link.URL != "https://example.com" || cell.Style.Fg == nil {
63 t.Fatalf("wrapped glyph lost style or link: %+v", cell)
64 }
65 }
66 }
67 }
68
68 lines GO