import { redirect } from "next/navigation"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { requirePermission } from "@/lib/admin/guard"; import { createHelpQuestion, deleteHelpQuestion, updateHelpQuestion, } from "./admin-help"; const { execute } = vi.hoisted(() => ({ execute: vi.fn() })); vi.mock("@/features/housekeeping/domains/content/services/mutations", () => ({ contentMutationService: { execute }, createContentMutationInvocation: ( actor: { id: number }, correlationId: string, ) => ({ expectedActorId: actor.id, correlationId, legacy: true }), })); vi.mock("@/lib/admin/guard", () => ({ requirePermission: vi.fn() })); vi.mock("@/lib/permissions", () => ({ PERMS: { PAGES_EDIT: "pages.edit" } })); vi.mock("next/cache", () => ({ revalidatePath: vi.fn() })); vi.mock("next/navigation", () => ({ redirect: vi.fn() })); const staff = { id: 1, rank: 7, username: "admin" }; const fakeForm = (data: Record) => ({ get: (key: string) => (key in data ? data[key] : null), }); beforeEach(() => { vi.clearAllMocks(); vi.mocked(requirePermission).mockResolvedValue(staff as never); execute.mockResolvedValue({ ok: true, data: { before: null, after: { id: "5" } }, correlationId: "legacy", }); }); describe("Content help legacy wrappers", () => { it("delegates create and redirects", async () => { await createHelpQuestion( fakeForm({ name: "FAQ", content: "

Answer

" }) as FormData, ); expect(execute).toHaveBeenCalledWith( expect.anything(), "help-question.change", expect.objectContaining({ action: "create", name: "FAQ" }), ); expect(redirect).toHaveBeenCalledWith("/admin/help-questions"); }); it("delegates update and redirects", async () => { await updateHelpQuestion( fakeForm({ id: "42", name: "Updated", content: "New" }) as FormData, ); expect(execute).toHaveBeenCalledWith( expect.anything(), "help-question.change", expect.objectContaining({ action: "update", id: "42" }), ); expect(redirect).toHaveBeenCalledWith("/admin/help-questions"); }); it("delegates delete and redirects", async () => { await deleteHelpQuestion(fakeForm({ id: "42" }) as FormData); expect(execute).toHaveBeenCalledWith( expect.anything(), "help-question.change", { action: "delete", id: "42" }, ); expect(redirect).toHaveBeenCalledWith("/admin/help-questions"); }); });