import { describe, it, expect, vi, beforeEach } from "vitest";
import { getTemplateHandler } from "./templates.js";
import fs from "fs/promises";
import path from "path";
vi.mock("fs/promises");
describe("Template Tools", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("getTemplateHandler", () => {
it("should list templates if no name provided", async () => {
(fs.access as any).mockResolvedValue(undefined);
(fs.readdir as any).mockResolvedValue(["template1.ts", "template2.ts"]);
const result = await getTemplateHandler({ category: "react" });
expect(result.content[0].text).toContain("template1.ts");
expect(result.content[0].text).toContain("template2.ts");
});
it("should retrieve template content", async () => {
(fs.access as any).mockResolvedValue(undefined);
// First readdir for recursive search
(fs.readdir as any).mockResolvedValue([
{
name: "my-template.ts",
isFile: () => true,
isDirectory: () => false,
},
]);
(fs.readFile as any).mockResolvedValue("const x = 1;");
const result = await getTemplateHandler({
category: "react",
templateName: "my-template",
});
expect(result.content[0].text).toContain("const x = 1;");
expect(result.content[0].text).toContain(
"Template: react/my-template.ts",
);
});
it("should return error for invalid category", async () => {
(fs.access as any).mockRejectedValue(new Error("Not found"));
const result = await getTemplateHandler({ category: "invalid" });
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain("Category 'invalid' not found");
});
});
});