/**
 * 登录核心模块测试套件
 * 
 * 功能描述：
 * - 测试LoginCoreModule的模块配置和依赖注入
 * - 验证服务提供者的正确注册和实例化
 * - 确保JWT配置的正确加载和访问
 * - 测试模块导出和依赖关系
 * 
 * 测试覆盖范围：
 * - 模块定义：模块实例化和配置验证
 * - 服务提供者：LoginCoreService和ConfigService的注入
 * - JWT配置：JWT密钥和过期时间的配置访问
 * - 模块依赖：依赖模块的正确导入
 * - 模块导出：服务的正确导出和可用性
 * 
 * 测试策略：
 * - 单元测试：独立测试模块配置
 * - Mock测试：模拟所有外部依赖服务
 * - 配置测试：验证配置项的正确读取
 * 
 * 依赖模块：
 * - Jest: 测试框架和Mock功能
 * - NestJS Testing: 提供测试模块和依赖注入
 * 
 * 最近修改：
 * - 2026-01-15: 代码规范优化 - 清理未使用的导入(UsersService) (修改者: moyin)
 * - 2026-01-15: 代码规范优化 - 添加文件头注释 (修改者: moyin)
 * 
 * @author moyin
 * @version 1.0.2
 * @since 2025-12-17
 * @lastModified 2026-01-15
 */

import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { LoginCoreService } from './login_core.service';
import { EmailService } from '../utils/email/email.service';
import { VerificationService } from '../utils/verification/verification.service';

describe('LoginCoreModule', () => {
  let module: TestingModule;
  let loginCoreService: LoginCoreService;
  let configService: ConfigService;

  beforeEach(async () => {
    const mockConfigService = {
      get: jest.fn((key: string, defaultValue?: any) => {
        switch (key) {
          case 'JWT_SECRET':
            return 'test-jwt-secret-key';
          case 'JWT_EXPIRES_IN':
            return defaultValue || '7d';
          default:
            return defaultValue;
        }
      }),
    };

    const mockUsersService = {
      findByUsername: jest.fn(),
      findByEmail: jest.fn(),
      findAll: jest.fn(),
      findOne: jest.fn(),
      create: jest.fn(),
      update: jest.fn(),
      remove: jest.fn(),
      findByGithubId: jest.fn(),
    };

    const mockEmailService = {
      sendVerificationCode: jest.fn(),
      sendWelcomeEmail: jest.fn(),
    };

    const mockVerificationService = {
      generateCode: jest.fn(),
      verifyCode: jest.fn(),
      clearCooldown: jest.fn(),
      debugCodeInfo: jest.fn(),
    };

    const mockJwtService = {
      signAsync: jest.fn(),
      verifyAsync: jest.fn(),
    };

    module = await Test.createTestingModule({
      providers: [
        LoginCoreService,
        {
          provide: ConfigService,
          useValue: mockConfigService,
        },
        {
          provide: 'UsersService',
          useValue: mockUsersService,
        },
        {
          provide: EmailService,
          useValue: mockEmailService,
        },
        {
          provide: VerificationService,
          useValue: mockVerificationService,
        },
        {
          provide: JwtService,
          useValue: mockJwtService,
        },
      ],
    }).compile();

    loginCoreService = module.get<LoginCoreService>(LoginCoreService);
    configService = module.get<ConfigService>(ConfigService);
  });

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

  it('should be defined', () => {
    expect(module).toBeDefined();
  });

  describe('Service Providers', () => {
    it('should provide LoginCoreService', () => {
      expect(loginCoreService).toBeDefined();
      expect(loginCoreService).toBeInstanceOf(LoginCoreService);
    });

    it('should provide ConfigService', () => {
      expect(configService).toBeDefined();
      // ConfigService is mocked, so we check if it has the expected methods
      expect(configService.get).toBeDefined();
      expect(typeof configService.get).toBe('function');
    });
  });

  describe('JWT Configuration', () => {
    it('should have access to JWT configuration', () => {
      // Test that the mock ConfigService can provide JWT configuration
      const jwtSecret = configService.get('JWT_SECRET');
      const jwtExpiresIn = configService.get('JWT_EXPIRES_IN', '7d');
      
      expect(jwtSecret).toBe('test-jwt-secret-key');
      expect(jwtExpiresIn).toBe('7d');
    });
  });

  describe('Module Dependencies', () => {
    it('should import required modules', () => {
      expect(module).toBeDefined();
      expect(loginCoreService).toBeDefined();
    });

    it('should not have circular dependencies', () => {
      expect(module).toBeDefined();
    });
  });

  describe('Module Exports', () => {
    it('should export LoginCoreService', () => {
      expect(loginCoreService).toBeDefined();
      expect(loginCoreService).toBeInstanceOf(LoginCoreService);
    });

    it('should make LoginCoreService available for injection', () => {
      const service = module.get<LoginCoreService>(LoginCoreService);
      expect(service).toBe(loginCoreService);
    });
  });

  describe('Configuration Validation', () => {
    it('should validate JWT configuration completeness', () => {
      // Test that all required configuration keys are accessible
      expect(configService.get('JWT_SECRET')).toBeDefined();
      expect(configService.get('JWT_EXPIRES_IN', '7d')).toBeDefined();
    });
  });
});