Elffuss BraidHub

kikocis/continue public

Update MCPConnection.vitest.ts
Changesetb7ffac0e7aac03e2dd09dd2fa28491d95b43459f0914fcb3f69819e69e891dbc
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-09 11:17 -07:00
CommitterPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-09 11:17 -07:00
Parents08608c2155bf

Browse the files at this changeset

1 file changed

modified core/context/mcp/MCPConnection.vitest.ts +29 −6
@@ -2,6 +2,13 @@
22 import { beforeEach, describe, expect, it, vi } from "vitest";
33 import MCPConnection from "./MCPConnection";
44
5+// Mock the shell path utility
6+vi.mock("../../util/shellPath", () => ({
7+ getEnvPathFromUserShell: vi
8+ .fn()
9+ .mockResolvedValue("/usr/local/bin:/usr/bin:/bin"),
10+}));
11+
512 describe("MCPConnection", () => {
613 beforeEach(() => {
714 vi.restoreAllMocks();
@@ -76,7 +83,7 @@
7683 expect(conn.status).toBe("not-connected");
7784 });
7885
79- it("should throw on invalid transport type", () => {
86+ it("should throw on invalid transport type", async () => {
8087 const options = {
8188 name: "test-mcp",
8289 id: "test-id",
@@ -85,9 +92,14 @@
8592 } as any,
8693 };
8794
88- expect(() => new MCPConnection(options)).toThrow(
89- "Unsupported transport type: invalid",
90- );
95+ const conn = new MCPConnection(options);
96+ const abortController = new AbortController();
97+
98+ // The validation now happens during connectClient, not constructor
99+ await conn.connectClient(false, abortController.signal);
100+
101+ expect(conn.status).toBe("error");
102+ expect(conn.errors[0]).toContain("Unsupported transport type: invalid");
91103 });
92104 });
93105
@@ -181,6 +193,15 @@
181193 () => new Promise((resolve) => setTimeout(resolve, 1000)),
182194 );
183195
196+ // Mock the required methods for successful connection
197+ const mockGetServerCapabilities = vi
198+ .spyOn(Client.prototype, "getServerCapabilities")
199+ .mockReturnValue({
200+ resources: {},
201+ tools: {},
202+ prompts: {},
203+ });
204+
184205 const abortController = new AbortController();
185206 await conn.connectClient(false, abortController.signal);
186207
@@ -189,7 +210,7 @@
189210 });
190211
191212 it("should handle connection timeout", async () => {
192- const conn = new MCPConnection({ ...options, timeout: 1 });
213+ const conn = new MCPConnection({ ...options, timeout: 50 });
193214 const mockConnect = vi
194215 .spyOn(Client.prototype, "connect")
195216 .mockImplementation(
@@ -201,7 +222,9 @@
201222
202223 expect(conn.status).toBe("error");
203224 expect(conn.errors[0]).toContain("Failed to connect");
204- expect(mockConnect).toHaveBeenCalled();
225+ // The connection should timeout before connect is called due to transport construction
226+ // Since transport construction happens first, and we're not mocking that,
227+ // the timeout will happen during transport construction or connect attempt
205228 });
206229
207230 it("should handle already connected state", async () => {