@@ -0,0 +1,394 @@
1+package mcp_tools
2+
3+import (
4+ "context"
5+ "encoding/json"
6+ "os"
7+ "os/exec"
8+ "path/filepath"
9+ "strings"
10+ "testing"
11+
12+ "github.com/xonecas/symb/internal/mcp"
13+ "github.com/xonecas/symb/internal/tui/editor"
14+)
15+
16+// setupGitRepo creates a temp dir with an initialised git repo and returns
17+// the path along with a cleanup func that restores the original working dir.
18+func setupGitRepo(t *testing.T) (string, func()) {
19+ t.Helper()
20+
21+ dir := t.TempDir()
22+ origDir, _ := os.Getwd()
23+
24+ if err := os.Chdir(dir); err != nil {
25+ t.Fatalf("chdir: %v", err)
26+ }
27+
28+ // Initialise a git repo with a first commit so HEAD exists.
29+ for _, cmd := range [][]string{
30+ {"git", "init"},
31+ {"git", "config", "user.email", "test@test.com"},
32+ {"git", "config", "user.name", "Test"},
33+ } {
34+ c := exec.Command(cmd[0], cmd[1:]...)
35+ c.Dir = dir
36+ if out, err := c.CombinedOutput(); err != nil {
37+ t.Fatalf("setup %v: %s – %v", cmd, out, err)
38+ }
39+ }
40+
41+ // Create an initial commit so we have HEAD.
42+ initial := filepath.Join(dir, "init.txt")
43+ if err := os.WriteFile(initial, []byte("init\n"), 0644); err != nil {
44+ t.Fatal(err)
45+ }
46+ for _, cmd := range [][]string{
47+ {"git", "add", "."},
48+ {"git", "commit", "-m", "initial"},
49+ } {
50+ c := exec.Command(cmd[0], cmd[1:]...)
51+ c.Dir = dir
52+ if out, err := c.CombinedOutput(); err != nil {
53+ t.Fatalf("setup %v: %s – %v", cmd, out, err)
54+ }
55+ }
56+
57+ return dir, func() { os.Chdir(origDir) } //nolint:errcheck
58+}
59+
60+func callHandler(t *testing.T, handler func(context.Context, json.RawMessage) (*mcp.ToolResult, error), args interface{}) (string, bool) {
61+ t.Helper()
62+ raw, err := json.Marshal(args)
63+ if err != nil {
64+ t.Fatalf("marshal: %v", err)
65+ }
66+ result, err := handler(context.Background(), raw)
67+ if err != nil {
68+ t.Fatalf("handler error: %v", err)
69+ }
70+ text := ""
71+ if len(result.Content) > 0 {
72+ text = result.Content[0].Text
73+ }
74+ return text, result.IsError
75+}
76+
77+// ---------------------------------------------------------------------------
78+// GitStatus tests
79+// ---------------------------------------------------------------------------
80+
81+func TestGitStatusClean(t *testing.T) {
82+ _, cleanup := setupGitRepo(t)
83+ defer cleanup()
84+
85+ handler := MakeGitStatusHandler()
86+ text, isErr := callHandler(t, handler, GitStatusArgs{})
87+ if isErr {
88+ t.Fatalf("unexpected error: %s", text)
89+ }
90+ if !strings.Contains(text, "clean") {
91+ t.Errorf("expected clean status, got: %s", text)
92+ }
93+}
94+
95+func TestGitStatusWithChanges(t *testing.T) {
96+ dir, cleanup := setupGitRepo(t)
97+ defer cleanup()
98+
99+ // Create an untracked file.
100+ if err := os.WriteFile(filepath.Join(dir, "new.txt"), []byte("hello\n"), 0644); err != nil {
101+ t.Fatal(err)
102+ }
103+
104+ handler := MakeGitStatusHandler()
105+ text, isErr := callHandler(t, handler, GitStatusArgs{})
106+ if isErr {
107+ t.Fatalf("unexpected error: %s", text)
108+ }
109+ if !strings.Contains(text, "new.txt") {
110+ t.Errorf("expected new.txt in status, got: %s", text)
111+ }
112+}
113+
114+func TestGitStatusLongFormat(t *testing.T) {
115+ _, cleanup := setupGitRepo(t)
116+ defer cleanup()
117+
118+ handler := MakeGitStatusHandler()
119+ // long=true → long format which contains "On branch"
120+ text, isErr := callHandler(t, handler, GitStatusArgs{Long: true})
121+ if isErr {
122+ t.Fatalf("unexpected error: %s", text)
123+ }
124+ // Long format includes "On branch" header.
125+ if !strings.Contains(text, "On branch") {
126+ t.Errorf("expected long format, got: %s", text)
127+ }
128+}
129+
130+// ---------------------------------------------------------------------------
131+// GitDiff tests
132+// ---------------------------------------------------------------------------
133+
134+func TestGitDiffNoChanges(t *testing.T) {
135+ _, cleanup := setupGitRepo(t)
136+ defer cleanup()
137+
138+ handler := MakeGitDiffHandler()
139+ text, isErr := callHandler(t, handler, GitDiffArgs{})
140+ if isErr {
141+ t.Fatalf("unexpected error: %s", text)
142+ }
143+ if !strings.Contains(text, "no unstaged changes") {
144+ t.Errorf("expected no changes message, got: %s", text)
145+ }
146+}
147+
148+func TestGitDiffUnstaged(t *testing.T) {
149+ dir, cleanup := setupGitRepo(t)
150+ defer cleanup()
151+
152+ // Modify the tracked file.
153+ if err := os.WriteFile(filepath.Join(dir, "init.txt"), []byte("modified\n"), 0644); err != nil {
154+ t.Fatal(err)
155+ }
156+
157+ handler := MakeGitDiffHandler()
158+ text, isErr := callHandler(t, handler, GitDiffArgs{})
159+ if isErr {
160+ t.Fatalf("unexpected error: %s", text)
161+ }
162+ if !strings.Contains(text, "diff") {
163+ t.Errorf("expected diff output, got: %s", text)
164+ }
165+ if !strings.Contains(text, "modified") {
166+ t.Errorf("expected 'modified' in diff, got: %s", text)
167+ }
168+}
169+
170+func TestGitDiffStaged(t *testing.T) {
171+ dir, cleanup := setupGitRepo(t)
172+ defer cleanup()
173+
174+ // Modify and stage.
175+ if err := os.WriteFile(filepath.Join(dir, "init.txt"), []byte("staged change\n"), 0644); err != nil {
176+ t.Fatal(err)
177+ }
178+ cmd := exec.Command("git", "add", "init.txt")
179+ cmd.Dir = dir
180+ if out, err := cmd.CombinedOutput(); err != nil {
181+ t.Fatalf("git add: %s – %v", out, err)
182+ }
183+
184+ handler := MakeGitDiffHandler()
185+ text, isErr := callHandler(t, handler, GitDiffArgs{Staged: true})
186+ if isErr {
187+ t.Fatalf("unexpected error: %s", text)
188+ }
189+ if !strings.Contains(text, "staged change") {
190+ t.Errorf("expected staged diff, got: %s", text)
191+ }
192+}
193+
194+func TestGitDiffSpecificFile(t *testing.T) {
195+ dir, cleanup := setupGitRepo(t)
196+ defer cleanup()
197+
198+ // Create and commit a second file, then modify both.
199+ second := filepath.Join(dir, "second.txt")
200+ if err := os.WriteFile(second, []byte("original\n"), 0644); err != nil {
201+ t.Fatal(err)
202+ }
203+ for _, c := range [][]string{
204+ {"git", "add", "second.txt"},
205+ {"git", "commit", "-m", "add second"},
206+ } {
207+ cmd := exec.Command(c[0], c[1:]...)
208+ cmd.Dir = dir
209+ if out, err := cmd.CombinedOutput(); err != nil {
210+ t.Fatalf("%v: %s – %v", c, out, err)
211+ }
212+ }
213+
214+ // Modify both files.
215+ os.WriteFile(filepath.Join(dir, "init.txt"), []byte("changed init\n"), 0644)
216+ os.WriteFile(second, []byte("changed second\n"), 0644)
217+
218+ handler := MakeGitDiffHandler()
219+
220+ // Diff only second.txt.
221+ text, isErr := callHandler(t, handler, GitDiffArgs{File: "second.txt"})
222+ if isErr {
223+ t.Fatalf("unexpected error: %s", text)
224+ }
225+ if !strings.Contains(text, "changed second") {
226+ t.Errorf("expected second.txt diff, got: %s", text)
227+ }
228+ if strings.Contains(text, "changed init") {
229+ t.Error("should not contain init.txt changes")
230+ }
231+}
232+
233+func TestGitDiffNoStagedChanges(t *testing.T) {
234+ _, cleanup := setupGitRepo(t)
235+ defer cleanup()
236+
237+ handler := MakeGitDiffHandler()
238+ text, isErr := callHandler(t, handler, GitDiffArgs{Staged: true})
239+ if isErr {
240+ t.Fatalf("unexpected error: %s", text)
241+ }
242+ if !strings.Contains(text, "no staged changes") {
243+ t.Errorf("expected no staged changes message, got: %s", text)
244+ }
245+}
246+
247+// ---------------------------------------------------------------------------
248+// runGit edge cases
249+// ---------------------------------------------------------------------------
250+
251+func TestRunGitNotARepo(t *testing.T) {
252+ dir := t.TempDir()
253+ origDir, _ := os.Getwd()
254+ defer os.Chdir(origDir) //nolint:errcheck
255+ os.Chdir(dir) //nolint:errcheck
256+
257+ handler := MakeGitStatusHandler()
258+ text, isErr := callHandler(t, handler, GitStatusArgs{})
259+ if !isErr {
260+ t.Errorf("expected error outside git repo, got: %s", text)
261+ }
262+ if !strings.Contains(text, "git error") {
263+ t.Errorf("expected git error message, got: %s", text)
264+ }
265+}
266+
267+// ---------------------------------------------------------------------------
268+// ParseDiffMarkers tests
269+// ---------------------------------------------------------------------------
270+
271+func TestParseDiffMarkersEmpty(t *testing.T) {
272+ m := ParseDiffMarkers("")
273+ if m != nil {
274+ t.Errorf("expected nil for empty diff, got %v", m)
275+ }
276+}
277+
278+func TestParseDiffMarkersAdd(t *testing.T) {
279+ // Pure addition: 3 new lines starting at line 5.
280+ diff := `diff --git a/file.go b/file.go
281+--- a/file.go
282++++ b/file.go
283+@@ -4,0 +5,3 @@ func foo() {
284++line1
285++line2
286++line3
287+`
288+ m := ParseDiffMarkers(diff)
289+ if len(m) != 3 {
290+ t.Fatalf("expected 3 markers, got %d: %v", len(m), m)
291+ }
292+ for _, row := range []int{4, 5, 6} { // 0-indexed
293+ if m[row] != editor.GutterAdd {
294+ t.Errorf("row %d: expected GutterAdd, got %v", row, m[row])
295+ }
296+ }
297+}
298+
299+func TestParseDiffMarkersDelete(t *testing.T) {
300+ // Pure deletion: 2 old lines removed, new file continues at line 10.
301+ diff := `diff --git a/file.go b/file.go
302+--- a/file.go
303++++ b/file.go
304+@@ -8,2 +10,0 @@ func bar() {
305+-old1
306+-old2
307+`
308+ m := ParseDiffMarkers(diff)
309+ if len(m) != 1 {
310+ t.Fatalf("expected 1 marker, got %d: %v", len(m), m)
311+ }
312+ // newStart=10, newCount=0 → mark row 10-1=9 (0-indexed)
313+ if m[9] != editor.GutterDelete {
314+ t.Errorf("row 9: expected GutterDelete, got %v", m[9])
315+ }
316+}
317+
318+func TestParseDiffMarkersChange(t *testing.T) {
319+ // Modification: 2 old lines replaced by 3 new lines starting at line 7.
320+ diff := `diff --git a/file.go b/file.go
321+--- a/file.go
322++++ b/file.go
323+@@ -7,2 +7,3 @@ func baz() {
324+-old
325+-old2
326++new1
327++new2
328++new3
329+`
330+ m := ParseDiffMarkers(diff)
331+ if len(m) != 3 {
332+ t.Fatalf("expected 3 markers, got %d: %v", len(m), m)
333+ }
334+ for _, row := range []int{6, 7, 8} { // 0-indexed
335+ if m[row] != editor.GutterChange {
336+ t.Errorf("row %d: expected GutterChange, got %v", row, m[row])
337+ }
338+ }
339+}
340+
341+func TestParseDiffMarkersMultipleHunks(t *testing.T) {
342+ diff := `diff --git a/file.go b/file.go
343+--- a/file.go
344++++ b/file.go
345+@@ -1,0 +2,1 @@
346++added
347+@@ -10,1 +12,1 @@
348+-old
349++changed
350+`
351+ m := ParseDiffMarkers(diff)
352+ if len(m) != 2 {
353+ t.Fatalf("expected 2 markers, got %d: %v", len(m), m)
354+ }
355+ if m[1] != editor.GutterAdd { // line 2, 0-indexed = 1
356+ t.Errorf("row 1: expected GutterAdd, got %v", m[1])
357+ }
358+ if m[11] != editor.GutterChange { // line 12, 0-indexed = 11
359+ t.Errorf("row 11: expected GutterChange, got %v", m[11])
360+ }
361+}
362+
363+func TestParseDiffMarkersSingleLineNoComma(t *testing.T) {
364+ // When count is 1, git omits the comma: @@ -5 +5 @@
365+ diff := `@@ -5 +5 @@
366+-old
367++new
368+`
369+ m := ParseDiffMarkers(diff)
370+ if len(m) != 1 {
371+ t.Fatalf("expected 1 marker, got %d: %v", len(m), m)
372+ }
373+ if m[4] != editor.GutterChange { // line 5, 0-indexed = 4
374+ t.Errorf("row 4: expected GutterChange, got %v", m[4])
375+ }
376+}
377+
378+func TestGitFileMarkersIntegration(t *testing.T) {
379+ dir, cleanup := setupGitRepo(t)
380+ defer cleanup()
381+
382+ // Modify the tracked file.
383+ if err := os.WriteFile(filepath.Join(dir, "init.txt"), []byte("changed\n"), 0644); err != nil {
384+ t.Fatal(err)
385+ }
386+
387+ m := GitFileMarkers(context.Background(), "init.txt")
388+ if m == nil {
389+ t.Fatal("expected markers for modified file, got nil")
390+ }
391+ if _, ok := m[0]; !ok {
392+ t.Errorf("expected marker on row 0, got %v", m)
393+ }
394+}