1+ import Binance from '../src/node-binance-api' ;
2+ import { assert } from 'chai' ;
3+ import util from 'util' ;
4+
5+ const WARN_SHOULD_BE_OBJ = 'should be an object' ;
6+ const WARN_SHOULD_BE_NULL = 'should be null' ;
7+ const WARN_SHOULD_BE_NOT_NULL = 'should not be null' ;
8+ const WARN_SHOULD_HAVE_KEY = 'should have key ' ;
9+ const WARN_SHOULD_NOT_HAVE_KEY = 'should not have key ' ;
10+ const WARN_SHOULD_BE_UNDEFINED = 'should be undefined' ;
11+ const WARN_SHOULD_BE_TYPE = 'should be a ' ;
12+ const TIMEOUT = 40000 ;
13+
14+
15+ const futuresBinance = new Binance ( ) . options ( {
16+ APIKEY : '227719da8d8499e8d3461587d19f259c0b39c2b462a77c9b748a6119abd74401' ,
17+ APISECRET : 'b14b935f9cfacc5dec829008733c40da0588051f29a44625c34967b45c11d73c' ,
18+ hedgeMode : true ,
19+ test : true
20+ } ) ;
21+
22+
23+ const stopSockets = function ( log = false ) {
24+ let endpoints = futuresBinance . websockets . subscriptions ( ) ;
25+ for ( let endpoint in endpoints ) {
26+ if ( log ) console . log ( 'Terminated ws endpoint: ' + endpoint ) ;
27+ futuresBinance . websockets . terminate ( endpoint ) ;
28+ }
29+ }
30+
31+ describe ( 'Websockets candlesticks' , function ( ) {
32+ let candlesticks ;
33+ let cnt = 0 ;
34+ /*global beforeEach*/
35+ beforeEach ( function ( done ) {
36+ this . timeout ( TIMEOUT ) ;
37+ futuresBinance . futuresCandlesticksStream ( [ 'BTCUSDT' ] , '1m' , a_candlesticks => {
38+ cnt ++ ;
39+ if ( cnt > 1 ) return ;
40+ candlesticks = a_candlesticks ;
41+ stopSockets ( ) ;
42+ done ( ) ;
43+ } ) ;
44+ } ) ;
45+
46+ it ( 'Calls spot candlesticks websocket' , function ( ) {
47+ assert ( typeof ( candlesticks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
48+ assert ( candlesticks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
49+ assert ( Object . keys ( candlesticks ) . length >= 0 , 'should at least 1 currency pairs?' ) ;
50+
51+ let keys = [ 't' , 'T' , 's' , 'i' , 'f' , 'L' , 'o' , 'c' , 'h' , 'l' , 'v' , 'n' , 'x' , 'q' , 'V' , 'Q' , 'B' ] ;
52+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 'e' ) , WARN_SHOULD_HAVE_KEY + 'e' ) ;
53+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 'E' ) , WARN_SHOULD_HAVE_KEY + 'E' ) ;
54+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 's' ) , WARN_SHOULD_HAVE_KEY + 's' ) ;
55+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 's' ) , WARN_SHOULD_HAVE_KEY + 'k' ) ;
56+
57+ keys . forEach ( function ( key ) {
58+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks . k , key ) , WARN_SHOULD_HAVE_KEY + key ) ;
59+ } ) ;
60+ } ) ;
61+ } ) ;
62+
63+ describe ( 'Websockets futures ticker stream' , function ( ) {
64+ let response ;
65+ let cnt = 0 ;
66+
67+ beforeEach ( function ( done ) {
68+ this . timeout ( TIMEOUT ) ;
69+ futuresBinance . websockets . futuresTicker ( undefined , a_response => {
70+ cnt ++ ;
71+ if ( cnt > 1 ) return ;
72+ stopSockets ( ) ;
73+ response = a_response ;
74+ done ( ) ;
75+ } )
76+ } ) ;
77+
78+ it ( 'Calls prevDay websocket for symbol' , function ( ) {
79+ assert ( typeof ( response ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
80+ } ) ;
81+ } ) ;
82+
83+
84+ describe ( 'Websockets miniticker' , function ( ) {
85+ let markets ;
86+ let cnt = 0 ;
87+ beforeEach ( function ( done ) {
88+ this . timeout ( TIMEOUT ) ;
89+ futuresBinance . websockets . futuresMiniTicker ( undefined , tick => {
90+ cnt ++ ;
91+ if ( cnt > 1 ) return ;
92+ markets = tick ;
93+ stopSockets ( ) ;
94+ done ( ) ;
95+ } ) ;
96+ } ) ;
97+
98+ it ( 'check miniticker websocket' , function ( ) {
99+ assert ( typeof ( markets ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
100+ assert ( markets !== null , WARN_SHOULD_BE_NOT_NULL ) ;
101+ assert ( Object . keys ( markets ) . length >= 0 , 'should at least 1 currency pairs?' ) ;
102+
103+ Object . keys ( markets ) . forEach ( function ( symbol ) {
104+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'close' ) , WARN_SHOULD_HAVE_KEY + 'close' ) ;
105+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'open' ) , WARN_SHOULD_HAVE_KEY + 'open' ) ;
106+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'high' ) , WARN_SHOULD_HAVE_KEY + 'high' ) ;
107+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'low' ) , WARN_SHOULD_HAVE_KEY + 'low' ) ;
108+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'volume' ) , WARN_SHOULD_HAVE_KEY + 'volume' ) ;
109+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'quoteVolume' ) , WARN_SHOULD_HAVE_KEY + 'quoteVolume' ) ;
110+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'eventTime' ) , WARN_SHOULD_HAVE_KEY + 'eventTime' ) ;
111+ } ) ;
112+ } ) ;
113+ } ) ;
114+
115+
116+
117+ describe ( 'Websockets chart' , function ( ) {
118+ let chart ;
119+ let interval ;
120+ let symbol ;
121+ let cnt = 0 ;
122+ beforeEach ( function ( done ) {
123+ this . timeout ( TIMEOUT ) ;
124+ futuresBinance . websockets . futuresChart ( 'BTCUSDT' , '1m' , ( a_symbol , a_interval , a_chart ) => {
125+ cnt ++ ;
126+ if ( cnt > 1 ) {
127+ stopSockets ( ) ;
128+ return ;
129+ }
130+ chart = a_chart ;
131+ interval = a_interval ;
132+ symbol = a_symbol ;
133+ stopSockets ( ) ;
134+ done ( ) ;
135+ } ) ;
136+ } ) ;
137+
138+ it ( 'Calls chart websocket' , function ( ) {
139+ assert ( typeof ( chart ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
140+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
141+ assert ( typeof ( interval ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
142+ assert ( chart !== null , WARN_SHOULD_BE_NOT_NULL ) ;
143+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
144+ assert ( interval !== null , WARN_SHOULD_BE_NOT_NULL ) ;
145+
146+ let keys = [ 'open' , 'high' , 'open' , 'close' , 'volume' ] ;
147+ assert ( Object . keys ( chart ) . length > 0 , 'Should not be empty' ) ;
148+
149+ Object . keys ( chart ) . forEach ( function ( c ) {
150+ keys . forEach ( function ( key ) {
151+ assert ( Object . prototype . hasOwnProperty . call ( chart [ c ] , key ) , WARN_SHOULD_HAVE_KEY + key ) ;
152+ } ) ;
153+ } ) ;
154+ } ) ;
155+ } ) ;
156+
157+
158+ describe ( 'Websockets aggregated trades' , function ( ) {
159+ let trades ;
160+ let cnt = 0 ;
161+ /*global beforeEach*/
162+ beforeEach ( function ( done ) {
163+ this . timeout ( TIMEOUT ) ;
164+ futuresBinance . websockets . futuresAggTrades ( [ 'BTCUSDT' , 'ETHBTC' ] , e_trades => {
165+ cnt ++ ;
166+ if ( cnt > 1 ) return ;
167+ trades = e_trades ;
168+ stopSockets ( ) ;
169+ done ( ) ;
170+ } ) ;
171+ } ) ;
172+
173+ it ( 'Calls trades websocket' , function ( ) {
174+ assert ( typeof ( trades ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
175+ assert ( trades !== null , WARN_SHOULD_BE_NOT_NULL ) ;
176+ } ) ;
177+ } ) ;
0 commit comments