mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Reconnect fixes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import * as assert from 'assert'
|
||||
import {WAConnection} from '../WAConnection/WAConnection'
|
||||
import { AuthenticationCredentialsBase64, BaileysError, MessageLogLevel } from '../WAConnection/Constants'
|
||||
import { AuthenticationCredentialsBase64, BaileysError, MessageLogLevel, ReconnectMode } from '../WAConnection/Constants'
|
||||
import { delay, promiseTimeout } from '../WAConnection/Utils'
|
||||
import { close } from 'fs'
|
||||
|
||||
describe('QR Generation', () => {
|
||||
it('should generate QR', async () => {
|
||||
@@ -72,7 +73,84 @@ describe('Test Connect', () => {
|
||||
.finally (() => conn.close())
|
||||
})
|
||||
})
|
||||
describe ('Pending Requests', async () => {
|
||||
describe ('Reconnects', () => {
|
||||
it ('should disconnect & reconnect phone', async () => {
|
||||
const conn = new WAConnection ()
|
||||
await conn.loadAuthInfo('./auth_info.json').connect ()
|
||||
assert.equal (conn.phoneConnected, true)
|
||||
|
||||
try {
|
||||
const waitForEvent = expect => new Promise (resolve => {
|
||||
conn.on ('connection-phone-change', ({connected}) => {
|
||||
assert.equal (connected, expect)
|
||||
conn.removeAllListeners ('connection-phone-change')
|
||||
resolve ()
|
||||
})
|
||||
})
|
||||
|
||||
console.log ('disconnect your phone from the internet')
|
||||
await waitForEvent (false)
|
||||
console.log ('reconnect your phone to the internet')
|
||||
await waitForEvent (true)
|
||||
} finally {
|
||||
conn.close ()
|
||||
}
|
||||
})
|
||||
it ('should reconnect connection', async () => {
|
||||
const conn = new WAConnection ()
|
||||
conn.autoReconnect = ReconnectMode.onConnectionLost
|
||||
|
||||
await conn.loadAuthInfo('./auth_info.json').connect ()
|
||||
assert.equal (conn.phoneConnected, true)
|
||||
|
||||
try {
|
||||
const closeConn = () => conn['conn']?.terminate ()
|
||||
|
||||
const task = new Promise (resolve => {
|
||||
let closes = 0
|
||||
conn.on ('closed', ({reason, isReconnecting}) => {
|
||||
console.log (`closed: ${reason}`)
|
||||
assert.ok (reason)
|
||||
assert.ok (isReconnecting)
|
||||
closes += 1
|
||||
|
||||
// let it fail reconnect a few times
|
||||
if (closes > 4) {
|
||||
console.log ('here')
|
||||
conn.removeAllListeners ('closed')
|
||||
conn.removeAllListeners ('connecting')
|
||||
resolve ()
|
||||
}
|
||||
})
|
||||
conn.on ('connecting', () => {
|
||||
// close again
|
||||
delay (500).then (closeConn)
|
||||
})
|
||||
})
|
||||
|
||||
closeConn ()
|
||||
await task
|
||||
|
||||
await new Promise (resolve => {
|
||||
conn.on ('open', () => {
|
||||
conn.removeAllListeners ('open')
|
||||
resolve ()
|
||||
})
|
||||
})
|
||||
|
||||
conn.close ()
|
||||
|
||||
conn.on ('connecting', () => assert.fail('should not connect'))
|
||||
await delay (2000)
|
||||
} finally {
|
||||
conn.removeAllListeners ('connecting')
|
||||
conn.removeAllListeners ('closed')
|
||||
conn.removeAllListeners ('open')
|
||||
conn.close ()
|
||||
}
|
||||
})
|
||||
})
|
||||
describe ('Pending Requests', () => {
|
||||
it('should queue requests when closed', async () => {
|
||||
const conn = new WAConnection ()
|
||||
conn.pendingRequestTimeoutMs = null
|
||||
|
||||
Reference in New Issue
Block a user