| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/charmbracelet/x/ansi" |
| 14 | ) |
| 15 | |
| 16 | func TestParseGitNumstat(t *testing.T) { |
| 17 | added, removed := parseGitNumstat("10\t2\ta.go\n-\t-\tasset.bin\n3\t0\tpath with spaces.go\n") |
| 18 | if added != 13 || removed != 2 { |
| 19 | t.Fatalf("parseGitNumstat = (+%d -%d), want (+13 -2)", added, removed) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestCountUntracked(t *testing.T) { |
| 24 | got := countUntracked(" M tracked.go\n?? new.go\n?? nested/\n") |
| 25 | if got != 2 { |
| 26 | t.Fatalf("countUntracked = %d, want 2", got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestGitStatusRender(t *testing.T) { |
| 31 | got := ansi.Strip(gitStatus{Repo: "mySkills", Branch: "main", Added: 15}.Render()) |
| 32 | if got != "mySkills@main +15 -0" { |
| 33 | t.Fatalf("Render = %q", got) |
| 34 | } |
| 35 | |
| 36 | got = ansi.Strip(gitStatus{Repo: "repo", Branch: "abc1234", Detached: true, Untracked: 2}.Render()) |
| 37 | if got != "repo@abc1234 ?2" { |
| 38 | t.Fatalf("detached Render = %q", got) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestGitStatusRenderRepoUsesSuppliedRepoStyle(t *testing.T) { |
| 43 | got := ansi.Strip(gitStatus{Repo: "repo", Branch: "main"}.RenderRepo("[repo]")) |
| 44 | if got != "[repo]@main" { |
| 45 | t.Fatalf("RenderRepo = %q, want styled repo name only", got) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestGitStatusRenderWithinCompactsRepoBeforeBranch(t *testing.T) { |
| 50 | status := gitStatus{Repo: "VeryLongDeepSeekReasonixWorkspace", Branch: "codex/cli-tui-status-row"} |
| 51 | |
| 52 | full := ansi.Strip(status.RenderWithin(80, statusAutoColor)) |
| 53 | if full != "VeryLongDeepSeekReasonixWorkspace@codex/cli-tui-status-row" { |
| 54 | t.Fatalf("wide RenderWithin = %q", full) |
| 55 | } |
| 56 | |
| 57 | got := ansi.Strip(status.RenderWithin(46, statusAutoColor)) |
| 58 | if ansi.StringWidth(got) > 46 { |
| 59 | t.Fatalf("compacted status width = %d, want <= 46: %q", ansi.StringWidth(got), got) |
| 60 | } |
| 61 | if !strings.Contains(got, "@codex/cli-tui-status-row") { |
| 62 | t.Fatalf("branch should stay intact while repo can be compacted: %q", got) |
| 63 | } |
| 64 | if !strings.Contains(got, "…") { |
| 65 | t.Fatalf("long repo should be compacted with ellipsis: %q", got) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestGitStatusRenderWithinKeepsDirtySuffix(t *testing.T) { |
| 70 | status := gitStatus{ |
| 71 | Repo: "VeryLongDeepSeekReasonixWorkspace", |
| 72 | Branch: "codex/cli-tui-status-row", |
| 73 | Added: 12, |
| 74 | Removed: 3, |
| 75 | Untracked: 4, |
| 76 | } |
| 77 | |
| 78 | got := ansi.Strip(status.RenderWithin(35, statusAutoColor)) |
| 79 | if ansi.StringWidth(got) > 35 { |
| 80 | t.Fatalf("compacted dirty status width = %d, want <= 35: %q", ansi.StringWidth(got), got) |
| 81 | } |
| 82 | if !strings.Contains(got, "+12 -3 ?4") { |
| 83 | t.Fatalf("dirty suffix should be preserved: %q", got) |
| 84 | } |
| 85 | if !strings.Contains(got, "@") || !strings.Contains(got, "…") { |
| 86 | t.Fatalf("identity should remain segmented and compacted: %q", got) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestLoadGitStatus(t *testing.T) { |
| 91 | if _, err := exec.LookPath("git"); err != nil { |
| 92 | t.Skip("git not found") |
| 93 | } |
| 94 | |
| 95 | root := t.TempDir() |
| 96 | runGitForTest(t, root, "init") |
| 97 | runGitForTest(t, root, "config", "user.email", "reasonix@example.invalid") |
| 98 | runGitForTest(t, root, "config", "user.name", "Reasonix Test") |
| 99 | writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\ntwo\n") |
| 100 | runGitForTest(t, root, "add", "tracked.txt") |
| 101 | runGitForTest(t, root, "commit", "-m", "initial") |
| 102 | runGitForTest(t, root, "branch", "-M", "main") |
| 103 | |
| 104 | writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\nchanged\nthree\n") |
| 105 | writeFileForTest(t, filepath.Join(root, "new.txt"), "untracked\n") |
| 106 | if err := os.Mkdir(filepath.Join(root, "subdir"), 0o755); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | |
| 110 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 111 | defer cancel() |
| 112 | status, err := loadGitStatus(ctx, filepath.Join(root, "subdir")) |
| 113 | if err != nil { |
| 114 | t.Fatalf("loadGitStatus: %v", err) |
| 115 | } |
| 116 | if status.Repo != filepath.Base(root) || status.Branch != "main" || status.Detached { |
| 117 | t.Fatalf("status identity = %+v", status) |
| 118 | } |
| 119 | if status.Added != 2 || status.Removed != 1 || status.Untracked != 1 { |
| 120 | t.Fatalf("status changes = (+%d -%d ?%d), want (+2 -1 ?1)", status.Added, status.Removed, status.Untracked) |
| 121 | } |
| 122 | if plain := ansi.Strip(status.Render()); !strings.Contains(plain, filepath.Base(root)+"@main") || !strings.Contains(plain, "+2 -1 ?1") { |
| 123 | t.Fatalf("rendered status = %q", plain) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestRunGitDisablesOptionalLocks(t *testing.T) { |
| 128 | if runtime.GOOS == "windows" { |
| 129 | t.Skip("uses a POSIX shell script fake git") |
| 130 | } |
| 131 | |
| 132 | bin := filepath.Join(t.TempDir(), "bin") |
| 133 | if err := os.Mkdir(bin, 0o755); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | fakeGit := filepath.Join(bin, "git") |
| 137 | if err := os.WriteFile(fakeGit, []byte("#!/bin/sh\nprintf '%s' \"$GIT_OPTIONAL_LOCKS\"\n"), 0o755); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 141 | |
| 142 | out, err := runGit(context.Background(), "", "status") |
| 143 | if err != nil { |
| 144 | t.Fatalf("runGit: %v", err) |
| 145 | } |
| 146 | if out != "0" { |
| 147 | t.Fatalf("GIT_OPTIONAL_LOCKS = %q, want 0", out) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func runGitForTest(t *testing.T, cwd string, args ...string) { |
| 152 | t.Helper() |
| 153 | cmd := exec.Command("git", args...) |
| 154 | cmd.Dir = cwd |
| 155 | if out, err := cmd.CombinedOutput(); err != nil { |
| 156 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func writeFileForTest(t *testing.T, path string, body string) { |
| 161 | t.Helper() |
| 162 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | } |
| 166 |