// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { ActionFunctionArgs } from "react-router"; vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn(), })); vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn(async () => ({ userId: "user_123" })), validateCSRFToken: vi.fn(async () => undefined), })); vi.mock("~/utils/db/users.db.server", () => ({ getUser: vi.fn(async () => ({ id: "user_123", email: "user@example.com" })), })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })), })); vi.mock("~/modules/notifications/services/.server/NotificationService", () => ({ default: { deleteNotificationTemplate: vi.fn(async () => null), getNotificationTemplates: vi.fn(async () => null), send: vi.fn(async () => null), }, })); import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import NotificationService from "~/modules/notifications/services/.server/NotificationService"; import { action } from "./channels"; function callAction(actionName = "create", fields: Record = {}) { const form = new FormData(); form.set("action", actionName); for (const [key, value] of Object.entries(fields)) form.set(key, value); return action({ request: new Request("https://admin.example.test/notifications/channels", { method: "POST", body: form, }), params: {}, context: {}, } as ActionFunctionArgs); } describe("admin notification channels action", () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never); }); it("requires manage permission for mutations", async () => { await callAction(); expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.notifications.manage"); }); it("does not expose provider diagnostics when deletion fails", async () => { vi.mocked(NotificationService.deleteNotificationTemplate).mockRejectedValueOnce(new Error("provider token expired")); const response = await callAction("delete", { id: "template_123" }); expect(response).toBeInstanceOf(Response); await expect((response as Response).json()).resolves.toEqual({ error: "notifications.admin.templates.errors.deleteFailed" }); }); it("does not expose provider diagnostics when preview delivery fails", async () => { vi.mocked(NotificationService.send).mockRejectedValueOnce(new Error("provider request leaked details")); const response = await callAction("send-preview", { channel: "in_app" }); expect(response).toBeInstanceOf(Response); await expect((response as Response).json()).resolves.toEqual({ error: "notifications.admin.templates.errors.previewFailed" }); }); });