| 1 | import * as fs from "fs"; |
| 2 | |
| 3 | import { ContinueError, ContinueErrorReason } from "core/util/errors.js"; |
| 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | |
| 6 | import { editTool } from "./edit.js"; |
| 7 | import { markFileAsRead, readFilesSet } from "./readFile.js"; |
| 8 | |
| 9 | // Mock the dependencies |
| 10 | vi.mock("../telemetry/telemetryService.js"); |
| 11 | vi.mock("../telemetry/utils.js", () => ({ |
| 12 | calculateLinesOfCodeDiff: vi.fn().mockReturnValue({ added: 1, removed: 0 }), |
| 13 | getLanguageFromFilePath: vi.fn().mockReturnValue("javascript"), |
| 14 | })); |
| 15 | vi.mock("./writeFile.js", () => ({ |
| 16 | generateDiff: vi.fn().mockReturnValue("mocked diff"), |
| 17 | })); |
| 18 | vi.mock("fs", async () => { |
| 19 | const actual = await vi.importActual("fs"); |
| 20 | return { |
| 21 | ...actual, |
| 22 | existsSync: vi.fn(), |
| 23 | readFileSync: vi.fn(), |
| 24 | writeFileSync: vi.fn(), |
| 25 | unlinkSync: vi.fn(), |
| 26 | realpathSync: vi.fn(), |
| 27 | }; |
| 28 | }); |
| 29 | |
| 30 | describe("editTool", () => { |
| 31 | const testFilePath = "/tmp/test-edit-file.txt"; |
| 32 | const originalContent = "Hello world\nThis is a test file\nGoodbye world"; |
| 33 | |
| 34 | beforeEach(() => { |
| 35 | vi.clearAllMocks(); |
| 36 | // Mock fs functions to simulate file operations |
| 37 | vi.mocked(fs.existsSync).mockReturnValue(true); |
| 38 | vi.mocked(fs.readFileSync).mockReturnValue(originalContent); |
| 39 | vi.mocked(fs.writeFileSync).mockImplementation(() => {}); |
| 40 | vi.mocked(fs.realpathSync).mockImplementation((path) => path.toString()); |
| 41 | }); |
| 42 | |
| 43 | afterEach(() => { |
| 44 | vi.clearAllMocks(); |
| 45 | readFilesSet.clear(); |
| 46 | }); |
| 47 | |
| 48 | describe("preprocess", () => { |
| 49 | it("should throw error if file has not been read", async () => { |
| 50 | const args = { |
| 51 | file_path: testFilePath, |
| 52 | old_string: "Hello world", |
| 53 | new_string: "Hi there", |
| 54 | }; |
| 55 | |
| 56 | const error = await editTool.preprocess!(args).catch((e) => e); |
| 57 | expect(error).toBeInstanceOf(ContinueError); |
| 58 | expect(error.reason).toBe(ContinueErrorReason.EditToolFileNotRead); |
| 59 | }); |
| 60 | |
| 61 | it("should throw error if file does not exist", async () => { |
| 62 | const nonExistentFile = "/tmp/non-existent-file.txt"; |
| 63 | markFileAsRead(nonExistentFile); |
| 64 | vi.mocked(fs.existsSync).mockReturnValue(false); |
| 65 | |
| 66 | const args = { |
| 67 | file_path: nonExistentFile, |
| 68 | old_string: "Hello", |
| 69 | new_string: "Hi", |
| 70 | }; |
| 71 | |
| 72 | const error = await editTool.preprocess!(args).catch((e) => e); |
| 73 | expect(error).toBeInstanceOf(ContinueError); |
| 74 | expect(error.reason).toBe(ContinueErrorReason.FileNotFound); |
| 75 | }); |
| 76 | |
| 77 | it("should throw error if old_string is not found", async () => { |
| 78 | markFileAsRead(testFilePath); |
| 79 | vi.mocked(fs.readFileSync).mockReturnValue("Different content"); |
| 80 | |
| 81 | const args = { |
| 82 | file_path: testFilePath, |
| 83 | old_string: "Not found", |
| 84 | new_string: "Hi there", |
| 85 | }; |
| 86 | |
| 87 | const error = await editTool.preprocess!(args).catch((e) => e); |
| 88 | expect(error).toBeInstanceOf(ContinueError); |
| 89 | expect(error.reason).toBe( |
| 90 | ContinueErrorReason.FindAndReplaceOldStringNotFound, |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | it("should throw error if old_string appears multiple times and replace_all is false", async () => { |
| 95 | markFileAsRead(testFilePath); |
| 96 | |
| 97 | const args = { |
| 98 | file_path: testFilePath, |
| 99 | old_string: "world", |
| 100 | new_string: "universe", |
| 101 | replace_all: false, |
| 102 | }; |
| 103 | |
| 104 | const error = await editTool.preprocess!(args).catch((e) => e); |
| 105 | expect(error).toBeInstanceOf(ContinueError); |
| 106 | expect(error.reason).toBe( |
| 107 | ContinueErrorReason.FindAndReplaceMultipleOccurrences, |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | it("should throw error if old_string and new_string are the same", async () => { |
| 112 | markFileAsRead(testFilePath); |
| 113 | |
| 114 | const args = { |
| 115 | file_path: testFilePath, |
| 116 | old_string: "Hello world", |
| 117 | new_string: "Hello world", |
| 118 | }; |
| 119 | |
| 120 | const error = await editTool.preprocess!(args).catch((e) => e); |
| 121 | expect(error).toBeInstanceOf(ContinueError); |
| 122 | expect(error.reason).toBe( |
| 123 | ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | it("should successfully preprocess valid single replacement", async () => { |
| 128 | markFileAsRead(testFilePath); |
| 129 | |
| 130 | const args = { |
| 131 | file_path: testFilePath, |
| 132 | old_string: "Hello world", |
| 133 | new_string: "Hi there", |
| 134 | }; |
| 135 | |
| 136 | const result = await editTool.preprocess!(args); |
| 137 | |
| 138 | expect(result.args).toEqual({ |
| 139 | resolvedPath: testFilePath, |
| 140 | newContent: "Hi there\nThis is a test file\nGoodbye world", |
| 141 | oldContent: originalContent, |
| 142 | }); |
| 143 | expect(result.preview).toHaveLength(2); |
| 144 | expect(result.preview?.[0]).toEqual({ |
| 145 | type: "text", |
| 146 | content: "Will make the following changes:", |
| 147 | }); |
| 148 | expect(result.preview?.[1]).toEqual({ |
| 149 | type: "diff", |
| 150 | content: "mocked diff", |
| 151 | }); |
| 152 | }); |
| 153 | |
| 154 | it("should successfully preprocess replace_all", async () => { |
| 155 | markFileAsRead(testFilePath); |
| 156 | |
| 157 | const args = { |
| 158 | file_path: testFilePath, |
| 159 | old_string: "world", |
| 160 | new_string: "universe", |
| 161 | replace_all: true, |
| 162 | }; |
| 163 | |
| 164 | const result = await editTool.preprocess!(args); |
| 165 | |
| 166 | expect(result.args.newContent).toBe( |
| 167 | "Hello universe\nThis is a test file\nGoodbye universe", |
| 168 | ); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | describe("run", () => { |
| 173 | it("should successfully write file and return success message", async () => { |
| 174 | const newContent = "Hi there\nThis is a test file\nGoodbye world"; |
| 175 | const args = { |
| 176 | resolvedPath: testFilePath, |
| 177 | newContent, |
| 178 | oldContent: originalContent, |
| 179 | }; |
| 180 | |
| 181 | const result = await editTool.run(args); |
| 182 | |
| 183 | expect(result).toBe( |
| 184 | `Successfully edited ${testFilePath}\nDiff:\nmocked diff`, |
| 185 | ); |
| 186 | expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 187 | testFilePath, |
| 188 | newContent, |
| 189 | "utf-8", |
| 190 | ); |
| 191 | }); |
| 192 | |
| 193 | it("should throw error if file write fails", async () => { |
| 194 | vi.mocked(fs.writeFileSync).mockImplementation(() => { |
| 195 | throw new Error("Write failed"); |
| 196 | }); |
| 197 | |
| 198 | const args = { |
| 199 | resolvedPath: testFilePath, |
| 200 | newContent: "new content", |
| 201 | oldContent: originalContent, |
| 202 | }; |
| 203 | |
| 204 | const error = await editTool.run(args).catch((e) => e); |
| 205 | expect(error).toBeInstanceOf(ContinueError); |
| 206 | expect(error.reason).toBe(ContinueErrorReason.FileWriteError); |
| 207 | }); |
| 208 | }); |
| 209 | |
| 210 | describe("markFileAsRead", () => { |
| 211 | it("should allow editing after marking file as read", async () => { |
| 212 | markFileAsRead(testFilePath); |
| 213 | |
| 214 | const args = { |
| 215 | file_path: testFilePath, |
| 216 | old_string: "Hello world", |
| 217 | new_string: "Hi there", |
| 218 | }; |
| 219 | |
| 220 | const result = await editTool.preprocess!(args); |
| 221 | expect(result.args.newContent).toContain("Hi there"); |
| 222 | }); |
| 223 | }); |
| 224 | }); |