diff --git a/jest.setup.ts b/jest.setup.ts index 64ae4e9ad..2e53a6e78 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -9,7 +9,6 @@ jest.mock("open", () => jest.fn().mockResolvedValue(undefined)); // Mock common modules that cause issues jest.mock("./src/logging.js"); -jest.mock("./src/util/logger.js"); jest.mock("./src/hooks/useService.js"); jest.mock("./src/auth/workos.js"); jest.mock("./src/commands/commands.js"); diff --git a/src/__mocks__/util/logger.ts b/src/__mocks__/util/logger.ts index 90c184456..a52441b03 100644 --- a/src/__mocks__/util/logger.ts +++ b/src/__mocks__/util/logger.ts @@ -1,13 +1,11 @@ import { vi } from "vitest"; -const mockLogger: any = { +export const logger: any = { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn(), - child: vi.fn((): any => mockLogger), + child: vi.fn((): any => logger), }; - -export default mockLogger; export const getLogPath = vi.fn(() => "/mock/log/path"); export const getSessionId = vi.fn(() => "mock-session-id"); diff --git a/src/commands/remote.test.ts b/src/commands/remote.test.ts index 385d73b49..62289d30a 100644 --- a/src/commands/remote.test.ts +++ b/src/commands/remote.test.ts @@ -8,7 +8,6 @@ vi.mock("../env.js"); vi.mock("../telemetry/telemetryService.js"); vi.mock("../ui/index.js"); vi.mock("../util/git.js"); -vi.mock("../util/logger.js"); const mockWorkos = vi.mocked( await import("../auth/workos.js") diff --git a/src/commands/serve.test.ts b/src/commands/serve.test.ts index 502c8d2ef..535f7cf81 100644 --- a/src/commands/serve.test.ts +++ b/src/commands/serve.test.ts @@ -27,11 +27,6 @@ describe("serve command", () => { vi.spyOn(console, "log").mockImplementation(() => {}); // Mock all required modules at test level with correct relative paths - vi.mock("fs", () => ({ - ...(vi.importActual("fs") as any), - existsSync: vi.fn(() => false), - })); - vi.mock("../auth/workos.js", () => ({ loadAuthConfig: vi.fn(() => null), ensureOrganization: vi.fn(), @@ -73,13 +68,6 @@ describe("serve command", () => { }, })); - vi.mock("../util/logger.js", () => ({ - default: { - error: vi.fn(), - debug: vi.fn(), - }, - })); - vi.mock("chalk", () => ({ default: { green: (str: string) => str, diff --git a/src/mcp.test.ts b/src/mcp.test.ts index 2e3a3521c..5f1b898ce 100644 --- a/src/mcp.test.ts +++ b/src/mcp.test.ts @@ -3,17 +3,6 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { MCPService } from "./mcp.js"; - -// Mock the logger -vi.mock("./util/logger.js", () => ({ - logger: { - debug: vi.fn(), - info: vi.fn(), - warn: vi.fn(), - error: vi.fn(), - }, -})); - // Mock the MCP SDK const mockClient = { connect: vi.fn(), @@ -44,16 +33,17 @@ describe("MCPService", () => { mcpServers: [ { name: "test-server", - command: "echo", - args: ["hello"], + command: "npx", + args: ["-v"], env: { TEST: "value" }, }, ], } as AssistantConfig; }); - afterEach(() => { + afterEach(async () => { vi.clearAllMocks(); + await mcpService.shutdown() }); describe("initialization", () => { @@ -110,10 +100,14 @@ describe("MCPService", () => { expect(state.isReady).toBe(true); }); - it("should get overall status with no connections", () => { - const status = mcpService.getOverallStatus(); - expect(status.status).toBe("idle"); - expect(status.hasWarnings).toBe(false); + it("should get overall status", async () => { + console.log(mcpService.getConnectionInfo()) + const firstStatus = mcpService.getOverallStatus(); + expect(firstStatus.status).toBe("connected"); + expect(firstStatus.hasWarnings).toBe(false); + await mcpService.shutdown() + const secondStatus = mcpService.getOverallStatus(); + expect(secondStatus.status).toBe("idle"); }); }); @@ -169,11 +163,11 @@ describe("MCPService", () => { describe("shutdown", () => { it("should handle shutdown gracefully", async () => { await mcpService.initialize(mockAssistant); - await expect(mcpService.shutdown()).resolves.not.toThrow(); + await mcpService.shutdown() const state = mcpService.getState(); expect(state.isReady).toBe(false); - expect(state.mcpService).toBeNull(); + expect(state.mcpService?.getState().connections.filter(c => c.status === "connected")).toHaveLength(0) }); it("should handle multiple shutdown calls", async () => { diff --git a/src/mcp.ts b/src/mcp.ts index da1eda632..85f8a4e84 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -74,28 +74,20 @@ export class MCPService { logger.debug("Starting MCP server connections", { serverCount: assistant.mcpServers.length, }); + } + const connectionPromises = assistant.mcpServers?.map(async (config) => { + if (config) { + return await this.connectServer(config); + } + }); - const connectionPromises = assistant.mcpServers.map(async (config) => { - if (config) { - return await this.connectServer(config); - } - }); - - Promise.all(connectionPromises) - .then(() => { - if (version === this.initVersion) { - this.updateState(); - logger.debug("All MCP server connections completed"); - } - }) - .catch(() => { - if (version === this.initVersion) { - this.updateState(); - } - }); + await Promise.all(connectionPromises ?? []); + if (version === this.initVersion) { + this.currentState.isReady = true; + logger.debug("All MCP server connections completed"); + this.updateState(); } - this.updateState(); return this.currentState; } @@ -119,7 +111,7 @@ export class MCPService { (sum, c) => sum + c.prompts.length, 0, ), - isReady: true, + isReady: this.currentState.isReady, }; // Propagate state changes to the service container so the UI updates @@ -343,7 +335,7 @@ export class MCPService { }); await client.connect(transport, {}); - client.setLoggingLevel("critical"); + // client.setLoggingLevel("critical"); connection.client = client; connection.status = "connected"; @@ -480,14 +472,7 @@ export class MCPService { await this.shutdownConnections(); - this.currentState = { - mcpService: null, - connections: [], - toolCount: 0, - promptCount: 0, - isReady: false, - }; - + this.currentState.isReady = false; this.updateState(); } }