Elffuss BraidHub

kikocis/continue public

be26cc4523c9 · 6.6 KiBRawPermalink
1import { MCPOptions } from "../..";
2import MCPConnection from "./MCPConnection";
3import { MCPManagerSingleton } from "./MCPManagerSingleton";
4
5// Create test versions with stubbed behavior
6class TestMCPConnection extends MCPConnection {
7 constructor(options: MCPOptions) {
8 super(options);
9
10 // Override with test implementations
11 this.client = {
12 close: async () => {},
13 connect: async () => {},
14 getServerCapabilities: () => ({}),
15 listResources: async () => ({ resources: [] }),
16 listTools: async () => ({ tools: [] }),
17 listPrompts: async () => ({ prompts: [] }),
18 } as any;
19
20 this.abortController = new AbortController();
21 this.connectClient = jest.fn().mockResolvedValue(undefined);
22 this.getStatus = jest
23 .fn()
24 .mockReturnValue({ status: "connected", errors: [] });
25 }
26}
27
28describe("MCPManagerSingleton", () => {
29 let manager: MCPManagerSingleton;
30 const testOptions: MCPOptions = {
31 name: "test-mcp",
32 id: "test-id",
33 transport: {
34 type: "stdio",
35 command: "test-command",
36 args: [],
37 },
38 };
39
40 beforeEach(() => {
41 // Reset singleton
42 (MCPManagerSingleton as any).instance = undefined;
43 manager = MCPManagerSingleton.getInstance();
44
45 // Replace the connections map with our own that will use TestMCPConnection
46 const connectionsMap = new Map<string, MCPConnection>();
47 Object.defineProperty(manager, "connections", {
48 value: connectionsMap,
49 writable: true,
50 });
51
52 // Override createConnection to use our TestMCPConnection
53 manager.createConnection = function (
54 id: string,
55 options: MCPOptions,
56 ): MCPConnection {
57 if (!this.connections.has(id)) {
58 const connection = new TestMCPConnection(options);
59 this.connections.set(id, connection);
60 return connection;
61 } else {
62 return this.connections.get(id)!;
63 }
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 = jest.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 = jest.spyOn(connection.abortController, "abort");
137 const closeSpy = jest.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 = jest.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 = jest.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});