Elffuss BraidHub

kikocis/continue public

main · 8.7 KiBRawPermalink
1import { beforeEach, describe, expect, it, vi } from "vitest";
2
3import * as hubLoader from "./hubLoader.js";
4
5const {
6 loadPackageFromHub,
7 mcpProcessor,
8 modelProcessor,
9 promptProcessor,
10 ruleProcessor,
11 processRule,
12} = hubLoader;
13
14// Store the original fetch before any mocking
15const originalFetch = global.fetch;
16
17// Mock fetch globally
18const mockFetch = vi.fn();
19global.fetch = mockFetch;
20
21// Mock JSZip
22vi.mock("jszip", () => ({
23 default: vi.fn(),
24}));
25
26const mockedJSZip = vi.fn();
27
28describe("hubLoader", () => {
29 beforeEach(async () => {
30 vi.clearAllMocks();
31 });
32
33 describe("loadPackageFromHub", () => {
34 it("should validate slug format", async () => {
35 await expect(
36 loadPackageFromHub("invalid-slug", ruleProcessor),
37 ).rejects.toThrow(
38 'Invalid hub slug format. Expected "owner/package", got: invalid-slug',
39 );
40 });
41
42 it("should handle HTTP errors", async () => {
43 mockFetch.mockResolvedValueOnce({
44 ok: false,
45 status: 404,
46 statusText: "Not Found",
47 });
48
49 await expect(
50 loadPackageFromHub("owner/package", ruleProcessor),
51 ).rejects.toThrow(
52 'Failed to load rule from hub "owner/package": HTTP 404: Not Found',
53 );
54 });
55
56 it("should load rule content", async () => {
57 const ruleContent = "# Test Rule\n\nThis is a test rule.";
58
59 // Check if JSZip is mocked
60 const JSZipModule = await import("jszip");
61 const JSZip = JSZipModule.default;
62
63 // Since JSZip is mocked, it should be a mock function
64 if (typeof (JSZip as any).mockImplementation === "function") {
65 (JSZip as any).mockImplementation(() => ({
66 loadAsync: vi.fn().mockResolvedValueOnce({
67 files: {
68 "README.md": {
69 dir: false,
70 async: vi.fn().mockResolvedValue(ruleContent),
71 },
72 },
73 }),
74 }));
75 } else {
76 // If mock doesn't work, skip this test
77 return;
78 }
79
80 mockFetch.mockResolvedValueOnce({
81 ok: true,
82 arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(8)),
83 });
84
85 const result = await loadPackageFromHub("owner/rule", ruleProcessor);
86 expect(result).toBe(ruleContent);
87 });
88
89 it("should load MCP configuration", async () => {
90 const mcpConfig = {
91 content: "name: test-mcp\nversion: 1.0.0",
92 };
93
94 mockFetch.mockResolvedValueOnce({
95 ok: true,
96 json: vi.fn().mockResolvedValue(mcpConfig),
97 });
98
99 const result = await loadPackageFromHub("owner/mcp", mcpProcessor);
100 // Should parse the YAML content, not return the wrapper
101 expect(result).toEqual({
102 name: "test-mcp",
103 version: "1.0.0",
104 });
105 });
106
107 it("should handle missing files", async () => {
108 // Check if JSZip is mocked
109 const JSZipModule = await import("jszip");
110 const JSZip = JSZipModule.default;
111
112 // Since JSZip is mocked, it should be a mock function
113 if (typeof (JSZip as any).mockImplementation === "function") {
114 (JSZip as any).mockImplementation(() => ({
115 loadAsync: vi.fn().mockResolvedValueOnce({
116 files: {
117 "other.txt": {
118 dir: false,
119 async: vi.fn().mockResolvedValue("not a rule"),
120 },
121 },
122 }),
123 }));
124 } else {
125 // If mock doesn't work, skip this test
126 return;
127 }
128
129 mockFetch.mockResolvedValueOnce({
130 ok: true,
131 arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(8)),
132 });
133
134 await expect(
135 loadPackageFromHub("owner/empty", ruleProcessor),
136 ).rejects.toThrow(
137 'Failed to load rule from hub "owner/empty": No rule content found in downloaded zip file. Expected files with extensions: .md',
138 );
139 });
140 });
141
142 describe("processRule", () => {
143 beforeEach(() => {
144 // Ensure mocks are set up properly for processRule tests
145 global.fetch = mockFetch;
146 });
147
148 it("should treat multi-line strings as inline rules", async () => {
149 const loadRuleSpy = vi
150 .spyOn(hubLoader, "loadRuleFromHub")
151 .mockResolvedValue("hub-content");
152
153 const multilineRule = "Line one\nEnsure /tmp is ignored\nFinal line";
154 const result = await processRule(multilineRule);
155
156 expect(result).toBe(multilineRule);
157 expect(loadRuleSpy).not.toHaveBeenCalled();
158
159 loadRuleSpy.mockRestore();
160 });
161
162 it("should still treat owner/package strings as hub slugs", async () => {
163 // Mock fetch to return a proper response for this test
164 const JSZip = (await import("jszip")).default;
165 const zip = new JSZip();
166 zip.file("rule.md", "hub-content");
167 const zipBuffer = await zip.generateAsync({ type: "arraybuffer" });
168
169 mockFetch.mockResolvedValueOnce({
170 ok: true,
171 arrayBuffer: () => Promise.resolve(zipBuffer),
172 } as Response);
173
174 const slug = "owner/package";
175 const result = await processRule(slug);
176
177 expect(result).toBe("hub-content");
178 expect(mockFetch).toHaveBeenCalled();
179 });
180 });
181
182 describe("processors", () => {
183 it("should define rule processor correctly", () => {
184 expect(ruleProcessor.type).toBe("rule");
185 expect(ruleProcessor.expectedFileExtensions).toEqual([".md"]);
186 expect(ruleProcessor.parseContent("test content", "test.md")).toBe(
187 "test content",
188 );
189 });
190
191 it("should define MCP processor correctly", () => {
192 expect(mcpProcessor.type).toBe("mcp");
193 expect(mcpProcessor.expectedFileExtensions).toEqual([
194 ".json",
195 ".yaml",
196 ".yml",
197 ]);
198 });
199
200 it("should define model processor correctly", () => {
201 expect(modelProcessor.type).toBe("model");
202 expect(modelProcessor.expectedFileExtensions).toEqual([
203 ".json",
204 ".yaml",
205 ".yml",
206 ]);
207 });
208
209 it("should define prompt processor correctly", () => {
210 expect(promptProcessor.type).toBe("prompt");
211 expect(promptProcessor.expectedFileExtensions).toEqual([".md", ".txt"]);
212 expect(promptProcessor.parseContent("test content", "test.md")).toBe(
213 "test content",
214 );
215 });
216 });
217
218 describe("Real Hub Integration Tests", () => {
219 // Restore real fetch and JSZip for these tests
220 beforeEach(async () => {
221 vi.clearAllMocks();
222 // Restore the original fetch
223 global.fetch = originalFetch;
224 // Unmock JSZip to use the real implementation
225 vi.unmock("jszip");
226 });
227
228 it("should load rule from real hub: sanity/sanity-opinionated", async () => {
229 const result = await loadPackageFromHub(
230 "sanity/sanity-opinionated",
231 ruleProcessor,
232 );
233
234 expect(result).toBeDefined();
235 expect(typeof result).toBe("string");
236 expect(result.length).toBeGreaterThan(0);
237 // Rules are markdown, should contain some markdown elements
238 expect(result).toMatch(/[#\-\*]/);
239 }, 30000);
240
241 it("should load MCP from real hub: upstash/context7-mcp", async () => {
242 // Restore real fetch and JSZip for this test
243 global.fetch = originalFetch;
244 vi.unmock("jszip");
245
246 const testSlug = "upstash/context7-mcp";
247 const result = await loadPackageFromHub(testSlug, mcpProcessor);
248
249 expect(result).toBeDefined();
250 expect(typeof result).toBe("object");
251 // Should now return the parsed MCP configuration
252 expect(result).toHaveProperty("name");
253 expect(typeof result.name).toBe("string");
254 // The MCP should have type and url properties
255 expect(result).toHaveProperty("type");
256 expect(result).toHaveProperty("url");
257 }, 30000);
258
259 it("should load model from real hub: openai/gpt-5", async () => {
260 // Restore real fetch and JSZip for this test
261 global.fetch = originalFetch;
262 vi.unmock("jszip");
263
264 const testSlug = "openai/gpt-5";
265 const result = await loadPackageFromHub(testSlug, modelProcessor);
266
267 expect(result).toBeDefined();
268 expect(typeof result).toBe("object");
269 // Should now return the extracted model from the models array
270 expect(result).toHaveProperty("name");
271 expect(result).toHaveProperty("provider");
272 expect(result).toHaveProperty("model");
273 // Check that the model properties are correct
274 expect(result.provider).toBe("openai");
275 expect(result.model).toBe("gpt-5");
276 expect(result.name).toBe("GPT-5");
277 }, 30000);
278
279 it("should load prompt from real hub: launchdarkly/using-flags", async () => {
280 const result = await loadPackageFromHub(
281 "launchdarkly/using-flags",
282 promptProcessor,
283 );
284
285 expect(result).toBeDefined();
286 expect(typeof result).toBe("string");
287 expect(result.length).toBeGreaterThan(0);
288 }, 30000);
289 });
290});