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