import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util'; import { MiniAuthService } from '../../src/services/index.mt'; import { SystemConfigServiceMt } from '@d8d/core-module-mt/system-config-module-mt'; import { SystemConfigMt } from '@d8d/core-module-mt/system-config-module-mt'; import { UserEntityMt, RoleMt } from '@d8d/core-module-mt/user-module-mt'; import { FileMt } from '@d8d/core-module-mt/file-module-mt'; // 设置集成测试钩子 setupIntegrationDatabaseHooksWithEntities([SystemConfigMt, UserEntityMt, RoleMt, FileMt]) /** * 认证模块系统配置集成测试 * 验证MiniAuthService与SystemConfigServiceMt的集成 */ describe('认证模块系统配置集成测试', () => { let miniAuthService: MiniAuthService; let systemConfigService: SystemConfigServiceMt; const testTenantId = 1; beforeEach(async () => { // 获取数据源 const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) throw new Error('Database not initialized'); miniAuthService = new MiniAuthService(dataSource); systemConfigService = new SystemConfigServiceMt(dataSource); // 清理测试配置 await systemConfigService.deleteConfig('wx.mini.app.id', testTenantId); await systemConfigService.deleteConfig('wx.mini.app.secret', testTenantId); }); afterEach(async () => { // 清理测试配置 await systemConfigService.deleteConfig('wx.mini.app.id', testTenantId); await systemConfigService.deleteConfig('wx.mini.app.secret', testTenantId); }); describe('系统配置读取功能', () => { it('应该从系统配置获取小程序AppID和Secret', async () => { // 设置测试配置 await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID'); await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', testTenantId, '测试小程序AppSecret'); // 验证配置读取 const appId = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId); const appSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', testTenantId); expect(appId).toBe('test-app-id'); expect(appSecret).toBe('test-app-secret'); }); it('应该支持批量获取配置', async () => { // 设置测试配置 await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID'); await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', testTenantId, '测试小程序AppSecret'); // 批量获取配置 const configs = await systemConfigService.getConfigsByKeys( ['wx.mini.app.id', 'wx.mini.app.secret'], testTenantId ); expect(configs['wx.mini.app.id']).toBe('test-app-id'); expect(configs['wx.mini.app.secret']).toBe('test-app-secret'); }); }); describe('租户配置隔离', () => { const tenant1Id = 1; const tenant2Id = 2; it('应该支持不同租户的独立配置', async () => { // 为租户1设置配置 await systemConfigService.setConfig('wx.mini.app.id', 'tenant1-app-id', tenant1Id, '租户1小程序AppID'); await systemConfigService.setConfig('wx.mini.app.secret', 'tenant1-app-secret', tenant1Id, '租户1小程序AppSecret'); // 为租户2设置不同的配置 await systemConfigService.setConfig('wx.mini.app.id', 'tenant2-app-id', tenant2Id, '租户2小程序AppID'); await systemConfigService.setConfig('wx.mini.app.secret', 'tenant2-app-secret', tenant2Id, '租户2小程序AppSecret'); // 验证租户1的配置 const tenant1AppId = await systemConfigService.getConfigByKey('wx.mini.app.id', tenant1Id); const tenant1AppSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', tenant1Id); // 验证租户2的配置 const tenant2AppId = await systemConfigService.getConfigByKey('wx.mini.app.id', tenant2Id); const tenant2AppSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', tenant2Id); expect(tenant1AppId).toBe('tenant1-app-id'); expect(tenant1AppSecret).toBe('tenant1-app-secret'); expect(tenant2AppId).toBe('tenant2-app-id'); expect(tenant2AppSecret).toBe('tenant2-app-secret'); // 验证配置隔离 expect(tenant1AppId).not.toBe(tenant2AppId); expect(tenant1AppSecret).not.toBe(tenant2AppSecret); }); }); describe('配置缓存功能', () => { it('应该支持配置缓存', async () => { // 设置配置 await systemConfigService.setConfig('wx.mini.app.id', 'cached-app-id', testTenantId, '缓存测试小程序AppID'); // 第一次读取(应该写入缓存) const firstRead = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId); expect(firstRead).toBe('cached-app-id'); // 第二次读取(应该从缓存读取) const secondRead = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId); expect(secondRead).toBe('cached-app-id'); }); it('应该支持空值缓存', async () => { // 读取不存在的配置(应该设置空值缓存) const nonExistentConfig = await systemConfigService.getConfigByKey('non.existent.key', testTenantId); expect(nonExistentConfig).toBeNull(); // 再次读取(应该从空值缓存返回) const cachedRead = await systemConfigService.getConfigByKey('non.existent.key', testTenantId); expect(cachedRead).toBeNull(); }); }); describe('MiniAuthService集成', () => { it('应该正确注入SystemConfigServiceMt依赖', () => { // 验证服务实例化 expect(miniAuthService).toBeDefined(); expect(miniAuthService).toBeInstanceOf(MiniAuthService); }); it('应该支持租户ID参数', async () => { // 验证miniLogin方法支持tenantId参数 const method = miniAuthService.miniLogin; expect(typeof method).toBe('function'); // 验证方法签名包含tenantId参数 expect(method.length).toBeGreaterThanOrEqual(1); }); }); });