// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { LoaderFunctionArgs } from "react-router"; import { getTranslations } from "~/locale/i18next.server"; import { getDeployAdminSnapshot } from "~/services/admin/deploy-admin.server"; import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import { loader, shortenRevision } from "./deploys"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); vi.mock("~/components/ui/layouts/EditPageLayout", () => ({ default: () => null })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() })); vi.mock("~/services/admin/deploy-admin.server", () => ({ getDeployAdminSnapshot: vi.fn() })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn() })); function callLoader(url = "https://admin.example.test/system/deploys") { return loader({ request: new Request(url), params: {}, context: {} } as LoaderFunctionArgs); } describe("admin deploys 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(getDeployAdminSnapshot).mockResolvedValue({ migrationDbReachable: true, metadata: { environment: "staging", nodeEnv: "production", release: "abc123", revision: "abc123", branch: "main", image: "hivegrid-web:abc123", buildTime: "2026-07-09T00:00:00.000Z", }, migrations: [{ name: "20260701000000_init", state: "applied", appliedAt: "2026-07-09T00:00:00.000Z" }], } as never); }); it("requires metrics permission and builds deploy data", async () => { const data = await callLoader(); expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.metrics.view"); expect(getDeployAdminSnapshot).toHaveBeenCalledOnce(); expect(data.vm.status).toBe("ready"); if (data.vm.status !== "ready") return; expect(data.vm.data.metadata.release).toBe("abc123"); expect(data.vm.data.migrationSummary.applied).toBe(1); }); }); describe("shortenRevision", () => { it("shortens a full commit hash so it stops overflowing its card", () => { // The 40-char hash pushed the value column past the card and ran over the // neighbouring rows (admin audit 2026-07-26, P0-07). expect(shortenRevision("b05b699647daae446ce6c30e8c1a5449851c5d2a")).toBe("b05b699647da"); }); it("leaves short and non-hash values untouched", () => { expect(shortenRevision("b05b699")).toBe("b05b699"); expect(shortenRevision("local")).toBe("local"); expect(shortenRevision("main")).toBe("main"); expect(shortenRevision("hivegrid-web:b05b699647daae446ce6c30e8c1a5449851c5d2a")).toBe("hivegrid-web:b05b699647daae446ce6c30e8c1a5449851c5d2a"); expect(shortenRevision("2026-07-09T00:00:00.000Z")).toBe("2026-07-09T00:00:00.000Z"); }); });