usdc options public ws test
This commit is contained in:
79
test/usdc/options/ws.public.test.ts
Normal file
79
test/usdc/options/ws.public.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
WebsocketClient,
|
||||
WSClientConfigurableOptions,
|
||||
WS_KEY_MAP,
|
||||
} from '../../../src';
|
||||
import {
|
||||
logAllEvents,
|
||||
getSilentLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../../ws.util';
|
||||
|
||||
describe('Public USDC Option Websocket Client', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
const wsClientOptions: WSClientConfigurableOptions = {
|
||||
market: 'usdcOption',
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(
|
||||
wsClientOptions,
|
||||
getSilentLogger('expectSuccessNoAuth')
|
||||
);
|
||||
// logAllEvents(wsClient);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
wsClient.removeAllListeners();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
wsClient.closeAll();
|
||||
});
|
||||
|
||||
it('should open a public ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.usdcOptionPublic,
|
||||
});
|
||||
|
||||
await Promise.all([wsOpenPromise]);
|
||||
});
|
||||
|
||||
it('should subscribe to public trade events', async () => {
|
||||
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
|
||||
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
|
||||
|
||||
wsClient.subscribe([
|
||||
'recenttrades.BTC',
|
||||
'recenttrades.ETH',
|
||||
'recenttrades.SOL',
|
||||
]);
|
||||
|
||||
try {
|
||||
expect(await wsResponsePromise).toMatchObject({
|
||||
success: true,
|
||||
data: {
|
||||
failTopics: [],
|
||||
successTopics: expect.any(Array),
|
||||
},
|
||||
type: 'COMMAND_RESP',
|
||||
});
|
||||
} catch (e) {
|
||||
// sub failed
|
||||
expect(e).toBeFalsy();
|
||||
}
|
||||
|
||||
// Takes a while to get an event from USDC options - testing this manually for now
|
||||
// try {
|
||||
// expect(await wsUpdatePromise).toStrictEqual('asdfasdf');
|
||||
// } catch (e) {
|
||||
// // no data
|
||||
// expect(e).toBeFalsy();
|
||||
// }
|
||||
});
|
||||
});
|
||||
@@ -73,6 +73,36 @@ export function waitForSocketEvent(
|
||||
});
|
||||
}
|
||||
|
||||
export function listenToSocketEvents(wsClient: WebsocketClient) {
|
||||
const retVal: Record<
|
||||
'update' | 'open' | 'response' | 'close' | 'error',
|
||||
typeof jest.fn
|
||||
> = {
|
||||
open: jest.fn(),
|
||||
response: jest.fn(),
|
||||
update: jest.fn(),
|
||||
close: jest.fn(),
|
||||
error: jest.fn(),
|
||||
};
|
||||
|
||||
wsClient.on('open', retVal.open);
|
||||
wsClient.on('response', retVal.response);
|
||||
wsClient.on('update', retVal.update);
|
||||
wsClient.on('close', retVal.close);
|
||||
wsClient.on('error', retVal.error);
|
||||
|
||||
return {
|
||||
...retVal,
|
||||
cleanup: () => {
|
||||
wsClient.removeListener('open', retVal.open);
|
||||
wsClient.removeListener('response', retVal.response);
|
||||
wsClient.removeListener('update', retVal.update);
|
||||
wsClient.removeListener('close', retVal.close);
|
||||
wsClient.removeListener('error', retVal.error);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function logAllEvents(wsClient: WebsocketClient) {
|
||||
wsClient.on('update', (data) => {
|
||||
console.log('wsUpdate: ', JSON.stringify(data, null, 2));
|
||||
|
||||
Reference in New Issue
Block a user