Track updated contacts

This commit is contained in:
Adhiraj Singh
2020-12-12 14:06:46 +05:30
parent eb7b8506a9
commit b72e2dfcf9
4 changed files with 53 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import * as assert from 'assert'
import {WAConnection} from '../WAConnection/WAConnection'
import { AuthenticationCredentialsBase64, BaileysError, ReconnectMode, DisconnectReason, WAChat } from '../WAConnection/Constants'
import { AuthenticationCredentialsBase64, BaileysError, ReconnectMode, DisconnectReason, WAChat, WAContact } from '../WAConnection/Constants'
import { delay } from '../WAConnection/Utils'
import { assertChatDBIntegrity, makeConnection, testJid } from './Common'
@@ -285,7 +285,7 @@ describe ('Reconnects', () => {
})
describe ('Pending Requests', () => {
it ('should correctly send updates', async () => {
it ('should correctly send updates for chats', async () => {
const conn = makeConnection ()
conn.pendingRequestTimeoutMs = null
conn.loadAuthInfo('./auth_info.json')
@@ -315,6 +315,42 @@ describe ('Pending Requests', () => {
conn.close ()
})
it ('should correctly send updates for contacts', async () => {
const conn = makeConnection ()
conn.pendingRequestTimeoutMs = null
conn.loadAuthInfo('./auth_info.json')
const task: any = new Promise(resolve => conn.once('contacts-received', resolve))
await conn.connect ()
const initialResult = await task
assert.strictEqual(
initialResult.updatedContacts.length,
Object.keys(conn.contacts).length
)
conn.close ()
const [jid] = Object.keys(conn.contacts)
const oldContact = conn.contacts[jid]
oldContact.name = 'Lol'
oldContact.index = 'L'
const promise = new Promise(resolve => conn.once('contacts-received', resolve))
const result = await conn.connect ()
assert.ok (!result.newConnection)
const {updatedContacts} = await promise as { updatedContacts: Partial<WAContact>[] }
const contact = updatedContacts.find (c => c.jid === jid)
assert.ok (contact)
assert.ok ('name' in contact)
assert.strictEqual (Object.keys(contact).length, 3)
assert.strictEqual (Object.keys(updatedContacts).length, 1)
conn.close ()
})
it('should queue requests when closed', async () => {
const conn = makeConnection ()
//conn.pendingRequestTimeoutMs = null

View File

@@ -203,7 +203,7 @@ WAConnectionTest('Misc', conn => {
if (!chat) {
// wait for chats
await new Promise(resolve => (
conn.once('chats-received', () => resolve())
conn.once('chats-received', resolve)
))
chat = conn.chats.get(testJid)
}