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