From f6d54793346b11c30c8daeba3af63a2bddebb641 Mon Sep 17 00:00:00 2001 From: SnapOtter Date: Sat, 13 Jun 2026 16:22:01 +0800 Subject: [PATCH] test: add enterprise feature mock utilities --- tests/helpers/enterprise-mock.ts | 34 +++++++++++++++++ tests/unit/api/enterprise-mock.test.ts | 51 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/helpers/enterprise-mock.ts create mode 100644 tests/unit/api/enterprise-mock.test.ts diff --git a/tests/helpers/enterprise-mock.ts b/tests/helpers/enterprise-mock.ts new file mode 100644 index 00000000..6fe3aab0 --- /dev/null +++ b/tests/helpers/enterprise-mock.ts @@ -0,0 +1,34 @@ +import { vi } from "vitest"; +import type { EnterpriseFeature } from "@snapotter/enterprise"; + +export function mockEnterpriseFeatures(features: EnterpriseFeature[]) { + vi.doMock("@snapotter/enterprise", () => ({ + isFeatureEnabled: (feature: string) => + features.includes(feature as EnterpriseFeature), + getActiveLicense: () => ({ + org: "test-org", + plan: "enterprise" as const, + features, + seats: 100, + expiresAt: new Date( + Date.now() + 365 * 24 * 60 * 60 * 1000, + ).toISOString(), + issuedAt: new Date().toISOString(), + }), + initEnterprise: vi.fn(), + loadS3Storage: vi.fn(), + ENTERPRISE_FEATURES: features, + PLAN_FEATURES: { team: [], enterprise: features }, + })); +} + +export function mockNoEnterprise() { + vi.doMock("@snapotter/enterprise", () => ({ + isFeatureEnabled: () => false, + getActiveLicense: () => null, + initEnterprise: vi.fn(), + loadS3Storage: vi.fn(), + ENTERPRISE_FEATURES: [], + PLAN_FEATURES: { team: [], enterprise: [] }, + })); +} diff --git a/tests/unit/api/enterprise-mock.test.ts b/tests/unit/api/enterprise-mock.test.ts new file mode 100644 index 00000000..50e67c43 --- /dev/null +++ b/tests/unit/api/enterprise-mock.test.ts @@ -0,0 +1,51 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; + +describe("enterprise mock", () => { + beforeEach(() => { + vi.resetModules(); + }); + + it("mocks feature enabled", async () => { + const { mockEnterpriseFeatures } = await import( + "../../helpers/enterprise-mock.js" + ); + mockEnterpriseFeatures(["audit_export", "siem_forwarding"]); + const { isFeatureEnabled } = await import("@snapotter/enterprise"); + expect(isFeatureEnabled("audit_export")).toBe(true); + expect(isFeatureEnabled("siem_forwarding")).toBe(true); + expect(isFeatureEnabled("scim")).toBe(false); + }); + + it("mocks no enterprise", async () => { + const { mockNoEnterprise } = await import( + "../../helpers/enterprise-mock.js" + ); + mockNoEnterprise(); + const { isFeatureEnabled } = await import("@snapotter/enterprise"); + expect(isFeatureEnabled("audit_export")).toBe(false); + }); + + it("returns license payload with correct shape", async () => { + const { mockEnterpriseFeatures } = await import( + "../../helpers/enterprise-mock.js" + ); + mockEnterpriseFeatures(["s3_storage", "mfa"]); + const { getActiveLicense } = await import("@snapotter/enterprise"); + const license = getActiveLicense(); + expect(license).not.toBeNull(); + expect(license!.org).toBe("test-org"); + expect(license!.plan).toBe("enterprise"); + expect(license!.features).toEqual(["s3_storage", "mfa"]); + expect(license!.seats).toBe(100); + expect(new Date(license!.expiresAt).getTime()).toBeGreaterThan(Date.now()); + }); + + it("returns null license when no enterprise", async () => { + const { mockNoEnterprise } = await import( + "../../helpers/enterprise-mock.js" + ); + mockNoEnterprise(); + const { getActiveLicense } = await import("@snapotter/enterprise"); + expect(getActiveLicense()).toBeNull(); + }); +});