/**
 * ZulipCoreModule单元测试
 * 
 * 测试范围：
 * - 模块配置验证
 * - 服务提供者注册
 * - 依赖注入配置
 * - 模块导入导出
 * 
 * @author moyin
 * @version 1.0.0
 */

import { Test, TestingModule } from '@nestjs/testing';
import { ConfigModule } from '@nestjs/config';
import { CacheModule } from '@nestjs/cache-manager';
import { ZulipCoreModule } from './zulip_core.module';
import { ApiKeySecurityService } from './services/api_key_security.service';
import { ConfigManagerService } from './services/config_manager.service';
import { DynamicConfigManagerService } from './services/dynamic_config_manager.service';
import { ErrorHandlerService } from './services/error_handler.service';
import { MonitoringService } from './services/monitoring.service';
import { StreamInitializerService } from './services/stream_initializer.service';
import { UserManagementService } from './services/user_management.service';
import { UserRegistrationService } from './services/user_registration.service';
import { ZulipAccountService } from './services/zulip_account.service';
import { ZulipClientPoolService } from './services/zulip_client_pool.service';
import { ZulipClientService } from './services/zulip_client.service';
import { AppLoggerService } from '../utils/logger/logger.service';

describe('ZulipCoreModule', () => {
  let module: TestingModule;

  beforeEach(async () => {
    module = await Test.createTestingModule({
      imports: [
        ConfigModule.forRoot({
          isGlobal: true,
        }),
        CacheModule.register({
          ttl: 300,
          max: 1000,
        }),
        ZulipCoreModule,
      ],
      providers: [
        // Mock Redis服务
        {
          provide: 'REDIS_SERVICE',
          useValue: {
            get: jest.fn(),
            set: jest.fn(),
            del: jest.fn(),
          },
        },
        // Mock AppLoggerService
        {
          provide: AppLoggerService,
          useValue: {
            log: jest.fn(),
            error: jest.fn(),
            warn: jest.fn(),
            debug: jest.fn(),
          },
        },
      ],
    }).compile();
  });

  afterEach(async () => {
    if (module) {
      await module.close();
    }
  });

  describe('Module Configuration', () => {
    it('should be defined', () => {
      expect(ZulipCoreModule).toBeDefined();
    });

    it('should compile successfully', () => {
      expect(module).toBeDefined();
    });
  });

  describe('Service Providers Registration', () => {
    it('should provide ApiKeySecurityService', () => {
      const service = module.get<ApiKeySecurityService>(ApiKeySecurityService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ApiKeySecurityService);
    });

    it('should provide ConfigManagerService', () => {
      const service = module.get<ConfigManagerService>(ConfigManagerService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ConfigManagerService);
    });

    it('should provide DynamicConfigManagerService', () => {
      const service = module.get<DynamicConfigManagerService>(DynamicConfigManagerService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(DynamicConfigManagerService);
    });

    it('should provide ErrorHandlerService', () => {
      const service = module.get<ErrorHandlerService>(ErrorHandlerService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ErrorHandlerService);
    });

    it('should provide MonitoringService', () => {
      const service = module.get<MonitoringService>(MonitoringService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(MonitoringService);
    });

    it('should provide StreamInitializerService', () => {
      const service = module.get<StreamInitializerService>(StreamInitializerService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(StreamInitializerService);
    });

    it('should provide UserManagementService', () => {
      const service = module.get<UserManagementService>(UserManagementService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(UserManagementService);
    });

    it('should provide UserRegistrationService', () => {
      const service = module.get<UserRegistrationService>(UserRegistrationService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(UserRegistrationService);
    });

    it('should provide ZulipAccountService', () => {
      const service = module.get<ZulipAccountService>(ZulipAccountService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ZulipAccountService);
    });

    it('should provide ZulipClientPoolService', () => {
      const service = module.get<ZulipClientPoolService>(ZulipClientPoolService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ZulipClientPoolService);
    });

    it('should provide ZulipClientService', () => {
      const service = module.get<ZulipClientService>(ZulipClientService);
      expect(service).toBeDefined();
      expect(service).toBeInstanceOf(ZulipClientService);
    });
  });

  describe('Dependency Injection Configuration', () => {
    it('should inject dependencies correctly for ApiKeySecurityService', () => {
      const service = module.get<ApiKeySecurityService>(ApiKeySecurityService);
      expect(service).toBeDefined();
      // ApiKeySecurityService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for ConfigManagerService', () => {
      const service = module.get<ConfigManagerService>(ConfigManagerService);
      expect(service).toBeDefined();
      // ConfigManagerService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for DynamicConfigManagerService', () => {
      const service = module.get<DynamicConfigManagerService>(DynamicConfigManagerService);
      expect(service).toBeDefined();
      // DynamicConfigManagerService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for ErrorHandlerService', () => {
      const service = module.get<ErrorHandlerService>(ErrorHandlerService);
      expect(service).toBeDefined();
      // ErrorHandlerService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for MonitoringService', () => {
      const service = module.get<MonitoringService>(MonitoringService);
      expect(service).toBeDefined();
      // MonitoringService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for StreamInitializerService', () => {
      const service = module.get<StreamInitializerService>(StreamInitializerService);
      expect(service).toBeDefined();
      // StreamInitializerService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for UserManagementService', () => {
      const service = module.get<UserManagementService>(UserManagementService);
      expect(service).toBeDefined();
      // UserManagementService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for UserRegistrationService', () => {
      const service = module.get<UserRegistrationService>(UserRegistrationService);
      expect(service).toBeDefined();
      // UserRegistrationService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for ZulipAccountService', () => {
      const service = module.get<ZulipAccountService>(ZulipAccountService);
      expect(service).toBeDefined();
      // ZulipAccountService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for ZulipClientPoolService', () => {
      const service = module.get<ZulipClientPoolService>(ZulipClientPoolService);
      expect(service).toBeDefined();
      // ZulipClientPoolService应该能够正常实例化，说明依赖注入配置正确
    });

    it('should inject dependencies correctly for ZulipClientService', () => {
      const service = module.get<ZulipClientService>(ZulipClientService);
      expect(service).toBeDefined();
      // ZulipClientService应该能够正常实例化，说明依赖注入配置正确
    });
  });

  describe('Module Exports', () => {
    it('should export all core services for external use', () => {
      // 验证模块导出的服务可以被外部模块使用
      const apiKeySecurityService = module.get<ApiKeySecurityService>(ApiKeySecurityService);
      const configManagerService = module.get<ConfigManagerService>(ConfigManagerService);
      const dynamicConfigManagerService = module.get<DynamicConfigManagerService>(DynamicConfigManagerService);
      const errorHandlerService = module.get<ErrorHandlerService>(ErrorHandlerService);
      const monitoringService = module.get<MonitoringService>(MonitoringService);
      const streamInitializerService = module.get<StreamInitializerService>(StreamInitializerService);
      const userManagementService = module.get<UserManagementService>(UserManagementService);
      const userRegistrationService = module.get<UserRegistrationService>(UserRegistrationService);
      const zulipAccountService = module.get<ZulipAccountService>(ZulipAccountService);
      const zulipClientPoolService = module.get<ZulipClientPoolService>(ZulipClientPoolService);
      const zulipClientService = module.get<ZulipClientService>(ZulipClientService);

      expect(apiKeySecurityService).toBeDefined();
      expect(configManagerService).toBeDefined();
      expect(dynamicConfigManagerService).toBeDefined();
      expect(errorHandlerService).toBeDefined();
      expect(monitoringService).toBeDefined();
      expect(streamInitializerService).toBeDefined();
      expect(userManagementService).toBeDefined();
      expect(userRegistrationService).toBeDefined();
      expect(zulipAccountService).toBeDefined();
      expect(zulipClientPoolService).toBeDefined();
      expect(zulipClientService).toBeDefined();
    });
  });

  describe('Module Integration', () => {
    it('should integrate all services without conflicts', () => {
      // 验证所有服务可以同时存在且无冲突
      const services = [
        module.get<ApiKeySecurityService>(ApiKeySecurityService),
        module.get<ConfigManagerService>(ConfigManagerService),
        module.get<DynamicConfigManagerService>(DynamicConfigManagerService),
        module.get<ErrorHandlerService>(ErrorHandlerService),
        module.get<MonitoringService>(MonitoringService),
        module.get<StreamInitializerService>(StreamInitializerService),
        module.get<UserManagementService>(UserManagementService),
        module.get<UserRegistrationService>(UserRegistrationService),
        module.get<ZulipAccountService>(ZulipAccountService),
        module.get<ZulipClientPoolService>(ZulipClientPoolService),
        module.get<ZulipClientService>(ZulipClientService),
      ];

      services.forEach(service => {
        expect(service).toBeDefined();
      });

      // 验证服务实例的唯一性（单例模式）
      const apiKeySecurityService1 = module.get<ApiKeySecurityService>(ApiKeySecurityService);
      const apiKeySecurityService2 = module.get<ApiKeySecurityService>(ApiKeySecurityService);
      expect(apiKeySecurityService1).toBe(apiKeySecurityService2);
    });

    it('should handle module lifecycle correctly', async () => {
      // 验证模块生命周期管理
      expect(module).toBeDefined();
      
      // 模块应该能够正常关闭
      await expect(module.close()).resolves.not.toThrow();
    });
  });
});