Skip to content

Commit f9810c5

Browse files
authored
Merge pull request #679 from pcriadoperez/ws-tests
chore: add websocket tests
2 parents d7daedf + 8fb10e7 commit f9810c5

9 files changed

Lines changed: 1485 additions & 0 deletions

File tree

test/websockets/bookTicker.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import test from 'ava'
2+
3+
import Binance from 'index'
4+
5+
import { checkFields } from '../utils'
6+
7+
const client = Binance()
8+
9+
test('[WS] bookTicker - single symbol', t => {
10+
return new Promise(resolve => {
11+
const clean = client.ws.bookTicker('ETHBTC', ticker => {
12+
checkFields(t, ticker, ['updateId', 'symbol', 'bestBid', 'bestBidQnt', 'bestAsk', 'bestAskQnt'])
13+
t.is(ticker.symbol, 'ETHBTC')
14+
t.truthy(typeof ticker.bestBid === 'string')
15+
t.truthy(typeof ticker.bestAsk === 'string')
16+
t.truthy(typeof ticker.bestBidQnt === 'string')
17+
t.truthy(typeof ticker.bestAskQnt === 'string')
18+
clean()
19+
resolve()
20+
})
21+
})
22+
})
23+
24+
test('[WS] bookTicker - multiple symbols', t => {
25+
return new Promise(resolve => {
26+
const symbols = ['ETHBTC', 'BTCUSDT', 'BNBBTC']
27+
28+
const clean = client.ws.bookTicker(symbols, ticker => {
29+
checkFields(t, ticker, ['updateId', 'symbol', 'bestBid', 'bestBidQnt', 'bestAsk', 'bestAskQnt'])
30+
t.truthy(symbols.includes(ticker.symbol))
31+
clean()
32+
resolve()
33+
})
34+
})
35+
})
36+
37+
test('[WS] bookTicker - raw data without transform', t => {
38+
return new Promise(resolve => {
39+
const clean = client.ws.bookTicker('ETHBTC', ticker => {
40+
// Raw data should have lowercase field names
41+
t.truthy(ticker.u)
42+
t.truthy(ticker.s)
43+
t.truthy(ticker.b)
44+
t.truthy(ticker.B)
45+
t.truthy(ticker.a)
46+
t.truthy(ticker.A)
47+
clean()
48+
resolve()
49+
}, false)
50+
})
51+
})
52+
53+
test('[WS] bookTicker - transformed data', t => {
54+
return new Promise(resolve => {
55+
const clean = client.ws.bookTicker('ETHBTC', ticker => {
56+
// Transformed data should have camelCase field names
57+
t.truthy(ticker.updateId)
58+
t.truthy(ticker.symbol)
59+
t.truthy(ticker.bestBid)
60+
t.truthy(ticker.bestBidQnt)
61+
t.truthy(ticker.bestAsk)
62+
t.truthy(ticker.bestAskQnt)
63+
// Should NOT have raw field names
64+
t.falsy(ticker.u)
65+
t.falsy(ticker.s)
66+
clean()
67+
resolve()
68+
}, true)
69+
})
70+
})
71+
72+
test('[WS] bookTicker - cleanup function', t => {
73+
const clean = client.ws.bookTicker('ETHBTC', () => {
74+
// Callback implementation
75+
})
76+
77+
// Verify clean is a function
78+
t.is(typeof clean, 'function')
79+
80+
// Clean up immediately
81+
clean()
82+
83+
// Give it a moment and verify cleanup executed properly
84+
return new Promise(resolve => {
85+
setTimeout(() => {
86+
t.pass('Cleanup function executed without errors')
87+
resolve()
88+
}, 100)
89+
})
90+
})

test/websockets/candles.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import test from 'ava'
2+
3+
import Binance from 'index'
4+
5+
import { checkFields } from '../utils'
6+
7+
const client = Binance()
8+
9+
test('[WS] candles - missing parameters', t => {
10+
try {
11+
client.ws.candles('ETHBTC', d => d)
12+
t.fail('Should have thrown an error')
13+
} catch (e) {
14+
t.is(e.message, 'Please pass a symbol, interval and callback.')
15+
}
16+
})
17+
18+
test('[WS] candles - single symbol', t => {
19+
return new Promise(resolve => {
20+
const clean = client.ws.candles('ETHBTC', '5m', candle => {
21+
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
22+
t.is(candle.symbol, 'ETHBTC')
23+
t.is(candle.interval, '5m')
24+
clean()
25+
resolve()
26+
})
27+
})
28+
})
29+
30+
test('[WS] candles - multiple symbols', t => {
31+
return new Promise(resolve => {
32+
const symbols = ['ETHBTC', 'BNBBTC', 'BNTBTC']
33+
34+
const clean = client.ws.candles(symbols, '5m', candle => {
35+
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
36+
t.truthy(symbols.includes(candle.symbol))
37+
clean()
38+
resolve()
39+
})
40+
})
41+
})
42+
43+
test('[WS] candles - raw data without transform', t => {
44+
return new Promise(resolve => {
45+
const clean = client.ws.candles('ETHBTC', '1m', candle => {
46+
// Raw data should have the structure with 'k' key
47+
t.truthy(candle.e)
48+
t.truthy(candle.E)
49+
t.truthy(candle.s)
50+
t.truthy(candle.k)
51+
clean()
52+
resolve()
53+
}, false)
54+
})
55+
})
56+
57+
test('[WS] candles - transformed data', t => {
58+
return new Promise(resolve => {
59+
const clean = client.ws.candles('ETHBTC', '1m', candle => {
60+
// Transformed data should have camelCase field names
61+
t.truthy(candle.eventType)
62+
t.truthy(candle.eventTime)
63+
t.truthy(candle.symbol)
64+
t.truthy(candle.open)
65+
t.truthy(candle.close)
66+
// Should NOT have raw field names
67+
t.falsy(candle.k)
68+
clean()
69+
resolve()
70+
}, true)
71+
})
72+
})
73+
74+
test('[WS] futuresCandles - single symbol', t => {
75+
return new Promise(resolve => {
76+
const clean = client.ws.futuresCandles('BTCUSDT', '5m', candle => {
77+
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
78+
t.is(candle.symbol, 'BTCUSDT')
79+
t.is(candle.interval, '5m')
80+
clean()
81+
resolve()
82+
})
83+
})
84+
})
85+
86+
test.skip('[WS] deliveryCandles - single symbol', t => {
87+
return new Promise(resolve => {
88+
const clean = client.ws.deliveryCandles('TRXUSD_PERP', '5m', candle => {
89+
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'baseVolume'])
90+
t.is(candle.symbol, 'TRXUSD_PERP')
91+
t.is(candle.interval, '5m')
92+
// Delivery candles have baseVolume instead of quoteVolume
93+
t.truthy(candle.baseVolume)
94+
t.falsy(candle.quoteVolume)
95+
clean()
96+
resolve()
97+
})
98+
})
99+
})

test/websockets/customSubStream.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import test from 'ava'
2+
3+
import Binance from 'index'
4+
5+
const client = Binance()
6+
7+
test('[WS] customSubStream - single stream', t => {
8+
return new Promise(resolve => {
9+
const clean = client.ws.customSubStream('ethbtc@ticker', data => {
10+
t.truthy(data)
11+
t.truthy(data.e || data.stream)
12+
clean()
13+
resolve()
14+
})
15+
})
16+
})
17+
18+
test('[WS] customSubStream - multiple streams', t => {
19+
return new Promise(resolve => {
20+
const streams = ['ethbtc@ticker', 'btcusdt@ticker']
21+
22+
const clean = client.ws.customSubStream(streams, data => {
23+
t.truthy(data)
24+
clean()
25+
resolve()
26+
})
27+
})
28+
})
29+
30+
test('[WS] customSubStream - depth stream', t => {
31+
return new Promise(resolve => {
32+
const clean = client.ws.customSubStream('ethbtc@depth', data => {
33+
t.truthy(data)
34+
t.truthy(data.e || data.lastUpdateId)
35+
clean()
36+
resolve()
37+
})
38+
})
39+
})
40+
41+
test('[WS] customSubStream - aggTrade stream', t => {
42+
return new Promise(resolve => {
43+
const clean = client.ws.customSubStream('ethbtc@aggTrade', data => {
44+
t.truthy(data)
45+
t.truthy(data.e || data.a)
46+
clean()
47+
resolve()
48+
})
49+
})
50+
})
51+
52+
test('[WS] customSubStream - kline stream', t => {
53+
return new Promise(resolve => {
54+
const clean = client.ws.customSubStream('ethbtc@kline_1m', data => {
55+
t.truthy(data)
56+
t.truthy(data.e || data.k)
57+
clean()
58+
resolve()
59+
})
60+
})
61+
})
62+
63+
test('[WS] customSubStream - cleanup function', t => {
64+
const clean = client.ws.customSubStream('ethbtc@ticker', () => {
65+
// Callback implementation
66+
})
67+
68+
// Verify clean is a function
69+
t.is(typeof clean, 'function')
70+
71+
// Clean up immediately
72+
clean()
73+
74+
// Give it a moment and verify cleanup executed properly
75+
return new Promise(resolve => {
76+
setTimeout(() => {
77+
t.pass('Cleanup function executed without errors')
78+
resolve()
79+
}, 100)
80+
})
81+
})
82+
83+
test('[WS] futuresCustomSubStream - single stream', t => {
84+
return new Promise(resolve => {
85+
const clean = client.ws.futuresCustomSubStream('btcusdt@ticker', data => {
86+
t.truthy(data)
87+
t.truthy(data.e || data.stream)
88+
clean()
89+
resolve()
90+
})
91+
})
92+
})
93+
94+
test('[WS] futuresCustomSubStream - aggTrade stream', t => {
95+
return new Promise(resolve => {
96+
const clean = client.ws.futuresCustomSubStream('btcusdt@aggTrade', data => {
97+
t.truthy(data)
98+
t.truthy(data.e || data.a)
99+
clean()
100+
resolve()
101+
})
102+
})
103+
})
104+
105+
test.skip('[WS] deliveryCustomSubStream - single stream', t => {
106+
return new Promise(resolve => {
107+
const clean = client.ws.deliveryCustomSubStream('trxusd_perp@ticker', data => {
108+
t.truthy(data)
109+
t.truthy(data.e || data.stream)
110+
clean()
111+
resolve()
112+
})
113+
})
114+
})
115+
116+
test('[WS] futuresCustomSubStream - cleanup function', t => {
117+
const clean = client.ws.futuresCustomSubStream('btcusdt@ticker', () => {
118+
// Callback implementation
119+
})
120+
121+
// Verify clean is a function
122+
t.is(typeof clean, 'function')
123+
124+
// Clean up immediately
125+
clean()
126+
127+
// Give it a moment and verify cleanup executed properly
128+
return new Promise(resolve => {
129+
setTimeout(() => {
130+
t.pass('Cleanup function executed without errors')
131+
resolve()
132+
}, 100)
133+
})
134+
})

0 commit comments

Comments
 (0)