Elffuss BraidHub

kikocis/continue public

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