|
| 1 | +import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test'; |
| 2 | +import { ExchangeDecorator } from '../../src/decorators/exchange'; |
| 3 | +import { FetchApiError } from '../../src/errors'; |
| 4 | + |
| 5 | +describe('decorators > exchange', () => { |
| 6 | + const mockTickerPrice = { symbol: 'ICPUSDT', price: '2.23800000' }; |
| 7 | + |
| 8 | + let exchange: ExchangeDecorator; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + exchange = new ExchangeDecorator(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + mock.clearAllMocks(); |
| 16 | + mock.restore(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('fetchTickerPrice', () => { |
| 20 | + it('should fetch and return ticker price', async () => { |
| 21 | + spyOn(global, 'fetch').mockResolvedValueOnce(Response.json(mockTickerPrice)); |
| 22 | + |
| 23 | + const result = await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 24 | + |
| 25 | + expect(result.symbol).toBe('ICPUSDT'); |
| 26 | + expect(result.price).toBe('2.23800000'); |
| 27 | + expect(result.fetchedAt).toBeString(); |
| 28 | + expect(new Date(result.fetchedAt).toISOString()).toBe(result.fetchedAt); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should call Binance API with correct URL', async () => { |
| 32 | + spyOn(global, 'fetch').mockResolvedValueOnce(Response.json(mockTickerPrice)); |
| 33 | + |
| 34 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 35 | + |
| 36 | + expect(global.fetch).toHaveBeenCalledWith( |
| 37 | + 'https://data-api.binance.vision/api/v3/ticker/price?symbol=ICPUSDT' |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return cached value within TTL', async () => { |
| 42 | + const fetchSpy = spyOn(global, 'fetch').mockResolvedValueOnce(Response.json(mockTickerPrice)); |
| 43 | + |
| 44 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 45 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 46 | + |
| 47 | + expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should refetch after TTL expires', async () => { |
| 51 | + const fetchSpy = spyOn(global, 'fetch') |
| 52 | + .mockResolvedValueOnce(Response.json(mockTickerPrice)) |
| 53 | + .mockResolvedValueOnce(Response.json(mockTickerPrice)); |
| 54 | + |
| 55 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 56 | + |
| 57 | + spyOn(Date, 'now').mockReturnValue(Date.now() + 61_000); |
| 58 | + |
| 59 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 60 | + |
| 61 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should cache different symbols independently', async () => { |
| 65 | + const fetchSpy = spyOn(global, 'fetch') |
| 66 | + .mockResolvedValueOnce(Response.json({ symbol: 'ICPUSDT', price: '2.23800000' })) |
| 67 | + .mockResolvedValueOnce(Response.json({ symbol: 'BTCUSDT', price: '50000.00' })); |
| 68 | + |
| 69 | + await exchange.fetchPrice({ symbol: 'ICPUSDT' }); |
| 70 | + await exchange.fetchPrice({ symbol: 'BTCUSDT' }); |
| 71 | + |
| 72 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw on Binance API error', async () => { |
| 76 | + spyOn(global, 'fetch').mockResolvedValueOnce(new Response('{}', { status: 500 })); |
| 77 | + |
| 78 | + expect(exchange.fetchPrice({ symbol: 'ICPUSDT' })).rejects.toThrow(FetchApiError); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should throw on invalid response schema', async () => { |
| 82 | + spyOn(global, 'fetch').mockResolvedValueOnce(Response.json({ unexpected: 'data' })); |
| 83 | + |
| 84 | + expect(exchange.fetchPrice({ symbol: 'ICPUSDT' })).rejects.toThrow(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments