Elffuss BraidHub

kikocis/continue public

0a1f7aa3091d · 12.7 KiBRawPermalink
1import { vi } from "vitest";
2
3import { parseArgs, processRule } from "./args.js";
4
5describe("parseArgs", () => {
6 const originalArgv = process.argv;
7
8 beforeEach(() => {
9 // Reset process.argv before each test
10 process.argv = ["node", "script.js"];
11 });
12
13 afterAll(() => {
14 // Restore original argv
15 process.argv = originalArgv;
16 });
17
18 it("should return default values when no arguments provided", () => {
19 const result = parseArgs();
20 expect(result).toEqual({});
21 });
22
23 it("should set resume to true when --resume flag is present", () => {
24 process.argv = ["node", "script.js", "--resume"];
25 const result = parseArgs();
26 expect(result.resume).toBe(true);
27 });
28
29 it("should set readonly to true when --readonly flag is present", () => {
30 process.argv = ["node", "script.js", "--readonly"];
31 const result = parseArgs();
32 expect(result.readonly).toBe(true);
33 });
34
35 it("should parse config path from --config flag", () => {
36 process.argv = ["node", "script.js", "--config", "/path/to/config.yaml"];
37 const result = parseArgs();
38 expect(result.configPath).toBe("/path/to/config.yaml");
39 });
40
41 it("should parse single rule from --rule flag", () => {
42 process.argv = ["node", "script.js", "--rule", "my-rule"];
43 const result = parseArgs();
44 expect(result.rules).toEqual(["my-rule"]);
45 });
46
47 it("should parse multiple rules from multiple --rule flags", () => {
48 process.argv = ["node", "script.js", "--rule", "rule1", "--rule", "rule2"];
49 const result = parseArgs();
50 expect(result.rules).toEqual(["rule1", "rule2"]);
51 });
52
53 it("should parse prompt from last non-flag argument", () => {
54 process.argv = ["node", "script.js", "Hello world"];
55 const result = parseArgs();
56 expect(result.prompt).toBe("Hello world");
57 });
58
59 it("should handle multiple flags together", () => {
60 process.argv = ["node", "script.js", "--print", "--readonly", "--resume"];
61 const result = parseArgs();
62 expect(result).toEqual({
63 readonly: true,
64 resume: true,
65 });
66 });
67
68 it("should handle config flag with other flags", () => {
69 process.argv = ["node", "script.js", "--config", "config.yaml", "--print"];
70 const result = parseArgs();
71 expect(result).toEqual({
72 configPath: "config.yaml",
73 });
74 });
75
76 it("should handle rule flag with other flags", () => {
77 process.argv = ["node", "script.js", "--rule", "my-rule", "--print"];
78 const result = parseArgs();
79 expect(result).toEqual({
80 rules: ["my-rule"],
81 });
82 });
83
84 it("should handle prompt with flags", () => {
85 process.argv = ["node", "script.js", "--print", "What is the weather?"];
86 const result = parseArgs();
87 expect(result).toEqual({
88 prompt: "What is the weather?",
89 });
90 });
91
92 it("should handle config flag with prompt", () => {
93 process.argv = [
94 "node",
95 "script.js",
96 "--config",
97 "config.yaml",
98 "Test prompt",
99 ];
100 const result = parseArgs();
101 expect(result).toEqual({
102 configPath: "config.yaml",
103 prompt: "Test prompt",
104 });
105 });
106
107 it("should handle rule flag with prompt", () => {
108 process.argv = ["node", "script.js", "--rule", "my-rule", "Test prompt"];
109 const result = parseArgs();
110 expect(result).toEqual({
111 rules: ["my-rule"],
112 prompt: "Test prompt",
113 });
114 });
115
116 it("should ignore config and rule flag values when extracting prompt", () => {
117 process.argv = [
118 "node",
119 "script.js",
120 "--config",
121 "config.yaml",
122 "--rule",
123 "my-rule",
124 "actual-prompt",
125 ];
126 const result = parseArgs();
127 expect(result.prompt).toBe("actual-prompt");
128 expect(result.configPath).toBe("config.yaml");
129 expect(result.rules).toEqual(["my-rule"]);
130 });
131
132 it("should handle multiple non-flag arguments and use the last one as prompt", () => {
133 process.argv = ["node", "script.js", "first", "second", "third"];
134 const result = parseArgs();
135 expect(result.prompt).toBe("third");
136 });
137
138 it("should handle complex argument combinations", () => {
139 process.argv = [
140 "node",
141 "script.js",
142 "--print",
143 "--config",
144 "my-config.yaml",
145 "--rule",
146 "rule1",
147 "--rule",
148 "rule2",
149 "--readonly",
150 "--resume",
151 "Complex prompt with spaces",
152 ];
153 const result = parseArgs();
154 expect(result).toEqual({
155 configPath: "my-config.yaml",
156 rules: ["rule1", "rule2"],
157 readonly: true,
158 resume: true,
159 prompt: "Complex prompt with spaces",
160 });
161 });
162
163 it("should handle config flag without value", () => {
164 process.argv = ["node", "script.js", "--config"];
165 const result = parseArgs();
166 expect(result.configPath).toBeUndefined();
167 });
168
169 it("should handle rule flag without value", () => {
170 process.argv = ["node", "script.js", "--rule"];
171 const result = parseArgs();
172 expect(result.rules).toEqual([]);
173 });
174
175 it("should handle config flag at the end without value", () => {
176 process.argv = ["node", "script.js", "--print", "--config"];
177 const result = parseArgs();
178 expect(result.configPath).toBeUndefined();
179 });
180
181 it("should handle rule flag at the end without value", () => {
182 process.argv = ["node", "script.js", "--print", "--rule"];
183 const result = parseArgs();
184 expect(result.rules).toEqual([]);
185 });
186
187 it("should handle empty arguments array", () => {
188 process.argv = ["node", "script.js"];
189 const result = parseArgs();
190 expect(result).toEqual({});
191 });
192
193 it("should handle multiple boolean flags", () => {
194 process.argv = ["node", "script.js", "--readonly", "--resume"];
195 const result = parseArgs();
196 expect(result.readonly).toBe(true);
197 expect(result.resume).toBe(true);
198 });
199
200 it("should handle multiple rules with complex combinations", () => {
201 process.argv = [
202 "node",
203 "script.js",
204 "--rule",
205 "file.txt",
206 "--rule",
207 "owner/package",
208 "--rule",
209 "direct string rule",
210 ];
211 const result = parseArgs();
212 expect(result.rules).toEqual([
213 "file.txt",
214 "owner/package",
215 "direct string rule",
216 ]);
217 });
218});
219
220describe("processRule (loadRuleFromHub integration)", () => {
221 // Mock fetch for hub tests
222 const originalFetch = global.fetch;
223 const mockFetch = vi.fn() as any;
224
225 beforeAll(() => {
226 global.fetch = mockFetch;
227 });
228
229 afterAll(() => {
230 global.fetch = originalFetch;
231 });
232
233 beforeEach(() => {
234 mockFetch.mockClear();
235 });
236
237 describe("loadRuleFromHub", () => {
238 it("should successfully load a rule from the Continue Hub", async () => {
239 // Create a mock zip file containing a markdown rule
240 const JSZip = (await import("jszip")).default;
241 const zip = new JSZip();
242 const ruleContent =
243 "# Sentry Next.js Rule\n\nThis is a sample rule for Sentry integration with Next.js applications.";
244 zip.file("rule.md", ruleContent);
245
246 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
247
248 // Mock successful fetch response
249 mockFetch.mockResolvedValue({
250 ok: true,
251 arrayBuffer: () => Promise.resolve(zipBuffer),
252 } as Response);
253
254 const result = await processRule("continuedev/sentry-nextjs");
255
256 expect(result).toBe(ruleContent);
257 expect(mockFetch).toHaveBeenCalledWith(
258 new URL(
259 "v0/continuedev/sentry-nextjs/latest/download",
260 "https://api.continue.dev/",
261 ),
262 );
263 });
264
265 it("should handle HTTP errors when loading from hub", async () => {
266 mockFetch.mockResolvedValue({
267 ok: false,
268 status: 404,
269 statusText: "Not Found",
270 } as Response);
271
272 await expect(processRule("continuedev/nonexistent-rule")).rejects.toThrow(
273 'Failed to load rule from hub "continuedev/nonexistent-rule": HTTP 404: Not Found',
274 );
275 });
276
277 it("should handle network errors when loading from hub", async () => {
278 mockFetch.mockRejectedValue(new Error("Network error"));
279
280 await expect(processRule("continuedev/sentry-nextjs")).rejects.toThrow(
281 'Failed to load rule from hub "continuedev/sentry-nextjs": Network error',
282 );
283 });
284
285 it("should handle invalid slug format", async () => {
286 // "invalid-slug" doesn't contain "/" so it's treated as direct content, not a hub slug
287 // Let's test with a slug that has wrong format but contains "/"
288 await expect(processRule("invalid/slug/too/many/parts")).rejects.toThrow(
289 'Invalid hub slug format. Expected "owner/package", got: invalid/slug/too/many/parts',
290 );
291 });
292
293 it("should handle empty zip file", async () => {
294 const JSZip = (await import("jszip")).default;
295 const zip = new JSZip();
296 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
297
298 mockFetch.mockResolvedValue({
299 ok: true,
300 arrayBuffer: () => Promise.resolve(zipBuffer),
301 } as Response);
302
303 await expect(processRule("continuedev/empty-rule")).rejects.toThrow(
304 'Failed to load rule from hub "continuedev/empty-rule": No rule content found in downloaded zip file',
305 );
306 });
307
308 it("should handle zip with only directories", async () => {
309 const JSZip = (await import("jszip")).default;
310 const zip = new JSZip();
311 zip.folder("docs"); // Create a directory but no files
312 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
313
314 mockFetch.mockResolvedValue({
315 ok: true,
316 arrayBuffer: () => Promise.resolve(zipBuffer),
317 } as Response);
318
319 await expect(processRule("continuedev/directory-only")).rejects.toThrow(
320 'Failed to load rule from hub "continuedev/directory-only": No rule content found in downloaded zip file',
321 );
322 });
323
324 it("should prefer markdown files and ignore other file types", async () => {
325 const JSZip = (await import("jszip")).default;
326 const zip = new JSZip();
327 const mdContent = "# Main Rule\n\nThis is the markdown rule content.";
328 const txtContent = "This is just a text file.";
329
330 zip.file("README.txt", txtContent);
331 zip.file("rule.md", mdContent);
332 zip.file("package.json", '{"name": "test"}');
333
334 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
335
336 mockFetch.mockResolvedValue({
337 ok: true,
338 arrayBuffer: () => Promise.resolve(zipBuffer),
339 } as Response);
340
341 const result = await processRule("continuedev/mixed-files");
342
343 expect(result).toBe(mdContent);
344 });
345
346 it("should handle multiple markdown files and use the first one", async () => {
347 const JSZip = (await import("jszip")).default;
348 const zip = new JSZip();
349 const rule1Content = "# First Rule\n\nThis is the first rule.";
350 const rule2Content = "# Second Rule\n\nThis is the second rule.";
351
352 zip.file("rule1.md", rule1Content);
353 zip.file("rule2.md", rule2Content);
354
355 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
356
357 mockFetch.mockResolvedValue({
358 ok: true,
359 arrayBuffer: () => Promise.resolve(zipBuffer),
360 } as Response);
361
362 const result = await processRule("continuedev/multiple-rules");
363
364 // Should use the first markdown file found (alphabetically)
365 expect(result).toBe(rule1Content);
366 });
367
368 it("should handle malformed zip files", async () => {
369 const invalidZipBuffer = new ArrayBuffer(10);
370
371 mockFetch.mockResolvedValue({
372 ok: true,
373 arrayBuffer: () => Promise.resolve(invalidZipBuffer),
374 } as Response);
375
376 await expect(processRule("continuedev/corrupted-zip")).rejects.toThrow(
377 'Failed to load rule from hub "continuedev/corrupted-zip"',
378 );
379 });
380 });
381
382 describe("processRule logic", () => {
383 it("should identify hub slugs correctly", async () => {
384 const JSZip = (await import("jszip")).default;
385 const zip = new JSZip();
386 zip.file("rule.md", "# Hub Rule");
387 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
388
389 mockFetch.mockResolvedValue({
390 ok: true,
391 arrayBuffer: () => Promise.resolve(zipBuffer),
392 } as Response);
393
394 const result = await processRule("owner/package");
395 expect(result).toBe("# Hub Rule");
396 expect(mockFetch).toHaveBeenCalled();
397 });
398
399 it("should treat direct strings as content", async () => {
400 const directContent = "This is direct rule content";
401 const result = await processRule(directContent);
402 expect(result).toBe(directContent);
403 expect(mockFetch).not.toHaveBeenCalled();
404 });
405
406 it("should distinguish between hub slugs and file paths", async () => {
407 // This would be treated as a file path, not a hub slug
408 // since it starts with "."
409 await expect(processRule("./owner/package")).rejects.toThrow(
410 'Failed to read rule file "./owner/package": Rule file not found',
411 );
412 expect(mockFetch).not.toHaveBeenCalled();
413 });
414 });
415});