Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 6.7 KiBRawPermalink
1import { beforeEach, describe, expect, it, vi } from "vitest";
2import { MCPOptions } from "../..";
3import MCPConnection from "./MCPConnection";
4import { MCPManagerSingleton } from "./MCPManagerSingleton";
5
6// Create test versions with stubbed behavior
7class TestMCPConnection extends MCPConnection {
8 constructor(options: MCPOptions) {
9 super(options);
10
11 // Override with test implementations
12 this.client = {
13 close: async () => {},
14 connect: async () => {},
15 getServerCapabilities: () => ({}),
16 listResources: async () => ({ resources: [] }),
17 listTools: async () => ({ tools: [] }),
18 listPrompts: async () => ({ prompts: [] }),
19 } as any;
20
21 this.abortController = new AbortController();
22 this.connectClient = vi.fn().mockResolvedValue(undefined);
23 this.getStatus = vi
24 .fn()
25 .mockReturnValue({ status: "connected", errors: [] });
26 }
27}
28
29describe("MCPManagerSingleton", () => {
30 let manager: MCPManagerSingleton;
31 const testOptions: MCPOptions = {
32 name: "test-mcp",
33 id: "test-id",
34 transport: {
35 type: "stdio",
36 command: "test-command",
37 args: [],
38 },
39 };
40
41 beforeEach(() => {
42 // Reset singleton
43 (MCPManagerSingleton as any).instance = undefined;
44 manager = MCPManagerSingleton.getInstance();
45
46 // Replace the connections map with our own that will use TestMCPConnection
47 const connectionsMap = new Map<string, MCPConnection>();
48 Object.defineProperty(manager, "connections", {
49 value: connectionsMap,
50 writable: true,
51 });
52
53 // Override createConnection to use our TestMCPConnection
54 manager.createConnection = function (
55 id: string,
56 options: MCPOptions,
57 ): MCPConnection {
58 if (this.connections.has(id)) {
59 return this.connections.get(id)!;
60 }
61 const connection = new TestMCPConnection(options);
62 this.connections.set(id, connection);
63 return connection;
64 };
65 });
66
67 describe("getInstance", () => {
68 it("should create singleton instance", () => {
69 const instance1 = MCPManagerSingleton.getInstance();
70 const instance2 = MCPManagerSingleton.getInstance();
71 expect(instance1).toBe(instance2);
72 });
73 });
74
75 describe("createConnection", () => {
76 it("should create new connection if none exists", () => {
77 const connection = manager.createConnection("test-id", testOptions);
78 expect(connection).toBeInstanceOf(MCPConnection);
79 });
80
81 it("should return existing connection if exists", () => {
82 const connection1 = manager.createConnection("test-id", testOptions);
83 const connection2 = manager.createConnection("test-id", testOptions);
84 expect(connection1).toBe(connection2);
85 });
86 });
87
88 describe("getConnection", () => {
89 it("should return undefined for non-existent connection", () => {
90 expect(manager.getConnection("non-existent")).toBeUndefined();
91 });
92
93 it("should return existing connection", () => {
94 const connection = manager.createConnection("test-id", testOptions);
95 expect(manager.getConnection("test-id")).toBe(connection);
96 });
97 });
98
99 describe("removeConnection", () => {
100 it("should remove connection and close client", async () => {
101 // Create a spy on the close method
102 const connection = manager.createConnection(
103 "test-id",
104 testOptions,
105 ) as TestMCPConnection;
106 const closeSpy = vi.spyOn(connection.client, "close");
107
108 await manager.removeConnection("test-id");
109
110 expect(closeSpy).toHaveBeenCalled();
111 expect(manager.getConnection("test-id")).toBeUndefined();
112 });
113
114 it("should handle non-existent connection", async () => {
115 await expect(
116 manager.removeConnection("non-existent"),
117 ).resolves.toBeUndefined();
118 });
119 });
120
121 describe("setConnections", () => {
122 it("should add new connections", () => {
123 const servers = [testOptions];
124 manager.setConnections(servers, false);
125 expect(manager.getConnection(testOptions.id)).toBeInstanceOf(
126 MCPConnection,
127 );
128 });
129
130 it("should remove old connections", () => {
131 // Create initial connection
132 const connection = manager.createConnection(
133 "test-id",
134 testOptions,
135 ) as TestMCPConnection;
136 const abortSpy = vi.spyOn(connection.abortController, "abort");
137 const closeSpy = vi.spyOn(connection.client, "close");
138
139 // Remove it by setting empty server list
140 manager.setConnections([], false);
141
142 expect(abortSpy).toHaveBeenCalled();
143 expect(closeSpy).toHaveBeenCalled();
144 expect(manager.getConnection("test-id")).toBeUndefined();
145 });
146 });
147
148 describe("refreshConnection", () => {
149 it("should refresh specific connection", async () => {
150 const connection = manager.createConnection(
151 "test-id",
152 testOptions,
153 ) as TestMCPConnection;
154
155 await manager.refreshConnection("test-id");
156
157 expect(connection.connectClient).toHaveBeenCalledWith(
158 true,
159 expect.any(AbortSignal),
160 );
161 });
162
163 it("should throw error for non-existent connection", async () => {
164 await expect(manager.refreshConnection("non-existent")).rejects.toThrow(
165 "MCP Connection non-existent not found",
166 );
167 });
168 });
169
170 describe("refreshConnections", () => {
171 it("should refresh all connections", async () => {
172 const connection1 = manager.createConnection("test-id-1", {
173 ...testOptions,
174 id: "test-id-1",
175 }) as TestMCPConnection;
176
177 const connection2 = manager.createConnection("test-id-2", {
178 ...testOptions,
179 id: "test-id-2",
180 }) as TestMCPConnection;
181
182 await manager.refreshConnections(true);
183
184 expect(connection1.connectClient).toHaveBeenCalledWith(
185 true,
186 expect.any(AbortSignal),
187 );
188 expect(connection2.connectClient).toHaveBeenCalledWith(
189 true,
190 expect.any(AbortSignal),
191 );
192 });
193
194 it("should call onConnectionsRefreshed if defined", async () => {
195 const mockCallback = vi.fn();
196 manager.onConnectionsRefreshed = mockCallback;
197 manager.createConnection("test-id", testOptions);
198
199 await manager.refreshConnections(true);
200
201 expect(mockCallback).toHaveBeenCalled();
202 });
203 });
204
205 describe("getStatuses", () => {
206 it("should return statuses for all connections", () => {
207 const connection = manager.createConnection(
208 "test-id",
209 testOptions,
210 ) as TestMCPConnection;
211 const mockStatus = { status: "connected", errors: [] };
212 connection.getStatus = vi.fn().mockReturnValue(mockStatus);
213
214 const statuses = manager.getStatuses();
215 expect(statuses).toHaveLength(1);
216 expect(statuses[0]).toEqual({ ...mockStatus, client: connection.client });
217 });
218 });
219});