fix: tests for mcp
This commit is contained in:
parent
2b56851cda
commit
05f677b53c
6 changed files with 30 additions and 67 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
43
src/mcp.ts
43
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue