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