improve private ws tests

This commit is contained in:
tiagosiebler
2022-09-15 16:47:39 +01:00
parent 1b422a1beb
commit 27bd81593c
4 changed files with 167 additions and 84 deletions

View File

@@ -4,30 +4,55 @@ import {
WS_KEY_MAP, WS_KEY_MAP,
} from '../../src'; } from '../../src';
import { import {
logAllEvents,
promiseSleep,
silentLogger, silentLogger,
waitForSocketEvent, waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL, WS_OPEN_EVENT_PARTIAL,
} from '../ws.util'; } from '../ws.util';
describe('Private Inverse Perps Websocket Client', () => { describe('Private Inverse Perps Websocket Client', () => {
let wsClient: WebsocketClient;
const API_KEY = process.env.API_KEY_COM; const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM; const API_SECRET = process.env.API_SECRET_COM;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
});
const wsClientOptions: WSClientConfigurableOptions = { const wsClientOptions: WSClientConfigurableOptions = {
market: 'inverse', market: 'inverse',
key: API_KEY, key: API_KEY,
secret: API_SECRET, secret: API_SECRET,
}; };
describe('with invalid credentials', () => {
it('should fail to open a connection if keys/signature are incorrect', async () => {
const badClient = new WebsocketClient(
{
...wsClientOptions,
key: 'bad',
secret: 'bad',
},
silentLogger
);
const wsOpenPromise = waitForSocketEvent(badClient, 'open', 2500);
badClient.connectPrivate();
expect(wsOpenPromise).rejects.toMatch('Failed to receive');
try {
await Promise.all([wsOpenPromise]);
} catch (e) {
// console.error()
}
badClient.closeAll();
});
});
describe('with valid API credentails', () => {
let wsClient: WebsocketClient;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
});
beforeAll(() => { beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger); wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPrivate(); wsClient.connectPrivate();
@@ -73,3 +98,4 @@ describe('Private Inverse Perps Websocket Client', () => {
await Promise.all([wsResponsePromise]); await Promise.all([wsResponsePromise]);
}); });
}); });
});

View File

@@ -4,18 +4,50 @@ import {
WS_KEY_MAP, WS_KEY_MAP,
} from '../../src'; } from '../../src';
import { import {
promiseSleep,
silentLogger, silentLogger,
waitForSocketEvent, waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL, WS_OPEN_EVENT_PARTIAL,
} from '../ws.util'; } from '../ws.util';
describe('Private Linear Websocket Client', () => { describe('Private Linear Websocket Client', () => {
let wsClient: WebsocketClient;
const API_KEY = process.env.API_KEY_COM; const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM; const API_SECRET = process.env.API_SECRET_COM;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'linear',
key: API_KEY,
secret: API_SECRET,
};
describe('with invalid credentials', () => {
it('should fail to open a connection if keys/signature are incorrect', async () => {
const badClient = new WebsocketClient(
{
...wsClientOptions,
key: 'bad',
secret: 'bad',
},
silentLogger
);
const wsOpenPromise = waitForSocketEvent(badClient, 'open', 2500);
badClient.connectPrivate();
expect(wsOpenPromise).rejects.toMatch('Failed to receive');
try {
await Promise.all([wsOpenPromise]);
} catch (e) {
// console.error()
}
badClient.closeAll();
});
});
describe('with valid API credentails', () => {
let wsClient: WebsocketClient;
it('should have api credentials to test with', () => { it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String)); expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String)); expect(API_SECRET).toStrictEqual(expect.any(String));
@@ -71,3 +103,4 @@ describe('Private Linear Websocket Client', () => {
await Promise.all([wsResponsePromise]); await Promise.all([wsResponsePromise]);
}); });
}); });
});

View File

@@ -73,4 +73,28 @@ describe('Public Linear Websocket Client', () => {
console.error(`Wait for "${wsTopic}" event exception: `, e); console.error(`Wait for "${wsTopic}" event exception: `, e);
} }
}); });
it('should fail to subscribe to private events (no keys)', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'wallet';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
// No easy way to trigger a private event (other than executing trades)
// expect(wsUpdatePromise).resolves.toMatchObject({
// topic: wsTopic,
// data: expect.any(Array),
// });
wsClient.subscribe(wsTopic);
await Promise.all([wsResponsePromise]);
});
}); });

View File

@@ -13,7 +13,7 @@ export const WS_OPEN_EVENT_PARTIAL = {
type: 'open', type: 'open',
}; };
/** Resolves a promise if an event is seen before a timeout (defaults to 2.5 seconds) */ /** Resolves a promise if an event is seen before a timeout (defaults to 4.5 seconds) */
export function waitForSocketEvent( export function waitForSocketEvent(
wsClient: WebsocketClient, wsClient: WebsocketClient,
event: WsClientEvent, event: WsClientEvent,