From a51bd9f6e4344cf3689ed914a794f5966c364597 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Sat, 12 Nov 2022 12:50:54 +0000 Subject: [PATCH] add public usdt contract ws test --- test/contract/ws.public.usdt.test.ts | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 test/contract/ws.public.usdt.test.ts diff --git a/test/contract/ws.public.usdt.test.ts b/test/contract/ws.public.usdt.test.ts new file mode 100644 index 0000000..aacc40a --- /dev/null +++ b/test/contract/ws.public.usdt.test.ts @@ -0,0 +1,74 @@ +import { + WebsocketClient, + WSClientConfigurableOptions, + WS_KEY_MAP, +} from '../../src'; +import { + logAllEvents, + getSilentLogger, + waitForSocketEvent, + WS_OPEN_EVENT_PARTIAL, +} from '../ws.util'; + +describe('Public Contract USDT Websocket Client', () => { + let wsClient: WebsocketClient; + + const wsClientOptions: WSClientConfigurableOptions = { + market: 'contractUSDT', + }; + + beforeAll(() => { + wsClient = new WebsocketClient( + wsClientOptions, + getSilentLogger('expectSuccessNoAuth') + ); + wsClient.connectPublic(); + }); + + afterAll(() => { + wsClient.closeAll(true); + }); + + it('should open a public ws connection', async () => { + const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); + try { + expect(await wsOpenPromise).toMatchObject({ + event: WS_OPEN_EVENT_PARTIAL, + wsKey: WS_KEY_MAP.contractUSDTPublic, + }); + } catch (e) { + console.error('open: ', e); + expect(e).toBeFalsy(); + } + }); + + it('should subscribe to public orderbook events', async () => { + const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); + const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); + + const wsTopic = 'orderbook.25.BTCUSDT'; + wsClient.subscribe(wsTopic); + + try { + expect(await wsResponsePromise).toMatchObject({ + success: true, + op: 'subscribe', + }); + } catch (e) { + console.error('response: ', e); + // sub failed (or jest expect threw) + expect(e).toBeFalsy(); + } + + try { + expect(await wsUpdatePromise).toMatchObject({ + topic: wsTopic, + data: { + a: expect.any(Array), + }, + }); + } catch (e) { + console.error(`Wait for "${wsTopic}" event exception: `, e); + } + }); +});