/**
 * ExchangeRateService 单元测试
 * 测试汇率服务的核心功能
 */

import ExchangeRateService from '../src/services/ExchangeRateService';

describe('ExchangeRateService', () => {
  let exchangeRateService: ExchangeRateService;

  beforeEach(() => {
    // 获取单例实例
    exchangeRateService = ExchangeRateService.getInstance();
  });

  describe('getRate', () => {
    it('should return correct exchange rate for USD to CNY', async () => {
      const rate = await exchangeRateService.getRate('USD', 'CNY');
      expect(rate).toBeGreaterThan(0);
    });

    it('should return 1 for same currency', async () => {
      const rate = await exchangeRateService.getRate('USD', 'USD');
      expect(rate).toBe(1);
    });

    it('should handle case insensitive currency codes', async () => {
      const rate1 = await exchangeRateService.getRate('usd', 'cny');
      const rate2 = await exchangeRateService.getRate('USD', 'CNY');
      expect(rate1).toBe(rate2);
    });
  });

  describe('convert', () => {
    it('should convert amount correctly', async () => {
      const amount = 100;
      const converted = await exchangeRateService.convert(amount, 'USD', 'CNY');
      expect(converted).toBeGreaterThan(amount);
    });

    it('should return same amount for same currency', async () => {
      const amount = 100;
      const converted = await exchangeRateService.convert(amount, 'USD', 'USD');
      expect(converted).toBe(amount);
    });

    it('should handle zero amount', async () => {
      const amount = 0;
      const converted = await exchangeRateService.convert(amount, 'USD', 'CNY');
      expect(converted).toBe(0);
    });
  });

  describe('getRates', () => {
    it('should return multiple exchange rates', async () => {
      const rates = await exchangeRateService.getRates('USD', ['CNY', 'EUR', 'GBP']);
      expect(rates).toHaveProperty('CNY');
      expect(rates).toHaveProperty('EUR');
      expect(rates).toHaveProperty('GBP');
      expect(rates.CNY).toBeGreaterThan(0);
      expect(rates.EUR).toBeGreaterThan(0);
      expect(rates.GBP).toBeGreaterThan(0);
    });

    it('should return empty object for empty target currencies', async () => {
      const rates = await exchangeRateService.getRates('USD', []);
      expect(rates).toEqual({});
    });
  });

  describe('getBaseCurrency', () => {
    it('should return base currency', () => {
      const baseCurrency = exchangeRateService.getBaseCurrency();
      expect(baseCurrency).toBe('USD');
    });
  });

  describe('getLastUpdate', () => {
    it('should return last update time', () => {
      const lastUpdate = exchangeRateService.getLastUpdate();
      expect(lastUpdate).toBeInstanceOf(Date);
    });
  });
});
