Elffuss BraidHub

kikocis/continue public

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