flnxi-Wu/lib/__tests__/check-limits.test.ts
WuYongJie ba98bf22cc feat: 添加许可证系统、Agent部署模块和Harbor镜像管理
- 新增许可证服务器 (license-server/)
- 新增 Agent 部署模块 (flnxi-agent/)
- 添加许可证验证和激活页面
- 添加 Harbor 镜像同步脚本
- 更新素材系统支持许可证验证
2025-12-15 21:19:46 +08:00

191 lines
6.1 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(),
}));
import { checkLimit, isFeatureEnabled, getLicenseType } from '../license/license-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>;
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.mockReturnValue('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.mockReturnValue('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.mockReturnValue('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.mockReturnValue('PRO');
const result = await canUploadMaterial();
expect(result.allowed).toBe(false);
expect(result.message).toContain('联系管理员');
});
});
describe('功能开关检查', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('canUseCustomTemplates', () => {
it('当功能启用时应该返回 true', () => {
mockIsFeatureEnabled.mockReturnValue(true);
expect(canUseCustomTemplates()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('customTemplates');
});
it('当功能禁用时应该返回 false', () => {
mockIsFeatureEnabled.mockReturnValue(false);
expect(canUseCustomTemplates()).toBe(false);
});
});
describe('canUseDepartmentIsolation', () => {
it('当功能启用时应该返回 true', () => {
mockIsFeatureEnabled.mockReturnValue(true);
expect(canUseDepartmentIsolation()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('departmentIsolation');
});
it('当功能禁用时应该返回 false', () => {
mockIsFeatureEnabled.mockReturnValue(false);
expect(canUseDepartmentIsolation()).toBe(false);
});
});
describe('canUseIntranetPenetration', () => {
it('当功能启用时应该返回 true', () => {
mockIsFeatureEnabled.mockReturnValue(true);
expect(canUseIntranetPenetration()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('intranetPenetration');
});
it('当功能禁用时应该返回 false', () => {
mockIsFeatureEnabled.mockReturnValue(false);
expect(canUseIntranetPenetration()).toBe(false);
});
});
describe('canUseSystemSettings', () => {
it('当功能启用时应该返回 true', () => {
mockIsFeatureEnabled.mockReturnValue(true);
expect(canUseSystemSettings()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('systemSettings');
});
it('当功能禁用时应该返回 false', () => {
mockIsFeatureEnabled.mockReturnValue(false);
expect(canUseSystemSettings()).toBe(false);
});
});
describe('canUseApiAccess', () => {
it('当功能启用时应该返回 true', () => {
mockIsFeatureEnabled.mockReturnValue(true);
expect(canUseApiAccess()).toBe(true);
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('apiAccess');
});
it('当功能禁用时应该返回 false', () => {
mockIsFeatureEnabled.mockReturnValue(false);
expect(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);
});
});