cq-websocket
Version:
A Node SDK for developing QQ chatbots based on WebSocket, which is depending on CoolQ and CQHTTP API plugin.
53 lines (46 loc) • 1.33 kB
JavaScript
const { CQWebsocket } = require('../fixture/connect-success')()
const { ApiTimoutError } = require('../../src/errors')
const { stub } = require('sinon')
const { test } = require('ava')
test.cb('Auto-fetch if no QQ account provided.', function (t) {
t.plan(2)
const bot = new CQWebsocket()
t.is(bot._qq, -1)
let stubSend
bot
.on('ready', function () {
stubSend = stub(bot._apiSock, 'send')
stubSend.callsFake(function (data) {
const { echo } = JSON.parse(data)
this.onMessage(JSON.stringify({
data: {
nickname: 'fake-QQ',
user_id: 123456789
},
retcode: 0,
status: 'ok',
echo
}))
})
})
.on('api.response', () => {
// when emitting api.response, the call to '/get_login_info' has actually been resolved
// but the Promise state changes at the next tick
setImmediate(() => {
t.is(bot._qq, 123456789)
t.end()
})
})
.connect()
})
test.cb('Auto-fetch failure due to gloabal request timeout', function (t) {
t.plan(2)
const bot = new CQWebsocket({ requestOptions: { timeout: 2000 } })
bot
.on('error', err => {
t.true(err instanceof ApiTimoutError)
t.is(err.req.action, 'get_login_info')
t.end()
})
.connect()
})