// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ tenantHasModule: vi.fn(), webConfig: { kwp: { hiveGridTenantId: "tenant-1" } }, })); vi.mock("~/utils/connector/tenantConnector.server", () => ({ tenantHasModule: mocks.tenantHasModule })); vi.mock("@hivegrid/config", () => ({ loadRequiredWebConfig: () => mocks.webConfig })); import { hasKwpIntegrationModule, KWP_INTEGRATION_MODULE_KEY, requireKwpIntegrationModule } from "./kwpAccess.server"; const CONTEXT = { tenantId: "tenant-1", userId: "user-1", permissions: ["integrations.read", "integrations.connect"], hasModule: true, } as const; beforeEach(() => { vi.clearAllMocks(); mocks.webConfig.kwp.hiveGridTenantId = "tenant-1"; mocks.tenantHasModule.mockResolvedValue(true); }); describe("KWP integration access", () => { it("uses the dedicated subscription-gated kwp_integration feature", async () => { expect(KWP_INTEGRATION_MODULE_KEY).toBe("kwp_integration"); await expect(hasKwpIntegrationModule(CONTEXT)).resolves.toBe(true); expect(mocks.tenantHasModule).toHaveBeenCalledWith({ tenantId: "tenant-1", userId: "user-1", moduleKey: "kwp_integration", }); }); it("returns a not-found domain error when the tenant lacks the add-on", async () => { mocks.tenantHasModule.mockResolvedValueOnce(false); await expect(requireKwpIntegrationModule(CONTEXT)).rejects.toMatchObject({ status: 404 }); }); it("fails closed before entitlement lookup when the process credentials belong to another tenant", async () => { mocks.webConfig.kwp.hiveGridTenantId = "tenant-other"; await expect(hasKwpIntegrationModule(CONTEXT)).resolves.toBe(false); await expect(requireKwpIntegrationModule(CONTEXT)).rejects.toMatchObject({ status: 404 }); expect(mocks.tenantHasModule).not.toHaveBeenCalled(); }); });