Elffuss BraidHub

kikocis/continue public

main · 3.1 KiBRawPermalink
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
3import { loadMcpFromHub } from "./args.js";
4
5// Store original fetch to restore later
6const originalFetch = global.fetch;
7const mockFetch = vi.fn();
8
9// Mock JSZip
10vi.mock("jszip", () => {
11 const MockJSZip = vi.fn();
12 MockJSZip.prototype.loadAsync = vi.fn();
13 return { default: MockJSZip };
14});
15
16describe("loadMcpFromHub", () => {
17 let mockJSZip: any;
18
19 beforeEach(async () => {
20 vi.clearAllMocks();
21 // Set mock fetch for each test
22 global.fetch = mockFetch;
23
24 // Get the mocked JSZip constructor
25 const JSZip = (await import("jszip")).default;
26 mockJSZip = new JSZip();
27 });
28
29 afterEach(() => {
30 // Restore original fetch after each test
31 global.fetch = originalFetch;
32 });
33
34 it("should validate slug format", async () => {
35 await expect(loadMcpFromHub("invalid-slug")).rejects.toThrow(
36 'Invalid hub slug format. Expected "owner/package", got: invalid-slug',
37 );
38 });
39
40 it("should handle HTTP errors", async () => {
41 mockFetch.mockResolvedValueOnce({
42 ok: false,
43 status: 404,
44 statusText: "Not Found",
45 });
46
47 await expect(loadMcpFromHub("continuedev/nonexistent-mcp")).rejects.toThrow(
48 'Failed to load mcp from hub "continuedev/nonexistent-mcp": HTTP 404: Not Found',
49 );
50 });
51
52 it("should handle network errors", async () => {
53 mockFetch.mockRejectedValueOnce(new Error("Network error"));
54
55 await expect(loadMcpFromHub("continuedev/test-mcp")).rejects.toThrow(
56 'Failed to load mcp from hub "continuedev/test-mcp": Network error',
57 );
58 });
59
60 it("should parse JSON configuration files", async () => {
61 const mockConfig = {
62 content:
63 "name: test-mcp\ncommand: npx test-mcp\nargs:\n - --port\n - 3000",
64 };
65
66 // MCP packages now use the registry endpoint and return JSON directly
67 mockFetch.mockResolvedValueOnce({
68 ok: true,
69 json: vi.fn().mockResolvedValue(mockConfig),
70 });
71
72 const result = await loadMcpFromHub("continuedev/test-mcp");
73 // Should now parse the YAML content and return the parsed object
74 expect(result).toEqual({
75 name: "test-mcp",
76 command: "npx test-mcp",
77 args: ["--port", 3000],
78 });
79 });
80
81 it("should handle missing configuration files", async () => {
82 // MCP packages now use the registry endpoint
83 // If the content field is missing or empty, it should fail
84 mockFetch.mockResolvedValueOnce({
85 ok: true,
86 json: vi.fn().mockResolvedValue({}),
87 });
88
89 const result = await loadMcpFromHub("continuedev/no-config");
90 // The registry endpoint might return an empty object if no config exists
91 expect(result).toEqual({});
92 });
93
94 it("should handle invalid JSON configuration", async () => {
95 // MCP packages now use the registry endpoint
96 // If the registry returns invalid JSON, it should fail at the fetch level
97 mockFetch.mockResolvedValueOnce({
98 ok: true,
99 json: vi.fn().mockRejectedValue(new Error("Invalid JSON")),
100 });
101
102 await expect(loadMcpFromHub("continuedev/invalid-json")).rejects.toThrow(
103 'Failed to load mcp from hub "continuedev/invalid-json": Invalid JSON',
104 );
105 });
106});