| 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 { FOUND_MULTIPLE_FIND_STRINGS_ERROR } from "./findAndReplaceUtils"; |
| 6 | import { multiEditImpl } from "./multiEditImpl"; |
| 7 | |
| 8 | vi.mock("uuid", () => ({ |
| 9 | v4: vi.fn(() => "test-uuid"), |
| 10 | })); |
| 11 | |
| 12 | vi.mock("core/util/ideUtils", () => ({ |
| 13 | resolveRelativePathInDir: vi.fn(), |
| 14 | inferResolvedUriFromRelativePath: vi.fn(), |
| 15 | })); |
| 16 | |
| 17 | vi.mock("../../redux/thunks/handleApplyStateUpdate", () => ({ |
| 18 | applyForEditTool: vi.fn(), |
| 19 | })); |
| 20 | |
| 21 | describe("multiEditImpl", () => { |
| 22 | let mockExtras: ClientToolExtras; |
| 23 | let mockResolveRelativePathInDir: Mock; |
| 24 | let mockInferResolvedUriFromRelativePath: Mock; |
| 25 | let mockApplyForEditTool: Mock; |
| 26 | |
| 27 | beforeEach(() => { |
| 28 | vi.clearAllMocks(); |
| 29 | |
| 30 | mockResolveRelativePathInDir = vi.mocked(ideUtils.resolveRelativePathInDir); |
| 31 | mockInferResolvedUriFromRelativePath = vi.mocked( |
| 32 | ideUtils.inferResolvedUriFromRelativePath, |
| 33 | ); |
| 34 | mockApplyForEditTool = vi.mocked(applyForEditTool); |
| 35 | |
| 36 | mockExtras = { |
| 37 | getState: vi.fn(() => ({ |
| 38 | config: { config: { allowAnonymousTelemetry: false } }, |
| 39 | })) as any, |
| 40 | dispatch: vi.fn() as any, |
| 41 | ideMessenger: { |
| 42 | ide: { |
| 43 | readFile: vi.fn(), |
| 44 | getWorkspaceDirs: vi.fn().mockResolvedValue(["dir1"]), |
| 45 | }, |
| 46 | request: vi.fn(), |
| 47 | } as any, |
| 48 | }; |
| 49 | }); |
| 50 | |
| 51 | describe("argument validation", () => { |
| 52 | it("should throw if filepath is missing", async () => { |
| 53 | await expect( |
| 54 | multiEditImpl({ edits: [] }, "id", mockExtras), |
| 55 | ).rejects.toThrow("filepath is required"); |
| 56 | }); |
| 57 | |
| 58 | it("should throw if edits array is empty", async () => { |
| 59 | await expect( |
| 60 | multiEditImpl({ filepath: "test.txt", edits: [] }, "id", mockExtras), |
| 61 | ).rejects.toThrow( |
| 62 | "edits array is required and must contain at least one edit", |
| 63 | ); |
| 64 | }); |
| 65 | |
| 66 | it("should throw if edit has invalid old_string", async () => { |
| 67 | await expect( |
| 68 | multiEditImpl( |
| 69 | { |
| 70 | filepath: "test.txt", |
| 71 | edits: [{ old_string: null, new_string: "test" }], |
| 72 | }, |
| 73 | "id", |
| 74 | mockExtras, |
| 75 | ), |
| 76 | ).rejects.toThrow("edit at index 0: old_string is required"); |
| 77 | }); |
| 78 | |
| 79 | it("should throw if edit has undefined new_string", async () => { |
| 80 | await expect( |
| 81 | multiEditImpl( |
| 82 | { |
| 83 | filepath: "test.txt", |
| 84 | edits: [{ old_string: "test", new_string: undefined }], |
| 85 | }, |
| 86 | "id", |
| 87 | mockExtras, |
| 88 | ), |
| 89 | ).rejects.toThrow("edit at index 0: new_string is required"); |
| 90 | }); |
| 91 | |
| 92 | it("should throw if old_string equals new_string", async () => { |
| 93 | await expect( |
| 94 | multiEditImpl( |
| 95 | { |
| 96 | filepath: "test.txt", |
| 97 | edits: [{ old_string: "same", new_string: "same" }], |
| 98 | }, |
| 99 | "id", |
| 100 | mockExtras, |
| 101 | ), |
| 102 | ).rejects.toThrow( |
| 103 | "edit at index 0: old_string and new_string must be different", |
| 104 | ); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | describe("sequential edits", () => { |
| 109 | beforeEach(() => { |
| 110 | mockResolveRelativePathInDir.mockResolvedValue( |
| 111 | "file:///dir/test/file.txt", |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | it("should apply single edit", async () => { |
| 116 | mockExtras.ideMessenger.ide.readFile = vi |
| 117 | .fn() |
| 118 | .mockResolvedValue("Hello world"); |
| 119 | |
| 120 | await multiEditImpl( |
| 121 | { |
| 122 | filepath: "file.txt", |
| 123 | edits: [{ old_string: "Hello", new_string: "Hi" }], |
| 124 | }, |
| 125 | "id", |
| 126 | mockExtras, |
| 127 | ); |
| 128 | |
| 129 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 130 | streamId: "test-uuid", |
| 131 | toolCallId: "id", |
| 132 | text: "Hi world", |
| 133 | filepath: "file:///dir/test/file.txt", |
| 134 | isSearchAndReplace: true, |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | it("should apply multiple edits sequentially", async () => { |
| 139 | mockExtras.ideMessenger.ide.readFile = vi |
| 140 | .fn() |
| 141 | .mockResolvedValue("Hello world\nGoodbye world"); |
| 142 | |
| 143 | await multiEditImpl( |
| 144 | { |
| 145 | filepath: "file.txt", |
| 146 | edits: [ |
| 147 | { old_string: "Hello", new_string: "Hi" }, |
| 148 | { old_string: "world", new_string: "universe", replace_all: true }, |
| 149 | ], |
| 150 | }, |
| 151 | "id", |
| 152 | mockExtras, |
| 153 | ); |
| 154 | |
| 155 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 156 | streamId: "test-uuid", |
| 157 | toolCallId: "id", |
| 158 | text: "Hi universe\nGoodbye universe", |
| 159 | filepath: "file:///dir/test/file.txt", |
| 160 | isSearchAndReplace: true, |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | it("should handle edits that depend on previous edits", async () => { |
| 165 | mockExtras.ideMessenger.ide.readFile = vi |
| 166 | .fn() |
| 167 | .mockResolvedValue("const x = 1;"); |
| 168 | |
| 169 | await multiEditImpl( |
| 170 | { |
| 171 | filepath: "file.txt", |
| 172 | edits: [ |
| 173 | { old_string: "const x", new_string: "let x" }, |
| 174 | { old_string: "let x = 1;", new_string: "let x = 2;" }, |
| 175 | ], |
| 176 | }, |
| 177 | "id", |
| 178 | mockExtras, |
| 179 | ); |
| 180 | |
| 181 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 182 | streamId: "test-uuid", |
| 183 | toolCallId: "id", |
| 184 | text: "let x = 2;", |
| 185 | filepath: "file:///dir/test/file.txt", |
| 186 | isSearchAndReplace: true, |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | it("should throw if string not found in edit sequence", async () => { |
| 191 | mockExtras.ideMessenger.ide.readFile = vi |
| 192 | .fn() |
| 193 | .mockResolvedValue("Hello world"); |
| 194 | |
| 195 | await expect( |
| 196 | multiEditImpl( |
| 197 | { |
| 198 | filepath: "file.txt", |
| 199 | edits: [ |
| 200 | { old_string: "Hello", new_string: "Hi" }, |
| 201 | { old_string: "not found", new_string: "test" }, |
| 202 | ], |
| 203 | }, |
| 204 | "id", |
| 205 | mockExtras, |
| 206 | ), |
| 207 | ).rejects.toThrow( |
| 208 | 'edit at index 1: string not found in file: "not found"', |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | it("should throw if multiple occurrences without replace_all", async () => { |
| 213 | mockExtras.ideMessenger.ide.readFile = vi |
| 214 | .fn() |
| 215 | .mockResolvedValue("test test test"); |
| 216 | |
| 217 | await expect( |
| 218 | multiEditImpl( |
| 219 | { |
| 220 | filepath: "file.txt", |
| 221 | edits: [{ old_string: "test", new_string: "replaced" }], |
| 222 | }, |
| 223 | "id", |
| 224 | mockExtras, |
| 225 | ), |
| 226 | ).rejects.toThrow( |
| 227 | `edit at index 0: String "test" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`, |
| 228 | ); |
| 229 | }); |
| 230 | }); |
| 231 | |
| 232 | describe("file creation", () => { |
| 233 | it("should create new file with empty old_string", async () => { |
| 234 | mockResolveRelativePathInDir.mockResolvedValue(null); |
| 235 | mockInferResolvedUriFromRelativePath.mockResolvedValue( |
| 236 | "file:///infered/new.txt", |
| 237 | ); |
| 238 | |
| 239 | await multiEditImpl( |
| 240 | { |
| 241 | filepath: "new.txt", |
| 242 | edits: [{ old_string: "", new_string: "New content\nLine 2" }], |
| 243 | }, |
| 244 | "id", |
| 245 | mockExtras, |
| 246 | ); |
| 247 | |
| 248 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 249 | streamId: "test-uuid", |
| 250 | toolCallId: "id", |
| 251 | text: "New content\nLine 2", |
| 252 | filepath: "file:///infered/new.txt", |
| 253 | isSearchAndReplace: true, |
| 254 | }); |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | describe("replace_all functionality", () => { |
| 259 | beforeEach(() => { |
| 260 | mockResolveRelativePathInDir.mockResolvedValue( |
| 261 | "file:///dir/test/file.txt", |
| 262 | ); |
| 263 | }); |
| 264 | |
| 265 | it("should replace all occurrences when specified", async () => { |
| 266 | mockExtras.ideMessenger.ide.readFile = vi |
| 267 | .fn() |
| 268 | .mockResolvedValue("foo bar foo baz foo"); |
| 269 | |
| 270 | await multiEditImpl( |
| 271 | { |
| 272 | filepath: "file.txt", |
| 273 | edits: [{ old_string: "foo", new_string: "qux", replace_all: true }], |
| 274 | }, |
| 275 | "id", |
| 276 | mockExtras, |
| 277 | ); |
| 278 | |
| 279 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 280 | streamId: "test-uuid", |
| 281 | toolCallId: "id", |
| 282 | text: "qux bar qux baz qux", |
| 283 | filepath: "file:///dir/test/file.txt", |
| 284 | isSearchAndReplace: true, |
| 285 | }); |
| 286 | }); |
| 287 | |
| 288 | it("should handle mixed replace_all settings", async () => { |
| 289 | mockExtras.ideMessenger.ide.readFile = vi |
| 290 | .fn() |
| 291 | .mockResolvedValue("x y x z x"); |
| 292 | |
| 293 | await multiEditImpl( |
| 294 | { |
| 295 | filepath: "file.txt", |
| 296 | edits: [ |
| 297 | { old_string: "x", new_string: "a", replace_all: true }, |
| 298 | { old_string: "y", new_string: "b" }, |
| 299 | ], |
| 300 | }, |
| 301 | "id", |
| 302 | mockExtras, |
| 303 | ); |
| 304 | |
| 305 | expect(mockApplyForEditTool).toHaveBeenCalledWith({ |
| 306 | streamId: "test-uuid", |
| 307 | toolCallId: "id", |
| 308 | text: "a b a z a", |
| 309 | filepath: "file:///dir/test/file.txt", |
| 310 | isSearchAndReplace: true, |
| 311 | }); |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | describe("error handling", () => { |
| 316 | it("should wrap readFile errors", async () => { |
| 317 | mockResolveRelativePathInDir.mockResolvedValue( |
| 318 | "file:///dir/test/file.txt", |
| 319 | ); |
| 320 | mockExtras.ideMessenger.ide.readFile = vi |
| 321 | .fn() |
| 322 | .mockRejectedValue(new Error("Read failed")); |
| 323 | |
| 324 | await expect( |
| 325 | multiEditImpl( |
| 326 | { |
| 327 | filepath: "file.txt", |
| 328 | edits: [{ old_string: "test", new_string: "new" }], |
| 329 | }, |
| 330 | "id", |
| 331 | mockExtras, |
| 332 | ), |
| 333 | ).rejects.toThrow("Read failed"); |
| 334 | }); |
| 335 | }); |
| 336 | |
| 337 | describe("return value", () => { |
| 338 | it("should return correct structure", async () => { |
| 339 | mockResolveRelativePathInDir.mockResolvedValue( |
| 340 | "file:///dir/test/file.txt", |
| 341 | ); |
| 342 | mockExtras.ideMessenger.ide.readFile = vi.fn().mockResolvedValue("test"); |
| 343 | |
| 344 | const result = await multiEditImpl( |
| 345 | { |
| 346 | filepath: "file.txt", |
| 347 | edits: [{ old_string: "test", new_string: "new" }], |
| 348 | }, |
| 349 | "id", |
| 350 | mockExtras, |
| 351 | ); |
| 352 | |
| 353 | expect(result).toEqual({ |
| 354 | respondImmediately: false, |
| 355 | output: undefined, |
| 356 | }); |
| 357 | }); |
| 358 | }); |
| 359 | }); |