Changesets · Branches · Tags · Compare
| Changeset | b7ffac0e7aac03e2dd09dd2fa28491d95b43459f0914fcb3f69819e69e891dbc |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-09 11:17 -07:00 |
| Committer | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-09 11:17 -07:00 |
| Parents | 08608c2155bf |
1 file changed
modified core/context/mcp/MCPConnection.vitest.ts +29 −6
| @@ -2,6 +2,13 @@ | ||
| 2 | 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | 3 | import MCPConnection from "./MCPConnection"; |
| 4 | 4 | |
| 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 | + | |
| 5 | 12 | describe("MCPConnection", () => { |
| 6 | 13 | beforeEach(() => { |
| 7 | 14 | vi.restoreAllMocks(); |
| @@ -76,7 +83,7 @@ | ||
| 76 | 83 | expect(conn.status).toBe("not-connected"); |
| 77 | 84 | }); |
| 78 | 85 | |
| 79 | - it("should throw on invalid transport type", () => { | |
| 86 | + it("should throw on invalid transport type", async () => { | |
| 80 | 87 | const options = { |
| 81 | 88 | name: "test-mcp", |
| 82 | 89 | id: "test-id", |
| @@ -85,9 +92,14 @@ | ||
| 85 | 92 | } as any, |
| 86 | 93 | }; |
| 87 | 94 | |
| 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"); | |
| 91 | 103 | }); |
| 92 | 104 | }); |
| 93 | 105 | |
| @@ -181,6 +193,15 @@ | ||
| 181 | 193 | () => new Promise((resolve) => setTimeout(resolve, 1000)), |
| 182 | 194 | ); |
| 183 | 195 | |
| 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 | + | |
| 184 | 205 | const abortController = new AbortController(); |
| 185 | 206 | await conn.connectClient(false, abortController.signal); |
| 186 | 207 | |
| @@ -189,7 +210,7 @@ | ||
| 189 | 210 | }); |
| 190 | 211 | |
| 191 | 212 | it("should handle connection timeout", async () => { |
| 192 | - const conn = new MCPConnection({ ...options, timeout: 1 }); | |
| 213 | + const conn = new MCPConnection({ ...options, timeout: 50 }); | |
| 193 | 214 | const mockConnect = vi |
| 194 | 215 | .spyOn(Client.prototype, "connect") |
| 195 | 216 | .mockImplementation( |
| @@ -201,7 +222,9 @@ | ||
| 201 | 222 | |
| 202 | 223 | expect(conn.status).toBe("error"); |
| 203 | 224 | 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 | |
| 205 | 228 | }); |
| 206 | 229 | |
| 207 | 230 | it("should handle already connected state", async () => { |