mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
More accurate phone connection detection
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds, toNumber, GET_MESSAGE_ID, waMessageKey } from '../WAConnection/WAConnection'
|
||||
import { WAConnection, MessageOptions, MessageType, unixTimestampSeconds, toNumber, GET_MESSAGE_ID, waMessageKey } from '../WAConnection/WAConnection'
|
||||
import * as assert from 'assert'
|
||||
import {promises as fs} from 'fs'
|
||||
|
||||
@@ -21,7 +21,7 @@ export const WAConnectionTest = (name: string, func: (conn: WAConnection) => voi
|
||||
describe(name, () => {
|
||||
const conn = new WAConnection()
|
||||
conn.connectOptions.maxIdleTimeMs = 30_000
|
||||
conn.logLevel = MessageLogLevel.unhandled
|
||||
conn.logger.level = 'debug'
|
||||
|
||||
before(async () => {
|
||||
const file = './auth_info.json'
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as assert from 'assert'
|
||||
import {WAConnection} from '../WAConnection/WAConnection'
|
||||
import { AuthenticationCredentialsBase64, BaileysError, ReconnectMode, DisconnectReason } from '../WAConnection/Constants'
|
||||
import { delay } from '../WAConnection/Utils'
|
||||
import { assertChatDBIntegrity } from './Common'
|
||||
import { assertChatDBIntegrity, testJid } from './Common'
|
||||
|
||||
describe('QR Generation', () => {
|
||||
it('should generate QR', async () => {
|
||||
@@ -73,22 +73,41 @@ describe('Test Connect', () => {
|
||||
})
|
||||
it ('should disconnect & reconnect phone', async () => {
|
||||
const conn = new WAConnection ()
|
||||
conn.logger.level = 'debug'
|
||||
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 ()
|
||||
if (connected === expect) {
|
||||
conn.removeAllListeners ('connection-phone-change')
|
||||
resolve ()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
console.log ('disconnect your phone from the internet')
|
||||
await delay (10_000)
|
||||
console.log ('phone should be disconnected now, testing...')
|
||||
|
||||
const messagesPromise = Promise.all (
|
||||
[
|
||||
conn.loadMessages (testJid, 50),
|
||||
conn.getStatus (testJid),
|
||||
conn.getProfilePicture (testJid).catch (() => '')
|
||||
]
|
||||
)
|
||||
|
||||
await waitForEvent (false)
|
||||
|
||||
console.log ('reconnect your phone to the internet')
|
||||
await waitForEvent (true)
|
||||
|
||||
console.log ('reconnected successfully')
|
||||
|
||||
const final = await messagesPromise
|
||||
assert.ok (final)
|
||||
} finally {
|
||||
conn.close ()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user