xonecas.io
62159
Sy

symb / files

symb / internal / mcptools / edit_test.go

8.2 KB
1package mcptools
2
3import (
4	"context"
5	"encoding/json"
6	"os"
7	"path/filepath"
8	"strings"
9	"testing"
10
11	"github.com/xonecas/symb/internal/hashline"
12	"github.com/xonecas/symb/internal/mcp"
13)
14
15const threeLineContent = "aaa\nbbb\nccc\n"
16
17func setupTestFile(t *testing.T) (string, string) {
18	t.Helper()
19	dir := t.TempDir()
20	path := filepath.Join(dir, "test.txt")
21	if err := os.WriteFile(path, []byte(threeLineContent), 0644); err != nil {
22		t.Fatal(err)
23	}
24	return dir, path
25}
26
27func newTrackedHandler(t *testing.T, dir string) *EditHandler {
28	t.Helper()
29	tracker := NewFileReadTracker()
30	h := NewEditHandler(tracker, nil, nil)
31	h.SetRootDir(dir)
32	return h
33}
34
35func callEdit(t *testing.T, handler *EditHandler, jsonStr string) *mcp.ToolResult {
36	t.Helper()
37	result, err := handler.Handle(context.Background(), json.RawMessage(jsonStr))
38	if err != nil {
39		t.Fatalf("handler error: %v", err)
40	}
41	return result
42}
43
44// hashFor returns the hash for a given line in content.
45func hashFor(content string, lineNum int) string {
46	tagged := hashline.TagLines(content, 1)
47	if lineNum < 1 || lineNum > len(tagged) {
48		panic("line out of range")
49	}
50	return tagged[lineNum-1].Hash
51}
52
53func TestEditReplace(t *testing.T) {
54	dir, path := setupTestFile(t)
55	handler := newTrackedHandler(t, dir)
56	handler.tracker.MarkRead(path)
57
58	h1 := hashFor(threeLineContent, 1)
59	h2 := hashFor(threeLineContent, 2)
60
61	result := callEdit(t, handler, `{
62		"file": "test.txt",
63		"operation": "replace",
64		"start": "1:`+h1+`",
65		"end": "2:`+h2+`",
66		"content": "xxx\nyyy"
67	}`)
68
69	if result.IsError {
70		t.Fatalf("unexpected error: %s", result.Content[0].Text)
71	}
72
73	got, _ := os.ReadFile(path)
74	if string(got) != "xxx\nyyy\nccc\n" {
75		t.Errorf("unexpected content: %q", got)
76	}
77}
78
79func TestEditReplaceSingleLine(t *testing.T) {
80	dir, path := setupTestFile(t)
81	handler := newTrackedHandler(t, dir)
82	handler.tracker.MarkRead(path)
83
84	h2 := hashFor(threeLineContent, 2)
85
86	result := callEdit(t, handler, `{
87		"file": "test.txt",
88		"operation": "replace",
89		"start": "2:`+h2+`",
90		"end": "2:`+h2+`",
91		"content": "BBB"
92	}`)
93
94	if result.IsError {
95		t.Fatalf("unexpected error: %s", result.Content[0].Text)
96	}
97
98	got, _ := os.ReadFile(path)
99	if string(got) != "aaa\nBBB\nccc\n" {
100		t.Errorf("unexpected content: %q", got)
101	}
102}
103
104func TestEditReplaceWithMoreLines(t *testing.T) {
105	dir, path := setupTestFile(t)
106	handler := newTrackedHandler(t, dir)
107	handler.tracker.MarkRead(path)
108
109	h2 := hashFor(threeLineContent, 2)
110
111	result := callEdit(t, handler, `{
112		"file": "test.txt",
113		"operation": "replace",
114		"start": "2:`+h2+`",
115		"end": "2:`+h2+`",
116		"content": "B1\nB2\nB3"
117	}`)
118
119	if result.IsError {
120		t.Fatalf("unexpected error: %s", result.Content[0].Text)
121	}
122
123	got, _ := os.ReadFile(path)
124	if string(got) != "aaa\nB1\nB2\nB3\nccc\n" {
125		t.Errorf("unexpected content: %q", got)
126	}
127}
128
129func TestEditInsert(t *testing.T) {
130	dir, path := setupTestFile(t)
131	handler := newTrackedHandler(t, dir)
132	handler.tracker.MarkRead(path)
133
134	h1 := hashFor(threeLineContent, 1)
135
136	result := callEdit(t, handler, `{
137		"file": "test.txt",
138		"operation": "insert",
139		"after": "1:`+h1+`",
140		"content": "inserted"
141	}`)
142
143	if result.IsError {
144		t.Fatalf("unexpected error: %s", result.Content[0].Text)
145	}
146
147	got, _ := os.ReadFile(path)
148	if string(got) != "aaa\ninserted\nbbb\nccc\n" {
149		t.Errorf("unexpected content: %q", got)
150	}
151}
152
153func TestEditDelete(t *testing.T) {
154	dir, path := setupTestFile(t)
155	handler := newTrackedHandler(t, dir)
156	handler.tracker.MarkRead(path)
157
158	h2 := hashFor(threeLineContent, 2)
159
160	result := callEdit(t, handler, `{
161		"file": "test.txt",
162		"operation": "delete",
163		"start": "2:`+h2+`",
164		"end": "2:`+h2+`"
165	}`)
166
167	if result.IsError {
168		t.Fatalf("unexpected error: %s", result.Content[0].Text)
169	}
170
171	got, _ := os.ReadFile(path)
172	if string(got) != "aaa\nccc\n" {
173		t.Errorf("unexpected content: %q", got)
174	}
175}
176
177func TestEditCreate(t *testing.T) {
178	dir, _ := setupTestFile(t)
179	handler := newTrackedHandler(t, dir)
180
181	result := callEdit(t, handler, `{
182		"file": "new.txt",
183		"operation": "create",
184		"content": "hello\nworld"
185	}`)
186
187	if result.IsError {
188		t.Fatalf("unexpected error: %s", result.Content[0].Text)
189	}
190
191	got, _ := os.ReadFile(filepath.Join(dir, "new.txt"))
192	if string(got) != "hello\nworld" {
193		t.Errorf("unexpected content: %q", got)
194	}
195}
196
197func TestEditCreateFailsIfExists(t *testing.T) {
198	dir, _ := setupTestFile(t)
199	handler := newTrackedHandler(t, dir)
200
201	result := callEdit(t, handler, `{
202		"file": "test.txt",
203		"operation": "create",
204		"content": "overwrite"
205	}`)
206
207	if !result.IsError {
208		t.Fatal("should fail when file exists")
209	}
210}
211
212func TestEditHashMismatch(t *testing.T) {
213	dir, path := setupTestFile(t)
214	handler := newTrackedHandler(t, dir)
215	handler.tracker.MarkRead(path)
216
217	result := callEdit(t, handler, `{
218		"file": "test.txt",
219		"operation": "replace",
220		"start": "1:zz",
221		"end": "1:zz",
222		"content": "xxx"
223	}`)
224
225	if !result.IsError {
226		t.Fatal("should fail with hash mismatch")
227	}
228}
229
230func TestEditNoOperation(t *testing.T) {
231	dir, _ := setupTestFile(t)
232	handler := newTrackedHandler(t, dir)
233
234	result := callEdit(t, handler, `{"file": "test.txt"}`)
235
236	if !result.IsError {
237		t.Fatal("should fail without operation")
238	}
239}
240
241func TestEditPathTraversal(t *testing.T) {
242	dir, _ := setupTestFile(t)
243	handler := newTrackedHandler(t, dir)
244
245	result := callEdit(t, handler, `{
246		"file": "../../../etc/passwd",
247		"operation": "create",
248		"content": "bad"
249	}`)
250
251	if !result.IsError {
252		t.Fatal("should reject path traversal")
253	}
254}
255
256func TestEditRequiresReadFirst(t *testing.T) {
257	dir, _ := setupTestFile(t)
258	handler := newTrackedHandler(t, dir)
259
260	h1 := hashFor(threeLineContent, 1)
261
262	result := callEdit(t, handler, `{
263		"file": "test.txt",
264		"operation": "replace",
265		"start": "1:`+h1+`",
266		"end": "1:`+h1+`",
267		"content": "xxx"
268	}`)
269
270	if !result.IsError {
271		t.Fatal("should require Read first")
272	}
273	if !strings.Contains(result.Content[0].Text, "must Read") {
274		t.Errorf("unexpected error: %s", result.Content[0].Text)
275	}
276}
277
278func TestEditCreateBypassesReadCheck(t *testing.T) {
279	dir, _ := setupTestFile(t)
280	handler := newTrackedHandler(t, dir)
281
282	// create should work without prior Read
283	result := callEdit(t, handler, `{
284		"file": "brand_new.txt",
285		"operation": "create",
286		"content": "hello"
287	}`)
288
289	if result.IsError {
290		t.Fatalf("create should not require Read: %s", result.Content[0].Text)
291	}
292}
293
294func TestEditWindowedResponse(t *testing.T) {
295	// Build a file with 100 lines.
296	var sb strings.Builder
297	for i := 1; i <= 100; i++ {
298		sb.WriteString("line\n")
299	}
300	bigContent := sb.String()
301
302	dir := t.TempDir()
303	path := filepath.Join(dir, "big.txt")
304	if err := os.WriteFile(path, []byte(bigContent), 0644); err != nil {
305		t.Fatal(err)
306	}
307
308	handler := newTrackedHandler(t, dir)
309	handler.tracker.MarkRead(path)
310
311	h50 := hashFor(bigContent, 50)
312
313	result := callEdit(t, handler, `{
314		"file": "big.txt",
315		"operation": "replace",
316		"start": "50:`+h50+`",
317		"end": "50:`+h50+`",
318		"content": "REPLACED"
319	}`)
320
321	if result.IsError {
322		t.Fatalf("unexpected error: %s", result.Content[0].Text)
323	}
324
325	text := result.Content[0].Text
326	if !strings.Contains(text, "showing") {
327		t.Errorf("expected windowed output, got: %s", text[:min(len(text), 200)])
328	}
329}
330
331func TestEditSmallFileFullResponse(t *testing.T) {
332	dir, path := setupTestFile(t)
333	handler := newTrackedHandler(t, dir)
334	handler.tracker.MarkRead(path)
335
336	h1 := hashFor(threeLineContent, 1)
337
338	result := callEdit(t, handler, `{
339		"file": "test.txt",
340		"operation": "replace",
341		"start": "1:`+h1+`",
342		"end": "1:`+h1+`",
343		"content": "XXX"
344	}`)
345
346	if result.IsError {
347		t.Fatalf("unexpected error: %s", result.Content[0].Text)
348	}
349
350	text := result.Content[0].Text
351	if strings.Contains(text, "showing") {
352		t.Errorf("small file should not be windowed: %s", text)
353	}
354}
355
356func TestEditUnknownOperation(t *testing.T) {
357	dir, path := setupTestFile(t)
358	handler := newTrackedHandler(t, dir)
359	handler.tracker.MarkRead(path)
360
361	result := callEdit(t, handler, `{
362		"file": "test.txt",
363		"operation": "patch"
364	}`)
365
366	if !result.IsError {
367		t.Fatal("should fail with unknown operation")
368	}
369}
370
371func TestEditBadAnchorFormat(t *testing.T) {
372	dir, path := setupTestFile(t)
373	handler := newTrackedHandler(t, dir)
374	handler.tracker.MarkRead(path)
375
376	// Missing hash
377	result := callEdit(t, handler, `{
378		"file": "test.txt",
379		"operation": "replace",
380		"start": "1",
381		"end": "2",
382		"content": "x"
383	}`)
384
385	if !result.IsError {
386		t.Fatal("should fail with bad anchor")
387	}
388}