test: add enterprise feature mock utilities

This commit is contained in:
SnapOtter
2026-06-13 16:22:01 +08:00
parent 86d6f50ea6
commit f6d5479334
2 changed files with 85 additions and 0 deletions
+34
View File
@@ -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: [] },
}));
}
+51
View File
@@ -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();
});
});