1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# frozen_string_literal: true
RSpec.describe "edit_file tool" do
let(:worktree_path) { Dir.mktmpdir("edit-file-test") }
let(:context) { { worktree_path: } }
let(:registry) { Dispatch::Tools::Registry.new }
before { Dispatch::Tool::Files.register(registry) }
after { FileUtils.remove_entry(worktree_path) }
subject(:tool) { registry.get("edit_file") }
describe "single edit" do
it "replaces old_text with new_text in the file" do
file_path = File.join(worktree_path, "code.rb")
File.write(file_path, "def hello\n puts 'hello'\nend\n")
result = tool.call({
"path" => "code.rb",
"edits" => [{ "old_text" => "puts 'hello'", "new_text" => "puts 'goodbye'" }]
}, context:)
expect(result.success?).to be true
expect(File.read(file_path)).to include("puts 'goodbye'")
expect(File.read(file_path)).not_to include("puts 'hello'")
end
it "returns a confirmation with the number of edits applied" do
file_path = File.join(worktree_path, "file.txt")
File.write(file_path, "foo bar baz")
result = tool.call({
"path" => "file.txt",
"edits" => [{ "old_text" => "bar", "new_text" => "qux" }]
}, context:)
expect(result.success?).to be true
expect(result.output).to match(/1/i)
end
end
describe "multiple edits" do
it "applies multiple edits sequentially" do
file_path = File.join(worktree_path, "multi.txt")
File.write(file_path, "aaa bbb ccc")
result = tool.call({
"path" => "multi.txt",
"edits" => [
{ "old_text" => "aaa", "new_text" => "xxx" },
{ "old_text" => "ccc", "new_text" => "zzz" }
]
}, context:)
expect(result.success?).to be true
expect(File.read(file_path)).to eq("xxx bbb zzz")
end
it "applies edits sequentially so later edits see results of earlier ones" do
file_path = File.join(worktree_path, "sequential.txt")
File.write(file_path, "hello world")
result = tool.call({
"path" => "sequential.txt",
"edits" => [
{ "old_text" => "hello", "new_text" => "hi" },
{ "old_text" => "hi world", "new_text" => "hi there" }
]
}, context:)
expect(result.success?).to be true
expect(File.read(file_path)).to eq("hi there")
end
end
describe "error cases" do
it "returns failure when old_text is not found in the file" do
file_path = File.join(worktree_path, "missing.txt")
File.write(file_path, "actual content")
result = tool.call({
"path" => "missing.txt",
"edits" => [{ "old_text" => "nonexistent text", "new_text" => "replacement" }]
}, context:)
expect(result.failure?).to be true
expect(result.error).to match(/not found/i)
end
it "returns failure when the file does not exist" do
result = tool.call({
"path" => "ghost.txt",
"edits" => [{ "old_text" => "a", "new_text" => "b" }]
}, context:)
expect(result.failure?).to be true
expect(result.error).to match(/not found|does not exist/i)
end
it "returns failure when the path escapes the sandbox" do
result = tool.call({
"path" => "../../../etc/passwd",
"edits" => [{ "old_text" => "root", "new_text" => "hacked" }]
}, context:)
expect(result.failure?).to be true
expect(result.error).to match(/sandbox|outside/i)
end
it "returns failure when old_text matches ambiguously (multiple occurrences)" do
file_path = File.join(worktree_path, "ambiguous.txt")
File.write(file_path, "foo bar foo baz foo")
result = tool.call({
"path" => "ambiguous.txt",
"edits" => [{ "old_text" => "foo", "new_text" => "qux" }]
}, context:)
expect(result.failure?).to be true
expect(result.error).to match(/ambiguous|multiple/i)
end
end
describe "parameter validation" do
it "requires the path parameter" do
result = tool.call({
"edits" => [{ "old_text" => "a", "new_text" => "b" }]
}, context:)
expect(result.failure?).to be true
end
it "requires the edits parameter" do
File.write(File.join(worktree_path, "file.txt"), "content")
result = tool.call({ "path" => "file.txt" }, context:)
expect(result.failure?).to be true
end
end
end
|