flnxi-Wu/lib/__tests__/check-limits.test.ts
2026-05-28 17:37:27 +08:00

219 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 许可证限制检查工具测试
* 测试 lib/license/check-limits.ts 中的限制检查函数
*/
import {
canCreateUser,
canUploadMaterial,
canUseCustomTemplates,
canUseDepartmentIsolation,
canUseIntranetPenetration,
canUseSystemSettings,
canUseApiAccess,
LimitCheckResult,
} from '../license/check-limits';
// Mock license-service 模块
jest.mock('../license/license-service', () => ({
checkLimit: jest.fn(),
isFeatureEnabled: jest.fn(),
getLicenseType: jest.fn(),
}));
jest.mock('../license/premium-module-service', () => ({
hasPremiumModule: jest.fn(),
PREMIUM_MODULE_IDS: {
CUSTOM_TEMPLATES_CORE: 'custom-templates-core',
INTRANET_PENETRATION_CORE: 'intranet-penetration-core',
},
}));
import { checkLimit, isFeatureEnabled, getLicenseType } from '../license/license-service';
import { hasPremiumModule } from '../license/premium-module-service';
const mockCheckLimit = checkLimit as jest.MockedFunction<typeof checkLimit>;
const mockIsFeatureEnabled = isFeatureEnabled as jest.MockedFunction<typeof isFeatureEnabled>;
const mockGetLicenseType = getLicenseType as jest.MockedFunction<typeof getLicenseType>;
const mockHasPremiumModule = hasPremiumModule as jest.MockedFunction<typeof hasPremiumModule>;
describe('canCreateUser', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('当未达到限制时应该返回 allowed: true', async () => {
mockCheckLimit.mockResolvedValue({ allowed: true, current: 5, max: 10 });
const result = await canCreateUser();
expect(result.allowed).toBe(true);
expect(result.current).toBe(5);
expect(result.max).toBe(10);
expect(result.message).toBeUndefined();
expect(mockCheckLimit).toHaveBeenCalledWith('users');
});
it('当达到限制时(FREE 版本)应该返回授权处理提示', async () => {
mockCheckLimit.mockResolvedValue({ allowed: false, current: 10, max: 10 });
mockGetLicenseType.mockResolvedValue('FREE');
const result = await canCreateUser();
expect(result.allowed).toBe(false);
expect(result.message).toContain('当前许可证');
expect(result.message).toContain('10');
expect(result.message).toContain('联系管理员');
});
it('当达到限制时(非 FREE 版本)应该返回联系管理员提示', async () => {
mockCheckLimit.mockResolvedValue({ allowed: false, current: 100, max: 100 });
mockGetLicenseType.mockResolvedValue('STANDARD');
const result = await canCreateUser();
expect(result.allowed).toBe(false);
expect(result.message).toContain('联系管理员');
expect(result.message).toContain('100');
});
});
describe('canUploadMaterial', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('当未达到限制时应该返回 allowed: true', async () => {
mockCheckLimit.mockResolvedValue({ allowed: true, current: 50, max: 100 });
const result = await canUploadMaterial();
expect(result.allowed).toBe(true);
expect(result.current).toBe(50);
expect(result.max).toBe(100);
expect(mockCheckLimit).toHaveBeenCalledWith('materials');
});
it('当达到限制时(FREE 版本)应该返回授权处理提示', async () => {
mockCheckLimit.mockResolvedValue({ allowed: false, current: 100, max: 100 });
mockGetLicenseType.mockResolvedValue('FREE');
const result = await canUploadMaterial();
expect(result.allowed).toBe(false);
expect(result.message).toContain('当前许可证');
expect(result.message).toContain('素材');
expect(result.message).toContain('联系管理员');
});
it('当达到限制时(非 FREE 版本)应该返回联系管理员提示', async () => {
mockCheckLimit.mockResolvedValue({ allowed: false, current: 1000, max: 1000 });
mockGetLicenseType.mockResolvedValue('PRO');
const result = await canUploadMaterial();
expect(result.allowed).toBe(false);
expect(result.message).toContain('联系管理员');
});
});
describe('功能开关检查', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('canUseCustomTemplates', () => {
it('当功能启用时应该返回 true', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
mockHasPremiumModule.mockResolvedValue(true);
expect(await canUseCustomTemplates()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('customTemplates');
expect(mockHasPremiumModule).toHaveBeenCalledWith('custom-templates-core');
});
it('当功能禁用时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(false);
expect(await canUseCustomTemplates()).toBe(false);
expect(mockHasPremiumModule).not.toHaveBeenCalled();
});
it('当缺少付费模块凭证时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
mockHasPremiumModule.mockResolvedValue(false);
expect(await canUseCustomTemplates()).toBe(false);
});
});
describe('canUseDepartmentIsolation', () => {
it('当功能启用时应该返回 true', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
expect(await canUseDepartmentIsolation()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('departmentIsolation');
});
it('当功能禁用时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(false);
expect(await canUseDepartmentIsolation()).toBe(false);
});
});
describe('canUseIntranetPenetration', () => {
it('当功能启用时应该返回 true', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
mockHasPremiumModule.mockResolvedValue(true);
expect(await canUseIntranetPenetration()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('intranetPenetration');
expect(mockHasPremiumModule).toHaveBeenCalledWith('intranet-penetration-core');
});
it('当功能禁用时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(false);
expect(await canUseIntranetPenetration()).toBe(false);
expect(mockHasPremiumModule).not.toHaveBeenCalled();
});
it('当缺少付费模块凭证时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
mockHasPremiumModule.mockResolvedValue(false);
expect(await canUseIntranetPenetration()).toBe(false);
});
});
describe('canUseSystemSettings', () => {
it('当功能启用时应该返回 true', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
expect(await canUseSystemSettings()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('systemSettings');
});
it('当功能禁用时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(false);
expect(await canUseSystemSettings()).toBe(false);
});
});
describe('canUseApiAccess', () => {
it('当功能启用时应该返回 true', async () => {
mockIsFeatureEnabled.mockResolvedValue(true);
expect(await canUseApiAccess()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('apiAccess');
});
it('当功能禁用时应该返回 false', async () => {
mockIsFeatureEnabled.mockResolvedValue(false);
expect(await canUseApiAccess()).toBe(false);
});
});
});
describe('LimitCheckResult 接口', () => {
it('应该正确处理无限制情况 (max = -1)', async () => {
mockCheckLimit.mockResolvedValue({ allowed: true, current: 0, max: -1 });
const result = await canCreateUser();
expect(result.allowed).toBe(true);
expect(result.max).toBe(-1);
});
});