Elffuss BraidHub

kikocis/continue public

main · 6.6 KiBRawPermalink
1// Generated by continue
2
3import fs from "fs";
4import path from "path";
5import { CodeSnippetsCodebaseIndex } from "../indexing/CodeSnippetsIndex";
6import { testIde, testLLM } from "../test/fixtures";
7import {
8 addToTestDir,
9 setUpTestDir,
10 tearDownTestDir,
11 TEST_DIR,
12} from "../test/testDir";
13import generateRepoMap from "./generateRepoMap";
14import { getRepoMapFilePath } from "./paths";
15
16describe.skip("generateRepoMap", () => {
17 beforeEach(() => {
18 setUpTestDir();
19 });
20
21 afterEach(() => {
22 tearDownTestDir();
23 jest.restoreAllMocks();
24 });
25
26 it("should generate a repo map with signatures included", async () => {
27 // Arrange
28 // Create sample files in the test directory
29 addToTestDir([
30 ["file1.js", "function foo() {}\nfunction bar() {}"],
31 ["subdir/file2.py", "def foo(): pass\ndef bar(): pass"],
32 ]);
33
34 // Do not mock testIde.getWorkspaceDirs
35
36 // Mock CodeSnippetsCodebaseIndex.getPathsAndSignatures
37 jest
38 .spyOn(CodeSnippetsCodebaseIndex, "getPathsAndSignatures")
39 .mockImplementation(async (dirs, offset, limit) => {
40 // Return test data
41 return {
42 groupedByUri: {
43 [path.join(TEST_DIR, "file1.js")]: [
44 "function foo() {}",
45 "function bar() {}",
46 ],
47 [path.join(TEST_DIR, "subdir/file2.py")]: [
48 "def foo():",
49 "def bar():",
50 ],
51 },
52 hasMoreSnippets: false,
53 hasMoreUris: false,
54 };
55 });
56
57 // Set testLLM properties
58 testLLM._contextLength = 2048;
59 testLLM.countTokens = jest.fn().mockReturnValue(10);
60
61 // Act
62 const repoMapContent = await generateRepoMap(testLLM, testIde, {
63 includeSignatures: true,
64 outputRelativeUriPaths: true,
65 });
66
67 // Assert
68 const expectedContent =
69 "Below is a repository map. \n" +
70 "For each file in the codebase, " +
71 "this map contains the name of the file, and the signature for any " +
72 "classes, methods, or functions in the file.\n\n" +
73 "file1.js:\n" +
74 "\tfunction foo() {}\n" +
75 "\t...\n" +
76 "\tfunction bar() {}\n\n" +
77 "subdir/file2.py:\n" +
78 "\tdef foo():\n" +
79 "\t...\n" +
80 "\tdef bar():\n\n";
81
82 expect(repoMapContent).toBe(expectedContent);
83
84 // Also check that the repo map file was created at the correct path
85 const repoMapPath = getRepoMapFilePath();
86 expect(fs.existsSync(repoMapPath)).toBe(true);
87
88 const fileContent = fs.readFileSync(repoMapPath, "utf8");
89 expect(fileContent).toBe(expectedContent);
90 });
91
92 it("should generate a repo map without signatures when includeSignatures is false", async () => {
93 // Arrange
94 // Create sample files in the test directory
95 addToTestDir([
96 ["file1.js", "function foo() {}\nfunction bar() {}"],
97 ["subdir/file2.py", "def foo(): pass\ndef bar(): pass"],
98 ]);
99
100 // Do not mock testIde.getWorkspaceDirs
101
102 // Mock CodeSnippetsCodebaseIndex.getPathsAndSignatures
103 jest
104 .spyOn(CodeSnippetsCodebaseIndex, "getPathsAndSignatures")
105 .mockImplementation(async (dirs, offset, limit) => {
106 // Return test data
107 return {
108 groupedByUri: {
109 [path.join(TEST_DIR, "file1.js")]: [],
110 [path.join(TEST_DIR, "subdir/file2.py")]: [],
111 },
112 hasMoreSnippets: false,
113 hasMoreUris: false,
114 };
115 });
116
117 // Set testLLM properties
118 testLLM._contextLength = 2048;
119 testLLM.countTokens = jest.fn().mockReturnValue(10);
120
121 // Act
122 const repoMapContent = await generateRepoMap(testLLM, testIde, {
123 includeSignatures: false,
124 outputRelativeUriPaths: true,
125 });
126
127 // Assert
128 const expectedContent =
129 "Below is a repository map. \n" +
130 "For each file in the codebase, " +
131 "this map contains the name of the file, and the signature for any " +
132 "classes, methods, or functions in the file.\n\n" +
133 "file1.js\n" +
134 "subdir/file2.py\n";
135
136 expect(repoMapContent).toBe(expectedContent);
137
138 // Also check that the repo map file was created at the correct path
139 const repoMapPath = getRepoMapFilePath();
140 expect(fs.existsSync(repoMapPath)).toBe(true);
141
142 const fileContent = fs.readFileSync(repoMapPath, "utf8");
143 expect(fileContent).toBe(expectedContent);
144 });
145
146 it("should handle file read errors gracefully", async () => {
147 // Arrange
148 // Create sample files in the test directory
149 addToTestDir([
150 ["file1.js", "function foo() {}\nfunction bar() {}"],
151 ["subdir/file2.py", "def foo(): pass\ndef bar(): pass"],
152 ]);
153
154 // Do not mock testIde.getWorkspaceDirs
155
156 // Mock CodeSnippetsCodebaseIndex.getPathsAndSignatures
157 jest
158 .spyOn(CodeSnippetsCodebaseIndex, "getPathsAndSignatures")
159 .mockImplementation(async (dirs, offset, limit) => {
160 // Return test data
161 return {
162 groupedByUri: {
163 [path.join(TEST_DIR, "file1.js")]: ["function foo() {}"],
164 [path.join(TEST_DIR, "subdir/file2.py")]: ["def foo():"],
165 },
166 hasMoreSnippets: false,
167 hasMoreUris: false,
168 };
169 });
170
171 // Mock fs.promises.readFile to throw an error when reading file1.js
172 jest
173 .spyOn(fs.promises, "readFile")
174 .mockImplementation((filePath, encoding) => {
175 if (filePath === path.join(TEST_DIR, "file1.js")) {
176 return Promise.reject(new Error("File not found"));
177 }
178 return fs.promises.readFile(filePath, encoding);
179 });
180
181 // Spy on console.error
182 const consoleErrorSpy = jest
183 .spyOn(console, "error")
184 .mockImplementation(() => {});
185
186 // Set testLLM properties
187 testLLM._contextLength = 2048;
188 testLLM.countTokens = jest.fn().mockReturnValue(10);
189
190 // Act
191 const repoMapContent = await generateRepoMap(testLLM, testIde, {
192 includeSignatures: true,
193 outputRelativeUriPaths: true,
194 });
195
196 // Assert
197 const expectedContent =
198 "Below is a repository map. \n" +
199 "For each file in the codebase, " +
200 "this map contains the name of the file, and the signature for any " +
201 "classes, methods, or functions in the file.\n\n" +
202 "subdir/file2.py:\n" +
203 "\tdef foo():\n\n" +
204 "file1.js\n"; // file1.js is added in writeRemainingPaths
205
206 expect(repoMapContent).toBe(expectedContent);
207
208 // Also check that an error was logged
209 expect(consoleErrorSpy).toHaveBeenCalledWith(
210 expect.stringContaining(
211 "Failed to read file:\n" +
212 ` Path: ${path.join(TEST_DIR, "file1.js")}\n` +
213 " Error: File not found",
214 ),
215 );
216
217 // Clean up mock
218 consoleErrorSpy.mockRestore();
219 });
220});