Elffuss BraidHub

kikocis/continue public

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