From a35c55563025e0d90a8f77778c42e252116b9763 Mon Sep 17 00:00:00 2001 From: "ilinikh.a1" Date: Sun, 29 Oct 2023 06:57:51 +0600 Subject: [PATCH] feat: add test --- .../bingx-account-socket-stream.spec.ts | 110 +++++++++++------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/src/bingx-socket/bingx-account-socket-stream.spec.ts b/src/bingx-socket/bingx-account-socket-stream.spec.ts index 207957f..895f6af 100644 --- a/src/bingx-socket/bingx-account-socket-stream.spec.ts +++ b/src/bingx-socket/bingx-account-socket-stream.spec.ts @@ -4,33 +4,46 @@ import { ApiAccount } from 'bingx-api/bingx/account/api-account'; import { BingxAccountSocketStream } from 'bingx-api/bingx-socket/bingx-account-socket-stream'; import * as zlib from 'zlib'; import { skip } from 'rxjs'; +import { + AccountOrderUpdatePushEvent, + AccountWebsocketEventType, +} from 'bingx-api/bingx-socket/events'; describe('bingx account socket stream', () => { let wss: Server; let port: number; const sockets: WebSocket[] = []; - - beforeAll(async () => { - port = await getPortFree(); - wss = new Server({ - port, + const sendToSocket = (socket: WebSocket, msg: string) => { + zlib.gzip(msg, (err, result) => { + socket.send(result); }); - wss.on('connection', (ws) => { - sockets[0] = ws; + }; - ws.on('close', () => { - sockets.splice(0, 1); + beforeAll(async () => { + return new Promise(async (resolve) => { + port = await getPortFree(); + wss = new Server({ + port, }); + wss.on('listening', resolve); + wss.on('connection', (ws) => { + sockets[0] = ws; - setInterval(() => { - zlib.gzip('Ping', (err, result) => { - ws.send(result); + ws.on('close', () => { + sockets.splice(0, 1); }); - }, 200); + + setInterval(() => { + zlib.gzip('Ping', (err, result) => { + ws.send(result); + }); + }, 200); + }); }); }); - afterAll(() => { + afterAll((done) => { + wss.on('close', done); wss.close(); }); @@ -40,30 +53,6 @@ describe('bingx account socket stream', () => { }); }); - describe('initialize connect', () => { - const requestExecutorMock = { - execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }), - }; - let stream: BingxAccountSocketStream; - beforeAll(() => { - const account = new ApiAccount('xxx', 'xxx'); - stream = new BingxAccountSocketStream(account, { - requestExecutor: requestExecutorMock, - url: new URL('', `ws://0.0.0.0:${port}`), - }); - }); - - it('must be got listen key', () => { - // expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1); - }); - - it('must be got heartbeat', (done) => { - stream.heartbeat$.subscribe(() => { - done(); - }); - }); - }); - describe('initialize connect', () => { const requestExecutorMock = { execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }), @@ -110,7 +99,7 @@ describe('bingx account socket stream', () => { }); it('must be got listen key', () => { - // expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1); + expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1); }); it('must be got disconnected', (done) => { @@ -163,4 +152,47 @@ describe('bingx account socket stream', () => { }); }); }); + + describe('order trade update', () => { + const requestExecutorMock = { + execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }), + }; + let stream: BingxAccountSocketStream; + beforeAll(() => { + const account = new ApiAccount('xxx', 'xxx'); + stream = new BingxAccountSocketStream(account, { + requestExecutor: requestExecutorMock, + url: new URL('', `ws://0.0.0.0:${port}`), + }); + }); + + afterAll(() => { + stream.disconnect(); + }); + + it('must be got listen key', () => { + expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1); + }); + + it('must be got order push', (done) => { + stream.accountOrderUpdatePushEvent$.subscribe((e) => { + expect(e).toStrictEqual({ + e: AccountWebsocketEventType.ORDER_TRADE_UPDATE, + o: {}, + E: 111, + } as AccountOrderUpdatePushEvent); + done(); + }); + setTimeout(() => { + sendToSocket( + sockets[0], + JSON.stringify({ + e: AccountWebsocketEventType.ORDER_TRADE_UPDATE, + o: {}, + E: 111, + } as AccountOrderUpdatePushEvent), + ); + }, 1000); + }); + }); });