From 362359cc3c0fcae73cecb5f677087ffcd2b84a00 Mon Sep 17 00:00:00 2001 From: aheizi Date: Sun, 9 Mar 2025 00:37:08 +0800 Subject: [PATCH] fix ut (cherry picked from commit b4f808d7dcb4f23c0de86bce47be392b2d9e4281) --- .../@modelcontextprotocol/sdk/client/sse.js | 14 +++++++++++++ src/services/mcp/__tests__/McpHub.test.ts | 21 ++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/__mocks__/@modelcontextprotocol/sdk/client/sse.js diff --git a/src/__mocks__/@modelcontextprotocol/sdk/client/sse.js b/src/__mocks__/@modelcontextprotocol/sdk/client/sse.js new file mode 100644 index 0000000000..b52145d25a --- /dev/null +++ b/src/__mocks__/@modelcontextprotocol/sdk/client/sse.js @@ -0,0 +1,14 @@ +class SSEClientTransport { + constructor(url, options = {}) { + this.url = url + this.options = options + this.onerror = null + this.connect = jest.fn().mockResolvedValue() + this.close = jest.fn().mockResolvedValue() + this.start = jest.fn().mockResolvedValue() + } +} + +module.exports = { + SSEClientTransport, +} diff --git a/src/services/mcp/__tests__/McpHub.test.ts b/src/services/mcp/__tests__/McpHub.test.ts index 1556208328..737ed7f11e 100644 --- a/src/services/mcp/__tests__/McpHub.test.ts +++ b/src/services/mcp/__tests__/McpHub.test.ts @@ -71,6 +71,7 @@ describe("McpHub", () => { JSON.stringify({ mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], alwaysAllow: ["allowed-tool"], @@ -87,6 +88,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], alwaysAllow: [], @@ -109,6 +111,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], alwaysAllow: ["existing-tool"], @@ -131,6 +134,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], }, @@ -155,6 +159,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], disabled: false, @@ -294,6 +299,7 @@ describe("McpHub", () => { it("should validate timeout values", () => { // Test valid timeout values const validConfig = { + type: "stdio", command: "test", timeout: 60, } @@ -301,9 +307,9 @@ describe("McpHub", () => { // Test invalid timeout values const invalidConfigs = [ - { command: "test", timeout: 0 }, // Too low - { command: "test", timeout: 3601 }, // Too high - { command: "test", timeout: -1 }, // Negative + { type: "stdio", command: "test", timeout: 0 }, // Too low + { type: "stdio", command: "test", timeout: 3601 }, // Too high + { type: "stdio", command: "test", timeout: -1 }, // Negative ] invalidConfigs.forEach((config) => { @@ -315,7 +321,7 @@ describe("McpHub", () => { const mockConnection: McpConnection = { server: { name: "test-server", - config: JSON.stringify({ command: "test" }), // No timeout specified + config: JSON.stringify({ type: "stdio", command: "test" }), // No timeout specified status: "connected", }, client: { @@ -338,7 +344,7 @@ describe("McpHub", () => { const mockConnection: McpConnection = { server: { name: "test-server", - config: JSON.stringify({ command: "test", timeout: 120 }), // 2 minutes + config: JSON.stringify({ type: "stdio", command: "test", timeout: 120 }), // 2 minutes status: "connected", }, client: { @@ -363,6 +369,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], timeout: 60, @@ -385,6 +392,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], timeout: 60, @@ -406,6 +414,7 @@ describe("McpHub", () => { server: { name: "test-server", config: JSON.stringify({ + type: "stdio", command: "node", args: ["test.js"], timeout: 3601, // Invalid timeout @@ -435,6 +444,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], timeout: 60, @@ -458,6 +468,7 @@ describe("McpHub", () => { const mockConfig = { mcpServers: { "test-server": { + type: "stdio", command: "node", args: ["test.js"], timeout: 60,