|
| 1 | +// Mock the problematic imports that aren't installed in the test env |
| 2 | +jest.mock('@nestjs/websockets', () => ({})); |
| 3 | +jest.mock('socket.io', () => ({ Server: jest.fn() })); |
| 4 | +jest.mock('uuid', () => ({ v4: jest.fn(() => 'test-uuid') })); |
| 5 | + |
| 6 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 7 | +import { OrderBookUpdateListener } from './order-book-update.listener'; |
| 8 | +import { WebSocketService } from '../../websocket/services/websocket.service'; |
| 9 | +import type { OrderBookResponse } from '../services/order-book.service'; |
| 10 | + |
| 11 | +const mockWebSocketService = { |
| 12 | + broadcastOrderBookUpdate: jest.fn(), |
| 13 | + broadcastOrderUpdate: jest.fn(), |
| 14 | + broadcastTradeExecution: jest.fn(), |
| 15 | +}; |
| 16 | + |
| 17 | +describe('OrderBookUpdateListener', () => { |
| 18 | + let listener: OrderBookUpdateListener; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + |
| 23 | + const module: TestingModule = await Test.createTestingModule({ |
| 24 | + providers: [ |
| 25 | + OrderBookUpdateListener, |
| 26 | + { provide: WebSocketService, useValue: mockWebSocketService }, |
| 27 | + ], |
| 28 | + }).compile(); |
| 29 | + |
| 30 | + listener = module.get(OrderBookUpdateListener); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('handleOrderBookUpdate', () => { |
| 34 | + it('should broadcast order book update with correct shape', () => { |
| 35 | + const response: OrderBookResponse = { |
| 36 | + assetId: 1, |
| 37 | + pair: 'BTC', |
| 38 | + topOfBook: { |
| 39 | + bestBid: { price: 99, amount: 5, count: 1 }, |
| 40 | + bestAsk: { price: 101, amount: 3, count: 1 }, |
| 41 | + spread: 2, |
| 42 | + midPrice: 100, |
| 43 | + }, |
| 44 | + bids: [ |
| 45 | + { price: 99, amount: 5, count: 1 }, |
| 46 | + { price: 98, amount: 10, count: 2 }, |
| 47 | + ], |
| 48 | + asks: [ |
| 49 | + { price: 101, amount: 3, count: 1 }, |
| 50 | + { price: 102, amount: 8, count: 3 }, |
| 51 | + ], |
| 52 | + timestamp: '2026-08-20T12:00:00.000Z', |
| 53 | + sequence: 42, |
| 54 | + }; |
| 55 | + |
| 56 | + listener.handleOrderBookUpdate(response); |
| 57 | + |
| 58 | + expect(mockWebSocketService.broadcastOrderBookUpdate).toHaveBeenCalledWith({ |
| 59 | + asset: '1', |
| 60 | + bids: [ |
| 61 | + { price: 99, amount: 5, count: 1 }, |
| 62 | + { price: 98, amount: 10, count: 2 }, |
| 63 | + ], |
| 64 | + asks: [ |
| 65 | + { price: 101, amount: 3, count: 1 }, |
| 66 | + { price: 102, amount: 8, count: 3 }, |
| 67 | + ], |
| 68 | + timestamp: '2026-08-20T12:00:00.000Z', |
| 69 | + sequence: 42, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle empty order book', () => { |
| 74 | + const response: OrderBookResponse = { |
| 75 | + assetId: 2, |
| 76 | + pair: 'ETH', |
| 77 | + topOfBook: { |
| 78 | + bestBid: null, |
| 79 | + bestAsk: null, |
| 80 | + spread: null, |
| 81 | + midPrice: null, |
| 82 | + }, |
| 83 | + bids: [], |
| 84 | + asks: [], |
| 85 | + timestamp: '2026-08-20T12:00:00.000Z', |
| 86 | + sequence: 1, |
| 87 | + }; |
| 88 | + |
| 89 | + listener.handleOrderBookUpdate(response); |
| 90 | + |
| 91 | + expect(mockWebSocketService.broadcastOrderBookUpdate).toHaveBeenCalledWith({ |
| 92 | + asset: '2', |
| 93 | + bids: [], |
| 94 | + asks: [], |
| 95 | + timestamp: '2026-08-20T12:00:00.000Z', |
| 96 | + sequence: 1, |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments