Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 12.9 KiBRawPermalink
1import { vi } from "vitest";
2
3import { parseArgs, processPromptOrRule } 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("processPromptOrRule (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 processPromptOrRule("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(
273 processPromptOrRule("continuedev/nonexistent-rule"),
274 ).rejects.toThrow(
275 'Failed to load rule from hub "continuedev/nonexistent-rule": HTTP 404: Not Found',
276 );
277 });
278
279 it("should handle network errors when loading from hub", async () => {
280 mockFetch.mockRejectedValue(new Error("Network error"));
281
282 await expect(
283 processPromptOrRule("continuedev/sentry-nextjs"),
284 ).rejects.toThrow(
285 'Failed to load rule from hub "continuedev/sentry-nextjs": Network error',
286 );
287 });
288
289 it("should handle invalid slug format", async () => {
290 // "invalid-slug" doesn't contain "/" so it's treated as direct content, not a hub slug
291 // Let's test with a slug that has wrong format but contains "/"
292 await expect(
293 processPromptOrRule("invalid/slug/too/many/parts"),
294 ).rejects.toThrow(
295 'Invalid hub slug format. Expected "owner/package", got: invalid/slug/too/many/parts',
296 );
297 });
298
299 it("should handle empty zip file", async () => {
300 const JSZip = (await import("jszip")).default;
301 const zip = new JSZip();
302 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
303
304 mockFetch.mockResolvedValue({
305 ok: true,
306 arrayBuffer: () => Promise.resolve(zipBuffer),
307 } as Response);
308
309 await expect(
310 processPromptOrRule("continuedev/empty-rule"),
311 ).rejects.toThrow(
312 'Failed to load rule from hub "continuedev/empty-rule": No rule content found in downloaded zip file',
313 );
314 });
315
316 it("should handle zip with only directories", async () => {
317 const JSZip = (await import("jszip")).default;
318 const zip = new JSZip();
319 zip.folder("docs"); // Create a directory but no files
320 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
321
322 mockFetch.mockResolvedValue({
323 ok: true,
324 arrayBuffer: () => Promise.resolve(zipBuffer),
325 } as Response);
326
327 await expect(
328 processPromptOrRule("continuedev/directory-only"),
329 ).rejects.toThrow(
330 'Failed to load rule from hub "continuedev/directory-only": No rule content found in downloaded zip file',
331 );
332 });
333
334 it("should prefer markdown files and ignore other file types", async () => {
335 const JSZip = (await import("jszip")).default;
336 const zip = new JSZip();
337 const mdContent = "# Main Rule\n\nThis is the markdown rule content.";
338 const txtContent = "This is just a text file.";
339
340 zip.file("README.txt", txtContent);
341 zip.file("rule.md", mdContent);
342 zip.file("package.json", '{"name": "test"}');
343
344 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
345
346 mockFetch.mockResolvedValue({
347 ok: true,
348 arrayBuffer: () => Promise.resolve(zipBuffer),
349 } as Response);
350
351 const result = await processPromptOrRule("continuedev/mixed-files");
352
353 expect(result).toBe(mdContent);
354 });
355
356 it("should handle multiple markdown files and use the first one", async () => {
357 const JSZip = (await import("jszip")).default;
358 const zip = new JSZip();
359 const rule1Content = "# First Rule\n\nThis is the first rule.";
360 const rule2Content = "# Second Rule\n\nThis is the second rule.";
361
362 zip.file("rule1.md", rule1Content);
363 zip.file("rule2.md", rule2Content);
364
365 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
366
367 mockFetch.mockResolvedValue({
368 ok: true,
369 arrayBuffer: () => Promise.resolve(zipBuffer),
370 } as Response);
371
372 const result = await processPromptOrRule("continuedev/multiple-rules");
373
374 // Should use the first markdown file found (alphabetically)
375 expect(result).toBe(rule1Content);
376 });
377
378 it("should handle malformed zip files", async () => {
379 const invalidZipBuffer = new ArrayBuffer(10);
380
381 mockFetch.mockResolvedValue({
382 ok: true,
383 arrayBuffer: () => Promise.resolve(invalidZipBuffer),
384 } as Response);
385
386 await expect(
387 processPromptOrRule("continuedev/corrupted-zip"),
388 ).rejects.toThrow(
389 'Failed to load rule from hub "continuedev/corrupted-zip"',
390 );
391 });
392 });
393
394 describe("processPromptOrRule logic", () => {
395 it("should identify hub slugs correctly", async () => {
396 const JSZip = (await import("jszip")).default;
397 const zip = new JSZip();
398 zip.file("rule.md", "# Hub Rule");
399 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
400
401 mockFetch.mockResolvedValue({
402 ok: true,
403 arrayBuffer: () => Promise.resolve(zipBuffer),
404 } as Response);
405
406 const result = await processPromptOrRule("owner/package");
407 expect(result).toBe("# Hub Rule");
408 expect(mockFetch).toHaveBeenCalled();
409 });
410
411 it("should treat direct strings as content", async () => {
412 const directContent = "This is direct rule content";
413 const result = await processPromptOrRule(directContent);
414 expect(result).toBe(directContent);
415 expect(mockFetch).not.toHaveBeenCalled();
416 });
417
418 it("should distinguish between hub slugs and file paths", async () => {
419 // This would be treated as a file path, not a hub slug
420 // since it starts with "."
421 await expect(processPromptOrRule("./owner/package")).rejects.toThrow(
422 'Failed to read rule file "./owner/package": Rule file not found',
423 );
424 expect(mockFetch).not.toHaveBeenCalled();
425 });
426 });
427});