// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { LoaderFunctionArgs } from "react-router"; import { getTranslations } from "~/locale/i18next.server"; import { getOperatorHealthSnapshot } from "~/services/admin/operator-health.server"; import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import { loader } from "./index"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); vi.mock("~/components/ui/layouts/EditPageLayout", () => ({ default: () => null })); vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => null })); vi.mock("~/locale/format", () => ({ useLocaleFormatters: () => ({ dateTime: (value: Date) => value.toISOString() }) })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() })); vi.mock("~/services/admin/operator-health.server", () => ({ getOperatorHealthSnapshot: vi.fn() })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn() })); function callLoader(url = "https://admin.example.test/system") { return loader({ request: new Request(url), params: {}, context: {} } as LoaderFunctionArgs); } describe("admin health route", () => { beforeEach(() => { vi.clearAllMocks(); process.env.APP_NAME = "HiveGrid"; vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never); vi.mocked(getTranslations).mockResolvedValue({ t: (key: string) => key } as never); vi.mocked(getOperatorHealthSnapshot).mockResolvedValue({ subsystems: [ { id: "system", status: "ok", href: "/admin/system" }, { id: "deploy", status: "ok", href: "/system/deploys" }, { id: "queues", status: "warning", href: "/system/jobs" }, { id: "webhooks", status: "ok", href: "/system/webhooks" }, { id: "billing", status: "warning", href: "/system/billing" }, ], deploy: { environment: "production", release: "abc123", revision: "abc123", branch: "main" }, signals: { failedJobsTotal: 2, pausedQueues: 0, webhookFailures: 0, billingErrors: 1 }, links: { jobs: "/system/jobs", webhooks: "/system/webhooks", billing: "/system/billing" }, billingErrors: [{ id: "log_1", eventType: "invoice.payment_failed", error: "boom", createdAt: "2026-07-09T00:00:00.000Z", relatedTenantId: "tenant_1" }], } as never); }); it("requires metrics permission and aggregates the operator health VM", async () => { const data = await callLoader(); expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.metrics.view"); expect(getOperatorHealthSnapshot).toHaveBeenCalledOnce(); expect(data.vm.status).toBe("ready"); if (data.vm.status !== "ready") return; expect(data.vm.data.overallStatus).toBe("warning"); expect(data.vm.data.attention.map((row) => row.id)).toEqual(["failedJobs", "billingErrors"]); expect(data.vm.data.billingErrors).toHaveLength(1); expect(data.title).toBe("admin.health.title | HiveGrid"); }); it("returns a renderable error state when aggregation fails", async () => { vi.mocked(getOperatorHealthSnapshot).mockRejectedValueOnce(new Error("snapshot failed")); const data = await callLoader(); expect(data.vm.status).toBe("error"); if (data.vm.status !== "error") return; expect(data.vm.error.messageKey).toBe("admin.health.error.body"); }); });