Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,128 +1,147 @@
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
import { loader } from "./dashboard";
import { loadAdminDashboardRouteData } from "~/utils/data/.server/adminDashboardRouteData";
import { getTranslations } from "~/locale/i18next.server";
import { getAdminDashboardStats } from "~/utils/services/adminDashboardService";
import { adminGetAllTenantsWithUsage } from "~/utils/db/tenants.db.server";
import PeriodHelper from "~/utils/helpers/PeriodHelper";
import { createMetrics } from "~/modules/metrics/services/.server/MetricTracker";
import { requireAuth } from "~/utils/loaders.middleware";
import { readRepoFile } from "~/utils/testing/repoPaths";
const translatedT = vi.fn((key: string) => `translated:${key}`);
const computedGte = new Date("2026-02-03T04:05:06.000Z");
const stats = [{ name: "Translated accounts", stat: "10" }];
const tenants = { items: [{ id: "tenant-1" }], pagination: { page: 1, pageSize: 10, totalItems: 1, totalPages: 1 } };
vi.mock("~/locale/i18next.server", () => ({
getTranslations: vi.fn(),
}));
vi.mock("~/utils/services/adminDashboardService", () => ({
getAdminDashboardStats: vi.fn(),
}));
vi.mock("~/utils/db/tenants.db.server", () => ({
adminGetAllTenantsWithUsage: vi.fn(),
}));
vi.mock("~/utils/helpers/PeriodHelper", () => ({
default: { getGreaterThanOrEqualsFromRequest: vi.fn() },
defaultPeriodFilter: "30d",
PeriodFilters: [],
}));
import { listAdminPermissionNames } from "@hivegrid/db/admin-rbac.server";
import { runAsAdminIdentity } from "~/services/admin/admin-identity.server";
import { loadPersonalizedAdminDashboard } from "~/services/admin/admin-dashboard-personalization.server";
import { getAdminDashboardOverview, getAdminDashboardStats } from "~/utils/services/adminDashboardService";
import { adminGetAllTenantsWithUsage } from "~/utils/db/tenants.db.server";
import { db } from "~/utils/db.server";
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
vi.mock("~/modules/metrics/services/.server/MetricTracker", () => ({
createMetrics: vi.fn(),
}));
vi.mock("~/utils/loaders.middleware", () => ({
requireAuth: vi.fn(),
vi.mock("@hivegrid/db/admin-rbac.server", () => ({
listAdminPermissionNames: vi.fn(),
}));
vi.mock("~/services/admin/admin-identity.server", () => ({
runAsAdminIdentity: vi.fn(async (_request, _permission, operation) =>
operation({ adminUserId: "admin_1" })
),
}));
vi.mock("~/services/admin/admin-dashboard-personalization.server", () => ({
loadPersonalizedAdminDashboard: vi.fn(),
}));
vi.mock("~/utils/services/adminDashboardService", () => ({
getAdminDashboardOverview: vi.fn(),
getAdminDashboardStats: vi.fn(),
}));
vi.mock("~/utils/db/tenants.db.server", () => ({
adminGetAllTenantsWithUsage: vi.fn(),
}));
vi.mock("~/utils/db.server", () => ({
db: { adminUserRole: { findMany: vi.fn() } },
}));
vi.mock("~/utils/helpers/PeriodHelper", () => ({
default: { getGreaterThanOrEqualsFromRequest: vi.fn(() => new Date("2026-07-01T00:00:00.000Z")) },
}));
vi.mock("~/utils/helpers/RowPaginationHelper", () => ({
getPaginationFromCurrentUrl: vi.fn(() => ({ page: 1, pageSize: 10 })),
}));
vi.mock("~/modules/metrics/utils/defaultHeaders", () => ({
serverTimingHeaders: vi.fn(),
}));
const dashboard = {
roles: ["sales"],
widgets: [
{
id: "sales_today",
group: "sales",
titleKey: "title",
descriptionKey: "description",
value: "2",
href: "/sales/tasks/today",
tone: "neutral",
},
],
};
const overview = {
billing: { openIssueCount: 0, pastDueCount: 0, frozenCount: 0, missingProviderCount: 0, signals: [] },
system: { signals: [] },
deploy: { environment: "test", release: "abc", signals: [] },
recentEvents: [],
};
vi.mock("~/components/ui/stats/DashboardStats", () => ({
DashboardStats: () => null,
}));
vi.mock("~/components/app/ProfileBanner", () => ({
default: () => null,
}));
vi.mock("~/components/core/tenants/TenantsTable", () => ({
default: () => null,
}));
vi.mock("~/components/ui/buttons/ButtonSecondary", () => ({
default: () => null,
}));
vi.mock("~/components/ui/input/InputSelect", () => ({
default: () => null,
}));
vi.mock("~/utils/data/useAppOrAdminData", () => ({
useAppOrAdminData: vi.fn(() => ({ user: {}, permissions: ["admin.dashboard.view"] })),
}));
vi.mock("~/utils/helpers/PermissionsHelper", () => ({
getUserHasPermission: vi.fn(() => true),
}));
vi.mock("react-i18next", () => ({
useTranslation: vi.fn(() => ({ t: (key: string) => key })),
}));
function readDashboardRoute() {
return readRepoFile("apps/web/app/utils/data/.server/adminDashboardRouteData.ts");
}
describe("admin dashboard KPI route contract", () => {
describe("personalized admin dashboard loader", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.APP_NAME = "HiveGrid";
vi.mocked(getTranslations).mockResolvedValue({ t: translatedT } as never);
vi.mocked(getAdminDashboardStats).mockResolvedValue(stats as never);
vi.mocked(adminGetAllTenantsWithUsage).mockResolvedValue(tenants as never);
vi.mocked(PeriodHelper.getGreaterThanOrEqualsFromRequest).mockReturnValue(computedGte);
vi.mocked(getTranslations).mockResolvedValue({
t: (key: string) => `translated:${key}`,
} as never);
vi.mocked(createMetrics).mockResolvedValue({
time: async <T,>(value: T | Promise<T>) => value,
getServerTimingHeader: () => ({ "Server-Timing": "admin.dashboard;dur=1" }),
getServerTimingHeader: () => ({ "Server-Timing": "dashboard;dur=1" }),
} as never);
vi.mocked(listAdminPermissionNames).mockResolvedValue([
"admin.dashboard.view",
"admin_sales.crm.read",
] as never);
vi.mocked(db.adminUserRole.findMany).mockResolvedValue([
{ role: { systemKey: "sales" } },
] as never);
vi.mocked(loadPersonalizedAdminDashboard).mockResolvedValue(
dashboard as never
);
vi.mocked(getAdminDashboardStats).mockResolvedValue([
{ name: "MRR", stat: "10 €", hint: "" },
] as never);
vi.mocked(getAdminDashboardOverview).mockResolvedValue(overview as never);
vi.mocked(adminGetAllTenantsWithUsage).mockResolvedValue({
items: [],
pagination: { page: 1, pageSize: 10, totalItems: 0, totalPages: 0 },
} as never);
});
it("passes translations and the computed period boundary into the KPI service while preserving loader data", async () => {
const request = new Request("https://admin.example.com/dashboard?period=month&page=2");
it("guards the route and never loads platform data for a sales persona", async () => {
const request = new Request("https://admin.example.test/dashboard");
const result = await loadAdminDashboardRouteData({ request, params: {} });
const response = (await loader({ request, params: {}, context: {}, unstable_pattern: "admin/dashboard" })) as Response;
const body = await response.json();
expect(requireAuth).toHaveBeenCalledWith({ request, params: {} });
expect(getTranslations).toHaveBeenCalledWith(request);
expect(PeriodHelper.getGreaterThanOrEqualsFromRequest).toHaveBeenCalledWith({ request });
expect(getAdminDashboardStats).toHaveBeenCalledWith({ t: translatedT, gte: computedGte });
expect(adminGetAllTenantsWithUsage).toHaveBeenCalledWith(undefined, { page: 1, pageSize: 10 });
expect(body).toMatchObject({
expect(runAsAdminIdentity).toHaveBeenCalledWith(
request,
"admin.dashboard.view",
expect.any(Function)
);
expect(loadPersonalizedAdminDashboard).toHaveBeenCalledWith({
adminUserId: "admin_1",
roleKeys: ["sales"],
permissions: ["admin.dashboard.view", "admin_sales.crm.read"],
});
expect(getAdminDashboardStats).not.toHaveBeenCalled();
expect(getAdminDashboardOverview).not.toHaveBeenCalled();
expect(adminGetAllTenantsWithUsage).not.toHaveBeenCalled();
expect(result.data).toEqual({
title: "translated:app.sidebar.dashboard | HiveGrid",
stats,
tenants,
dashboard,
superOverview: null,
});
});
it("keeps KPI labels behind the translated dashboard service boundary", () => {
const source = readDashboardRoute();
it("loads the legacy full platform overview only for Super Admin", async () => {
vi.mocked(db.adminUserRole.findMany).mockResolvedValue([
{ role: { systemKey: "super_admin" } },
] as never);
vi.mocked(listAdminPermissionNames).mockResolvedValue([
"admin.dashboard.view",
"admin_identity.roles.manage",
] as never);
vi.mocked(loadPersonalizedAdminDashboard).mockResolvedValue({
roles: ["super_admin"],
widgets: [],
} as never);
expect(source).toContain('import { getAdminDashboardStats } from "~/utils/services/adminDashboardService";');
expect(source).toContain("getAdminDashboardStats({ t, gte: PeriodHelper.getGreaterThanOrEqualsFromRequest({ request }) })");
expect(source).not.toContain("admin.kpi.accounts.name");
expect(source).not.toContain("admin.kpi.mrr.name");
expect(source).not.toContain("admin.kpi.activeUsers.name");
expect(source).not.toContain("Translated accounts");
const result = await loadAdminDashboardRouteData({
request: new Request("https://admin.example.test/dashboard?period=month"),
params: {},
});
expect(getAdminDashboardStats).toHaveBeenCalledOnce();
expect(getAdminDashboardOverview).toHaveBeenCalledOnce();
expect(adminGetAllTenantsWithUsage).toHaveBeenCalledOnce();
expect(result.data.superOverview).toMatchObject({
overview,
tenants: { items: [] },
});
});
});