import { exchangeDataSource } from '../exchangeDataSource';

// Mock axios
jest.mock('axios', () => ({
  get: jest.fn(() => Promise.resolve({
    data: {
      amount: 1.0,
      base: 'EUR',
      date: '2026-03-26',
      rates: {
        USD: 1.1539,
        CNY: 7.9737,
        JPY: 184.19
      }
    }
  }))
}));

describe('ExchangeDataSource', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  test('getLatestRates should return exchange rates', async () => {
    const rates = await exchangeDataSource.getLatestRates('EUR');
    expect(rates).toHaveProperty('base', 'EUR');
    expect(rates).toHaveProperty('rates');
    expect(typeof rates.rates).toBe('object');
  });

  test('convertCurrency should convert amount between currencies', async () => {
    const result = await exchangeDataSource.convertCurrency('EUR', 'USD', 100);
    expect(typeof result).toBe('number');
    expect(result).toBeGreaterThan(0);
  });

  test('batchConvertCurrency should convert to multiple currencies', async () => {
    const result = await exchangeDataSource.batchConvertCurrency('EUR', ['USD', 'CNY'], 100);
    expect(typeof result).toBe('object');
    expect(result).toHaveProperty('USD');
    expect(result).toHaveProperty('CNY');
  });

  test('getExchangeRate should return exchange rate', async () => {
    const rate = await exchangeDataSource.getExchangeRate('EUR', 'USD');
    expect(typeof rate).toBe('number');
    expect(rate).toBeGreaterThan(0);
  });

  test('getSupportedCurrencies should return list of currencies', async () => {
    const currencies = await exchangeDataSource.getSupportedCurrencies();
    expect(Array.isArray(currencies)).toBe(true);
    expect(currencies.length).toBeGreaterThan(0);
  });

  test('refreshRates should update rates cache', async () => {
    await exchangeDataSource.refreshRates();
    // 验证是否调用了getLatestRates
    const rate = await exchangeDataSource.getExchangeRate('EUR', 'USD');
    expect(typeof rate).toBe('number');
  });
});