/**
 * 邮件模块测试套件
 * 
 * 功能描述：
 * - 测试EmailModule的模块配置和依赖注入
 * - 验证模块导入、提供者和导出的正确性
 * - 确保邮件服务的正确配置
 * - 测试模块间的依赖关系
 * 
 * 测试覆盖范围：
 * - 模块实例化：模块能够正确创建和初始化
 * - 依赖注入：所有服务的正确注入
 * - 服务导出：EmailService的正确导出
 * - 配置验证：邮件配置的正确性
 * 
 * 最近修改：
 * - 2026-01-12: 功能新增 - 创建EmailModule测试文件，确保模块配置测试覆盖 (修改者: moyin)
 * 
 * @author moyin
 * @version 1.0.0
 * @since 2026-01-12
 * @lastModified 2026-01-12
 */

import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { EmailModule } from './email.module';
import { EmailService } from './email.service';

describe('EmailModule', () => {
  let module: TestingModule;
  let emailService: EmailService;
  let configService: ConfigService;

  beforeEach(async () => {
    const mockConfigService = {
      get: jest.fn((key: string, defaultValue?: any) => {
        switch (key) {
          case 'EMAIL_HOST':
            return 'smtp.test.com';
          case 'EMAIL_PORT':
            return 587;
          case 'EMAIL_USER':
            return 'test@test.com';
          case 'EMAIL_PASS':
            return 'test-password';
          default:
            return defaultValue;
        }
      }),
    };

    module = await Test.createTestingModule({
      providers: [
        EmailService,
        {
          provide: ConfigService,
          useValue: mockConfigService,
        },
      ],
    }).compile();

    emailService = module.get<EmailService>(EmailService);
    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 EmailService', () => {
      expect(emailService).toBeDefined();
      expect(emailService).toBeInstanceOf(EmailService);
    });

    it('should provide ConfigService', () => {
      expect(configService).toBeDefined();
      expect(configService.get).toBeDefined();
    });
  });

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

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

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

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

  describe('Configuration Validation', () => {
    it('should validate email configuration completeness', () => {
      expect(configService.get('EMAIL_HOST')).toBeDefined();
      expect(configService.get('EMAIL_PORT')).toBeDefined();
      expect(configService.get('EMAIL_USER')).toBeDefined();
      expect(configService.get('EMAIL_PASS')).toBeDefined();
    });
  });
});