import { beforeAll, describe, expect, it, vi } from "vitest";
// Import tool fns and schemas to compare identity with what's registered
import {
nowTool,
formatTool,
addTool,
subtractTool,
differenceTool,
compareTool,
} from "../tools.js";
import {
formatParams,
addParams,
subtractParams,
differenceParams,
compareParams,
} from "../params.js";
// Mock fastmcp to capture server wiring without starting a real server
vi.mock("fastmcp", () => {
const instances: any[] = [];
class FakeFastMCP {
public name: string;
public version: string;
public tools: any[] = [];
public startConfig: any | undefined;
constructor(opts: { name: string; version: string }) {
this.name = opts.name;
this.version = opts.version;
instances.push(this);
}
addTool(tool: any) {
this.tools.push(tool);
}
start(config: any) {
this.startConfig = config;
}
}
return { FastMCP: FakeFastMCP, __instances: instances };
});
let server: any;
beforeAll(async () => {
// Import after mocking to trigger registration into our fake
await import("../index.js");
const fastmcpModule: any = await import("fastmcp");
const instances = fastmcpModule.__instances as any[];
expect(Array.isArray(instances)).toBe(true);
expect(instances.length).toBe(1);
server = instances[0];
});
describe("index server wiring", () => {
it("constructs FastMCP and starts server with httpStream on port 3000", () => {
expect(server.name).toBe("Date Operations");
expect(server.version).toBe("1.0.0");
expect(server.startConfig).toEqual({
transportType: "httpStream",
httpStream: { port: 3000 },
});
});
it("registers all 6 tools", () => {
expect(server.tools.length).toBe(6);
});
const cases = [
{
name: "now",
desc: "Get the current date and time.",
params: undefined,
exec: nowTool,
},
{
name: "format",
desc: "Get the current date.",
params: formatParams,
exec: formatTool,
},
{
name: "add",
desc: "Add the specified years, months, weeks, days, hours, minutes, and seconds to the given date.",
params: addParams,
exec: addTool,
},
{
name: "subtract",
desc: "Subtract the specified years, months, weeks, days, hours, minutes, and seconds to the given date.",
params: subtractParams,
exec: subtractTool,
},
{
name: "difference",
desc: "Get the difference between two dates in years, months, weeks, days, hours, minutes, and seconds.",
params: differenceParams,
exec: differenceTool,
},
{
name: "compare",
desc: "Compare whether the first date is after, before, or equal to the second date.",
params: compareParams,
exec: compareTool,
},
] as const;
it.each(cases)(
"registers '%s' with expected config",
(c: (typeof cases)[number]) => {
const { name, desc, params, exec } = c;
const tool = server.tools.find((t: any) => t.name === name);
expect(tool).toBeTruthy();
expect(tool.description).toBe(desc);
if (params) {
expect(tool.parameters).toBe(params);
expect(typeof tool.parameters.parse).toBe("function");
} else {
expect(tool.parameters).toBeUndefined();
}
expect(tool.execute).toBe(exec);
}
);
});