Elffuss BraidHub

kikocis/continue public

main · 4.7 KiBRawPermalink
1import { ContinueErrorReason } from "core/util/errors";
2import * as ideUtils from "core/util/ideUtils";
3import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
4import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate";
5import { ClientToolExtras } from "./callClientTool";
6import { multiEditImpl } from "./multiEditImpl";
7
8vi.mock("uuid", () => ({
9 v4: vi.fn(() => "test-uuid"),
10}));
11
12vi.mock("core/util/ideUtils", () => ({
13 resolveRelativePathInDir: vi.fn(),
14}));
15
16vi.mock("../../redux/thunks/handleApplyStateUpdate", () => ({
17 applyForEditTool: vi.fn(),
18}));
19
20describe("multiEditImpl GUI specific", () => {
21 let mockExtras: ClientToolExtras;
22 let mockResolveRelativePathInDir: Mock;
23 let mockApplyForEditTool: Mock;
24
25 beforeEach(() => {
26 vi.clearAllMocks();
27
28 mockResolveRelativePathInDir = vi.mocked(ideUtils.resolveRelativePathInDir);
29 mockApplyForEditTool = vi.mocked(applyForEditTool);
30
31 mockExtras = {
32 getState: vi.fn(() => ({
33 config: { config: { allowAnonymousTelemetry: false } },
34 })) as any,
35 dispatch: vi.fn() as any,
36 ideMessenger: {
37 ide: {
38 readFile: vi.fn(),
39 getWorkspaceDirs: vi.fn().mockResolvedValue(["dir1"]),
40 },
41 request: vi.fn(),
42 } as any,
43 };
44 });
45
46 describe("filepath validation", () => {
47 it("should throw if filepath is missing", async () => {
48 await expect(
49 multiEditImpl({ edits: [] }, "id", mockExtras),
50 ).rejects.toThrowError(
51 expect.objectContaining({
52 reason: ContinueErrorReason.FindAndReplaceMissingFilepath,
53 }),
54 );
55 });
56
57 it("should throw if file does not exist in workspace", async () => {
58 mockResolveRelativePathInDir.mockResolvedValue(null);
59
60 await expect(
61 multiEditImpl(
62 {
63 filepath: "nonexistent.txt",
64 edits: [{ old_string: "test", new_string: "new" }],
65 },
66 "id",
67 mockExtras,
68 ),
69 ).rejects.toThrowError(
70 expect.objectContaining({
71 reason: ContinueErrorReason.FileNotFound,
72 }),
73 );
74 });
75 });
76
77 describe("GUI integration", () => {
78 beforeEach(() => {
79 mockResolveRelativePathInDir.mockResolvedValue(
80 "file:///dir/test/file.txt",
81 );
82 });
83
84 it("should read file from IDE and dispatch edit", async () => {
85 mockExtras.ideMessenger.ide.readFile = vi
86 .fn()
87 .mockResolvedValue("Hello world");
88
89 await multiEditImpl(
90 {
91 filepath: "file.txt",
92 edits: [{ old_string: "Hello", new_string: "Hi" }],
93 },
94 "id",
95 mockExtras,
96 );
97
98 expect(mockExtras.ideMessenger.ide.readFile).toHaveBeenCalledWith(
99 "file:///dir/test/file.txt",
100 );
101 expect(mockApplyForEditTool).toHaveBeenCalledWith({
102 streamId: "test-uuid",
103 toolCallId: "id",
104 text: "Hi world",
105 filepath: "file:///dir/test/file.txt",
106 isSearchAndReplace: true,
107 });
108 });
109
110 it("should use editingFileContents if provided instead of reading file", async () => {
111 const editingContents = "Custom content";
112
113 await multiEditImpl(
114 {
115 filepath: "file.txt",
116 edits: [{ old_string: "content", new_string: "text" }],
117 editingFileContents: editingContents,
118 },
119 "id",
120 mockExtras,
121 );
122
123 expect(mockExtras.ideMessenger.ide.readFile).not.toHaveBeenCalled();
124 expect(mockApplyForEditTool).toHaveBeenCalledWith({
125 streamId: "test-uuid",
126 toolCallId: "id",
127 text: "Custom text",
128 filepath: "file:///dir/test/file.txt",
129 isSearchAndReplace: true,
130 });
131 });
132
133 it("should wrap IDE readFile errors", async () => {
134 mockExtras.ideMessenger.ide.readFile = vi
135 .fn()
136 .mockRejectedValue(new Error("Read failed"));
137
138 await expect(
139 multiEditImpl(
140 {
141 filepath: "file.txt",
142 edits: [{ old_string: "test", new_string: "new" }],
143 },
144 "id",
145 mockExtras,
146 ),
147 ).rejects.toThrow("Read failed");
148 });
149 });
150
151 describe("return value", () => {
152 it("should return structure for async completion", async () => {
153 mockResolveRelativePathInDir.mockResolvedValue(
154 "file:///dir/test/file.txt",
155 );
156 mockExtras.ideMessenger.ide.readFile = vi.fn().mockResolvedValue("test");
157
158 const result = await multiEditImpl(
159 {
160 filepath: "file.txt",
161 edits: [{ old_string: "test", new_string: "new" }],
162 },
163 "id",
164 mockExtras,
165 );
166
167 expect(result).toEqual({
168 respondImmediately: false,
169 output: undefined,
170 });
171 });
172 });
173});