import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { createDataSource, initializeDataSource } from '../../src/data-source';

// 模拟实体类
class MockEntity {}

describe('data-source', () => {
  const originalEnv = process.env;

  beforeEach(() => {
    // 重置环境变量
    process.env = { ...originalEnv };
  });

  afterEach(() => {
    process.env = originalEnv;
  });

  describe('createDataSource', () => {
    it('应该创建生产环境数据源', () => {
      process.env.NODE_ENV = 'production';

      const dataSource = createDataSource([MockEntity]);

      expect(dataSource).toBeDefined();
      expect(dataSource.options.type).toBe('postgres');
      expect(dataSource.options.entities).toEqual([MockEntity]);
      expect(dataSource.options.synchronize).toBe(true); // 默认值
    });

    it('应该创建测试环境数据源', () => {
      process.env.NODE_ENV = 'test';
      process.env.TEST_DATABASE_URL = 'postgresql://test:test@localhost:5432/test';

      const dataSource = createDataSource([MockEntity]);

      expect(dataSource).toBeDefined();
      expect(dataSource.options.type).toBe('postgres');
      expect(dataSource.options.url).toBe('postgresql://test:test@localhost:5432/test');
      expect(dataSource.options.synchronize).toBe(true);
      expect(dataSource.options.dropSchema).toBe(true);
      expect(dataSource.options.logging).toBe(false);
    });

    it('应该使用环境变量配置', () => {
      process.env.NODE_ENV = 'production';
      // 确保没有设置 TEST_DATABASE_URL，这样会使用生产环境配置
      delete process.env.TEST_DATABASE_URL;
      process.env.DB_HOST = 'custom-host';
      process.env.DB_PORT = '5433';
      process.env.DB_USERNAME = 'custom-user';
      process.env.DB_PASSWORD = 'custom-password';
      process.env.DB_DATABASE = 'custom-db';
      process.env.DB_SYNCHRONIZE = 'false';
      process.env.DB_LOGGING = 'true';

      const dataSource = createDataSource([MockEntity]);

      expect(dataSource.options).toMatchObject({
        host: 'custom-host',
        port: 5433,
        username: 'custom-user',
        password: 'custom-password',
        database: 'custom-db',
        synchronize: false,
        logging: true
      });
    });

    it('应该使用默认值当环境变量未设置时', () => {
      process.env.NODE_ENV = 'production';
      // 确保没有设置 TEST_DATABASE_URL，这样会使用生产环境配置
      delete process.env.TEST_DATABASE_URL;
      delete process.env.DB_HOST;
      delete process.env.DB_PORT;
      delete process.env.DB_USERNAME;
      delete process.env.DB_PASSWORD;
      delete process.env.DB_DATABASE;
      delete process.env.DB_SYNCHRONIZE;
      delete process.env.DB_LOGGING;

      const dataSource = createDataSource([MockEntity]);

      expect(dataSource.options).toMatchObject({
        host: 'localhost',
        port: 5432,
        username: 'postgres',
        password: '',
        database: 'postgres',
        synchronize: true,
        logging: false
      });
    });
  });

  describe('initializeDataSource', () => {
    it('应该初始化默认数据源', () => {
      process.env.NODE_ENV = 'production';

      initializeDataSource([MockEntity]);

      // 导入 AppDataSource 来验证它被正确设置
      import('../../src/data-source').then(({ AppDataSource }) => {
        expect(AppDataSource).toBeDefined();
        expect(AppDataSource.options.entities).toEqual([MockEntity]);
      });
    });
  });
});