// ABOUTME: Unit tests for MCP server tool-call helpers.
// ABOUTME: Verifies tool responses and error formatting.
import assert from "node:assert/strict";
import { test } from "node:test";
import { handleToolCall } from "../../src/server.js";
import { ValidationError } from "../../src/tools/validation.js";
test("handleToolCall returns error for unknown tool", async () => {
const handlers = new Map();
const result = await handleToolCall(handlers, "unknown_tool", {});
assert.equal(result.isError, true);
assert.equal(result.content[0]?.text, "Unknown tool: unknown_tool");
});
test("handleToolCall returns JSON payload for successful handler", async () => {
const handlers = new Map([
[
"demo",
async () => ({
ok: true,
}),
],
]);
const result = await handleToolCall(handlers, "demo", {});
assert.equal(result.isError, undefined);
assert.equal(result.content[0]?.text, JSON.stringify({ ok: true }));
});
test("handleToolCall formats ValidationError as tool error", async () => {
const handlers = new Map([
[
"demo",
async () => {
throw new ValidationError("bad input");
},
],
]);
const originalError = console.error;
console.error = () => {};
try {
const result = await handleToolCall(handlers, "demo", {});
assert.equal(result.isError, true);
assert.equal(result.content[0]?.text, "bad input");
} finally {
console.error = originalError;
}
});