| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { EditOperation } from "../../tools/definitions/multiEdit"; |
| 3 | import { ContinueErrorReason } from "../../util/errors"; |
| 4 | import { validateMultiEdit } from "./multiEditValidation"; |
| 5 | import { executeMultiFindAndReplace } from "./performReplace"; |
| 6 | |
| 7 | describe("multiEdit shared validation", () => { |
| 8 | describe("validateMultiEdit", () => { |
| 9 | it("should throw error if edits is not an array", () => { |
| 10 | expect(() => validateMultiEdit({ edits: "not an array" })).toThrowError( |
| 11 | expect.objectContaining({ |
| 12 | reason: ContinueErrorReason.MultiEditEditsArrayRequired, |
| 13 | }), |
| 14 | ); |
| 15 | }); |
| 16 | |
| 17 | it("should throw error if edits is missing", () => { |
| 18 | expect(() => validateMultiEdit({})).toThrowError( |
| 19 | expect.objectContaining({ |
| 20 | reason: ContinueErrorReason.MultiEditEditsArrayRequired, |
| 21 | }), |
| 22 | ); |
| 23 | }); |
| 24 | |
| 25 | it("should throw error if edits array is empty", () => { |
| 26 | expect(() => validateMultiEdit({ edits: [] })).toThrowError( |
| 27 | expect.objectContaining({ |
| 28 | reason: ContinueErrorReason.MultiEditEditsArrayEmpty, |
| 29 | }), |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | it("should return valid edits", () => { |
| 34 | const edits: EditOperation[] = [ |
| 35 | { old_string: "hello", new_string: "world" }, |
| 36 | ]; |
| 37 | const result = validateMultiEdit({ edits }); |
| 38 | expect(result.edits).toEqual(edits); |
| 39 | }); |
| 40 | }); |
| 41 | |
| 42 | describe("validateMultiEdit - individual edit validation", () => { |
| 43 | it("should throw error if edit has missing old_string", () => { |
| 44 | const args = { |
| 45 | edits: [ |
| 46 | { |
| 47 | old_string: undefined as any, |
| 48 | new_string: "Hi there", |
| 49 | }, |
| 50 | ], |
| 51 | }; |
| 52 | |
| 53 | expect(() => validateMultiEdit(args)).toThrowError( |
| 54 | expect.objectContaining({ |
| 55 | reason: ContinueErrorReason.FindAndReplaceMissingOldString, |
| 56 | }), |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | it("should throw error if edit has missing new_string", () => { |
| 61 | const args = { |
| 62 | edits: [ |
| 63 | { |
| 64 | old_string: "Hello world", |
| 65 | new_string: undefined as any, |
| 66 | }, |
| 67 | ], |
| 68 | }; |
| 69 | |
| 70 | expect(() => validateMultiEdit(args)).toThrowError( |
| 71 | expect.objectContaining({ |
| 72 | reason: ContinueErrorReason.FindAndReplaceMissingNewString, |
| 73 | }), |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | it("should throw error if old_string and new_string are the same", () => { |
| 78 | const args = { |
| 79 | edits: [ |
| 80 | { |
| 81 | old_string: "Hello world", |
| 82 | new_string: "Hello world", |
| 83 | }, |
| 84 | ], |
| 85 | }; |
| 86 | |
| 87 | expect(() => validateMultiEdit(args)).toThrowError( |
| 88 | expect.objectContaining({ |
| 89 | reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 90 | }), |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | it("should throw error if non-first edit has empty old_string", () => { |
| 95 | const args = { |
| 96 | edits: [ |
| 97 | { |
| 98 | old_string: "Hello world", |
| 99 | new_string: "Hi there", |
| 100 | }, |
| 101 | { |
| 102 | old_string: "", |
| 103 | new_string: "Invalid insertion", |
| 104 | }, |
| 105 | ], |
| 106 | }; |
| 107 | |
| 108 | expect(() => validateMultiEdit(args)).toThrowError( |
| 109 | expect.objectContaining({ |
| 110 | reason: ContinueErrorReason.FindAndReplaceNonFirstEmptyOldString, |
| 111 | }), |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | it("should allow first edit to have empty old_string", () => { |
| 116 | const args = { |
| 117 | edits: [ |
| 118 | { |
| 119 | old_string: "", |
| 120 | new_string: "New content", |
| 121 | }, |
| 122 | ], |
| 123 | }; |
| 124 | |
| 125 | expect(() => validateMultiEdit(args)).not.toThrow(); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | describe("executeMultiFindAndReplace", () => { |
| 130 | const originalContent = "Hello world\nThis is a test file\nGoodbye world"; |
| 131 | |
| 132 | it("should handle single edit", () => { |
| 133 | const edits = [ |
| 134 | { |
| 135 | old_string: "Hello world", |
| 136 | new_string: "Hi there", |
| 137 | }, |
| 138 | ]; |
| 139 | |
| 140 | const result = executeMultiFindAndReplace(originalContent, edits); |
| 141 | expect(result).toBe("Hi there\nThis is a test file\nGoodbye world"); |
| 142 | }); |
| 143 | |
| 144 | it("should handle multiple edits", () => { |
| 145 | const edits = [ |
| 146 | { |
| 147 | old_string: "Hello world", |
| 148 | new_string: "Hi there", |
| 149 | }, |
| 150 | { |
| 151 | old_string: "Goodbye world", |
| 152 | new_string: "See you later", |
| 153 | }, |
| 154 | ]; |
| 155 | |
| 156 | const result = executeMultiFindAndReplace(originalContent, edits); |
| 157 | expect(result).toBe("Hi there\nThis is a test file\nSee you later"); |
| 158 | }); |
| 159 | |
| 160 | it("should handle replace_all", () => { |
| 161 | const edits = [ |
| 162 | { |
| 163 | old_string: "world", |
| 164 | new_string: "universe", |
| 165 | replace_all: true, |
| 166 | }, |
| 167 | ]; |
| 168 | |
| 169 | const result = executeMultiFindAndReplace(originalContent, edits); |
| 170 | expect(result).toBe( |
| 171 | "Hello universe\nThis is a test file\nGoodbye universe", |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | it("should handle empty old_string as insertion at beginning", () => { |
| 176 | const edits = [ |
| 177 | { |
| 178 | old_string: "", |
| 179 | new_string: "New first line\n", |
| 180 | }, |
| 181 | ]; |
| 182 | |
| 183 | const result = executeMultiFindAndReplace(originalContent, edits); |
| 184 | expect(result).toBe( |
| 185 | "New first line\nHello world\nThis is a test file\nGoodbye world", |
| 186 | ); |
| 187 | }); |
| 188 | |
| 189 | it("should handle empty string in empty file", () => { |
| 190 | const edits = [ |
| 191 | { |
| 192 | old_string: "", |
| 193 | new_string: "Content for empty file", |
| 194 | }, |
| 195 | ]; |
| 196 | |
| 197 | // Empty old_string matches at position 0 (beginning of file) |
| 198 | const result = executeMultiFindAndReplace("", edits); |
| 199 | expect(result).toBe("Content for empty file"); |
| 200 | }); |
| 201 | |
| 202 | it("should apply edits sequentially", () => { |
| 203 | const edits = [ |
| 204 | { |
| 205 | old_string: "Hello world", |
| 206 | new_string: "Hi universe", |
| 207 | }, |
| 208 | { |
| 209 | old_string: "Hi universe", |
| 210 | new_string: "Greetings cosmos", |
| 211 | }, |
| 212 | ]; |
| 213 | |
| 214 | const result = executeMultiFindAndReplace(originalContent, edits); |
| 215 | expect(result).toBe( |
| 216 | "Greetings cosmos\nThis is a test file\nGoodbye world", |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | it("should throw error if old_string is not found", () => { |
| 221 | const edits = [ |
| 222 | { |
| 223 | old_string: "Not found", |
| 224 | new_string: "Hi there", |
| 225 | }, |
| 226 | ]; |
| 227 | |
| 228 | expect(() => |
| 229 | executeMultiFindAndReplace(originalContent, edits), |
| 230 | ).toThrowError( |
| 231 | expect.objectContaining({ |
| 232 | reason: ContinueErrorReason.FindAndReplaceOldStringNotFound, |
| 233 | }), |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | it("should throw error if old_string appears multiple times and replace_all is false", () => { |
| 238 | const edits = [ |
| 239 | { |
| 240 | old_string: "world", |
| 241 | new_string: "universe", |
| 242 | replace_all: false, |
| 243 | }, |
| 244 | ]; |
| 245 | |
| 246 | expect(() => |
| 247 | executeMultiFindAndReplace(originalContent, edits), |
| 248 | ).toThrowError( |
| 249 | expect.objectContaining({ |
| 250 | reason: ContinueErrorReason.FindAndReplaceMultipleOccurrences, |
| 251 | }), |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | it("should throw if string not found in edit sequence", () => { |
| 256 | const content = "Hello world"; |
| 257 | const edits = [ |
| 258 | { old_string: "Hello", new_string: "Hi" }, |
| 259 | { old_string: "not found", new_string: "test" }, |
| 260 | ]; |
| 261 | |
| 262 | expect(() => executeMultiFindAndReplace(content, edits)).toThrowError( |
| 263 | expect.objectContaining({ |
| 264 | reason: ContinueErrorReason.FindAndReplaceOldStringNotFound, |
| 265 | }), |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | it("should handle whitespace differences using findSearchMatch", () => { |
| 270 | const content = "function test() {\n return true;\n}"; |
| 271 | const edits = [ |
| 272 | { |
| 273 | old_string: "function test(){return true;}", |
| 274 | new_string: "function test() { return false; }", |
| 275 | }, |
| 276 | ]; |
| 277 | |
| 278 | const result = executeMultiFindAndReplace(content, edits); |
| 279 | expect(result).toBe("function test() { return false; }"); |
| 280 | }); |
| 281 | |
| 282 | it("should handle mixed replace_all settings", () => { |
| 283 | const content = "x y x z x"; |
| 284 | const edits = [ |
| 285 | { old_string: "x", new_string: "a", replace_all: true }, |
| 286 | { old_string: "y", new_string: "b" }, |
| 287 | ]; |
| 288 | |
| 289 | const result = executeMultiFindAndReplace(content, edits); |
| 290 | expect(result).toBe("a b a z a"); |
| 291 | }); |
| 292 | }); |
| 293 | |
| 294 | describe("preprocessing logic", () => { |
| 295 | const originalContent = "Hello world\nThis is a test file\nGoodbye world"; |
| 296 | |
| 297 | it("should handle single edit preprocessing", () => { |
| 298 | const edits = [ |
| 299 | { |
| 300 | old_string: "Hello world", |
| 301 | new_string: "Hi there", |
| 302 | }, |
| 303 | ]; |
| 304 | |
| 305 | const newContent = executeMultiFindAndReplace(originalContent, edits); |
| 306 | |
| 307 | expect(newContent).toBe("Hi there\nThis is a test file\nGoodbye world"); |
| 308 | }); |
| 309 | |
| 310 | it("should handle multiple edits preprocessing", () => { |
| 311 | const edits = [ |
| 312 | { |
| 313 | old_string: "Hello world", |
| 314 | new_string: "Hi there", |
| 315 | }, |
| 316 | { |
| 317 | old_string: "Goodbye world", |
| 318 | new_string: "See you later", |
| 319 | }, |
| 320 | ]; |
| 321 | |
| 322 | const newContent = executeMultiFindAndReplace(originalContent, edits); |
| 323 | |
| 324 | expect(newContent).toBe("Hi there\nThis is a test file\nSee you later"); |
| 325 | }); |
| 326 | |
| 327 | it("should count edits correctly", () => { |
| 328 | const edits = [ |
| 329 | { old_string: "Hello", new_string: "Hi" }, |
| 330 | { old_string: "world", new_string: "universe", replace_all: true }, |
| 331 | ]; |
| 332 | |
| 333 | const newContent = executeMultiFindAndReplace(originalContent, edits); |
| 334 | |
| 335 | expect(newContent).toBe( |
| 336 | "Hi universe\nThis is a test file\nGoodbye universe", |
| 337 | ); |
| 338 | expect(edits.length).toBe(2); |
| 339 | }); |
| 340 | }); |
| 341 | }); |