From 0acda9fbb315a4fefaf75e17243d01f8a6627e4b Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Tue, 29 Jul 2025 17:02:35 +0530 Subject: [PATCH] added tests - correct lints in MCPManagerSingleton --- core/context/mcp/MCPManagerSingleton.ts | 14 +- core/context/mcp/MCPOauth.vitest.ts | 181 ++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 7 deletions(-) create mode 100644 core/context/mcp/MCPOauth.vitest.ts diff --git a/core/context/mcp/MCPManagerSingleton.ts b/core/context/mcp/MCPManagerSingleton.ts index 4a704f427..22495bef4 100644 --- a/core/context/mcp/MCPManagerSingleton.ts +++ b/core/context/mcp/MCPManagerSingleton.ts @@ -26,12 +26,12 @@ export class MCPManagerSingleton { } createConnection(id: string, options: MCPOptions): MCPConnection { - if (!this.connections.has(id)) { + if (this.connections.has(id)) { + return this.connections.get(id)!; + } else { const connection = new MCPConnection(options); this.connections.set(id, connection); return connection; - } else { - return this.connections.get(id)!; } } @@ -77,15 +77,15 @@ export class MCPManagerSingleton { // Add any connections that are not yet in manager servers.forEach((server) => { - if (!this.connections.has(server.id)) { - refresh = true; - this.connections.set(server.id, new MCPConnection(server, extras)); - } else { + if (this.connections.has(server.id)) { const conn = this.connections.get(server.id); if (conn) { // We need to update it. Some attributes may have changed, such as name, faviconUrl, etc. conn.options = server; } + } else { + refresh = true; + this.connections.set(server.id, new MCPConnection(server, extras)); } }); diff --git a/core/context/mcp/MCPOauth.vitest.ts b/core/context/mcp/MCPOauth.vitest.ts new file mode 100644 index 000000000..44deff10b --- /dev/null +++ b/core/context/mcp/MCPOauth.vitest.ts @@ -0,0 +1,181 @@ +import fs from "node:fs"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { GlobalContext } from "../../util/GlobalContext"; +import { getGlobalContextFilePath } from "../../util/paths"; +import { getOauthToken, performAuth, removeMCPAuth } from "./MCPOauth"; + +// Mock the MCP SDK auth module +vi.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({ + auth: vi.fn(), +})); + +// Mock the MCPManagerSingleton to avoid circular imports +vi.mock("./MCPManagerSingleton", () => ({ + MCPManagerSingleton: { + getInstance: vi.fn(() => ({ + refreshConnection: vi.fn(), + })), + }, +})); + +describe("MCPOauth", () => { + let globalContextFilePath: string; + let mockIde: any; + let mockMcpServer: any; + + beforeEach(() => { + // file is present in the core/test directory + globalContextFilePath = getGlobalContextFilePath(); + if (fs.existsSync(globalContextFilePath)) { + fs.unlinkSync(globalContextFilePath); + } + + mockIde = { + openUrl: vi.fn(), + showToast: vi.fn(), + }; + + mockMcpServer = { + id: "test-server", + transport: { + type: "sse", + url: "https://test-server.com", + }, + }; + + vi.clearAllMocks(); + }); + + afterEach(() => { + if (fs.existsSync(globalContextFilePath)) { + fs.unlinkSync(globalContextFilePath); + } + }); + + describe("getOauthToken", () => { + test("should return undefined when no tokens are stored", async () => { + const result = await getOauthToken("https://test-server.com", mockIde); + expect(result).toBeUndefined(); + }); + + test("should return access token when tokens are stored", async () => { + const globalContext = new GlobalContext(); + globalContext.update("mcpOauthStorage", { + "https://test-server.com": { + tokens: { + access_token: "test-access-token", + token_type: "Bearer", + }, + }, + }); + + const result = await getOauthToken("https://test-server.com", mockIde); + expect(result).toBe("test-access-token"); + }); + + test("should handle different server URLs independently", async () => { + const globalContext = new GlobalContext(); + globalContext.update("mcpOauthStorage", { + "https://server1.com": { + tokens: { + access_token: "token1", + token_type: "Bearer", + }, + }, + "https://server2.com": { + tokens: { + access_token: "token2", + token_type: "Bearer", + }, + }, + }); + + const result1 = await getOauthToken("https://server1.com", mockIde); + const result2 = await getOauthToken("https://server2.com", mockIde); + const result3 = await getOauthToken("https://server3.com", mockIde); + + expect(result1).toBe("token1"); + expect(result2).toBe("token2"); + expect(result3).toBeUndefined(); + }); + }); + + describe("performAuth", () => { + test("should call auth with correct parameters and return auth result", async () => { + const { auth } = await import("@modelcontextprotocol/sdk/client/auth.js"); + const mockAuth = vi.mocked(auth); + mockAuth.mockResolvedValue("AUTHORIZED"); + + const result = await performAuth(mockMcpServer, mockIde); + + expect(mockAuth).toHaveBeenCalledWith( + expect.any(Object), // MCPConnectionOauthProvider instance + { + serverUrl: "https://test-server.com", + }, + ); + expect(result).toBe("AUTHORIZED"); + }); + }); + + describe("removeMCPAuth", () => { + test("should clear oauth storage for the server", () => { + const globalContext = new GlobalContext(); + globalContext.update("mcpOauthStorage", { + "https://test-server.com": { + tokens: { + access_token: "test-access-token", + token_type: "Bearer", + }, + clientInformation: { + client_id: "test-client-id", + redirect_uris: ["http://localhost:3000"], + }, + codeVerifier: "test-code-verifier", + }, + "https://other-server.com": { + tokens: { + access_token: "other-access-token", + token_type: "Bearer", + }, + }, + }); + + removeMCPAuth(mockMcpServer, mockIde); + + const updatedStorage = globalContext.get("mcpOauthStorage"); + expect(updatedStorage).toEqual({ + "https://other-server.com": { + tokens: { + access_token: "other-access-token", + token_type: "Bearer", + }, + }, + }); + }); + + test("should handle non-existent server gracefully", () => { + const globalContext = new GlobalContext(); + globalContext.update("mcpOauthStorage", { + "https://other-server.com": { + tokens: { + access_token: "other-access-token", + token_type: "Bearer", + }, + }, + }); + + removeMCPAuth(mockMcpServer, mockIde); + + const updatedStorage = globalContext.get("mcpOauthStorage"); + expect(updatedStorage).toEqual({ + "https://other-server.com": { + tokens: { + access_token: "other-access-token", + token_type: "Bearer", + }, + }, + }); + }); + }); +});