| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/charmbracelet/x/ansi" |
| 14 | |
| 15 | "reasonix/internal/gitcmd" |
| 16 | ) |
| 17 | |
| 18 | func TestParseGitNumstat(t *testing.T) { |
| 19 | added, removed := parseGitNumstat("10\t2\ta.go\n-\t-\tasset.bin\n3\t0\tpath with spaces.go\n") |
| 20 | if added != 13 || removed != 2 { |
| 21 | t.Fatalf("parseGitNumstat = (+%d -%d), want (+13 -2)", added, removed) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestCountUntracked(t *testing.T) { |
| 26 | got := countUntracked(" M tracked.go\n?? new.go\n?? nested/\n") |
| 27 | if got != 2 { |
| 28 | t.Fatalf("countUntracked = %d, want 2", got) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestGitStatusRender(t *testing.T) { |
| 33 | got := ansi.Strip(gitStatus{Repo: "mySkills", Branch: "main", Added: 15}.Render()) |
| 34 | if got != "mySkills@main +15 -0" { |
| 35 | t.Fatalf("Render = %q", got) |
| 36 | } |
| 37 | |
| 38 | got = ansi.Strip(gitStatus{Repo: "repo", Branch: "abc1234", Detached: true, Untracked: 2}.Render()) |
| 39 | if got != "repo@abc1234 ?2" { |
| 40 | t.Fatalf("detached Render = %q", got) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestGitStatusRenderRepoUsesSuppliedRepoStyle(t *testing.T) { |
| 45 | got := ansi.Strip(gitStatus{Repo: "repo", Branch: "main"}.RenderRepo("[repo]")) |
| 46 | if got != "[repo]@main" { |
| 47 | t.Fatalf("RenderRepo = %q, want styled repo name only", got) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestGitStatusRenderWithinCompactsRepoBeforeBranch(t *testing.T) { |
| 52 | status := gitStatus{Repo: "VeryLongDeepSeekReasonixWorkspace", Branch: "codex/cli-tui-status-row"} |
| 53 | |
| 54 | full := ansi.Strip(status.RenderWithin(80, statusAutoColor)) |
| 55 | if full != "VeryLongDeepSeekReasonixWorkspace@codex/cli-tui-status-row" { |
| 56 | t.Fatalf("wide RenderWithin = %q", full) |
| 57 | } |
| 58 | |
| 59 | got := ansi.Strip(status.RenderWithin(46, statusAutoColor)) |
| 60 | if ansi.StringWidth(got) > 46 { |
| 61 | t.Fatalf("compacted status width = %d, want <= 46: %q", ansi.StringWidth(got), got) |
| 62 | } |
| 63 | if !strings.Contains(got, "@codex/cli-tui-status-row") { |
| 64 | t.Fatalf("branch should stay intact while repo can be compacted: %q", got) |
| 65 | } |
| 66 | if !strings.Contains(got, "…") { |
| 67 | t.Fatalf("long repo should be compacted with ellipsis: %q", got) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestGitStatusRenderWithinKeepsDirtySuffix(t *testing.T) { |
| 72 | status := gitStatus{ |
| 73 | Repo: "VeryLongDeepSeekReasonixWorkspace", |
| 74 | Branch: "codex/cli-tui-status-row", |
| 75 | Added: 12, |
| 76 | Removed: 3, |
| 77 | Untracked: 4, |
| 78 | } |
| 79 | |
| 80 | got := ansi.Strip(status.RenderWithin(35, statusAutoColor)) |
| 81 | if ansi.StringWidth(got) > 35 { |
| 82 | t.Fatalf("compacted dirty status width = %d, want <= 35: %q", ansi.StringWidth(got), got) |
| 83 | } |
| 84 | if !strings.Contains(got, "+12 -3 ?4") { |
| 85 | t.Fatalf("dirty suffix should be preserved: %q", got) |
| 86 | } |
| 87 | if !strings.Contains(got, "@") || !strings.Contains(got, "…") { |
| 88 | t.Fatalf("identity should remain segmented and compacted: %q", got) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestLoadGitStatus(t *testing.T) { |
| 93 | if _, err := exec.LookPath("git"); err != nil { |
| 94 | t.Skip("git not found") |
| 95 | } |
| 96 | |
| 97 | root := t.TempDir() |
| 98 | runGitForTest(t, root, "init") |
| 99 | runGitForTest(t, root, "config", "user.email", "reasonix@example.invalid") |
| 100 | runGitForTest(t, root, "config", "user.name", "Reasonix Test") |
| 101 | writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\ntwo\n") |
| 102 | runGitForTest(t, root, "add", "tracked.txt") |
| 103 | runGitForTest(t, root, "commit", "-m", "initial") |
| 104 | runGitForTest(t, root, "branch", "-M", "main") |
| 105 | |
| 106 | writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\nchanged\nthree\n") |
| 107 | writeFileForTest(t, filepath.Join(root, "new.txt"), "untracked\n") |
| 108 | if err := os.Mkdir(filepath.Join(root, "subdir"), 0o755); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | |
| 112 | // This checks Git semantics, not subprocess speed on a shared CI runner. |
| 113 | status, err := loadGitStatus(t.Context(), openedRepo(t, filepath.Join(root, "subdir"))) |
| 114 | if err != nil { |
| 115 | t.Fatalf("loadGitStatus: %v", err) |
| 116 | } |
| 117 | if status.Repo != filepath.Base(root) || status.Branch != "main" || status.Detached { |
| 118 | t.Fatalf("status identity = %+v", status) |
| 119 | } |
| 120 | if status.Added != 2 || status.Removed != 1 || status.Untracked != 1 { |
| 121 | t.Fatalf("status changes = (+%d -%d ?%d), want (+2 -1 ?1)", status.Added, status.Removed, status.Untracked) |
| 122 | } |
| 123 | if plain := ansi.Strip(status.Render()); !strings.Contains(plain, filepath.Base(root)+"@main") || !strings.Contains(plain, "+2 -1 ?1") { |
| 124 | t.Fatalf("rendered status = %q", plain) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestLoadGitStatusRejectsCanceledSnapshot(t *testing.T) { |
| 129 | for _, cancelAt := range []string{"symbolic-ref", "diff", "status"} { |
| 130 | t.Run(cancelAt, func(t *testing.T) { |
| 131 | ctx, cancel := context.WithCancel(t.Context()) |
| 132 | defer cancel() |
| 133 | run := func(ctx context.Context, _ gitcmd.Repo, args ...string) (string, error) { |
| 134 | if args[0] == cancelAt { |
| 135 | cancel() |
| 136 | } |
| 137 | if err := ctx.Err(); err != nil { |
| 138 | return "", err |
| 139 | } |
| 140 | switch args[0] { |
| 141 | case "rev-parse": |
| 142 | return filepath.Join("workspace", "repo"), nil |
| 143 | case "symbolic-ref": |
| 144 | return "main", nil |
| 145 | case "diff": |
| 146 | return "2\t1\ttracked.txt\n", nil |
| 147 | case "status": |
| 148 | return "?? new.txt\n", nil |
| 149 | default: |
| 150 | t.Fatalf("unexpected git args: %v", args) |
| 151 | return "", nil |
| 152 | } |
| 153 | } |
| 154 | repo := gitcmd.Repo{Dir: "repo", GitDir: "repo", CommonDir: "repo", WorkTree: "repo"} |
| 155 | status, err := loadGitStatusWithRunner(ctx, repo, run) |
| 156 | if !errors.Is(err, context.Canceled) || status != (gitStatus{}) { |
| 157 | t.Fatalf("canceled query returned status=%+v err=%v", status, err) |
| 158 | } |
| 159 | }) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // openedRepo is dir's identity as a session opening it resolves it. |
| 164 | func openedRepo(t *testing.T, dir string) gitcmd.Repo { |
| 165 | t.Helper() |
| 166 | repo, err := gitcmd.Open(context.Background(), dir) |
| 167 | if err != nil { |
| 168 | t.Fatalf("Open(%s): %v", dir, err) |
| 169 | } |
| 170 | return repo |
| 171 | } |
| 172 | |
| 173 | func TestRunGitDisablesOptionalLocks(t *testing.T) { |
| 174 | if runtime.GOOS == "windows" { |
| 175 | t.Skip("uses a POSIX shell script fake git") |
| 176 | } |
| 177 | |
| 178 | bin := filepath.Join(t.TempDir(), "bin") |
| 179 | if err := os.Mkdir(bin, 0o755); err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | fakeGit := filepath.Join(bin, "git") |
| 183 | if err := os.WriteFile(fakeGit, []byte("#!/bin/sh\nprintf '%s' \"$GIT_OPTIONAL_LOCKS\"\n"), 0o755); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 187 | |
| 188 | dir := t.TempDir() |
| 189 | out, err := runGit(context.Background(), gitcmd.Repo{Dir: dir, GitDir: dir, CommonDir: dir, WorkTree: dir}, "status") |
| 190 | if err != nil { |
| 191 | t.Fatalf("runGit: %v", err) |
| 192 | } |
| 193 | if out != "0" { |
| 194 | t.Fatalf("GIT_OPTIONAL_LOCKS = %q, want 0", out) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func runGitForTest(t *testing.T, cwd string, args ...string) { |
| 199 | t.Helper() |
| 200 | cmd := exec.Command("git", args...) |
| 201 | cmd.Dir = cwd |
| 202 | if out, err := cmd.CombinedOutput(); err != nil { |
| 203 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func writeFileForTest(t *testing.T, path string, body string) { |
| 208 | t.Helper() |
| 209 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | } |
| 213 |