import { describe, it, expect } from 'vitest';
import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '../../src/integration-test-db';

// 模拟实体用于测试
class MockEntity {
  id?: number;
  name?: string;
}

// 设置集成测试钩子 - 这是关键步骤！
setupIntegrationDatabaseHooksWithEntities([MockEntity])

describe('IntegrationTestDatabase', () => {
  describe('getDataSource', () => {
    it('应该返回数据源实例', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();

      expect(dataSource).toBeDefined();
      expect(dataSource.isInitialized).toBe(true);
    });

    it('应该重用已初始化的数据源', async () => {
      const dataSource1 = await IntegrationTestDatabase.getDataSource();
      const dataSource2 = await IntegrationTestDatabase.getDataSource();

      expect(dataSource1).toBe(dataSource2);
    });
  });

  describe('cleanup', () => {
    it('应该清理数据源', async () => {
      // 先初始化数据源
      await IntegrationTestDatabase.getDataSource();

      // 然后清理
      await IntegrationTestDatabase.cleanup();

      // 验证数据源已销毁
      const dataSource = await IntegrationTestDatabase.getDataSource();
      expect(dataSource.isInitialized).toBe(true); // 重新初始化了
    });

    it('应该在数据源未初始化时安全清理', async () => {
      // 确保数据源未初始化
      await IntegrationTestDatabase.cleanup();

      // 清理未初始化的数据源应该不会抛出错误
      await expect(IntegrationTestDatabase.cleanup()).resolves.not.toThrow();
    });
  });

  describe('initializeWithEntities', () => {
    it('应该使用特定实体初始化数据源', async () => {
      const entities = [MockEntity];
      const dataSource = await IntegrationTestDatabase.initializeWithEntities(entities);

      expect(dataSource).toBeDefined();
      expect(dataSource.isInitialized).toBe(true);
    });
  });

  
});