Elffuss BraidHub

kikocis/continue public

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