improve private ws tests
This commit is contained in:
@@ -4,72 +4,98 @@ import {
|
||||
WS_KEY_MAP,
|
||||
} from '../../src';
|
||||
import {
|
||||
logAllEvents,
|
||||
promiseSleep,
|
||||
silentLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../ws.util';
|
||||
|
||||
describe('Private Inverse Perps Websocket Client', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
const API_KEY = process.env.API_KEY_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 = {
|
||||
market: 'inverse',
|
||||
key: API_KEY,
|
||||
secret: API_SECRET,
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPrivate();
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// await promiseSleep(2000);
|
||||
wsClient.closeAll();
|
||||
});
|
||||
describe('with valid API credentails', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
it('should open a ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.inverse,
|
||||
it('should have api credentials to test with', () => {
|
||||
expect(API_KEY).toStrictEqual(expect.any(String));
|
||||
expect(API_SECRET).toStrictEqual(expect.any(String));
|
||||
});
|
||||
|
||||
await Promise.all([wsOpenPromise]);
|
||||
});
|
||||
|
||||
it('should subscribe to private wallet events', 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,
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPrivate();
|
||||
});
|
||||
|
||||
// No easy way to trigger a private event (other than executing trades)
|
||||
// expect(wsUpdatePromise).resolves.toMatchObject({
|
||||
// topic: wsTopic,
|
||||
// data: expect.any(Array),
|
||||
// });
|
||||
afterAll(() => {
|
||||
// await promiseSleep(2000);
|
||||
wsClient.closeAll();
|
||||
});
|
||||
|
||||
wsClient.subscribe(wsTopic);
|
||||
it('should open a ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
|
||||
await Promise.all([wsResponsePromise]);
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.inverse,
|
||||
});
|
||||
|
||||
await Promise.all([wsOpenPromise]);
|
||||
});
|
||||
|
||||
it('should subscribe to private wallet events', 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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,70 +4,103 @@ import {
|
||||
WS_KEY_MAP,
|
||||
} from '../../src';
|
||||
import {
|
||||
promiseSleep,
|
||||
silentLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../ws.util';
|
||||
|
||||
describe('Private Linear Websocket Client', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
const API_KEY = process.env.API_KEY_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 = {
|
||||
market: 'linear',
|
||||
key: API_KEY,
|
||||
secret: API_SECRET,
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPrivate();
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
wsClient.closeAll();
|
||||
});
|
||||
describe('with valid API credentails', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
it('should open a ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.linearPrivate,
|
||||
it('should have api credentials to test with', () => {
|
||||
expect(API_KEY).toStrictEqual(expect.any(String));
|
||||
expect(API_SECRET).toStrictEqual(expect.any(String));
|
||||
});
|
||||
|
||||
await Promise.all([wsOpenPromise]);
|
||||
});
|
||||
const wsClientOptions: WSClientConfigurableOptions = {
|
||||
market: 'linear',
|
||||
key: API_KEY,
|
||||
secret: API_SECRET,
|
||||
};
|
||||
|
||||
it('should subscribe to private wallet events', 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,
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPrivate();
|
||||
});
|
||||
|
||||
// No easy way to trigger a private event (other than executing trades)
|
||||
// expect(wsUpdatePromise).resolves.toMatchObject({
|
||||
// topic: wsTopic,
|
||||
// data: expect.any(Array),
|
||||
// });
|
||||
afterAll(() => {
|
||||
wsClient.closeAll();
|
||||
});
|
||||
|
||||
wsClient.subscribe(wsTopic);
|
||||
it('should open a ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
|
||||
await Promise.all([wsResponsePromise]);
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.linearPrivate,
|
||||
});
|
||||
|
||||
await Promise.all([wsOpenPromise]);
|
||||
});
|
||||
|
||||
it('should subscribe to private wallet events', 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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -73,4 +73,28 @@ describe('Public Linear Websocket Client', () => {
|
||||
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]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ export const WS_OPEN_EVENT_PARTIAL = {
|
||||
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(
|
||||
wsClient: WebsocketClient,
|
||||
event: WsClientEvent,
|
||||
|
||||
Reference in New Issue
Block a user