import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { generateHook } from "./hook.js"; describe("add hook generator", () => { let tempRoot: string; beforeEach(async () => { tempRoot = await mkdtemp(path.join(tmpdir(), "toolctrl-cli-hook-")); }); afterEach(async () => { await rm(tempRoot, { recursive: true, force: true }); }); it("emits a hook file under apps/web/app/hooks/_generated/", async () => { const result = await generateHook({ name: "on-contact-created", repoRoot: tempRoot, dryRun: true }); const rels = result.files.map((f) => path.relative(tempRoot, f.path)); expect(rels).toEqual(["apps/web/app/hooks/_generated/onContactCreated.hook.ts"]); }); it("file contains a forward-compat header pointing at Phase 06 docs", async () => { const result = await generateHook({ name: "on-foo", repoRoot: tempRoot, dryRun: true }); expect(result.files[0]!.contents).toContain("Phase 06"); expect(result.files[0]!.contents).toMatch(/registerOnFooHook/); expect(result.files[0]!.contents).toMatch(/triggerOnFooHook/); expect(result.files[0]!.contents).not.toContain("TODO"); }); it("notes mention the plugin SDK migration path", async () => { const result = await generateHook({ name: "on-foo", repoRoot: tempRoot, dryRun: true }); expect(result.notes.join("\n")).toMatch(/@toolctrl\/plugin-sdk/); }); it("refuses to overwrite an existing hook", async () => { await generateHook({ name: "on-foo", repoRoot: tempRoot, dryRun: false }); await expect(generateHook({ name: "on-foo", repoRoot: tempRoot, dryRun: false })).rejects.toThrow( /already exists/, ); }); it("rejects invalid hook names", async () => { await expect(generateHook({ name: "1bad", repoRoot: tempRoot, dryRun: true })).rejects.toThrow(/Invalid/); }); });