drawio-mcp-server
Version:
Provides Draw.io services to MCP Clients
105 lines (104 loc) • 3.74 kB
JavaScript
import { jest } from "@jest/globals";
import { build_channel, default_tool } from "./tool.js";
import { create_logger } from "./standard_console_logger.js";
describe("build_channel", () => {
let mockBus;
let mockIdGenerator;
let context;
const mockHandler = jest.fn();
const log = create_logger();
beforeEach(() => {
mockBus = {
send_to_extension: jest.fn(),
on_reply_from_extension: jest.fn(),
};
mockIdGenerator = {
generate: jest.fn().mockReturnValue("123"),
};
context = {
bus: mockBus,
id_generator: mockIdGenerator,
log,
};
mockHandler.mockReset();
});
it("should create a function that sends a message via bus", async () => {
const eventName = "test-event";
const toolFn = build_channel(context, eventName, mockHandler);
const args = { key: "value" };
const extra = {};
const promise = toolFn(args, extra);
expect(mockBus.send_to_extension).toHaveBeenCalledWith({
__event: eventName,
__request_id: "123",
key: "value",
});
});
it("should wait for reply and call handler with response", async () => {
const eventName = "test-event";
const toolFn = build_channel(context, eventName, mockHandler);
const mockResponse = {
content: [{ type: "text", text: "response" }],
};
mockHandler.mockReturnValue(mockResponse);
const promise = toolFn({}, {});
// Simulate reply callback
const replyCallback = mockBus.on_reply_from_extension.mock.calls[0][1];
replyCallback({ data: "test" });
const result = await promise;
expect(mockBus.on_reply_from_extension).toHaveBeenCalledWith("test-event.123", expect.any(Function));
expect(mockHandler).toHaveBeenCalledWith({ data: "test" });
expect(result).toEqual(mockResponse);
});
it("should use correct reply channel name format", async () => {
mockIdGenerator.generate.mockReturnValue("456");
const eventName = "another-event";
const toolFn = build_channel(context, eventName, mockHandler);
toolFn({}, {});
expect(mockBus.on_reply_from_extension).toHaveBeenCalledWith("another-event.456", expect.any(Function));
});
});
describe("default_tool", () => {
let mockBus;
let mockIdGenerator;
const log = create_logger();
let context;
beforeEach(() => {
mockBus = {
send_to_extension: jest.fn(),
on_reply_from_extension: jest.fn((_, callback) => {
callback({ test: "data" });
}),
};
mockIdGenerator = {
generate: jest.fn().mockReturnValue("789"),
};
context = {
bus: mockBus,
id_generator: mockIdGenerator,
log,
};
});
it("should create a tool that returns JSON stringified response", async () => {
const toolName = "default-tool";
const tool = default_tool(toolName, context);
const result = await tool({}, {});
expect(result).toEqual({
content: [
{
type: "text",
text: JSON.stringify({ test: "data" }),
},
],
});
});
it("should use the provided tool name in the channel", async () => {
const toolName = "custom-tool";
const tool = default_tool(toolName, context);
await tool({}, {});
expect(mockBus.send_to_extension).toHaveBeenCalledWith({
__event: toolName,
__request_id: "789",
});
});
});