// @vitest-environment jsdom import "@testing-library/jest-dom"; import { render, screen } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }), })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })), })); vi.mock("~/utils/app/UrlUtils", () => ({ default: { currentTenantUrl: (params: { tenant?: string }, path: string) => `/${params.tenant}/${path}`, stripTrailingSlash: (value: string) => value.replace(/\/$/, ""), }, })); const navigate = vi.fn(); let pathname = "/acme/settings"; let demoDataUiEnabled = true; let telephonyFeatureAllowed = true; let subscriptionActivationRequired = false; const environmentMocks = vi.hoisted(() => ({ isDemoDataUiEnabled: vi.fn(() => true), })); vi.mock("~/utils/services/.server/demoDataUi.server", () => ({ isDemoDataUiEnabled: () => environmentMocks.isDemoDataUiEnabled(), })); vi.mock("react-router", async () => { const actual = await vi.importActual("react-router"); return { ...actual, Outlet: () =>
, useLoaderData: () => ({ title: "Settings | HiveGrid", demoDataUiEnabled }), useLocation: () => ({ pathname }), useNavigate: () => navigate, useParams: () => ({ tenant: "acme" }), }; }); vi.mock("~/components/ui/layouts/SidebarIconsLayout", () => ({ default: ({ children, items }: { children: React.ReactNode; items: Array<{ name: string; href: string }> }) => ( ), })); vi.mock("~/components/ui/icons/settings/CustomerIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/settings/CustomerIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/settings/PeopleIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/settings/PeopleIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/settings/MembershipCardIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/settings/MembershipCardIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/crm/CompanyIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/crm/CompanyIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/crud/LinkIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/crud/LinkIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/RestApiIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/RestApiIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/ActivityHistoryIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/ActivityHistoryIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/ModulesIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/entities/ModulesIconFilled", () => ({ default: () => null })); vi.mock("~/components/ui/icons/CreditsIcon", () => ({ default: () => null })); vi.mock("~/components/ui/icons/CreditsIconFilled", () => ({ default: () => null })); vi.mock("lucide-react", () => ({ BellIcon: () => null, FileTextIcon: () => null, PaintbrushIcon: () => null, PlugIcon: () => null, ShieldCheckIcon: () => null, })); let grantedPermissions = new Set(); vi.mock("~/utils/data/useAppOrAdminData", () => ({ useAppOrAdminData: () => ({ isSuperUser: true, permissions: Array.from(grantedPermissions), productAccess: { api: { allowed: true }, integrations: { allowed: true }, "email-inbox": { allowed: true }, telephony: { allowed: telephonyFeatureAllowed }, chat: { allowed: true }, }, }), })); vi.mock("~/utils/data/useAppData", () => ({ useAppData: () => ({ subscriptionActivationRequired }), })); vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ appConfiguration: { app: { features: { tenantApiKeys: true, tenantEntityCustomization: true, }, }, }, }), })); vi.mock("~/utils/helpers/PermissionsHelper", () => ({ getUserHasPermission: (_data: unknown, permission: string) => grantedPermissions.has(permission) })); vi.mock("~/modules/usage/dtos/CreditType", () => ({ CreditTypes: [{ name: "credits" }] })); import SettingsRoute, { loader } from "./settings"; describe("workspace settings shell", () => { beforeEach(() => { navigate.mockClear(); pathname = "/acme/settings/profile"; demoDataUiEnabled = true; telephonyFeatureAllowed = true; subscriptionActivationRequired = false; environmentMocks.isDemoDataUiEnabled.mockReturnValue(true); grantedPermissions = new Set([ "app.settings.members.view", "app.settings.subscription.view", "app.settings.subscription.invoices.view", "app.onboarding.demoData.manage", "app.settings.account.view", "app.settings.roles.view", "app.settings.apiKeys.view", "app.settings.auditTrails.view", "app.settings.account.update", "portal.admin", "portal.read", "app.plugins.use", "integrations.read", "email.read_own", "calendar.connect_own", "telephony.manage", "chat.read_all_channels", ]); }); it("renders the tenant settings navigation and outlet", () => { render(); expect(screen.getByRole("navigation", { name: "Workspace settings" })).toBeInTheDocument(); // AK-SET-01: Profil, Sicherheit, Benachrichtigungen und "Meine Daten" sind Tabs im Mein-Konto-Hub. expect(screen.getByRole("link", { name: "settings.hubs.account.title" })).toHaveAttribute("href", "/acme/settings/profile"); expect(screen.queryByRole("link", { name: "settings.security.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.privacy.title" })).not.toBeInTheDocument(); // AK-SET-01: Mitglieder, Rollen und Gruppen sind Tabs im Team-Hub. expect(screen.getByRole("link", { name: "settings.hubs.team.title" })).toHaveAttribute("href", "/acme/settings/team"); expect(screen.queryByRole("link", { name: "settings.members.title" })).not.toBeInTheDocument(); // AK-SET-01: Abo, Nutzung und Rechnungen sind Tabs im Abrechnung-Hub, kein eigener Rail-Eintrag mehr. expect(screen.getByRole("link", { name: "settings.hubs.billing.title" })).toHaveAttribute("href", "/acme/settings/billing"); expect(screen.queryByRole("link", { name: "settings.subscription.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "billing:usage.title" })).not.toBeInTheDocument(); // AK-SET-01: Stammdaten, Richtlinien und Demo-Daten sind Tabs im Workspace-Hub. expect(screen.getByRole("link", { name: "settings.hubs.workspace.title" })).toHaveAttribute("href", "/acme/settings/workspace"); expect(screen.queryByRole("link", { name: "onboarding.demoData.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.tenant.title" })).not.toBeInTheDocument(); // AK-SET-01: die Mailvorlagen sind ein Tab im Kundenportal-Hub, kein eigener Rail-Eintrag. expect(screen.queryByRole("link", { name: "customerPortal.emailTemplates.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.theme.title" })).not.toBeInTheDocument(); // AK-SET-01: Katalog, Telefonie, Plugins und API-Schlüssel sind Tabs im Erweiterungen-Hub. expect(screen.getByRole("link", { name: "settings.hubs.integrations.title" })).toHaveAttribute("href", "/acme/settings/integrations"); expect(screen.queryByRole("link", { name: "telephony.nav.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "models.apiKey.plural" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.plugins.title" })).not.toBeInTheDocument(); // AK-SET-01: Chat-Audit und Protokolle sind Tabs im Compliance-Hub. expect(screen.getByRole("link", { name: "settings.hubs.compliance.title" })).toHaveAttribute("href", "/acme/settings/compliance"); expect(screen.queryByRole("link", { name: "chat.audit.navTitle" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.forms.title" })).not.toBeInTheDocument(); // R-P2b (D-P2 S01/S02): roles are a tab INSIDE members — no separate icon tab when members.view is granted. expect(screen.queryByRole("link", { name: "models.role.plural" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.notifications.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.policies.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.api.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "models.log.plural" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "models.entity.plural" })).not.toBeInTheDocument(); expect(screen.getByTestId("settings-outlet")).toBeInTheDocument(); }); it("removes the settings rail while signup billing activation is required", () => { pathname = "/acme/settings/billing"; subscriptionActivationRequired = true; render(); expect(screen.queryByRole("navigation", { name: "Workspace settings" })).not.toBeInTheDocument(); expect(screen.getByTestId("settings-outlet")).toBeInTheDocument(); }); it("keeps roles hidden without role settings permission while preserving public workspace settings tabs", () => { grantedPermissions = new Set(["app.settings.account.view"]); render(); expect(screen.queryByRole("link", { name: "models.role.plural" })).not.toBeInTheDocument(); // AK-SET-01: Profil, Sicherheit, Benachrichtigungen und "Meine Daten" sind Tabs im Mein-Konto-Hub. expect(screen.getByRole("link", { name: "settings.hubs.account.title" })).toHaveAttribute("href", "/acme/settings/profile"); expect(screen.queryByRole("link", { name: "settings.security.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.privacy.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.forms.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.notifications.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.policies.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "customerPortal.emailTemplates.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "models.entity.plural" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "telephony.nav.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "chat.audit.navTitle" })).not.toBeInTheDocument(); }); it("hides telephony settings when the product feature gate is closed", () => { telephonyFeatureAllowed = false; render(); expect(screen.queryByRole("link", { name: "telephony.nav.title" })).not.toBeInTheDocument(); }); it("keeps roles reachable through the hub when the user may see roles but not members", () => { grantedPermissions = new Set(["app.settings.roles.view"]); render(); // AK-SET-01: the separate roles fallback entry is gone — the Team hub itself // is visible on roles.view alone, and its loader drops the members tab. expect(screen.getByRole("link", { name: "settings.hubs.team.title" })).toHaveAttribute("href", "/acme/settings/team/roles"); expect(screen.queryByRole("link", { name: "models.role.plural" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "customerPortal.emailTemplates.title" })).not.toBeInTheDocument(); }); it("keeps a portal.admin-only user on an admin-capable child route", () => { grantedPermissions = new Set(["portal.admin"]); render(); expect(screen.getByRole("link", { name: "customerPortal.admin.title" })).toHaveAttribute( "href", "/acme/settings/customer-portal/email-templates" ); expect(screen.queryByRole("link", { name: "customerPortal.emailTemplates.title" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "settings.tenant.title" })).not.toBeInTheDocument(); }); it("keeps the customer portal visible to portal.read users", () => { grantedPermissions = new Set(["portal.read"]); render(); expect(screen.getByRole("link", { name: "customerPortal.admin.title" })).toHaveAttribute("href", "/acme/settings/customer-portal"); }); it("keeps billing reachable with invoice permission alone", () => { grantedPermissions = new Set(["app.settings.subscription.invoices.view"]); render(); expect(screen.getByRole("link", { name: "settings.hubs.billing.title" })).toHaveAttribute("href", "/acme/settings/billing/invoices"); }); it("keeps the billing hub reachable with the subscription permission alone", () => { grantedPermissions = new Set(["app.settings.subscription.view"]); render(); // Either billing permission opens the hub; the hub loader decides which of // its tabs the user actually gets. expect(screen.getByRole("link", { name: "settings.hubs.billing.title" })).toHaveAttribute("href", "/acme/settings/billing"); }); it("hides the demo-data settings entry when the shared environment gate is disabled", () => { demoDataUiEnabled = false; render(); // Der Demo-Daten-Tab verschwindet aus dem Hub-Loader; der Rail-Eintrag // bleibt, solange die Konto-Permission ihn trägt. expect(screen.getByRole("link", { name: "settings.hubs.billing.title" })).toHaveAttribute("href", "/acme/settings/billing"); }); it("links a demo-only user directly to the reachable workspace tab", () => { grantedPermissions = new Set(["app.onboarding.demoData.manage"]); render(); expect(screen.getByRole("link", { name: "settings.hubs.workspace.title" })).toHaveAttribute("href", "/acme/settings/workspace/demo-data"); }); it("uses the atomic email permission and module gate for the integrations hub", () => { grantedPermissions = new Set(["email.read_own"]); render(); expect(screen.getByRole("link", { name: "settings.hubs.integrations.title" })).toHaveAttribute("href", "/acme/settings/integrations/email"); }); it("does not expose API settings through the product gate alone", () => { grantedPermissions = new Set(); render(); expect(screen.queryByRole("link", { name: "settings.hubs.integrations.title" })).not.toBeInTheDocument(); }); it("links a privacy-permission-only user directly to the reachable compliance tab", () => { grantedPermissions = new Set(["privacy.avv.read"]); render(); expect(screen.getByRole("link", { name: "settings.hubs.compliance.title" })).toHaveAttribute("href", "/acme/settings/compliance/avv"); }); it("derives demo-data visibility from the shared environment helper", async () => { environmentMocks.isDemoDataUiEnabled.mockReturnValue(false); const data = await loader({ request: new Request("https://app.example.test/acme/settings"), params: { tenant: "acme" }, context: {}, unstable_pattern: "app.$tenant/settings", }); expect(environmentMocks.isDemoDataUiEnabled).toHaveBeenCalledOnce(); expect(data.demoDataUiEnabled).toBe(false); }); it("redirects the bare tenant settings path to the profile tab", () => { pathname = "/acme/settings"; render(); expect(navigate).toHaveBeenCalledWith("/acme/settings/profile"); }); });