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