cleaning around tests

This commit is contained in:
tiagosiebler
2022-09-15 19:06:35 +01:00
parent 27bd81593c
commit f61e79934d
10 changed files with 323 additions and 76 deletions

View File

@@ -0,0 +1,58 @@
import {
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
logAllEvents,
promiseSleep,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Private Spot V1 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: 'spot',
key: API_KEY,
secret: API_SECRET,
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
logAllEvents(wsClient);
});
afterAll(() => {
wsClient.closeAll();
});
// TODO: how to detect if auth failed for the v1 spot ws
it('should open a private ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
wsClient.connectPrivate();
expect(wsOpenPromise).resolves.toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.spotPrivate,
});
// expect(wsUpdatePromise).resolves.toMatchObject({
// topic: 'wsTopic',
// data: expect.any(Array),
// });
await Promise.all([wsOpenPromise]);
// await Promise.all([wsUpdatePromise]);
// await promiseSleep(4000);
});
});

View File

@@ -0,0 +1,64 @@
import {
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
logAllEvents,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Public Spot V1 Websocket Client', () => {
let wsClient: WebsocketClient;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'spot',
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPublic();
// logAllEvents(wsClient);
});
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.spotPublic,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to public orderbook events', async () => {
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const symbol = 'BTCUSDT';
expect(wsUpdatePromise).resolves.toMatchObject({
symbol: symbol,
symbolName: symbol,
topic: 'diffDepth',
params: {
realtimeInterval: '24h',
binary: 'false',
},
data: expect.any(Array),
});
wsClient.subscribePublicSpotOrderbook(symbol, 'delta');
try {
await wsUpdatePromise;
} catch (e) {
console.error(`Wait for spot v1 orderbook event exception: `, e);
}
});
});

View File

@@ -0,0 +1,72 @@
import {
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
logAllEvents,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Public Spot V3 Websocket Client', () => {
let wsClient: WebsocketClient;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'spotV3',
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPublic();
// logAllEvents(wsClient);
});
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.spotV3Public,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to public orderbook events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'orderbook.40.BTCUSDT';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
expect(wsUpdatePromise).resolves.toStrictEqual('');
wsClient.subscribe(wsTopic);
try {
await wsResponsePromise;
} catch (e) {
console.error(
`Wait for "${wsTopic}" subscription response exception: `,
e
);
}
try {
await wsUpdatePromise;
} catch (e) {
console.error(`Wait for "${wsTopic}" event exception: `, e);
}
});
});