mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Initial V3.0
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import * as assert from 'assert'
|
||||
import * as QR from 'qrcode-terminal'
|
||||
import {WAConnection} from '../WAConnection/WAConnection'
|
||||
import { AuthenticationCredentialsBase64 } from '../WAConnection/Constants'
|
||||
import { createTimeout } from '../WAConnection/Utils'
|
||||
import { AuthenticationCredentialsBase64, BaileysError, MessageLogLevel } from '../WAConnection/Constants'
|
||||
import { delay, promiseTimeout } from '../WAConnection/Utils'
|
||||
|
||||
describe('QR Generation', () => {
|
||||
it('should generate QR', async () => {
|
||||
|
||||
const conn = new WAConnection()
|
||||
let calledQR = false
|
||||
conn.onReadyForPhoneAuthentication = ([ref, curveKey, clientID]) => {
|
||||
assert.ok(ref, 'ref nil')
|
||||
assert.ok(curveKey, 'curve key nil')
|
||||
assert.ok(clientID, 'client ID nil')
|
||||
calledQR = true
|
||||
}
|
||||
await assert.rejects(async () => conn.connectSlim(null, 5000), 'should have failed connect')
|
||||
assert.equal(calledQR, true, 'QR not called')
|
||||
conn.regenerateQRIntervalMs = 5000
|
||||
let calledQR = 0
|
||||
conn.removeAllListeners ('qr')
|
||||
conn.on ('qr', qr => calledQR += 1)
|
||||
|
||||
await conn.connect(15000)
|
||||
.then (() => assert.fail('should not have succeeded'))
|
||||
.catch (error => {
|
||||
assert.equal (error.message, 'timed out')
|
||||
})
|
||||
assert.equal (conn['pendingRequests'].length, 0)
|
||||
assert.equal (Object.keys(conn['callbacks']).filter(key => !key.startsWith('function:')).length, 0)
|
||||
assert.ok(calledQR >= 2, 'QR not called')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -23,54 +27,49 @@ describe('Test Connect', () => {
|
||||
let auth: AuthenticationCredentialsBase64
|
||||
it('should connect', async () => {
|
||||
console.log('please be ready to scan with your phone')
|
||||
|
||||
const conn = new WAConnection()
|
||||
const user = await conn.connectSlim(null)
|
||||
assert.ok(user)
|
||||
assert.ok(user.id)
|
||||
await conn.connect (null)
|
||||
assert.ok(conn.user?.id)
|
||||
assert.ok(conn.user?.phone)
|
||||
assert.ok (conn.user?.imgUrl || conn.user.imgUrl === '')
|
||||
|
||||
conn.close()
|
||||
auth = conn.base64EncodedAuthInfo()
|
||||
})
|
||||
it('should re-generate QR & connect', async () => {
|
||||
const conn = new WAConnection()
|
||||
conn.onReadyForPhoneAuthentication = async ([ref, publicKey, clientID]) => {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
console.log ('called QR ' + i + ' times')
|
||||
await createTimeout (3000)
|
||||
ref = await conn.generateNewQRCode ()
|
||||
}
|
||||
const str = ref + ',' + publicKey + ',' + clientID
|
||||
QR.generate(str, { small: true })
|
||||
}
|
||||
const user = await conn.connectSlim(null)
|
||||
assert.ok(user)
|
||||
assert.ok(user.id)
|
||||
|
||||
conn.close()
|
||||
})
|
||||
it('should reconnect', async () => {
|
||||
const conn = new WAConnection()
|
||||
const [user, chats, contacts] = await conn.connect(auth, 20*1000)
|
||||
await conn
|
||||
.loadAuthInfo (auth)
|
||||
.connect (20*1000)
|
||||
.then (conn => {
|
||||
assert.ok(conn.user)
|
||||
assert.ok(conn.user.id)
|
||||
|
||||
assert.ok(user)
|
||||
assert.ok(user.id)
|
||||
|
||||
assert.ok(chats)
|
||||
|
||||
const chatArray = chats.all()
|
||||
if (chatArray.length > 0) {
|
||||
assert.ok(chatArray[0].jid)
|
||||
assert.ok(chatArray[0].count !== null)
|
||||
if (chatArray[0].messages.length > 0) {
|
||||
assert.ok(chatArray[0].messages[0])
|
||||
}
|
||||
}
|
||||
assert.ok(contacts)
|
||||
if (contacts.length > 0) {
|
||||
assert.ok(contacts[0].jid)
|
||||
}
|
||||
await conn.logout()
|
||||
await assert.rejects(async () => conn.connectSlim(auth), 'reconnect should have failed')
|
||||
const chatArray = conn.chats.all()
|
||||
if (chatArray.length > 0) {
|
||||
assert.ok(chatArray[0].jid)
|
||||
assert.ok(chatArray[0].count !== null)
|
||||
if (chatArray[0].messages.length > 0) {
|
||||
assert.ok(chatArray[0].messages[0])
|
||||
}
|
||||
}
|
||||
const contactValues = Object.values(conn.contacts)
|
||||
if (contactValues[0]) {
|
||||
assert.ok(contactValues[0].jid)
|
||||
}
|
||||
})
|
||||
.then (() => conn.logout())
|
||||
.then (() => conn.loadAuthInfo(auth))
|
||||
.then (() => (
|
||||
conn.connect()
|
||||
.then (() => assert.fail('should not have reconnected'))
|
||||
.catch (err => {
|
||||
assert.ok (err instanceof BaileysError)
|
||||
assert.ok ((err as BaileysError).status >= 400)
|
||||
})
|
||||
))
|
||||
.finally (() => conn.close())
|
||||
})
|
||||
})
|
||||
describe ('Pending Requests', async () => {
|
||||
@@ -78,21 +77,17 @@ describe ('Pending Requests', async () => {
|
||||
const conn = new WAConnection ()
|
||||
conn.pendingRequestTimeoutMs = null
|
||||
|
||||
await conn.connectSlim ()
|
||||
await conn.loadAuthInfo('./auth_info.json').connect ()
|
||||
|
||||
await createTimeout (2000)
|
||||
await delay (2000)
|
||||
|
||||
conn.close ()
|
||||
|
||||
const task: Promise<any> = new Promise ((resolve, reject) => {
|
||||
conn.query(['query', 'Status', conn.userMetaData.id])
|
||||
.then (json => resolve(json))
|
||||
.catch (error => reject ('should not have failed, got error: ' + error))
|
||||
})
|
||||
const task: Promise<any> = conn.query({json: ['query', 'Status', conn.user.id]})
|
||||
|
||||
await createTimeout (2000)
|
||||
await delay (2000)
|
||||
|
||||
await conn.connectSlim ()
|
||||
conn.connect ()
|
||||
const json = await task
|
||||
|
||||
assert.ok (json.status)
|
||||
|
||||
Reference in New Issue
Block a user