| 1 | import * as fs from "fs"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { ContinueError, ContinueErrorReason } from "core/util/errors.js"; |
| 5 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | |
| 7 | import { multiEditTool } from "./multiEdit.js"; |
| 8 | import { markFileAsRead } from "./readFile.js"; |
| 9 | |
| 10 | // Mock the dependencies |
| 11 | vi.mock("../telemetry/telemetryService.js"); |
| 12 | vi.mock("../telemetry/utils.js", () => ({ |
| 13 | calculateLinesOfCodeDiff: vi.fn().mockReturnValue({ added: 1, removed: 0 }), |
| 14 | getLanguageFromFilePath: vi.fn().mockReturnValue("javascript"), |
| 15 | })); |
| 16 | vi.mock("./writeFile.js", () => ({ |
| 17 | generateDiff: vi.fn().mockReturnValue("mocked diff"), |
| 18 | })); |
| 19 | |
| 20 | vi.mock("fs", async () => { |
| 21 | const actual = await vi.importActual("fs"); |
| 22 | return { |
| 23 | ...actual, |
| 24 | existsSync: vi.fn(), |
| 25 | readFileSync: vi.fn(), |
| 26 | writeFileSync: vi.fn(), |
| 27 | unlinkSync: vi.fn(), |
| 28 | realpathSync: vi.fn(), |
| 29 | }; |
| 30 | }); |
| 31 | |
| 32 | describe("multiEditTool CLI specific", () => { |
| 33 | const testFilePath = "/tmp/test-multi-edit-file.txt"; |
| 34 | const originalContent = "Hello world\nThis is a test file\nGoodbye world"; |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | vi.clearAllMocks(); |
| 38 | // Mock fs functions to simulate file operations |
| 39 | vi.mocked(fs.existsSync).mockReturnValue(true); |
| 40 | vi.mocked(fs.readFileSync).mockReturnValue(originalContent); |
| 41 | vi.mocked(fs.writeFileSync).mockImplementation(() => {}); |
| 42 | vi.mocked(fs.realpathSync).mockImplementation((path) => path.toString()); |
| 43 | }); |
| 44 | |
| 45 | afterEach(() => { |
| 46 | vi.clearAllMocks(); |
| 47 | }); |
| 48 | |
| 49 | describe("file system validation", () => { |
| 50 | it("should throw error if file has not been read", async () => { |
| 51 | const args = { |
| 52 | file_path: testFilePath, |
| 53 | edits: [ |
| 54 | { |
| 55 | old_string: "Hello world", |
| 56 | new_string: "Hi there", |
| 57 | }, |
| 58 | ], |
| 59 | }; |
| 60 | |
| 61 | const error = await multiEditTool.preprocess!(args).catch((e) => e); |
| 62 | expect(error).toBeInstanceOf(ContinueError); |
| 63 | expect(error.reason).toBe(ContinueErrorReason.EditToolFileNotRead); |
| 64 | }); |
| 65 | |
| 66 | it("should throw error if file does not exist", async () => { |
| 67 | const nonExistentFile = "/tmp/non-existent-file.txt"; |
| 68 | markFileAsRead(nonExistentFile); |
| 69 | vi.mocked(fs.existsSync).mockReturnValue(false); |
| 70 | |
| 71 | const args = { |
| 72 | file_path: nonExistentFile, |
| 73 | edits: [ |
| 74 | { |
| 75 | old_string: "Hello", |
| 76 | new_string: "Hi", |
| 77 | }, |
| 78 | ], |
| 79 | }; |
| 80 | |
| 81 | const error = await multiEditTool.preprocess!(args).catch((e) => e); |
| 82 | expect(error).toBeInstanceOf(ContinueError); |
| 83 | expect(error.reason).toBe(ContinueErrorReason.FileNotFound); |
| 84 | }); |
| 85 | |
| 86 | it("should throw error if file_path is missing", async () => { |
| 87 | const args = { |
| 88 | edits: [ |
| 89 | { |
| 90 | old_string: "Hello", |
| 91 | new_string: "Hi", |
| 92 | }, |
| 93 | ], |
| 94 | }; |
| 95 | |
| 96 | const error = await multiEditTool.preprocess!(args).catch((e) => e); |
| 97 | expect(error).toBeInstanceOf(ContinueError); |
| 98 | expect(error.reason).toBe( |
| 99 | ContinueErrorReason.FindAndReplaceMissingFilepath, |
| 100 | ); |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | describe("preprocess CLI specific", () => { |
| 105 | it("should generate preview with diff", async () => { |
| 106 | markFileAsRead(testFilePath); |
| 107 | |
| 108 | const args = { |
| 109 | file_path: testFilePath, |
| 110 | edits: [ |
| 111 | { |
| 112 | old_string: "Hello world", |
| 113 | new_string: "Hi there", |
| 114 | }, |
| 115 | ], |
| 116 | }; |
| 117 | |
| 118 | const result = await multiEditTool.preprocess!(args); |
| 119 | |
| 120 | expect(result.preview).toHaveLength(2); |
| 121 | expect(result.preview?.[0]).toEqual({ |
| 122 | type: "text", |
| 123 | content: "Will apply 1 edit to /tmp/test-multi-edit-file.txt:", |
| 124 | }); |
| 125 | expect(result.preview?.[1]).toEqual({ |
| 126 | type: "diff", |
| 127 | content: "mocked diff", |
| 128 | }); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe("run", () => { |
| 133 | it("should successfully write file and return success message", async () => { |
| 134 | const newContent = "Hi there\nThis is a test file\nGoodbye world"; |
| 135 | const args = { |
| 136 | file_path: testFilePath, |
| 137 | newContent, |
| 138 | originalContent: originalContent, |
| 139 | editCount: 1, |
| 140 | }; |
| 141 | |
| 142 | const result = await multiEditTool.run(args); |
| 143 | |
| 144 | expect(result).toBe( |
| 145 | `Successfully edited ${testFilePath} with 1 edit\nDiff:\nmocked diff`, |
| 146 | ); |
| 147 | expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 148 | testFilePath, |
| 149 | newContent, |
| 150 | "utf-8", |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | it("should return correct message for multiple edits", async () => { |
| 155 | const args = { |
| 156 | file_path: testFilePath, |
| 157 | newContent: "New content", |
| 158 | originalContent: originalContent, |
| 159 | editCount: 3, |
| 160 | }; |
| 161 | |
| 162 | const result = await multiEditTool.run(args); |
| 163 | |
| 164 | expect(result).toBe( |
| 165 | `Successfully edited ${testFilePath} with 3 edits\nDiff:\nmocked diff`, |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | it("should throw error if file write fails", async () => { |
| 170 | vi.mocked(fs.writeFileSync).mockImplementation(() => { |
| 171 | throw new Error("Write failed"); |
| 172 | }); |
| 173 | |
| 174 | const args = { |
| 175 | file_path: testFilePath, |
| 176 | newContent: "new content", |
| 177 | originalContent: originalContent, |
| 178 | editCount: 1, |
| 179 | }; |
| 180 | |
| 181 | const error = await multiEditTool.run(args).catch((e) => e); |
| 182 | expect(error).toBeInstanceOf(ContinueError); |
| 183 | expect(error.reason).toBe(ContinueErrorReason.FileWriteError); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe("relative path handling", () => { |
| 188 | it("should convert relative paths to absolute paths", async () => { |
| 189 | const relativePath = "test-file.txt"; |
| 190 | const absolutePath = path.resolve(process.cwd(), relativePath); |
| 191 | markFileAsRead(absolutePath); |
| 192 | |
| 193 | const args = { |
| 194 | file_path: relativePath, |
| 195 | edits: [ |
| 196 | { |
| 197 | old_string: "Hello world", |
| 198 | new_string: "Hi there", |
| 199 | }, |
| 200 | ], |
| 201 | }; |
| 202 | |
| 203 | const result = await multiEditTool.preprocess!(args); |
| 204 | |
| 205 | expect(result.args.file_path).toBe(absolutePath); |
| 206 | expect(result.args.newContent).toBe( |
| 207 | "Hi there\nThis is a test file\nGoodbye world", |
| 208 | ); |
| 209 | }); |
| 210 | |
| 211 | it("should handle relative paths with subdirectories", async () => { |
| 212 | const relativePath = "src/components/test.js"; |
| 213 | const absolutePath = path.resolve(process.cwd(), relativePath); |
| 214 | markFileAsRead(absolutePath); |
| 215 | |
| 216 | const args = { |
| 217 | file_path: relativePath, |
| 218 | edits: [ |
| 219 | { |
| 220 | old_string: "Hello world", |
| 221 | new_string: "Hi there", |
| 222 | }, |
| 223 | ], |
| 224 | }; |
| 225 | |
| 226 | const result = await multiEditTool.preprocess!(args); |
| 227 | |
| 228 | expect(result.args.file_path).toBe(absolutePath); |
| 229 | }); |
| 230 | |
| 231 | it("should handle relative paths with ../ patterns", async () => { |
| 232 | const relativePath = "../test-file.txt"; |
| 233 | const absolutePath = path.resolve(process.cwd(), relativePath); |
| 234 | markFileAsRead(absolutePath); |
| 235 | |
| 236 | const args = { |
| 237 | file_path: relativePath, |
| 238 | edits: [ |
| 239 | { |
| 240 | old_string: "Hello world", |
| 241 | new_string: "Hi there", |
| 242 | }, |
| 243 | ], |
| 244 | }; |
| 245 | |
| 246 | const result = await multiEditTool.preprocess!(args); |
| 247 | |
| 248 | expect(result.args.file_path).toBe(absolutePath); |
| 249 | }); |
| 250 | |
| 251 | it("should leave absolute paths unchanged", async () => { |
| 252 | const absolutePath = "/tmp/absolute-file.txt"; |
| 253 | markFileAsRead(absolutePath); |
| 254 | |
| 255 | const args = { |
| 256 | file_path: absolutePath, |
| 257 | edits: [ |
| 258 | { |
| 259 | old_string: "Hello world", |
| 260 | new_string: "Hi there", |
| 261 | }, |
| 262 | ], |
| 263 | }; |
| 264 | |
| 265 | const result = await multiEditTool.preprocess!(args); |
| 266 | |
| 267 | expect(result.args.file_path).toBe(absolutePath); |
| 268 | }); |
| 269 | }); |
| 270 | |
| 271 | describe("markFileAsRead", () => { |
| 272 | it("should allow editing after marking file as read", async () => { |
| 273 | markFileAsRead(testFilePath); |
| 274 | |
| 275 | const args = { |
| 276 | file_path: testFilePath, |
| 277 | edits: [ |
| 278 | { |
| 279 | old_string: "Hello world", |
| 280 | new_string: "Hi there", |
| 281 | }, |
| 282 | ], |
| 283 | }; |
| 284 | |
| 285 | const result = await multiEditTool.preprocess!(args); |
| 286 | expect(result.args.newContent).toBe( |
| 287 | "Hi there\nThis is a test file\nGoodbye world", |
| 288 | ); |
| 289 | }); |
| 290 | }); |
| 291 | }); |