Elffuss BraidHub

kikocis/continue public

e9ecc1af12bb · 4.6 KiBRawPermalink
1import { AssistantConfig } from "@continuedev/sdk";
2import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
3
4import { MCPService } from "./MCPService.js";
5
6// Mock the MCP SDK
7const mockClient = {
8 connect: vi.fn(),
9 getServerCapabilities: vi.fn(() => ({ prompts: true, tools: true })),
10 listPrompts: vi.fn(() => Promise.resolve({ prompts: [] })),
11 listTools: vi.fn(() => Promise.resolve({ tools: [] })),
12 callTool: vi.fn(() => Promise.resolve({})),
13 close: vi.fn(() => Promise.resolve()),
14};
15
16vi.mock("@modelcontextprotocol/sdk/client/index.js", () => ({
17 Client: vi.fn(() => mockClient),
18}));
19
20vi.mock("@modelcontextprotocol/sdk/client/stdio.js", () => ({
21 StdioClientTransport: vi.fn(),
22}));
23
24describe("MCPService", () => {
25 let mcpService: MCPService;
26 let mockAssistant: AssistantConfig;
27
28 beforeEach(() => {
29 mcpService = new MCPService();
30 mockAssistant = {
31 name: "test-assistant",
32 version: "1.0.0",
33 mcpServers: [
34 {
35 name: "test-server",
36 command: "npx",
37 args: ["-v"],
38 env: { TEST: "value" },
39 },
40 ],
41 } as AssistantConfig;
42 });
43
44 afterEach(async () => {
45 vi.clearAllMocks();
46 await mcpService.cleanup();
47 });
48
49 describe("initialization", () => {
50 it("should initialize with empty state", () => {
51 const state = mcpService.getState();
52 expect(mcpService.isReady()).toBe(false);
53 expect(state.connections).toEqual([]);
54 expect(state.tools.length).toBe(0);
55 expect(state.prompts.length).toBe(0);
56 });
57
58 it("should initialize with no servers", async () => {
59 const emptyAssistant = {
60 name: "empty",
61 version: "1.0.0",
62 } as AssistantConfig;
63 const state = await mcpService.initialize(emptyAssistant);
64
65 expect(mcpService.isReady()).toBe(true);
66 expect(state.connections).toEqual([]);
67 expect(state.tools.length).toBe(0);
68 expect(state.prompts.length).toBe(0);
69 });
70
71 it("should initialize and return immediately even with server configs", async () => {
72 const state = await mcpService.initialize(mockAssistant);
73
74 // Should return immediately with ready state
75 expect(mcpService.isReady()).toBe(true);
76 expect(state.mcpService).toBe(mcpService);
77 });
78 });
79
80 describe("service management", () => {
81 beforeEach(async () => {
82 await mcpService.initialize(mockAssistant);
83 });
84
85 it("should get overall status", async () => {
86 const firstStatus = mcpService.getOverallStatus();
87 expect(firstStatus.status).toBe("connected");
88 expect(firstStatus.hasWarnings).toBe(false);
89 await mcpService.cleanup();
90 const secondStatus = mcpService.getOverallStatus();
91 expect(secondStatus.status).toBe("idle");
92 });
93 });
94
95 describe("server management", () => {
96 beforeEach(async () => {
97 await mcpService.initialize(mockAssistant);
98 });
99
100 it("should handle restart all servers", async () => {
101 await expect(mcpService.restartAllServers()).resolves.not.toThrow();
102 });
103
104 it("should handle stop all servers", async () => {
105 await expect(mcpService.shutdownConnections()).resolves.not.toThrow();
106 });
107
108 it("should handle server not found error", async () => {
109 await expect(mcpService.restartServer("nonexistent")).rejects.toThrow(
110 "Server nonexistent not found in configuration",
111 );
112 });
113
114 it("should handle stop individual server", async () => {
115 await expect(mcpService.stopServer("test-server")).resolves.not.toThrow();
116 });
117 });
118
119 describe("tools and prompts", () => {
120 beforeEach(async () => {
121 await mcpService.initialize(mockAssistant);
122 });
123
124 it("should return empty arrays for tools and prompts initially", () => {
125 expect(Array.isArray(mcpService.getState().tools)).toBe(true);
126 expect(Array.isArray(mcpService.getState().tools)).toBe(true);
127 });
128
129 it("should throw error for unknown tool", async () => {
130 await expect(mcpService.runTool("unknown-tool", {})).rejects.toThrow(
131 "Tool unknown-tool not found",
132 );
133 });
134 });
135
136 describe("shutdown", () => {
137 it("should handle shutdown gracefully", async () => {
138 await mcpService.initialize(mockAssistant);
139 await mcpService.cleanup();
140
141 const state = mcpService.getState();
142 expect(
143 state.mcpService
144 ?.getState()
145 .connections.filter((c) => c.status === "connected"),
146 ).toHaveLength(0);
147 });
148
149 it("should handle multiple shutdown calls", async () => {
150 await mcpService.initialize(mockAssistant);
151 await mcpService.cleanup();
152 await expect(mcpService.cleanup()).resolves.not.toThrow();
153 });
154 });
155});