Skip to content

Commit

Permalink
Merge pull request #56 from singlesly/feat-client-id
Browse files Browse the repository at this point in the history
feat: add test
  • Loading branch information
singlesly authored Oct 29, 2023
2 parents 6db3256 + a35c555 commit 3c006ba
Showing 1 changed file with 71 additions and 39 deletions.
110 changes: 71 additions & 39 deletions src/bingx-socket/bingx-account-socket-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,46 @@ import { ApiAccount } from 'bingx-api/bingx/account/api-account';
import { BingxAccountSocketStream } from 'bingx-api/bingx-socket/bingx-account-socket-stream';
import * as zlib from 'zlib';
import { skip } from 'rxjs';
import {
AccountOrderUpdatePushEvent,
AccountWebsocketEventType,
} from 'bingx-api/bingx-socket/events';

describe('bingx account socket stream', () => {
let wss: Server;
let port: number;
const sockets: WebSocket[] = [];

beforeAll(async () => {
port = await getPortFree();
wss = new Server({
port,
const sendToSocket = (socket: WebSocket, msg: string) => {
zlib.gzip(msg, (err, result) => {
socket.send(result);
});
wss.on('connection', (ws) => {
sockets[0] = ws;
};

ws.on('close', () => {
sockets.splice(0, 1);
beforeAll(async () => {
return new Promise(async (resolve) => {
port = await getPortFree();
wss = new Server({
port,
});
wss.on('listening', resolve);
wss.on('connection', (ws) => {
sockets[0] = ws;

setInterval(() => {
zlib.gzip('Ping', (err, result) => {
ws.send(result);
ws.on('close', () => {
sockets.splice(0, 1);
});
}, 200);

setInterval(() => {
zlib.gzip('Ping', (err, result) => {
ws.send(result);
});
}, 200);
});
});
});

afterAll(() => {
afterAll((done) => {
wss.on('close', done);
wss.close();
});

Expand All @@ -40,30 +53,6 @@ describe('bingx account socket stream', () => {
});
});

describe('initialize connect', () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }),
};
let stream: BingxAccountSocketStream;
beforeAll(() => {
const account = new ApiAccount('xxx', 'xxx');
stream = new BingxAccountSocketStream(account, {
requestExecutor: requestExecutorMock,
url: new URL('', `ws://0.0.0.0:${port}`),
});
});

it('must be got listen key', () => {
// expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1);
});

it('must be got heartbeat', (done) => {
stream.heartbeat$.subscribe(() => {
done();
});
});
});

describe('initialize connect', () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }),
Expand Down Expand Up @@ -110,7 +99,7 @@ describe('bingx account socket stream', () => {
});

it('must be got listen key', () => {
// expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1);
expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1);
});

it('must be got disconnected', (done) => {
Expand Down Expand Up @@ -163,4 +152,47 @@ describe('bingx account socket stream', () => {
});
});
});

describe('order trade update', () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValueOnce({ data: { listenKey: '123' } }),
};
let stream: BingxAccountSocketStream;
beforeAll(() => {
const account = new ApiAccount('xxx', 'xxx');
stream = new BingxAccountSocketStream(account, {
requestExecutor: requestExecutorMock,
url: new URL('', `ws://0.0.0.0:${port}`),
});
});

afterAll(() => {
stream.disconnect();
});

it('must be got listen key', () => {
expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1);
});

it('must be got order push', (done) => {
stream.accountOrderUpdatePushEvent$.subscribe((e) => {
expect(e).toStrictEqual({
e: AccountWebsocketEventType.ORDER_TRADE_UPDATE,
o: {},
E: 111,
} as AccountOrderUpdatePushEvent);
done();
});
setTimeout(() => {
sendToSocket(
sockets[0],
JSON.stringify({
e: AccountWebsocketEventType.ORDER_TRADE_UPDATE,
o: {},
E: 111,
} as AccountOrderUpdatePushEvent),
);
}, 1000);
});
});
});

0 comments on commit 3c006ba

Please sign in to comment.