Elffuss BraidHub

kikocis/continue public

main · 10.5 KiBRawPermalink
1import { ContinueErrorReason } from "core/util/errors";
2import * as ideUtils from "core/util/ideUtils";
3import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
4import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate";
5import { ClientToolExtras } from "./callClientTool";
6import { singleFindAndReplaceImpl } from "./singleFindAndReplaceImpl";
7vi.mock("uuid", () => ({
8 v4: vi.fn(() => "test-uuid"),
9}));
10
11vi.mock("core/util/ideUtils", () => ({
12 resolveRelativePathInDir: vi.fn(),
13}));
14
15vi.mock("../../redux/thunks/handleApplyStateUpdate", () => ({
16 applyForEditTool: vi.fn(),
17}));
18
19describe("singleFindAndReplaceImpl", () => {
20 let mockExtras: ClientToolExtras;
21 let mockResolveRelativePathInDir: Mock;
22 let mockApplyForEditTool: Mock;
23
24 beforeEach(() => {
25 vi.clearAllMocks();
26
27 mockResolveRelativePathInDir = vi.mocked(ideUtils.resolveRelativePathInDir);
28 mockApplyForEditTool = vi.mocked(applyForEditTool);
29
30 mockExtras = {
31 getState: vi.fn(() => ({
32 config: {
33 config: {
34 allowAnonymousTelemetry: false,
35 },
36 },
37 })) as any,
38 dispatch: vi.fn() as any,
39 ideMessenger: {
40 ide: {
41 readFile: vi.fn(),
42 },
43 request: vi.fn(),
44 } as any,
45 };
46 });
47
48 describe("argument validation", () => {
49 beforeEach(() => {
50 // For validation tests, make the file exist so we can test validation errors
51 mockResolveRelativePathInDir.mockResolvedValue("/test/file.txt");
52 mockExtras.ideMessenger.ide.readFile = vi
53 .fn()
54 .mockResolvedValue("content");
55 });
56
57 it("should throw error if filepath is missing", async () => {
58 const args = {
59 old_string: "test",
60 new_string: "replacement",
61 };
62
63 await expect(
64 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
65 ).rejects.toThrowError(
66 expect.objectContaining({
67 reason: ContinueErrorReason.FindAndReplaceMissingFilepath,
68 }),
69 );
70 });
71
72 it("should throw error if old_string is missing", async () => {
73 const args = {
74 filepath: "test.txt",
75 new_string: "replacement",
76 };
77
78 await expect(
79 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
80 ).rejects.toThrowError(
81 expect.objectContaining({
82 reason: ContinueErrorReason.FindAndReplaceMissingOldString,
83 }),
84 );
85 });
86
87 it("should throw error if new_string is missing", async () => {
88 const args = {
89 filepath: "test.txt",
90 old_string: "test",
91 };
92
93 await expect(
94 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
95 ).rejects.toThrowError(
96 expect.objectContaining({
97 reason: ContinueErrorReason.FindAndReplaceMissingNewString,
98 }),
99 );
100 });
101
102 it("should throw error if old_string and new_string are the same", async () => {
103 const args = {
104 filepath: "test.txt",
105 old_string: "same",
106 new_string: "same",
107 };
108
109 await expect(
110 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
111 ).rejects.toThrowError(
112 expect.objectContaining({
113 reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings,
114 }),
115 );
116 });
117 });
118
119 describe("file resolution", () => {
120 it("should throw error if file does not exist", async () => {
121 mockResolveRelativePathInDir.mockResolvedValue(null);
122
123 const args = {
124 filepath: "nonexistent.txt",
125 old_string: "test",
126 new_string: "replacement",
127 };
128
129 await expect(
130 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
131 ).rejects.toThrowError(
132 expect.objectContaining({
133 reason: ContinueErrorReason.FileNotFound,
134 }),
135 );
136 });
137
138 it("should resolve relative file paths", async () => {
139 mockResolveRelativePathInDir.mockResolvedValue("/absolute/path/test.txt");
140 mockExtras.ideMessenger.ide.readFile = vi
141 .fn()
142 .mockResolvedValue("test content");
143
144 const args = {
145 filepath: "test.txt",
146 old_string: "test",
147 new_string: "replacement",
148 };
149
150 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
151
152 expect(mockResolveRelativePathInDir).toHaveBeenCalledWith(
153 "test.txt",
154 mockExtras.ideMessenger.ide,
155 );
156 expect(mockExtras.ideMessenger.ide.readFile).toHaveBeenCalledWith(
157 "/absolute/path/test.txt",
158 );
159 });
160 });
161
162 describe("string replacement", () => {
163 beforeEach(() => {
164 mockResolveRelativePathInDir.mockResolvedValue("/test/file.txt");
165 });
166
167 it("should throw error if old_string is not found in file", async () => {
168 mockExtras.ideMessenger.ide.readFile = vi
169 .fn()
170 .mockResolvedValue("different content");
171
172 const args = {
173 filepath: "file.txt",
174 old_string: "not found",
175 new_string: "replacement",
176 };
177
178 await expect(
179 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
180 ).rejects.toThrowError(
181 expect.objectContaining({
182 reason: ContinueErrorReason.FindAndReplaceOldStringNotFound,
183 }),
184 );
185 });
186
187 it("should replace single occurrence", async () => {
188 mockExtras.ideMessenger.ide.readFile = vi
189 .fn()
190 .mockResolvedValue("Hello world\nThis is a test file\nGoodbye world");
191
192 const args = {
193 filepath: "file.txt",
194 old_string: "Hello world",
195 new_string: "Hi there",
196 };
197
198 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
199
200 // Check that the dispatch was called with the applyForEditTool thunk
201 expect(mockApplyForEditTool).toHaveBeenCalledWith({
202 streamId: "test-uuid",
203 toolCallId: "tool-call-id",
204 text: "Hi there\nThis is a test file\nGoodbye world",
205 filepath: "/test/file.txt",
206 isSearchAndReplace: true,
207 });
208 });
209
210 it("should throw error if old_string appears multiple times and replace_all is false", async () => {
211 mockExtras.ideMessenger.ide.readFile = vi
212 .fn()
213 .mockResolvedValue("Hello world\nThis is a test file\nGoodbye world");
214
215 const args = {
216 filepath: "file.txt",
217 old_string: "world",
218 new_string: "universe",
219 replace_all: false,
220 };
221
222 await expect(
223 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
224 ).rejects.toThrowError(
225 expect.objectContaining({
226 reason: ContinueErrorReason.FindAndReplaceMultipleOccurrences,
227 }),
228 );
229 });
230
231 it("should replace all occurrences when replace_all is true", async () => {
232 mockExtras.ideMessenger.ide.readFile = vi
233 .fn()
234 .mockResolvedValue("Hello world\nThis is a test file\nGoodbye world");
235
236 const args = {
237 filepath: "file.txt",
238 old_string: "world",
239 new_string: "universe",
240 replace_all: true,
241 };
242
243 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
244
245 expect(mockApplyForEditTool).toHaveBeenCalledWith({
246 streamId: "test-uuid",
247 toolCallId: "tool-call-id",
248 text: "Hello universe\nThis is a test file\nGoodbye universe",
249 filepath: "/test/file.txt",
250 isSearchAndReplace: true,
251 });
252 });
253
254 it("should handle empty new_string (deletion)", async () => {
255 mockExtras.ideMessenger.ide.readFile = vi
256 .fn()
257 .mockResolvedValue("Hello world\nThis is a test file");
258
259 const args = {
260 filepath: "file.txt",
261 old_string: "Hello ",
262 new_string: "",
263 };
264
265 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
266
267 expect(mockApplyForEditTool).toHaveBeenCalledWith({
268 streamId: "test-uuid",
269 toolCallId: "tool-call-id",
270 text: "world\nThis is a test file",
271 filepath: "/test/file.txt",
272 isSearchAndReplace: true,
273 });
274 });
275
276 it("should handle special characters in strings", async () => {
277 mockExtras.ideMessenger.ide.readFile = vi
278 .fn()
279 .mockResolvedValue(
280 'const regex = /[a-z]+/g;\nconst text = "Hello $world"',
281 );
282
283 const args = {
284 filepath: "file.txt",
285 old_string: '"Hello $world"',
286 new_string: '"Hi $universe"',
287 };
288
289 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
290
291 expect(mockApplyForEditTool).toHaveBeenCalledWith({
292 streamId: "test-uuid",
293 toolCallId: "tool-call-id",
294 text: 'const regex = /[a-z]+/g;\nconst text = "Hi $universe"',
295 filepath: "/test/file.txt",
296 isSearchAndReplace: true,
297 });
298 });
299
300 it("should preserve whitespace and indentation", async () => {
301 mockExtras.ideMessenger.ide.readFile = vi
302 .fn()
303 .mockResolvedValue(
304 "function test() {\n const value = 'old';\n return value;\n}",
305 );
306
307 const args = {
308 filepath: "file.txt",
309 old_string: " const value = 'old';",
310 new_string: " const value = 'new';",
311 };
312
313 await singleFindAndReplaceImpl(args, "tool-call-id", mockExtras);
314
315 expect(mockApplyForEditTool).toHaveBeenCalledWith({
316 streamId: "test-uuid",
317 toolCallId: "tool-call-id",
318 text: "function test() {\n const value = 'new';\n return value;\n}",
319 filepath: "/test/file.txt",
320 isSearchAndReplace: true,
321 });
322 });
323 });
324
325 describe("return value", () => {
326 it("should return correct response structure", async () => {
327 mockResolveRelativePathInDir.mockResolvedValue("/test/file.txt");
328 mockExtras.ideMessenger.ide.readFile = vi
329 .fn()
330 .mockResolvedValue("test content");
331
332 const args = {
333 filepath: "file.txt",
334 old_string: "test",
335 new_string: "replacement",
336 };
337
338 const result = await singleFindAndReplaceImpl(
339 args,
340 "tool-call-id",
341 mockExtras,
342 );
343
344 expect(result).toEqual({
345 respondImmediately: false,
346 output: undefined,
347 });
348 });
349 });
350
351 describe("error handling", () => {
352 it("should wrap and rethrow errors from readFile", async () => {
353 mockResolveRelativePathInDir.mockResolvedValue("/test/file.txt");
354 mockExtras.ideMessenger.ide.readFile = vi
355 .fn()
356 .mockRejectedValue(new Error("Permission denied"));
357
358 const args = {
359 filepath: "file.txt",
360 old_string: "test",
361 new_string: "replacement",
362 };
363
364 await expect(
365 singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
366 ).rejects.toThrow("Permission denied");
367 });
368 });
369});