mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Updates
- removed timeout, use maxIdleTimeMs - made messages a keyedDB to better utitlize message cache - possible fix for group ID bug
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds, toNumber } from '../WAConnection/WAConnection'
|
||||
import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds, toNumber, GET_MESSAGE_ID, WA_MESSAGE_KEY } from '../WAConnection/WAConnection'
|
||||
import * as assert from 'assert'
|
||||
import {promises as fs} from 'fs'
|
||||
|
||||
@@ -13,8 +13,7 @@ export async function sendAndRetreiveMessage(conn: WAConnection, content, type:
|
||||
|
||||
const chat = conn.chats.get(testJid)
|
||||
|
||||
assertChatDBIntegrity (conn)
|
||||
assert.ok (chat.messages.find(m => m.key.id === response.key.id))
|
||||
assert.ok (chat.messages.get(GET_MESSAGE_ID(message.key)))
|
||||
assert.ok (chat.t >= (unixTimestampSeconds()-5) )
|
||||
return message
|
||||
}
|
||||
@@ -37,13 +36,13 @@ export const WAConnectionTest = (name: string, func: (conn: WAConnection) => voi
|
||||
export const assertChatDBIntegrity = (conn: WAConnection) => {
|
||||
conn.chats.all ().forEach (chat => (
|
||||
assert.deepEqual (
|
||||
[...chat.messages].sort ((m1, m2) => toNumber(m1.messageTimestamp)-toNumber(m2.messageTimestamp)),
|
||||
chat.messages
|
||||
[...chat.messages.all()].sort ((m1, m2) => WA_MESSAGE_KEY(m1)-WA_MESSAGE_KEY(m2)),
|
||||
chat.messages.all()
|
||||
)
|
||||
))
|
||||
conn.chats.all ().forEach (chat => (
|
||||
assert.deepEqual (
|
||||
chat.messages.filter (m => chat.messages.filter(m1 => m1.key.id === m.key.id).length > 1),
|
||||
chat.messages.all().filter (m => chat.messages.all().filter(m1 => m1.key.id === m.key.id).length > 1),
|
||||
[]
|
||||
)
|
||||
))
|
||||
|
||||
@@ -7,7 +7,7 @@ import { assertChatDBIntegrity } from './Common'
|
||||
describe('QR Generation', () => {
|
||||
it('should generate QR', async () => {
|
||||
const conn = new WAConnection()
|
||||
conn.regenerateQRIntervalMs = 5000
|
||||
conn.connectOptions.regenerateQRIntervalMs = 5000
|
||||
|
||||
let calledQR = 0
|
||||
conn.removeAllListeners ('qr')
|
||||
@@ -45,7 +45,6 @@ describe('Test Connect', () => {
|
||||
})
|
||||
it('should reconnect', async () => {
|
||||
const conn = new WAConnection()
|
||||
conn.connectOptions.timeoutMs = 20*1000
|
||||
|
||||
await conn.loadAuthInfo (auth).connect ()
|
||||
assert.ok(conn.user)
|
||||
@@ -131,7 +130,6 @@ describe ('Reconnects', () => {
|
||||
it('should disrupt connect loop', async () => {
|
||||
const conn = new WAConnection()
|
||||
conn.autoReconnect = ReconnectMode.onAllErrors
|
||||
conn.connectOptions.timeoutMs = 20000
|
||||
conn.loadAuthInfo ('./auth_info.json')
|
||||
|
||||
let timeout = 1000
|
||||
@@ -231,7 +229,6 @@ describe ('Reconnects', () => {
|
||||
describe ('Pending Requests', () => {
|
||||
it ('should correctly send updates', async () => {
|
||||
const conn = new WAConnection ()
|
||||
conn.connectOptions.timeoutMs = 20*1000
|
||||
conn.pendingRequestTimeoutMs = null
|
||||
|
||||
conn.loadAuthInfo('./auth_info.json')
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { MessageType, Mimetype, delay, promiseTimeout, WA_MESSAGE_STATUS_TYPE, WAMessageStatusUpdate } from '../WAConnection/WAConnection'
|
||||
import { MessageType, Mimetype, delay, promiseTimeout, WA_MESSAGE_STATUS_TYPE, WAMessageStatusUpdate, MessageOptions, toNumber } from '../WAConnection/WAConnection'
|
||||
import {promises as fs} from 'fs'
|
||||
import * as assert from 'assert'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage, assertChatDBIntegrity } from './Common'
|
||||
|
||||
WAConnectionTest('Messages', conn => {
|
||||
|
||||
afterEach (() => assertChatDBIntegrity (conn))
|
||||
|
||||
it('should send a text message', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
|
||||
assert.strictEqual(message.message.conversation || message.message.extendedTextMessage?.text, 'hello fren')
|
||||
@@ -160,13 +163,40 @@ WAConnectionTest('Messages', conn => {
|
||||
}
|
||||
})
|
||||
})
|
||||
it('should not duplicate messages', async () => {
|
||||
it('should maintain message integrity', async () => {
|
||||
// loading twice does not alter the results
|
||||
const results = await Promise.all ([
|
||||
conn.loadMessages (testJid, 50),
|
||||
conn.loadMessages (testJid, 50)
|
||||
])
|
||||
assert.deepEqual (results[0].messages, results[1].messages)
|
||||
assert.equal (results[0].messages.length, results[1].messages.length)
|
||||
for (let i = 0; i < results[1].messages.length;i++) {
|
||||
assert.deepEqual (results[0].messages[i], results[1].messages[i], `failed equal at ${i}`)
|
||||
}
|
||||
assert.ok (results[0].messages.length <= 50)
|
||||
|
||||
// check if messages match server
|
||||
let msgs = await conn.fetchMessagesFromWA (testJid, 50)
|
||||
for (let i = 0; i < results[1].messages.length;i++) {
|
||||
assert.deepEqual (results[0].messages[i].key, msgs[i].key, `failed equal at ${i}`)
|
||||
}
|
||||
// check with some arbitary cursors
|
||||
let cursor = results[0].messages.slice(-1)[0].key
|
||||
|
||||
msgs = await conn.fetchMessagesFromWA (testJid, 20, cursor)
|
||||
let {messages} = await conn.loadMessages (testJid, 20, cursor)
|
||||
for (let i = 0; i < messages.length;i++) {
|
||||
assert.deepEqual (messages[i].key, msgs[i].key, `failed equal at ${i}`)
|
||||
}
|
||||
|
||||
cursor = results[0].messages[2].key
|
||||
|
||||
msgs = await conn.fetchMessagesFromWA (testJid, 20, cursor)
|
||||
messages = (await conn.loadMessages (testJid, 20, cursor)).messages
|
||||
for (let i = 0; i < messages.length;i++) {
|
||||
assert.deepEqual (messages[i].key, msgs[i].key, `failed equal at ${i}`)
|
||||
}
|
||||
|
||||
})
|
||||
it('should deliver a message', async () => {
|
||||
const waitForUpdate =
|
||||
|
||||
Reference in New Issue
Block a user