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

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

describe('IntegrationTestDatabase', () => {
  beforeEach(async () => {
    // 确保数据库已清理
    await IntegrationTestDatabase.cleanup();
  });

  afterEach(async () => {
    // 清理测试数据库
    await IntegrationTestDatabase.cleanup();
  });

  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);
    });
  });

  describe('createTestData', () => {
    it('应该创建测试数据', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();

      // 注意：这里只是测试方法调用，实际数据库操作需要真实的实体
      // 在真实环境中，应该使用具体的实体类
      const testData = { name: 'test' };

      // 验证方法存在且可调用
      expect(IntegrationTestDatabase.createTestData).toBeDefined();
      expect(typeof IntegrationTestDatabase.createTestData).toBe('function');
    });
  });

  describe('findTestData', () => {
    it('应该查找测试数据', async () => {
      // 验证方法存在且可调用
      expect(IntegrationTestDatabase.findTestData).toBeDefined();
      expect(typeof IntegrationTestDatabase.findTestData).toBe('function');
    });
  });

  describe('deleteTestData', () => {
    it('应该删除测试数据', async () => {
      // 验证方法存在且可调用
      expect(IntegrationTestDatabase.deleteTestData).toBeDefined();
      expect(typeof IntegrationTestDatabase.deleteTestData).toBe('function');
    });
  });

  describe('cleanupTable', () => {
    it('应该清理表数据', async () => {
      // 验证方法存在且可调用
      expect(IntegrationTestDatabase.cleanupTable).toBeDefined();
      expect(typeof IntegrationTestDatabase.cleanupTable).toBe('function');
    });
  });
});

describe('setupIntegrationDatabaseHooks', () => {
  it('应该定义生命周期钩子函数', () => {
    const hooks = require('../../src/integration-test-db');

    expect(hooks.setupIntegrationDatabaseHooks).toBeDefined();
    expect(typeof hooks.setupIntegrationDatabaseHooks).toBe('function');

    expect(hooks.setupIntegrationDatabaseHooksWithEntities).toBeDefined();
    expect(typeof hooks.setupIntegrationDatabaseHooksWithEntities).toBe('function');
  });
});