| 1 | import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch"; |
| 2 | import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSearchReplaceBlock"; |
| 3 | import { resolveRelativePathInDir } from "core/util/ideUtils"; |
| 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | import { ClientToolExtras } from "./callClientTool"; |
| 6 | import { searchReplaceToolImpl } from "./searchReplaceImpl"; |
| 7 | |
| 8 | // Mock the dependencies |
| 9 | vi.mock("core/edit/searchAndReplace/findSearchMatch"); |
| 10 | vi.mock("core/edit/searchAndReplace/parseSearchReplaceBlock"); |
| 11 | vi.mock("core/util/ideUtils"); |
| 12 | vi.mock("uuid", () => ({ |
| 13 | v4: vi.fn(() => "test-stream-id"), |
| 14 | })); |
| 15 | |
| 16 | const mockFindSearchMatch = vi.mocked(findSearchMatch); |
| 17 | const mockParseAllSearchReplaceBlocks = vi.mocked(parseAllSearchReplaceBlocks); |
| 18 | const mockResolveRelativePathInDir = vi.mocked(resolveRelativePathInDir); |
| 19 | |
| 20 | describe("searchReplaceToolImpl", () => { |
| 21 | let mockExtras: ClientToolExtras; |
| 22 | let mockIdeMessenger: any; |
| 23 | |
| 24 | beforeEach(() => { |
| 25 | // Reset all mocks |
| 26 | vi.clearAllMocks(); |
| 27 | |
| 28 | // Create mock IDE messenger |
| 29 | mockIdeMessenger = { |
| 30 | ide: { |
| 31 | readFile: vi.fn(), |
| 32 | }, |
| 33 | request: vi.fn(), |
| 34 | }; |
| 35 | |
| 36 | // Create mock extras |
| 37 | mockExtras = { |
| 38 | ideMessenger: mockIdeMessenger, |
| 39 | getState: vi.fn(), |
| 40 | dispatch: vi.fn(), |
| 41 | }; |
| 42 | }); |
| 43 | |
| 44 | describe("basic validation", () => { |
| 45 | it("should throw error when file does not exist", async () => { |
| 46 | mockResolveRelativePathInDir.mockResolvedValue(undefined); |
| 47 | |
| 48 | await expect( |
| 49 | searchReplaceToolImpl( |
| 50 | { filepath: "nonexistent.txt", diff: "some diff" }, |
| 51 | "tool-call-id", |
| 52 | mockExtras, |
| 53 | ), |
| 54 | ).rejects.toThrow("File nonexistent.txt does not exist"); |
| 55 | }); |
| 56 | |
| 57 | it("should throw error when no search/replace blocks found", async () => { |
| 58 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 59 | mockParseAllSearchReplaceBlocks.mockReturnValue([]); |
| 60 | |
| 61 | await expect( |
| 62 | searchReplaceToolImpl( |
| 63 | { filepath: "test.txt", diff: "invalid diff content" }, |
| 64 | "tool-call-id", |
| 65 | mockExtras, |
| 66 | ), |
| 67 | ).rejects.toThrow("No complete search/replace blocks found"); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe("single block replacement", () => { |
| 72 | it("should successfully apply single search/replace block", async () => { |
| 73 | const originalContent = `function hello() { |
| 74 | console.log("Hello"); |
| 75 | return "world"; |
| 76 | }`; |
| 77 | |
| 78 | const expectedFinalContent = `function hello() { |
| 79 | console.log("Hi there!"); |
| 80 | return "world"; |
| 81 | }`; |
| 82 | |
| 83 | // Setup mocks |
| 84 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.js"); |
| 85 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 86 | { |
| 87 | isComplete: true, |
| 88 | searchContent: 'console.log("Hello");', |
| 89 | replaceContent: 'console.log("Hi there!");', |
| 90 | }, |
| 91 | ]); |
| 92 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 93 | |
| 94 | // Calculate correct positions for 'console.log("Hello");' in the original content |
| 95 | const searchText = 'console.log("Hello");'; |
| 96 | const startIndex = originalContent.indexOf(searchText); |
| 97 | const endIndex = startIndex + searchText.length; |
| 98 | |
| 99 | mockFindSearchMatch.mockReturnValue({ |
| 100 | startIndex, |
| 101 | endIndex, |
| 102 | }); |
| 103 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 104 | |
| 105 | const result = await searchReplaceToolImpl( |
| 106 | { filepath: "test.js", diff: "mock diff content" }, |
| 107 | "tool-call-id", |
| 108 | mockExtras, |
| 109 | ); |
| 110 | |
| 111 | // Verify the result |
| 112 | expect(result).toEqual({ |
| 113 | respondImmediately: false, |
| 114 | output: undefined, |
| 115 | }); |
| 116 | |
| 117 | // Verify applyToFile was called with correct parameters |
| 118 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 119 | text: expectedFinalContent, |
| 120 | streamId: "test-stream-id", |
| 121 | filepath: "/resolved/path/test.js", |
| 122 | toolCallId: "tool-call-id", |
| 123 | isSearchAndReplace: true, |
| 124 | }); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe("multiple block replacement", () => { |
| 129 | it("should successfully apply multiple search/replace blocks sequentially", async () => { |
| 130 | const originalContent = `const a = 1; |
| 131 | const b = 2; |
| 132 | const c = 3;`; |
| 133 | |
| 134 | const expectedFinalContent = `const a = 100; |
| 135 | const b = 200; |
| 136 | const c = 3;`; |
| 137 | |
| 138 | // Setup mocks |
| 139 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.js"); |
| 140 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 141 | { |
| 142 | isComplete: true, |
| 143 | searchContent: "const a = 1;", |
| 144 | replaceContent: "const a = 100;", |
| 145 | }, |
| 146 | { |
| 147 | isComplete: true, |
| 148 | searchContent: "const b = 2;", |
| 149 | replaceContent: "const b = 200;", |
| 150 | }, |
| 151 | ]); |
| 152 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 153 | |
| 154 | // Calculate positions for sequential replacements |
| 155 | const firstSearchText = "const a = 1;"; |
| 156 | const secondSearchText = "const b = 2;"; |
| 157 | |
| 158 | const firstStartIndex = originalContent.indexOf(firstSearchText); |
| 159 | const firstEndIndex = firstStartIndex + firstSearchText.length; |
| 160 | |
| 161 | // After first replacement: "const a = 100;\nconst b = 2;\nconst c = 3;" |
| 162 | const contentAfterFirstReplacement = |
| 163 | originalContent.substring(0, firstStartIndex) + |
| 164 | "const a = 100;" + |
| 165 | originalContent.substring(firstEndIndex); |
| 166 | |
| 167 | const secondStartIndex = |
| 168 | contentAfterFirstReplacement.indexOf(secondSearchText); |
| 169 | const secondEndIndex = secondStartIndex + secondSearchText.length; |
| 170 | |
| 171 | // Mock sequential search matches |
| 172 | mockFindSearchMatch |
| 173 | .mockReturnValueOnce({ |
| 174 | startIndex: firstStartIndex, |
| 175 | endIndex: firstEndIndex, |
| 176 | }) |
| 177 | .mockReturnValueOnce({ |
| 178 | startIndex: secondStartIndex, |
| 179 | endIndex: secondEndIndex, |
| 180 | }); |
| 181 | |
| 182 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 183 | |
| 184 | const result = await searchReplaceToolImpl( |
| 185 | { filepath: "test.js", diff: "mock diff content" }, |
| 186 | "tool-call-id", |
| 187 | mockExtras, |
| 188 | ); |
| 189 | |
| 190 | // Verify the result |
| 191 | expect(result).toEqual({ |
| 192 | respondImmediately: false, |
| 193 | output: undefined, |
| 194 | }); |
| 195 | |
| 196 | // Verify findSearchMatch was called twice with updated content |
| 197 | expect(mockFindSearchMatch).toHaveBeenCalledTimes(2); |
| 198 | expect(mockFindSearchMatch).toHaveBeenNthCalledWith( |
| 199 | 1, |
| 200 | originalContent, |
| 201 | "const a = 1;", |
| 202 | ); |
| 203 | expect(mockFindSearchMatch).toHaveBeenNthCalledWith( |
| 204 | 2, |
| 205 | contentAfterFirstReplacement, |
| 206 | "const b = 2;", |
| 207 | ); |
| 208 | // Verify final applyToFile call |
| 209 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 210 | text: expectedFinalContent, |
| 211 | streamId: "test-stream-id", |
| 212 | filepath: "/resolved/path/test.js", |
| 213 | toolCallId: "tool-call-id", |
| 214 | isSearchAndReplace: true, |
| 215 | }); |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | describe("deletion scenarios", () => { |
| 220 | it("should handle empty replacement (deletion)", async () => { |
| 221 | const originalContent = `keep this |
| 222 | remove this line |
| 223 | keep this too`; |
| 224 | |
| 225 | const expectedFinalContent = `keep this |
| 226 | |
| 227 | keep this too`; |
| 228 | |
| 229 | // Setup mocks |
| 230 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 231 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 232 | { |
| 233 | isComplete: true, |
| 234 | searchContent: "remove this line", |
| 235 | replaceContent: "", |
| 236 | }, |
| 237 | ]); |
| 238 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 239 | mockFindSearchMatch.mockReturnValue({ |
| 240 | startIndex: 10, // Position of "remove this line" |
| 241 | endIndex: 26, // End of "remove this line" |
| 242 | }); |
| 243 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 244 | |
| 245 | const result = await searchReplaceToolImpl( |
| 246 | { filepath: "test.txt", diff: "mock diff content" }, |
| 247 | "tool-call-id", |
| 248 | mockExtras, |
| 249 | ); |
| 250 | |
| 251 | expect(result).toEqual({ |
| 252 | respondImmediately: false, |
| 253 | output: undefined, |
| 254 | }); |
| 255 | |
| 256 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 257 | text: expectedFinalContent, |
| 258 | streamId: "test-stream-id", |
| 259 | filepath: "/resolved/path/test.txt", |
| 260 | toolCallId: "tool-call-id", |
| 261 | isSearchAndReplace: true, |
| 262 | }); |
| 263 | }); |
| 264 | |
| 265 | it("should handle undefined replaceContent as empty string", async () => { |
| 266 | const originalContent = "line1\nline2\nline3"; |
| 267 | const expectedFinalContent = "line1\n\nline3"; |
| 268 | |
| 269 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 270 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 271 | { |
| 272 | isComplete: true, |
| 273 | searchContent: "line2", |
| 274 | replaceContent: undefined, // undefined should be treated as empty string |
| 275 | }, |
| 276 | ]); |
| 277 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 278 | mockFindSearchMatch.mockReturnValue({ |
| 279 | startIndex: 6, |
| 280 | endIndex: 11, |
| 281 | }); |
| 282 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 283 | |
| 284 | await searchReplaceToolImpl( |
| 285 | { filepath: "test.txt", diff: "mock diff content" }, |
| 286 | "tool-call-id", |
| 287 | mockExtras, |
| 288 | ); |
| 289 | |
| 290 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 291 | text: expectedFinalContent, |
| 292 | streamId: "test-stream-id", |
| 293 | filepath: "/resolved/path/test.txt", |
| 294 | toolCallId: "tool-call-id", |
| 295 | isSearchAndReplace: true, |
| 296 | }); |
| 297 | }); |
| 298 | }); |
| 299 | |
| 300 | describe("error handling", () => { |
| 301 | it("should throw error when search content is not found", async () => { |
| 302 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 303 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 304 | { |
| 305 | isComplete: true, |
| 306 | searchContent: "nonexistent content", |
| 307 | replaceContent: "replacement", |
| 308 | }, |
| 309 | ]); |
| 310 | mockIdeMessenger.ide.readFile.mockResolvedValue("some file content"); |
| 311 | mockFindSearchMatch.mockReturnValue(null); // Search content not found |
| 312 | |
| 313 | await expect( |
| 314 | searchReplaceToolImpl( |
| 315 | { filepath: "test.txt", diff: "mock diff content" }, |
| 316 | "tool-call-id", |
| 317 | mockExtras, |
| 318 | ), |
| 319 | ).rejects.toThrow( |
| 320 | "Search content not found in block 1:\nnonexistent content", |
| 321 | ); |
| 322 | }); |
| 323 | |
| 324 | it("should throw error when search content is not found in second block", async () => { |
| 325 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 326 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 327 | { |
| 328 | isComplete: true, |
| 329 | searchContent: "found content", |
| 330 | replaceContent: "replacement1", |
| 331 | }, |
| 332 | { |
| 333 | isComplete: true, |
| 334 | searchContent: "missing content", |
| 335 | replaceContent: "replacement2", |
| 336 | }, |
| 337 | ]); |
| 338 | mockIdeMessenger.ide.readFile.mockResolvedValue( |
| 339 | "found content and other stuff", |
| 340 | ); |
| 341 | |
| 342 | // First search succeeds, second fails |
| 343 | mockFindSearchMatch |
| 344 | .mockReturnValueOnce({ |
| 345 | startIndex: 0, |
| 346 | endIndex: 13, |
| 347 | }) |
| 348 | .mockReturnValueOnce(null); // Second search fails |
| 349 | |
| 350 | await expect( |
| 351 | searchReplaceToolImpl( |
| 352 | { filepath: "test.txt", diff: "mock diff content" }, |
| 353 | "tool-call-id", |
| 354 | mockExtras, |
| 355 | ), |
| 356 | ).rejects.toThrow( |
| 357 | "Search content not found in block 2:\nmissing content", |
| 358 | ); |
| 359 | }); |
| 360 | |
| 361 | it("should handle file read errors", async () => { |
| 362 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 363 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 364 | { |
| 365 | isComplete: true, |
| 366 | searchContent: "content", |
| 367 | replaceContent: "replacement", |
| 368 | }, |
| 369 | ]); |
| 370 | mockIdeMessenger.ide.readFile.mockRejectedValue( |
| 371 | new Error("File read error"), |
| 372 | ); |
| 373 | |
| 374 | await expect( |
| 375 | searchReplaceToolImpl( |
| 376 | { filepath: "test.txt", diff: "mock diff content" }, |
| 377 | "tool-call-id", |
| 378 | mockExtras, |
| 379 | ), |
| 380 | ).rejects.toThrow("Failed to apply search and replace: File read error"); |
| 381 | }); |
| 382 | |
| 383 | it("should handle applyToFile errors", async () => { |
| 384 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 385 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 386 | { |
| 387 | isComplete: true, |
| 388 | searchContent: "content", |
| 389 | replaceContent: "replacement", |
| 390 | }, |
| 391 | ]); |
| 392 | mockIdeMessenger.ide.readFile.mockResolvedValue("content"); |
| 393 | mockFindSearchMatch.mockReturnValue({ |
| 394 | startIndex: 0, |
| 395 | endIndex: 7, |
| 396 | }); |
| 397 | mockIdeMessenger.request.mockRejectedValue(new Error("Apply failed")); |
| 398 | |
| 399 | await expect( |
| 400 | searchReplaceToolImpl( |
| 401 | { filepath: "test.txt", diff: "mock diff content" }, |
| 402 | "tool-call-id", |
| 403 | mockExtras, |
| 404 | ), |
| 405 | ).rejects.toThrow("Failed to apply search and replace: Apply failed"); |
| 406 | }); |
| 407 | }); |
| 408 | |
| 409 | describe("edge cases", () => { |
| 410 | it("should handle empty search content", async () => { |
| 411 | const originalContent = "existing content"; |
| 412 | const expectedFinalContent = "new contentexisting content"; |
| 413 | |
| 414 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 415 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 416 | { |
| 417 | isComplete: true, |
| 418 | searchContent: "", |
| 419 | replaceContent: "new content", |
| 420 | }, |
| 421 | ]); |
| 422 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 423 | mockFindSearchMatch.mockReturnValue({ |
| 424 | startIndex: 0, // Empty search matches at beginning |
| 425 | endIndex: 0, |
| 426 | }); |
| 427 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 428 | |
| 429 | await searchReplaceToolImpl( |
| 430 | { filepath: "test.txt", diff: "mock diff content" }, |
| 431 | "tool-call-id", |
| 432 | mockExtras, |
| 433 | ); |
| 434 | |
| 435 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 436 | text: expectedFinalContent, |
| 437 | streamId: "test-stream-id", |
| 438 | filepath: "/resolved/path/test.txt", |
| 439 | toolCallId: "tool-call-id", |
| 440 | isSearchAndReplace: true, |
| 441 | }); |
| 442 | }); |
| 443 | |
| 444 | it("should handle whole file replacement", async () => { |
| 445 | const originalContent = "old file content"; |
| 446 | const expectedFinalContent = "completely new content"; |
| 447 | |
| 448 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 449 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 450 | { |
| 451 | isComplete: true, |
| 452 | searchContent: originalContent, |
| 453 | replaceContent: "completely new content", |
| 454 | }, |
| 455 | ]); |
| 456 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 457 | mockFindSearchMatch.mockReturnValue({ |
| 458 | startIndex: 0, |
| 459 | endIndex: originalContent.length, |
| 460 | }); |
| 461 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 462 | |
| 463 | await searchReplaceToolImpl( |
| 464 | { filepath: "test.txt", diff: "mock diff content" }, |
| 465 | "tool-call-id", |
| 466 | mockExtras, |
| 467 | ); |
| 468 | |
| 469 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 470 | text: expectedFinalContent, |
| 471 | streamId: "test-stream-id", |
| 472 | filepath: "/resolved/path/test.txt", |
| 473 | toolCallId: "tool-call-id", |
| 474 | isSearchAndReplace: true, |
| 475 | }); |
| 476 | }); |
| 477 | }); |
| 478 | |
| 479 | describe("dependency integration", () => { |
| 480 | it("should call all dependencies with correct parameters", async () => { |
| 481 | const originalContent = "test content"; |
| 482 | |
| 483 | mockResolveRelativePathInDir.mockResolvedValue("/resolved/path/test.txt"); |
| 484 | mockParseAllSearchReplaceBlocks.mockReturnValue([ |
| 485 | { |
| 486 | isComplete: true, |
| 487 | searchContent: "test", |
| 488 | replaceContent: "updated", |
| 489 | }, |
| 490 | ]); |
| 491 | mockIdeMessenger.ide.readFile.mockResolvedValue(originalContent); |
| 492 | mockFindSearchMatch.mockReturnValue({ |
| 493 | startIndex: 0, |
| 494 | endIndex: 4, |
| 495 | }); |
| 496 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 497 | |
| 498 | await searchReplaceToolImpl( |
| 499 | { filepath: "relative/test.txt", diff: "mock diff content" }, |
| 500 | "tool-call-id", |
| 501 | mockExtras, |
| 502 | ); |
| 503 | |
| 504 | // Verify all dependencies were called correctly |
| 505 | expect(mockResolveRelativePathInDir).toHaveBeenCalledWith( |
| 506 | "relative/test.txt", |
| 507 | mockIdeMessenger.ide, |
| 508 | ); |
| 509 | expect(mockParseAllSearchReplaceBlocks).toHaveBeenCalledWith( |
| 510 | "mock diff content", |
| 511 | ); |
| 512 | expect(mockIdeMessenger.ide.readFile).toHaveBeenCalledWith( |
| 513 | "/resolved/path/test.txt", |
| 514 | ); |
| 515 | expect(mockFindSearchMatch).toHaveBeenCalledWith(originalContent, "test"); |
| 516 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("applyToFile", { |
| 517 | text: "updated content", |
| 518 | streamId: "test-stream-id", |
| 519 | filepath: "/resolved/path/test.txt", |
| 520 | toolCallId: "tool-call-id", |
| 521 | isSearchAndReplace: true, |
| 522 | }); |
| 523 | }); |
| 524 | }); |
| 525 | }); |