| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/charmbracelet/colorprofile" |
| 8 | |
| 9 | "github.com/charmbracelet/x/ansi" |
| 10 | |
| 11 | "reasonix/internal/event" |
| 12 | ) |
| 13 | |
| 14 | // TestDiffTabExpansion proves tab-indented code (Go) renders with no literal tabs |
| 15 | // and a bar that runs exactly to width — a tab has zero StringWidth but the |
| 16 | // terminal expands it, which would otherwise overflow the background bar. |
| 17 | func TestDiffTabExpansion(t *testing.T) { |
| 18 | defer func(prev colorprofile.Profile) { activeColorProfile = prev }(activeColorProfile) |
| 19 | activeColorProfile = colorprofile.ANSI256 |
| 20 | width := 40 |
| 21 | d := event.FileDiff{Diff: "@@ -1 +1 @@\n+\t\tresult := compute()\n", Added: 1} |
| 22 | for _, r := range diffBody(d, "x.go", width, 40) { |
| 23 | if strings.ContainsRune(r, '\t') { |
| 24 | t.Errorf("row keeps a literal tab (terminal overflows the bar): %q", r) |
| 25 | } |
| 26 | if strings.Contains(r, "result") && ansi.StringWidth(r) != width { |
| 27 | t.Errorf("bar width = %d, want %d: %q", ansi.StringWidth(r), width, r) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // TestRenderNarrowNoPanic guards the width math against tiny terminals. |
| 33 | func TestRenderNarrowNoPanic(t *testing.T) { |
| 34 | defer func(prev colorprofile.Profile) { activeColorProfile = prev }(activeColorProfile) |
| 35 | activeColorProfile = colorprofile.ANSI256 |
| 36 | d := event.FileDiff{Diff: "@@ -1 +1 @@\n-\told 你好\n+\tnew 世界\n", Added: 1, Removed: 1} |
| 37 | for _, w := range []int{1, 2, 3, 5, 8, 20} { |
| 38 | _ = diffBody(d, "x.go", w, 40) |
| 39 | _ = toolCard("bash", `{"command":"go test ./... 你好 long command"}`, w) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // TestDiffKeepsHeaderLikeContent proves a deleted "-- x" / added "++ y" line |
| 44 | // (which render as "--- x" / "+++ y") is kept, not mistaken for a file header. |
| 45 | func TestDiffKeepsHeaderLikeContent(t *testing.T) { |
| 46 | d := event.FileDiff{ |
| 47 | Diff: "--- a/q.sql\n+++ b/q.sql\n@@ -1,2 +1,2 @@\n-- old comment\n++ new tally\n", |
| 48 | Added: 1, |
| 49 | Removed: 1, |
| 50 | } |
| 51 | joined := strings.Join(diffBody(d, "q.sql", 80, 40), "\n") |
| 52 | if !strings.Contains(joined, "old comment") || !strings.Contains(joined, "new tally") { |
| 53 | t.Fatalf("header-like content was dropped:\n%s", joined) |
| 54 | } |
| 55 | } |
| 56 |