/**
 * 用户模块测试套件
 * 
 * 功能描述：
 * - 测试UsersModule的模块配置和依赖注入
 * - 验证模块导入、提供者和导出的正确性
 * - 确保用户服务的正确配置
 * - 测试模块间的依赖关系
 * 
 * 测试覆盖范围：
 * - 模块实例化：模块能够正确创建和初始化
 * - 依赖注入：所有服务的正确注入
 * - 服务导出：UsersService的正确导出
 * - 双模式配置：内存模式和数据库模式的正确配置
 * 
 * 最近修改：
 * - 2026-01-12: 功能新增 - 创建UsersModule测试文件，确保模块配置测试覆盖 (修改者: 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 { UsersModule } from './users.module';
import { UsersService } from './users.service';
import { UsersMemoryService } from './users_memory.service';

describe('UsersModule', () => {
  let module: TestingModule;
  let usersService: any;

  beforeEach(async () => {
    const mockConfigService = {
      get: jest.fn((key: string, defaultValue?: any) => {
        switch (key) {
          case 'DATABASE_MODE':
            return 'memory';
          default:
            return defaultValue;
        }
      }),
    };

    module = await Test.createTestingModule({
      imports: [UsersModule.forMemory()],
    })
      .overrideProvider(ConfigService)
      .useValue(mockConfigService)
      .compile();

    usersService = module.get<UsersService>('UsersService');
  });

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

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

  describe('Service Providers', () => {
    it('should provide UsersService', () => {
      expect(usersService).toBeDefined();
      expect(usersService).toBeInstanceOf(UsersMemoryService);
    });
  });

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

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

  describe('Module Exports', () => {
    it('should export UsersService', () => {
      expect(usersService).toBeDefined();
      expect(usersService).toBeInstanceOf(UsersMemoryService);
    });

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

  describe('Dynamic Module Configuration', () => {
    it('should create memory module correctly', () => {
      const memoryModule = UsersModule.forMemory();
      expect(memoryModule).toBeDefined();
      expect(memoryModule.module).toBe(UsersModule);
      expect(memoryModule.providers).toBeDefined();
      expect(memoryModule.exports).toBeDefined();
    });

    it('should create database module correctly', () => {
      const databaseModule = UsersModule.forDatabase();
      expect(databaseModule).toBeDefined();
      expect(databaseModule.module).toBe(UsersModule);
      expect(databaseModule.providers).toBeDefined();
      expect(databaseModule.exports).toBeDefined();
    });
  });
});