@@ -3,513 +3,390 @@ package mcptools
33 import (
44 "context"
55 "encoding/json"
6- "fmt"
76 "os"
87 "path/filepath"
98 "strings"
109 "testing"
1110
1211 "github.com/xonecas/symb/internal/hashline"
12+ "github.com/xonecas/symb/internal/mcp"
1313 )
1414
15-const threeLineContent = "line one\nline two\nline three"
15+const threeLineContent = "aaa\nbbb\nccc\n"
1616
17-// setupTestFile creates a temp file with the given content and returns its path and cleanup func.
18-func setupTestFile(t *testing.T, content string) (string, func()) {
17+func setupTestFile(t *testing.T) (string, string) {
1918 t.Helper()
2019 dir := t.TempDir()
21- path := filepath.Join(dir, "test.go")
22- if err := os.WriteFile(path, []byte(content), 0644); err != nil {
23- t.Fatalf("write test file: %v", err)
24- }
25-
26- // Change to temp dir so path validation passes
27- origDir, _ := os.Getwd()
28- if err := os.Chdir(dir); err != nil {
29- t.Fatalf("chdir: %v", err)
30- }
31-
32- return path, func() {
33- os.Chdir(origDir) //nolint:errcheck
20+ path := filepath.Join(dir, "test.txt")
21+ if err := os.WriteFile(path, []byte(threeLineContent), 0644); err != nil {
22+ t.Fatal(err)
3423 }
24+ return dir, path
3525 }
3626
37-// newTrackedHandler creates an EditHandler with a tracker where the file is already marked as read.
38-func newTrackedHandler(t *testing.T, absPath string) *EditHandler {
27+func newTrackedHandler(t *testing.T, dir string) *EditHandler {
3928 t.Helper()
4029 tracker := NewFileReadTracker()
41- tracker.MarkRead(absPath)
42- return NewEditHandler(tracker, nil, nil)
30+ oldDir, _ := os.Getwd()
31+ if err := os.Chdir(dir); err != nil {
32+ t.Fatal(err)
33+ }
34+ t.Cleanup(func() { os.Chdir(oldDir) })
35+ h := NewEditHandler(tracker, nil, nil)
36+ return h
4337 }
4438
45-func callEdit(t *testing.T, handler *EditHandler, args EditArgs) (string, bool) {
39+func callEdit(t *testing.T, handler *EditHandler, jsonStr string) *mcp.ToolResult {
4640 t.Helper()
47- argsJSON, err := json.Marshal(args)
48- if err != nil {
49- t.Fatalf("marshal args: %v", err)
50- }
51-
52- result, err := handler.Handle(context.Background(), argsJSON)
41+ result, err := handler.Handle(context.Background(), json.RawMessage(jsonStr))
5342 if err != nil {
5443 t.Fatalf("handler error: %v", err)
5544 }
45+ return result
46+}
5647
57- text := ""
58- if len(result.Content) > 0 {
59- text = result.Content[0].Text
48+// hashFor returns the hash for a given line in content.
49+func hashFor(content string, lineNum int) string {
50+ tagged := hashline.TagLines(content, 1)
51+ if lineNum < 1 || lineNum > len(tagged) {
52+ panic("line out of range")
6053 }
61- return text, result.IsError
54+ return tagged[lineNum-1].Hash
6255 }
6356
6457 func TestEditReplace(t *testing.T) {
65- content := "line one\nline two\nline three\nline four"
66- path, cleanup := setupTestFile(t, content)
67- defer cleanup()
68-
69- lines := strings.Split(content, "\n")
70- h2 := hashline.LineHash(lines[1])
71- h3 := hashline.LineHash(lines[2])
72-
73- handler := newTrackedHandler(t, path)
74- text, isErr := callEdit(t, handler, EditArgs{
75- File: filepath.Base(path),
76- Replace: &ReplaceOp{
77- Start: hashline.Anchor{Num: 2, Hash: h2},
78- End: hashline.Anchor{Num: 3, Hash: h3},
79- Content: "replaced line",
80- },
81- })
82-
83- if isErr {
84- t.Fatalf("replace failed: %s", text)
85- }
58+ dir, path := setupTestFile(t)
59+ handler := newTrackedHandler(t, dir)
60+ handler.tracker.MarkRead(path)
8661
87- got, _ := os.ReadFile(path)
88- expected := "line one\nreplaced line\nline four"
89- if string(got) != expected {
90- t.Errorf("file content:\ngot: %q\nwant: %q", string(got), expected)
62+ h1 := hashFor(threeLineContent, 1)
63+ h2 := hashFor(threeLineContent, 2)
64+
65+ result := callEdit(t, handler, `{
66+ "file": "test.txt",
67+ "operation": "replace",
68+ "start": "1:`+h1+`",
69+ "end": "2:`+h2+`",
70+ "content": "xxx\nyyy"
71+ }`)
72+
73+ if result.IsError {
74+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
9175 }
9276
93- // Result should contain hashline-tagged output
94- if !strings.Contains(text, "Edited") {
95- t.Errorf("result should mention 'Edited': %s", text)
77+ got, _ := os.ReadFile(path)
78+ if string(got) != "xxx\nyyy\nccc\n" {
79+ t.Errorf("unexpected content: %q", got)
9680 }
9781 }
9882
99-func TestEditInsert(t *testing.T) {
100- content := threeLineContent
101- path, cleanup := setupTestFile(t, content)
102- defer cleanup()
103-
104- lines := strings.Split(content, "\n")
105- h1 := hashline.LineHash(lines[0])
106-
107- handler := newTrackedHandler(t, path)
108- text, isErr := callEdit(t, handler, EditArgs{
109- File: filepath.Base(path),
110- Insert: &InsertOp{
111- After: hashline.Anchor{Num: 1, Hash: h1},
112- Content: "inserted a\ninserted b",
113- },
114- })
115-
116- if isErr {
117- t.Fatalf("insert failed: %s", text)
83+func TestEditReplaceSingleLine(t *testing.T) {
84+ dir, path := setupTestFile(t)
85+ handler := newTrackedHandler(t, dir)
86+ handler.tracker.MarkRead(path)
87+
88+ h2 := hashFor(threeLineContent, 2)
89+
90+ result := callEdit(t, handler, `{
91+ "file": "test.txt",
92+ "operation": "replace",
93+ "start": "2:`+h2+`",
94+ "end": "2:`+h2+`",
95+ "content": "BBB"
96+ }`)
97+
98+ if result.IsError {
99+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
118100 }
119101
120102 got, _ := os.ReadFile(path)
121- expected := "line one\ninserted a\ninserted b\nline two\nline three"
122- if string(got) != expected {
123- t.Errorf("file content:\ngot: %q\nwant: %q", string(got), expected)
103+ if string(got) != "aaa\nBBB\nccc\n" {
104+ t.Errorf("unexpected content: %q", got)
124105 }
125106 }
126107
127-func TestEditDelete(t *testing.T) {
128- content := "line one\nline two\nline three\nline four"
129- path, cleanup := setupTestFile(t, content)
130- defer cleanup()
131-
132- lines := strings.Split(content, "\n")
133- h2 := hashline.LineHash(lines[1])
134- h3 := hashline.LineHash(lines[2])
135-
136- handler := newTrackedHandler(t, path)
137- text, isErr := callEdit(t, handler, EditArgs{
138- File: filepath.Base(path),
139- Delete: &DeleteOp{
140- Start: hashline.Anchor{Num: 2, Hash: h2},
141- End: hashline.Anchor{Num: 3, Hash: h3},
142- },
143- })
144-
145- if isErr {
146- t.Fatalf("delete failed: %s", text)
108+func TestEditReplaceWithMoreLines(t *testing.T) {
109+ dir, path := setupTestFile(t)
110+ handler := newTrackedHandler(t, dir)
111+ handler.tracker.MarkRead(path)
112+
113+ h2 := hashFor(threeLineContent, 2)
114+
115+ result := callEdit(t, handler, `{
116+ "file": "test.txt",
117+ "operation": "replace",
118+ "start": "2:`+h2+`",
119+ "end": "2:`+h2+`",
120+ "content": "B1\nB2\nB3"
121+ }`)
122+
123+ if result.IsError {
124+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
147125 }
148126
149127 got, _ := os.ReadFile(path)
150- expected := "line one\nline four"
151- if string(got) != expected {
152- t.Errorf("file content:\ngot: %q\nwant: %q", string(got), expected)
128+ if string(got) != "aaa\nB1\nB2\nB3\nccc\n" {
129+ t.Errorf("unexpected content: %q", got)
153130 }
154131 }
155132
156-func TestEditCreate(t *testing.T) {
157- dir := t.TempDir()
158- origDir, _ := os.Getwd()
159- if err := os.Chdir(dir); err != nil {
160- t.Fatalf("chdir: %v", err)
161- }
162- defer os.Chdir(origDir) //nolint:errcheck
133+func TestEditInsert(t *testing.T) {
134+ dir, path := setupTestFile(t)
135+ handler := newTrackedHandler(t, dir)
136+ handler.tracker.MarkRead(path)
163137
164- tracker := NewFileReadTracker()
165- handler := NewEditHandler(tracker, nil, nil)
166- text, isErr := callEdit(t, handler, EditArgs{
167- File: "newfile.go",
168- Create: &CreateOp{Content: "package main\n\nfunc main() {}\n"},
169- })
170-
171- if isErr {
172- t.Fatalf("create failed: %s", text)
173- }
138+ h1 := hashFor(threeLineContent, 1)
174139
175- got, err := os.ReadFile(filepath.Join(dir, "newfile.go"))
176- if err != nil {
177- t.Fatalf("read created file: %v", err)
140+ result := callEdit(t, handler, `{
141+ "file": "test.txt",
142+ "operation": "insert",
143+ "after": "1:`+h1+`",
144+ "content": "inserted"
145+ }`)
146+
147+ if result.IsError {
148+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
178149 }
179- expected := "package main\n\nfunc main() {}\n"
180- if string(got) != expected {
181- t.Errorf("file content:\ngot: %q\nwant: %q", string(got), expected)
150+
151+ got, _ := os.ReadFile(path)
152+ if string(got) != "aaa\ninserted\nbbb\nccc\n" {
153+ t.Errorf("unexpected content: %q", got)
182154 }
183155 }
184156
185-func TestEditCreateFailsIfExists(t *testing.T) {
186- _, cleanup := setupTestFile(t, "existing content")
187- defer cleanup()
157+func TestEditDelete(t *testing.T) {
158+ dir, path := setupTestFile(t)
159+ handler := newTrackedHandler(t, dir)
160+ handler.tracker.MarkRead(path)
188161
189- tracker := NewFileReadTracker()
190- handler := NewEditHandler(tracker, nil, nil)
191- _, isErr := callEdit(t, handler, EditArgs{
192- File: "test.go",
193- Create: &CreateOp{Content: "new content"},
194- })
195-
196- if !isErr {
197- t.Error("create should fail if file already exists")
162+ h2 := hashFor(threeLineContent, 2)
163+
164+ result := callEdit(t, handler, `{
165+ "file": "test.txt",
166+ "operation": "delete",
167+ "start": "2:`+h2+`",
168+ "end": "2:`+h2+`"
169+ }`)
170+
171+ if result.IsError {
172+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
198173 }
199-}
200174
201-func TestEditHashMismatch(t *testing.T) {
202- content := threeLineContent
203- path, cleanup := setupTestFile(t, content)
204- defer cleanup()
205-
206- handler := newTrackedHandler(t, path)
207- _, isErr := callEdit(t, handler, EditArgs{
208- File: "test.go",
209- Replace: &ReplaceOp{
210- Start: hashline.Anchor{Num: 1, Hash: "ff"},
211- End: hashline.Anchor{Num: 2, Hash: "ff"},
212- Content: "whatever",
213- },
214- })
215-
216- if !isErr {
217- t.Error("should fail on hash mismatch")
175+ got, _ := os.ReadFile(path)
176+ if string(got) != "aaa\nccc\n" {
177+ t.Errorf("unexpected content: %q", got)
218178 }
219179 }
220180
221-func TestEditNoOperation(t *testing.T) {
222- _, cleanup := setupTestFile(t, "single line")
223- defer cleanup()
181+func TestEditCreate(t *testing.T) {
182+ dir, _ := setupTestFile(t)
183+ handler := newTrackedHandler(t, dir)
224184
225- tracker := NewFileReadTracker()
226- handler := NewEditHandler(tracker, nil, nil)
227- _, isErr := callEdit(t, handler, EditArgs{
228- File: "test.go",
229- })
185+ result := callEdit(t, handler, `{
186+ "file": "new.txt",
187+ "operation": "create",
188+ "content": "hello\nworld"
189+ }`)
230190
231- if !isErr {
232- t.Error("should fail when no operation specified")
191+ if result.IsError {
192+ t.Fatalf("unexpected error: %s", result.Content[0].Text)
233193 }
234-}
235194
236-func TestEditMultipleOperations(t *testing.T) {
237- testContent := "single line"
238- path, cleanup := setupTestFile(t, testContent)
239- defer cleanup()
240-
241- lines := strings.Split(testContent, "\n")
242- h1 := hashline.LineHash(lines[0])
243-
244- handler := newTrackedHandler(t, path)
245- _, isErr := callEdit(t, handler, EditArgs{
246- File: "test.go",
247- Replace: &ReplaceOp{
248- Start: hashline.Anchor{Num: 1, Hash: h1},
249- End: hashline.Anchor{Num: 1, Hash: h1},
250- Content: "new",
251- },
252- Delete: &DeleteOp{
253- Start: hashline.Anchor{Num: 1, Hash: h1},
254- End: hashline.Anchor{Num: 1, Hash: h1},
255- },
256- })
257-
258- if !isErr {
259- t.Error("should fail when multiple operations specified")
195+ got, _ := os.ReadFile(filepath.Join(dir, "new.txt"))
196+ if string(got) != "hello\nworld" {
197+ t.Errorf("unexpected content: %q", got)
260198 }
261199 }
262200
263-func TestEditPathTraversal(t *testing.T) {
264- _, cleanup := setupTestFile(t, "single line")
265- defer cleanup()
201+func TestEditCreateFailsIfExists(t *testing.T) {
202+ dir, _ := setupTestFile(t)
203+ handler := newTrackedHandler(t, dir)
266204
267- tracker := NewFileReadTracker()
268- handler := NewEditHandler(tracker, nil, nil)
269- _, isErr := callEdit(t, handler, EditArgs{
270- File: "../../../etc/passwd",
271- Create: &CreateOp{Content: "hacked"},
272- })
273-
274- if !isErr {
275- t.Error("should reject path traversal")
205+ result := callEdit(t, handler, `{
206+ "file": "test.txt",
207+ "operation": "create",
208+ "content": "overwrite"
209+ }`)
210+
211+ if !result.IsError {
212+ t.Fatal("should fail when file exists")
276213 }
277214 }
278215
279-func TestEditReplaceSingleLine(t *testing.T) {
280- content := "aaa\nbbb\nccc"
281- path, cleanup := setupTestFile(t, content)
282- defer cleanup()
283-
284- lines := strings.Split(content, "\n")
285- h2 := hashline.LineHash(lines[1])
286-
287- handler := newTrackedHandler(t, path)
288- _, isErr := callEdit(t, handler, EditArgs{
289- File: filepath.Base(path),
290- Replace: &ReplaceOp{
291- Start: hashline.Anchor{Num: 2, Hash: h2},
292- End: hashline.Anchor{Num: 2, Hash: h2},
293- Content: "BBB",
294- },
295- })
296-
297- if isErr {
298- t.Fatal("single line replace should work")
299- }
216+func TestEditHashMismatch(t *testing.T) {
217+ dir, path := setupTestFile(t)
218+ handler := newTrackedHandler(t, dir)
219+ handler.tracker.MarkRead(path)
220+
221+ result := callEdit(t, handler, `{
222+ "file": "test.txt",
223+ "operation": "replace",
224+ "start": "1:zz",
225+ "end": "1:zz",
226+ "content": "xxx"
227+ }`)
300228
301- got, _ := os.ReadFile(path)
302- if string(got) != "aaa\nBBB\nccc" {
303- t.Errorf("got: %q", string(got))
229+ if !result.IsError {
230+ t.Fatal("should fail with hash mismatch")
304231 }
305232 }
306233
307-func TestEditReplaceWithMoreLines(t *testing.T) {
308- content := "aaa\nbbb\nccc"
309- path, cleanup := setupTestFile(t, content)
310- defer cleanup()
311-
312- lines := strings.Split(content, "\n")
313- h2 := hashline.LineHash(lines[1])
314-
315- handler := newTrackedHandler(t, path)
316- _, isErr := callEdit(t, handler, EditArgs{
317- File: filepath.Base(path),
318- Replace: &ReplaceOp{
319- Start: hashline.Anchor{Num: 2, Hash: h2},
320- End: hashline.Anchor{Num: 2, Hash: h2},
321- Content: "BBB\nDDD\nEEE",
322- },
323- })
324-
325- if isErr {
326- t.Fatal("replace expanding lines should work")
234+func TestEditNoOperation(t *testing.T) {
235+ dir, _ := setupTestFile(t)
236+ handler := newTrackedHandler(t, dir)
237+
238+ result := callEdit(t, handler, `{"file": "test.txt"}`)
239+
240+ if !result.IsError {
241+ t.Fatal("should fail without operation")
327242 }
243+}
328244
329- got, _ := os.ReadFile(path)
330- if string(got) != "aaa\nBBB\nDDD\nEEE\nccc" {
331- t.Errorf("got: %q", string(got))
245+func TestEditPathTraversal(t *testing.T) {
246+ dir, _ := setupTestFile(t)
247+ handler := newTrackedHandler(t, dir)
248+
249+ result := callEdit(t, handler, `{
250+ "file": "../../../etc/passwd",
251+ "operation": "create",
252+ "content": "bad"
253+ }`)
254+
255+ if !result.IsError {
256+ t.Fatal("should reject path traversal")
332257 }
333258 }
334259
335260 func TestEditRequiresReadFirst(t *testing.T) {
336- content := "line one\nline two"
337- path, cleanup := setupTestFile(t, content)
338- defer cleanup()
261+ dir, _ := setupTestFile(t)
262+ handler := newTrackedHandler(t, dir)
339263
340- lines := strings.Split(content, "\n")
341- h1 := hashline.LineHash(lines[0])
264+ h1 := hashFor(threeLineContent, 1)
342265
343- // Handler with empty tracker — file NOT read
344- tracker := NewFileReadTracker()
345- handler := NewEditHandler(tracker, nil, nil)
346-
347- text, isErr := callEdit(t, handler, EditArgs{
348- File: filepath.Base(path),
349- Replace: &ReplaceOp{
350- Start: hashline.Anchor{Num: 1, Hash: h1},
351- End: hashline.Anchor{Num: 1, Hash: h1},
352- Content: "replaced",
353- },
354- })
355-
356- if !isErr {
357- t.Fatal("should fail when file was not read first")
358- }
359- if !strings.Contains(text, "Read") {
360- t.Errorf("error should mention Read tool: %s", text)
361- }
266+ result := callEdit(t, handler, `{
267+ "file": "test.txt",
268+ "operation": "replace",
269+ "start": "1:`+h1+`",
270+ "end": "1:`+h1+`",
271+ "content": "xxx"
272+ }`)
362273
363- // Now mark as read and retry — should succeed
364- absPath, _ := filepath.Abs(filepath.Base(path))
365- tracker.MarkRead(absPath)
366-
367- _, isErr = callEdit(t, handler, EditArgs{
368- File: filepath.Base(path),
369- Replace: &ReplaceOp{
370- Start: hashline.Anchor{Num: 1, Hash: h1},
371- End: hashline.Anchor{Num: 1, Hash: h1},
372- Content: "replaced",
373- },
374- })
375-
376- if isErr {
377- t.Fatal("should succeed after file was read")
274+ if !result.IsError {
275+ t.Fatal("should require Read first")
276+ }
277+ if !strings.Contains(result.Content[0].Text, "must Read") {
278+ t.Errorf("unexpected error: %s", result.Content[0].Text)
378279 }
379280 }
380281
381282 func TestEditCreateBypassesReadCheck(t *testing.T) {
382- dir := t.TempDir()
383- origDir, _ := os.Getwd()
384- if err := os.Chdir(dir); err != nil {
385- t.Fatalf("chdir: %v", err)
386- }
387- defer os.Chdir(origDir) //nolint:errcheck
283+ dir, _ := setupTestFile(t)
284+ handler := newTrackedHandler(t, dir)
388285
389- // Empty tracker — no file read. Create should still work.
390- tracker := NewFileReadTracker()
391- handler := NewEditHandler(tracker, nil, nil)
392- _, isErr := callEdit(t, handler, EditArgs{
393- File: "brand-new.go",
394- Create: &CreateOp{Content: "package new\n"},
395- })
396-
Cut short: this commit's diff is over 1000 lines.