// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { LoaderFunctionArgs } from "react-router"; const mocks = vi.hoisted(() => ({ getTranslations: vi.fn(), getTenantIdFromUrl: vi.fn(), getUserInfo: vi.fn(), verifyUserHasPermission: vi.fn(), getUser: vi.fn(), getTenant: vi.fn(), getFirstStepsChecklist: vi.fn(), runWithRlsContext: vi.fn((_context: unknown, operation: () => unknown) => operation()), })); vi.mock("@hivegrid/db/rls-context.server", () => ({ runWithRlsContext: (...args: unknown[]) => mocks.runWithRlsContext(...args) })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: (...args: unknown[]) => mocks.getTranslations(...args) })); vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: (...args: unknown[]) => mocks.getTenantIdFromUrl(...args) })); vi.mock("~/utils/session.server", () => ({ getUserInfo: (...args: unknown[]) => mocks.getUserInfo(...args) })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: (...args: unknown[]) => mocks.verifyUserHasPermission(...args), })); vi.mock("~/utils/db/users.db.server", () => ({ getUser: (...args: unknown[]) => mocks.getUser(...args) })); vi.mock("~/utils/db/tenants.db.server", () => ({ getTenant: (...args: unknown[]) => mocks.getTenant(...args) })); vi.mock("~/utils/services/.server/akP20OnboardingService", () => ({ getFirstStepsChecklist: (...args: unknown[]) => mocks.getFirstStepsChecklist(...args), })); vi.mock("~/utils/app/UrlUtils", () => ({ default: { currentTenantUrl: ({ tenant }: { tenant?: string }, path: string) => `/${tenant}/${path}` }, })); import { loader } from "./welcome"; const request = new Request("https://app.example.test/acme/onboarding/welcome"); const params = { tenant: "acme" }; describe("welcome setup loader", () => { beforeEach(() => { vi.clearAllMocks(); process.env.APP_NAME = "HiveGrid"; mocks.getTranslations.mockResolvedValue({ t: (key: string) => key }); mocks.getTenantIdFromUrl.mockResolvedValue("tenant_1"); mocks.getUserInfo.mockResolvedValue({ userId: "user_1" }); mocks.getUser.mockResolvedValue({ id: "user_1", firstName: "Ada" }); mocks.getTenant.mockResolvedValue({ id: "tenant_1", name: "Acme", slug: "acme" }); mocks.getFirstStepsChecklist.mockResolvedValue({ id: "session_1", status: "active", dismissed: false, items: [ { id: "profile", block: "profile", titleKey: "onboarding.firstSteps.profile", href: "settings/profile", completed: true }, { id: "team", block: "team", titleKey: "onboarding.firstSteps.team", href: "settings/team/members/new", completed: false, canComplete: false, }, ], }); }); it("builds a tenant-scoped welcome VM with the first unfinished task", async () => { const data = await loader({ request, params, context: {}, unstable_pattern: "app.$tenant/onboarding/welcome" } as LoaderFunctionArgs); expect(mocks.verifyUserHasPermission).toHaveBeenCalledWith(request, "app.onboarding.checklist.update", "tenant_1"); expect(mocks.runWithRlsContext).toHaveBeenCalledWith({ tenantId: "tenant_1", userId: "user_1" }, expect.any(Function)); expect(data).toMatchObject({ title: "onboarding.welcome.metaTitle | HiveGrid", homeHref: "/acme/dashboard", vm: { status: "ready", data: { firstName: "Ada", workspaceName: "Acme", dashboardHref: "/acme/dashboard", nextItem: { id: "team", href: "/acme/settings/team/members/new", canComplete: false }, }, }, }); }); it("fails closed when the tenant is no longer visible in the bound RLS context", async () => { mocks.getTenant.mockResolvedValueOnce(null); await expect(loader({ request, params, context: {}, unstable_pattern: "app.$tenant/onboarding/welcome" } as LoaderFunctionArgs)).rejects.toMatchObject({ status: 404, }); }); });