import { describe, it, expect, vi, beforeEach } from "vitest"; vi.mock("node:fs", () => ({ readFileSync: vi.fn(), })); vi.mock("@skills-hub-ai/skill-parser", () => ({ parseSkillMd: vi.fn(), })); import { readAndParseSkillMd } from "./skill-file.js"; import { readFileSync } from "node:fs"; import { parseSkillMd } from "@skills-hub-ai/skill-parser"; const mockReadFileSync = vi.mocked(readFileSync); const mockParseSkillMd = vi.mocked(parseSkillMd); beforeEach(() => { vi.clearAllMocks(); }); describe("readAndParseSkillMd", () => { it("returns parsed skill on success", () => { mockReadFileSync.mockReturnValue("---\nname: Test\n---\nInstructions"); mockParseSkillMd.mockReturnValue({ success: true, skill: { name: "Test", instructions: "Instructions" } as any, errors: [], }); const result = readAndParseSkillMd("SKILL.md"); expect(result.name).toBe("Test"); }); it("exits when file not found", () => { mockReadFileSync.mockImplementation(() => { throw new Error("ENOENT"); }); const exit = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); expect(() => readAndParseSkillMd("missing.md")).toThrow("exit"); exit.mockRestore(); }); it("exits when parsing fails", () => { mockReadFileSync.mockReturnValue("bad content"); mockParseSkillMd.mockReturnValue({ success: false, skill: null, errors: [{ field: "name", message: "required" }], } as any); const exit = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); expect(() => readAndParseSkillMd("bad.md")).toThrow("exit"); exit.mockRestore(); }); });