/**
 * Zulip集成业务模块测试
 * 
 * 功能描述：
 * - 测试模块配置的正确性
 * - 验证依赖注入配置的完整性
 * - 测试服务和控制器的注册
 * - 验证模块导出的正确性
 * 
 * 测试范围：
 * - 模块导入配置验证
 * - 服务提供者注册验证
 * - 控制器注册验证
 * - 模块导出验证
 * 
 * 最近修改：
 * - 2026-01-12: 代码规范优化 - 创建测试文件，确保模块配置逻辑的测试覆盖 (修改者: moyin)
 * 
 * @author moyin
 * @version 1.0.0
 * @since 2026-01-12
 * @lastModified 2026-01-12
 */

import { ZulipModule } from './zulip.module';
import { ZulipService } from './zulip.service';
import { SessionManagerService } from './services/session_manager.service';
import { MessageFilterService } from './services/message_filter.service';
import { ZulipEventProcessorService } from './services/zulip_event_processor.service';
import { SessionCleanupService } from './services/session_cleanup.service';
import { CleanWebSocketGateway } from './clean_websocket.gateway';
import { ChatController } from './chat.controller';
import { WebSocketDocsController } from './websocket_docs.controller';
import { WebSocketOpenApiController } from './websocket_openapi.controller';
import { ZulipAccountsController } from './zulip_accounts.controller';
import { WebSocketTestController } from './websocket_test.controller';
import { DynamicConfigController } from './dynamic_config.controller';
import { DynamicConfigManagerService } from '../../core/zulip_core/services/dynamic_config_manager.service';

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

    it('should have correct module metadata', () => {
      const moduleMetadata = Reflect.getMetadata('imports', ZulipModule) || [];
      const providersMetadata = Reflect.getMetadata('providers', ZulipModule) || [];
      const controllersMetadata = Reflect.getMetadata('controllers', ZulipModule) || [];
      const exportsMetadata = Reflect.getMetadata('exports', ZulipModule) || [];

      // 验证导入的模块数量
      expect(moduleMetadata).toHaveLength(6);
      
      // 验证提供者数量
      expect(providersMetadata).toHaveLength(7);
      
      // 验证控制器数量
      expect(controllersMetadata).toHaveLength(6);
      
      // 验证导出数量
      expect(exportsMetadata).toHaveLength(7);
    });
  });

  describe('Service Providers', () => {
    it('should include ZulipService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipService);
    });

    it('should include SessionManagerService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(SessionManagerService);
    });

    it('should include MessageFilterService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(MessageFilterService);
    });

    it('should include ZulipEventProcessorService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipEventProcessorService);
    });

    it('should include SessionCleanupService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(SessionCleanupService);
    });

    it('should include CleanWebSocketGateway in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(CleanWebSocketGateway);
    });

    it('should include DynamicConfigManagerService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(DynamicConfigManagerService);
    });
  });

  describe('Controllers', () => {
    it('should include ChatController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(ChatController);
    });

    it('should include WebSocketDocsController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(WebSocketDocsController);
    });

    it('should include WebSocketOpenApiController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(WebSocketOpenApiController);
    });

    it('should include ZulipAccountsController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(ZulipAccountsController);
    });

    it('should include WebSocketTestController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(WebSocketTestController);
    });

    it('should include DynamicConfigController in controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      expect(controllers).toContain(DynamicConfigController);
    });
  });

  describe('Module Structure', () => {
    it('should have proper module architecture', () => {
      // 验证模块结构的合理性
      const moduleClass = ZulipModule;
      expect(moduleClass).toBeDefined();
      expect(typeof moduleClass).toBe('function');
    });

    it('should follow NestJS module conventions', () => {
      // 验证模块遵循NestJS约定
      const moduleMetadata = Reflect.getMetadata('imports', ZulipModule) ||
                           Reflect.getMetadata('providers', ZulipModule) ||
                           Reflect.getMetadata('controllers', ZulipModule) ||
                           Reflect.getMetadata('exports', ZulipModule);
      expect(moduleMetadata).toBeDefined();
    });
  });

  describe('Dependency Integration', () => {
    it('should integrate with core modules correctly', () => {
      // 验证与核心模块的集成
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      expect(imports.length).toBeGreaterThan(0);
    });

    it('should have proper service dependencies', () => {
      // 验证服务依赖关系
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipService);
      expect(providers).toContain(SessionManagerService);
      expect(providers).toContain(MessageFilterService);
    });

    it('should export essential services', () => {
      // 验证导出的服务
      const exports = Reflect.getMetadata('exports', ZulipModule) || [];
      expect(exports).toContain(ZulipService);
      expect(exports).toContain(SessionManagerService);
      expect(exports).toContain(MessageFilterService);
      expect(exports).toContain(ZulipEventProcessorService);
      expect(exports).toContain(SessionCleanupService);
      expect(exports).toContain(CleanWebSocketGateway);
      expect(exports).toContain(DynamicConfigManagerService);
    });
  });

  describe('Module Instantiation', () => {
    it('should create module instance without errors', () => {
      expect(() => new ZulipModule()).not.toThrow();
    });

    it('should be a valid NestJS module', () => {
      const instance = new ZulipModule();
      expect(instance).toBeInstanceOf(ZulipModule);
    });
  });

  describe('Configuration Validation', () => {
    it('should have all required imports', () => {
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      
      // 验证必需的模块导入
      expect(imports.length).toBe(6);
    });

    it('should have all required providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      
      // 验证所有必需的服务提供者
      const requiredProviders = [
        ZulipService,
        SessionManagerService,
        MessageFilterService,
        ZulipEventProcessorService,
        SessionCleanupService,
        CleanWebSocketGateway,
        DynamicConfigManagerService,
      ];
      
      requiredProviders.forEach(provider => {
        expect(providers).toContain(provider);
      });
    });

    it('should have all required controllers', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      
      // 验证所有必需的控制器
      const requiredControllers = [
        ChatController,
        WebSocketDocsController,
        WebSocketOpenApiController,
        ZulipAccountsController,
        WebSocketTestController,
        DynamicConfigController,
      ];
      
      requiredControllers.forEach(controller => {
        expect(controllers).toContain(controller);
      });
    });
  });

  describe('Module Metadata Validation', () => {
    it('should have correct imports configuration', () => {
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      
      // 验证导入模块的数量和类型
      expect(Array.isArray(imports)).toBe(true);
      expect(imports.length).toBe(6);
    });

    it('should have correct providers configuration', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      
      // 验证提供者的数量和类型
      expect(Array.isArray(providers)).toBe(true);
      expect(providers.length).toBe(7);
    });

    it('should have correct controllers configuration', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      
      // 验证控制器的数量和类型
      expect(Array.isArray(controllers)).toBe(true);
      expect(controllers.length).toBe(6);
    });

    it('should have correct exports configuration', () => {
      const exports = Reflect.getMetadata('exports', ZulipModule) || [];
      
      // 验证导出的数量和类型
      expect(Array.isArray(exports)).toBe(true);
      expect(exports.length).toBe(7);
    });
  });
});