/**
 * DataSourceFactory 单元测试
 * 测试 DataSource 工厂模式的核心功能
 */

import { DataSourceFactory, BaseDataSource, BaseMockDataSource } from '../src/services/dataSourceFactory';

// 测试接口
interface TestItem {
  id: string;
  name: string;
  value: number;
}

interface TestQueryParams {
  name?: string;
  value?: number;
}

// 测试 API DataSource
class TestApiDataSource extends BaseDataSource<TestItem, TestQueryParams> {
  constructor() {
    super('/api/test');
  }

  // 自定义方法
  async customMethod(): Promise<string> {
    return 'API custom method';
  }
}

// 测试 Mock DataSource
class TestMockDataSource extends BaseMockDataSource<TestItem, TestQueryParams> {
  readonly __MOCK_NAME__ = 'TestMockDataSource';
  protected mockData: TestItem[] = [
    { id: '1', name: 'Test 1', value: 100 },
    { id: '2', name: 'Test 2', value: 200 },
  ];

  // 自定义方法
  async customMethod(): Promise<string> {
    return 'Mock custom method';
  }
}

describe('DataSourceFactory', () => {
  describe('create', () => {
    it('should create DataSource instance', () => {
      const dataSource = DataSourceFactory.create(
        TestApiDataSource,
        TestMockDataSource
      );
      expect(dataSource).toBeDefined();
      expect(typeof dataSource.list).toBe('function');
      expect(typeof dataSource.detail).toBe('function');
      expect(typeof dataSource.create).toBe('function');
      expect(typeof dataSource.update).toBe('function');
      expect(typeof dataSource.delete).toBe('function');
    });
  });

  describe('createWithMethods', () => {
    it('should create DataSource instance with custom methods', () => {
      const dataSource = DataSourceFactory.createWithMethods<
        TestItem,
        TestQueryParams,
        {
          customMethod(): Promise<string>;
        }
      >({
        apiDataSource: TestApiDataSource,
        mockDataSource: TestMockDataSource,
      });

      expect(dataSource).toBeDefined();
      expect(typeof dataSource.list).toBe('function');
      expect(typeof dataSource.customMethod).toBe('function');
    });
  });

  describe('BaseDataSource', () => {
    let dataSource: TestApiDataSource;

    beforeEach(() => {
      dataSource = new TestApiDataSource();
    });

    describe('buildQueryParams', () => {
      it('should build query params from object', () => {
        const params = { name: 'test', value: 100, page: 1 };
        const query = dataSource['buildQueryParams'](params);
        expect(query).toBe('name=test&value=100&page=1');
      });

      it('should handle undefined params', () => {
        const query = dataSource['buildQueryParams'](undefined);
        expect(query).toBe('');
      });

      it('should handle null values', () => {
        const params = { name: 'test', value: null };
        const query = dataSource['buildQueryParams'](params);
        expect(query).toBe('name=test');
      });
    });
  });

  describe('BaseMockDataSource', () => {
    let dataSource: TestMockDataSource;

    beforeEach(() => {
      dataSource = new TestMockDataSource();
    });

    describe('list', () => {
      it('should return mock data', async () => {
        const items = await dataSource.list();
        expect(items).toHaveLength(2);
        expect(items[0].id).toBe('1');
        expect(items[1].id).toBe('2');
      });
    });

    describe('detail', () => {
      it('should return item by id', async () => {
        const item = await dataSource.detail('1');
        expect(item).toBeDefined();
        expect(item?.id).toBe('1');
        expect(item?.name).toBe('Test 1');
      });

      it('should return null for non-existent id', async () => {
        const item = await dataSource.detail('999');
        expect(item).toBeNull();
      });
    });

    describe('create', () => {
      it('should create new item', async () => {
        const newItem = await dataSource.create({ name: 'Test 3', value: 300 });
        expect(newItem).toBeDefined();
        expect(newItem.id).toBeDefined();
        expect(newItem.name).toBe('Test 3');
        expect(newItem.value).toBe(300);

        // Verify item was added to mock data
        const items = await dataSource.list();
        expect(items).toHaveLength(3);
      });
    });

    describe('update', () => {
      it('should update existing item', async () => {
        const updatedItem = await dataSource.update('1', { name: 'Updated Test 1', value: 150 });
        expect(updatedItem).toBeDefined();
        expect(updatedItem.id).toBe('1');
        expect(updatedItem.name).toBe('Updated Test 1');
        expect(updatedItem.value).toBe(150);
      });

      it('should throw error for non-existent id', async () => {
        await expect(dataSource.update('999', { name: 'Test' })).rejects.toThrow('Item not found');
      });
    });

    describe('delete', () => {
      it('should delete item', async () => {
        await dataSource.delete('1');
        const items = await dataSource.list();
        expect(items).toHaveLength(1);
        expect(items[0].id).toBe('2');
      });
    });

    describe('delay', () => {
      it('should delay execution', async () => {
        const startTime = Date.now();
        await dataSource['delay'](100);
        const endTime = Date.now();
        expect(endTime - startTime).toBeGreaterThanOrEqual(100);
      });
    });
  });
});
