Elffuss BraidHub

kikocis/continue public

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