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