Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions test/websockets/bookTicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import test from 'ava'

import Binance from 'index'

import { checkFields } from '../utils'

const client = Binance()

test('[WS] bookTicker - single symbol', t => {
return new Promise(resolve => {
const clean = client.ws.bookTicker('ETHBTC', ticker => {
checkFields(t, ticker, ['updateId', 'symbol', 'bestBid', 'bestBidQnt', 'bestAsk', 'bestAskQnt'])
t.is(ticker.symbol, 'ETHBTC')
t.truthy(typeof ticker.bestBid === 'string')
t.truthy(typeof ticker.bestAsk === 'string')
t.truthy(typeof ticker.bestBidQnt === 'string')
t.truthy(typeof ticker.bestAskQnt === 'string')
clean()
resolve()
})
})
})

test('[WS] bookTicker - multiple symbols', t => {
return new Promise(resolve => {
const symbols = ['ETHBTC', 'BTCUSDT', 'BNBBTC']

const clean = client.ws.bookTicker(symbols, ticker => {
checkFields(t, ticker, ['updateId', 'symbol', 'bestBid', 'bestBidQnt', 'bestAsk', 'bestAskQnt'])
t.truthy(symbols.includes(ticker.symbol))
clean()
resolve()
})
})
})

test('[WS] bookTicker - raw data without transform', t => {
return new Promise(resolve => {
const clean = client.ws.bookTicker('ETHBTC', ticker => {
// Raw data should have lowercase field names
t.truthy(ticker.u)
t.truthy(ticker.s)
t.truthy(ticker.b)
t.truthy(ticker.B)
t.truthy(ticker.a)
t.truthy(ticker.A)
clean()
resolve()
}, false)
})
})

test('[WS] bookTicker - transformed data', t => {
return new Promise(resolve => {
const clean = client.ws.bookTicker('ETHBTC', ticker => {
// Transformed data should have camelCase field names
t.truthy(ticker.updateId)
t.truthy(ticker.symbol)
t.truthy(ticker.bestBid)
t.truthy(ticker.bestBidQnt)
t.truthy(ticker.bestAsk)
t.truthy(ticker.bestAskQnt)
// Should NOT have raw field names
t.falsy(ticker.u)
t.falsy(ticker.s)
clean()
resolve()
}, true)
})
})

test('[WS] bookTicker - cleanup function', t => {
const clean = client.ws.bookTicker('ETHBTC', () => {
// Callback implementation
})

// Verify clean is a function
t.is(typeof clean, 'function')

// Clean up immediately
clean()

// Give it a moment and verify cleanup executed properly
return new Promise(resolve => {
setTimeout(() => {
t.pass('Cleanup function executed without errors')
resolve()
}, 100)
})
})
99 changes: 99 additions & 0 deletions test/websockets/candles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import test from 'ava'

import Binance from 'index'

import { checkFields } from '../utils'

const client = Binance()

test('[WS] candles - missing parameters', t => {
try {
client.ws.candles('ETHBTC', d => d)
t.fail('Should have thrown an error')
} catch (e) {
t.is(e.message, 'Please pass a symbol, interval and callback.')
}
})

test('[WS] candles - single symbol', t => {
return new Promise(resolve => {
const clean = client.ws.candles('ETHBTC', '5m', candle => {
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
t.is(candle.symbol, 'ETHBTC')
t.is(candle.interval, '5m')
clean()
resolve()
})
})
})

test('[WS] candles - multiple symbols', t => {
return new Promise(resolve => {
const symbols = ['ETHBTC', 'BNBBTC', 'BNTBTC']

const clean = client.ws.candles(symbols, '5m', candle => {
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
t.truthy(symbols.includes(candle.symbol))
clean()
resolve()
})
})
})

test('[WS] candles - raw data without transform', t => {
return new Promise(resolve => {
const clean = client.ws.candles('ETHBTC', '1m', candle => {
// Raw data should have the structure with 'k' key
t.truthy(candle.e)
t.truthy(candle.E)
t.truthy(candle.s)
t.truthy(candle.k)
clean()
resolve()
}, false)
})
})

test('[WS] candles - transformed data', t => {
return new Promise(resolve => {
const clean = client.ws.candles('ETHBTC', '1m', candle => {
// Transformed data should have camelCase field names
t.truthy(candle.eventType)
t.truthy(candle.eventTime)
t.truthy(candle.symbol)
t.truthy(candle.open)
t.truthy(candle.close)
// Should NOT have raw field names
t.falsy(candle.k)
clean()
resolve()
}, true)
})
})

test('[WS] futuresCandles - single symbol', t => {
return new Promise(resolve => {
const clean = client.ws.futuresCandles('BTCUSDT', '5m', candle => {
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'quoteVolume'])
t.is(candle.symbol, 'BTCUSDT')
t.is(candle.interval, '5m')
clean()
resolve()
})
})
})

test.skip('[WS] deliveryCandles - single symbol', t => {
return new Promise(resolve => {
const clean = client.ws.deliveryCandles('TRXUSD_PERP', '5m', candle => {
checkFields(t, candle, ['open', 'high', 'low', 'close', 'volume', 'trades', 'baseVolume'])
t.is(candle.symbol, 'TRXUSD_PERP')
t.is(candle.interval, '5m')
// Delivery candles have baseVolume instead of quoteVolume
t.truthy(candle.baseVolume)
t.falsy(candle.quoteVolume)
clean()
resolve()
})
})
})
134 changes: 134 additions & 0 deletions test/websockets/customSubStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import test from 'ava'

import Binance from 'index'

const client = Binance()

test('[WS] customSubStream - single stream', t => {
return new Promise(resolve => {
const clean = client.ws.customSubStream('ethbtc@ticker', data => {
t.truthy(data)
t.truthy(data.e || data.stream)
clean()
resolve()
})
})
})

test('[WS] customSubStream - multiple streams', t => {
return new Promise(resolve => {
const streams = ['ethbtc@ticker', 'btcusdt@ticker']

const clean = client.ws.customSubStream(streams, data => {
t.truthy(data)
clean()
resolve()
})
})
})

test('[WS] customSubStream - depth stream', t => {
return new Promise(resolve => {
const clean = client.ws.customSubStream('ethbtc@depth', data => {
t.truthy(data)
t.truthy(data.e || data.lastUpdateId)
clean()
resolve()
})
})
})

test('[WS] customSubStream - aggTrade stream', t => {
return new Promise(resolve => {
const clean = client.ws.customSubStream('ethbtc@aggTrade', data => {
t.truthy(data)
t.truthy(data.e || data.a)
clean()
resolve()
})
})
})

test('[WS] customSubStream - kline stream', t => {
return new Promise(resolve => {
const clean = client.ws.customSubStream('ethbtc@kline_1m', data => {
t.truthy(data)
t.truthy(data.e || data.k)
clean()
resolve()
})
})
})

test('[WS] customSubStream - cleanup function', t => {
const clean = client.ws.customSubStream('ethbtc@ticker', () => {
// Callback implementation
})

// Verify clean is a function
t.is(typeof clean, 'function')

// Clean up immediately
clean()

// Give it a moment and verify cleanup executed properly
return new Promise(resolve => {
setTimeout(() => {
t.pass('Cleanup function executed without errors')
resolve()
}, 100)
})
})

test('[WS] futuresCustomSubStream - single stream', t => {
return new Promise(resolve => {
const clean = client.ws.futuresCustomSubStream('btcusdt@ticker', data => {
t.truthy(data)
t.truthy(data.e || data.stream)
clean()
resolve()
})
})
})

test('[WS] futuresCustomSubStream - aggTrade stream', t => {
return new Promise(resolve => {
const clean = client.ws.futuresCustomSubStream('btcusdt@aggTrade', data => {
t.truthy(data)
t.truthy(data.e || data.a)
clean()
resolve()
})
})
})

test.skip('[WS] deliveryCustomSubStream - single stream', t => {
return new Promise(resolve => {
const clean = client.ws.deliveryCustomSubStream('trxusd_perp@ticker', data => {
t.truthy(data)
t.truthy(data.e || data.stream)
clean()
resolve()
})
})
})

test('[WS] futuresCustomSubStream - cleanup function', t => {
const clean = client.ws.futuresCustomSubStream('btcusdt@ticker', () => {
// Callback implementation
})

// Verify clean is a function
t.is(typeof clean, 'function')

// Clean up immediately
clean()

// Give it a moment and verify cleanup executed properly
return new Promise(resolve => {
setTimeout(() => {
t.pass('Cleanup function executed without errors')
resolve()
}, 100)
})
})
Loading