Initial V3.0

This commit is contained in:
Adhiraj
2020-08-19 14:19:30 +05:30
parent 3825824d71
commit 95d2567e76
20 changed files with 1593 additions and 1027 deletions

View File

@@ -1,4 +1,4 @@
import { WAConnection, MessageLogLevel, MessageOptions, MessageType } from '../WAConnection/WAConnection'
import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds } from '../WAConnection/WAConnection'
import * as assert from 'assert'
import {promises as fs} from 'fs'
@@ -7,22 +7,29 @@ export const testJid = process.env.TEST_JID || '1234@s.whatsapp.net' // set TEST
export async function sendAndRetreiveMessage(conn: WAConnection, content, type: MessageType, options: MessageOptions = {}) {
const response = await conn.sendMessage(testJid, content, type, options)
const messages = await conn.loadConversation(testJid, 10, null, true)
const {messages} = await conn.loadMessages(testJid, 10)
const message = messages.find (m => m.key.id === response.key.id)
assert.ok(message)
const chat = conn.chats.get(testJid)
assert.ok (chat.messages.find(m => m.key.id === response.key.id))
assert.ok (chat.t >= (unixTimestampSeconds()-5) )
return message
}
export function WAConnectionTest(name: string, func: (conn: WAConnection) => void) {
export const WAConnectionTest = (name: string, func: (conn: WAConnection) => void) => (
describe(name, () => {
const conn = new WAConnection()
conn.logLevel = MessageLogLevel.info
before(async () => {
//conn.logLevel = MessageLogLevel.unhandled
const file = './auth_info.json'
await conn.connectSlim(file)
await conn.loadAuthInfo(file).connect()
await fs.writeFile(file, JSON.stringify(conn.base64EncodedAuthInfo(), null, '\t'))
})
after(() => conn.close())
func(conn)
})
}
)

View File

@@ -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)

View File

@@ -1,12 +1,18 @@
import { MessageType, GroupSettingChange, createTimeout, ChatModification, whatsappID } from '../WAConnection/WAConnection'
import { MessageType, GroupSettingChange, delay, ChatModification } from '../WAConnection/WAConnection'
import * as assert from 'assert'
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
import { WAConnectionTest, testJid } from './Common'
WAConnectionTest('Groups', (conn) => {
let gid: string
it('should create a group', async () => {
const response = await conn.groupCreate('Cool Test Group', [testJid])
assert.ok (conn.chats.get(response.gid))
const {chats} = await conn.loadChats(10, null)
assert.equal (chats[0].jid, response.gid) // first chat should be new group
gid = response.gid
console.log('created group: ' + JSON.stringify(response))
})
it('should retreive group invite code', async () => {
@@ -22,8 +28,18 @@ WAConnectionTest('Groups', (conn) => {
it('should update the group description', async () => {
const newDesc = 'Wow this was set from Baileys'
const waitForEvent = new Promise (resolve => {
conn.on ('group-description-update', ({jid, actor}) => {
if (jid === gid) {
assert.ok (actor, conn.user.id)
resolve ()
}
})
})
await conn.groupUpdateDescription (gid, newDesc)
await createTimeout (1000)
await waitForEvent
conn.removeAllListeners ('group-description-update')
const metadata = await conn.groupMetadata(gid)
assert.strictEqual(metadata.desc, newDesc)
@@ -32,39 +48,102 @@ WAConnectionTest('Groups', (conn) => {
await conn.sendMessage(gid, 'hello', MessageType.text)
})
it('should quote a message on the group', async () => {
const messages = await conn.loadConversation (gid, 20)
const {messages} = await conn.loadMessages (gid, 100)
const quotableMessage = messages.find (m => m.message)
assert.ok (quotableMessage, 'need at least one message')
const response = await conn.sendMessage(gid, 'hello', MessageType.extendedText, {quoted: messages[0]})
const messagesNew = await conn.loadConversation(gid, 10, null, true)
const message = messagesNew.find (m => m.key.id === response.key.id)?.message?.extendedTextMessage
const response = await conn.sendMessage(gid, 'hello', MessageType.extendedText, {quoted: quotableMessage})
const loaded = await conn.loadMessages(gid, 10)
const message = loaded.messages.find (m => m.key.id === response.key.id)?.message?.extendedTextMessage
assert.ok(message)
assert.equal (message.contextInfo.stanzaId, quotableMessage.key.id)
})
it('should update the subject', async () => {
const subject = 'V Cool Title'
const subject = 'Baileyz ' + Math.floor(Math.random()*5)
const waitForEvent = new Promise (resolve => {
conn.on ('chat-update', ({jid, title}) => {
if (jid === gid) {
assert.equal (title, subject)
resolve ()
}
})
})
await conn.groupUpdateSubject(gid, subject)
await waitForEvent
conn.removeAllListeners ('chat-update')
const metadata = await conn.groupMetadata(gid)
assert.strictEqual(metadata.subject, subject)
})
it('should update the group settings', async () => {
const waitForEvent = new Promise (resolve => {
conn.on ('group-settings-update', ({jid, announce}) => {
if (jid === gid) {
assert.equal (announce, 'true')
resolve ()
}
})
})
await conn.groupSettingChange (gid, GroupSettingChange.messageSend, true)
await createTimeout (5000)
await waitForEvent
conn.removeAllListeners ('group-settings-update')
await delay (2000)
await conn.groupSettingChange (gid, GroupSettingChange.settingsChange, true)
})
it('should remove someone from a group', async () => {
const waitForEvent = new Promise (resolve => {
conn.on ('group-participants-remove', ({jid, participants}) => {
if (jid === gid) {
assert.equal (participants[0], testJid)
resolve ()
}
})
})
await conn.groupRemove(gid, [testJid])
await waitForEvent
conn.removeAllListeners ('group-participants-remove')
})
it('should leave the group', async () => {
const waitForEvent = new Promise (resolve => {
conn.on ('chat-update', ({jid, read_only}) => {
if (jid === gid) {
assert.equal (read_only, 'true')
resolve ()
}
})
})
await conn.groupLeave(gid)
await waitForEvent
conn.removeAllListeners ('chat-update')
await conn.groupMetadataMinimal (gid)
})
it('should archive the group', async () => {
const waitForEvent = new Promise (resolve => {
conn.on ('chat-update', ({jid, archive}) => {
if (jid === gid) {
assert.equal (archive, 'true')
resolve ()
}
})
})
await conn.modifyChat(gid, ChatModification.archive)
await waitForEvent
conn.removeAllListeners ('chat-update')
})
it('should delete the group', async () => {
const waitForEvent = new Promise (resolve => {
conn.on ('chat-update', (chat) => {
if (chat.jid === gid) {
assert.equal (chat['delete'], 'true')
resolve ()
}
})
})
await conn.deleteChat(gid)
await waitForEvent
conn.removeAllListeners ('chat-update')
})
})

View File

@@ -1,18 +1,18 @@
import { MessageType, Mimetype, createTimeout } from '../WAConnection/WAConnection'
import { MessageType, Mimetype, delay, promiseTimeout, WAMessage, WA_MESSAGE_STATUS_TYPE } from '../WAConnection/WAConnection'
import {promises as fs} from 'fs'
import * as assert from 'assert'
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
WAConnectionTest('Messages', (conn) => {
it('should send a text message', async () => {
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
assert.strictEqual(message.message.conversation, 'hello fren')
//const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
//assert.strictEqual(message.message.conversation || message.message.extendedTextMessage?.text, 'hello fren')
})
it('should forward a message', async () => {
let messages = await conn.loadConversation (testJid, 1)
let messages = await conn.loadMessages (testJid, 1)
await conn.forwardMessage (testJid, messages[0], true)
messages = await conn.loadConversation (testJid, 1)
messages = await conn.loadMessages (testJid, 1)
const message = messages[0]
const content = message.message[ Object.keys(message.message)[0] ]
assert.equal (content?.contextInfo?.isForwarded, true)
@@ -28,7 +28,7 @@ WAConnectionTest('Messages', (conn) => {
assert.ok (received.jpegThumbnail)
})
it('should quote a message', async () => {
const messages = await conn.loadConversation(testJid, 2)
const messages = await conn.loadMessages(testJid, 2)
const message = await sendAndRetreiveMessage(conn, 'hello fren 2', MessageType.extendedText, {
quoted: messages[0],
})
@@ -48,21 +48,36 @@ WAConnectionTest('Messages', (conn) => {
//const message2 = await sendAndRetreiveMessage (conn, 'this is a quote', MessageType.extendedText)
})
it('should send an image & quote', async () => {
const messages = await conn.loadConversation(testJid, 1)
const messages = await conn.loadMessages(testJid, 1)
const content = await fs.readFile('./Media/meme.jpeg')
const message = await sendAndRetreiveMessage(conn, content, MessageType.image, { quoted: messages[0] })
await conn.downloadMediaMessage(message) // check for successful decoding
assert.strictEqual(message.message.imageMessage.contextInfo.stanzaId, messages[0].key.id)
})
it('should send a text message & delete it', async () => {
it('should send a message & delete it', async () => {
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
await createTimeout (2000)
await delay (2000)
await conn.deleteMessage (testJid, message.key)
})
it('should clear the most recent message', async () => {
const messages = await conn.loadConversation (testJid, 1)
await createTimeout (2000)
const messages = await conn.loadMessages (testJid, 1)
await delay (2000)
await conn.clearMessage (messages[0].key)
})
})
})
WAConnectionTest('Message Events', (conn) => {
it('should deliver a message', async () => {
const waitForUpdate =
promiseTimeout(15000, resolve => {
conn.on('message-update', message => {
if (message.key.id === response.key.id) {
resolve(message)
}
})
}) as Promise<WAMessage>
const response = await conn.sendMessage(testJid, 'My Name Jeff', MessageType.text)
const m = await waitForUpdate
assert.ok (m.status >= WA_MESSAGE_STATUS_TYPE.DELIVERY_ACK)
})
})

View File

@@ -1,20 +1,9 @@
import { MessageType, Presence, ChatModification, promiseTimeout, createTimeout } from '../WAConnection/WAConnection'
import { Presence, ChatModification, delay } from '../WAConnection/WAConnection'
import {promises as fs} from 'fs'
import * as assert from 'assert'
import fetch from 'node-fetch'
import { WAConnectionTest, testJid } from './Common'
WAConnectionTest('Presence', (conn) => {
it('should update presence', async () => {
const presences = Object.values(Presence)
for (const i in presences) {
const response = await conn.updatePresence(testJid, presences[i])
assert.strictEqual(response.status, 200)
await createTimeout(1500)
}
})
})
WAConnectionTest('Misc', (conn) => {
it('should tell if someone has an account on WhatsApp', async () => {
const response = await conn.isOnWhatsApp(testJid)
@@ -30,16 +19,28 @@ WAConnectionTest('Misc', (conn) => {
it('should update status', async () => {
const newStatus = 'v cool status'
const waitForEvent = new Promise (resolve => {
conn.on ('user-status-update', ({jid, status}) => {
if (jid === conn.user.id) {
assert.equal (status, newStatus)
conn.removeAllListeners ('user-status-update')
resolve ()
}
})
})
const response = await conn.getStatus()
assert.strictEqual(typeof response.status, 'string')
await createTimeout (1000)
await delay (1000)
await conn.setStatus (newStatus)
const response2 = await conn.getStatus()
assert.equal (response2.status, newStatus)
await createTimeout (1000)
await waitForEvent
await delay (1000)
await conn.setStatus (response.status) // update back
})
@@ -47,18 +48,18 @@ WAConnectionTest('Misc', (conn) => {
await conn.getStories()
})
it('should change the profile picture', async () => {
await createTimeout (5000)
await delay (5000)
const ppUrl = await conn.getProfilePicture(conn.userMetaData.id)
const ppUrl = await conn.getProfilePicture(conn.user.id)
const fetched = await fetch(ppUrl, { headers: { Origin: 'https://web.whatsapp.com' } })
const buff = await fetched.buffer ()
const newPP = await fs.readFile ('./Media/cat.jpeg')
const response = await conn.updateProfilePicture (conn.userMetaData.id, newPP)
const response = await conn.updateProfilePicture (conn.user.id, newPP)
await createTimeout (10000)
await delay (10000)
await conn.updateProfilePicture (conn.userMetaData.id, buff) // revert back
await conn.updateProfilePicture (conn.user.id, buff) // revert back
})
it('should return the profile picture', async () => {
const response = await conn.getProfilePicture(testJid)
@@ -70,22 +71,32 @@ WAConnectionTest('Misc', (conn) => {
assert.ok(response)
})
it('should mark a chat unread', async () => {
await conn.sendReadReceipt(testJid, null, 'unread')
const waitForEvent = new Promise (resolve => {
conn.on ('chat-update', ({jid, count}) => {
if (jid === testJid) {
assert.ok (count < 0)
conn.removeAllListeners ('chat-update')
resolve ()
}
})
})
await conn.sendReadReceipt(testJid, null, -2)
await waitForEvent
})
it('should archive & unarchive', async () => {
await conn.modifyChat (testJid, ChatModification.archive)
await createTimeout (2000)
await delay (2000)
await conn.modifyChat (testJid, ChatModification.unarchive)
})
it('should pin & unpin a chat', async () => {
const response = await conn.modifyChat (testJid, ChatModification.pin)
await createTimeout (2000)
await delay (2000)
await conn.modifyChat (testJid, ChatModification.unpin, {stamp: response.stamp})
})
it('should mute & unmute a chat', async () => {
const mutedate = new Date (new Date().getTime() + 8*60*60*1000) // 8 hours in the future
await conn.modifyChat (testJid, ChatModification.mute, {stamp: mutedate})
await createTimeout (2000)
await delay (2000)
await conn.modifyChat (testJid, ChatModification.unmute, {stamp: mutedate})
})
it('should return search results', async () => {
@@ -96,18 +107,14 @@ WAConnectionTest('Misc', (conn) => {
assert.ok (response.messages.length >= 0)
}
})
})
WAConnectionTest('Events', (conn) => {
it('should deliver a message', async () => {
const waitForUpdate = () =>
new Promise((resolve) => {
conn.setOnMessageStatusChange((update) => {
if (update.ids.includes(response.key.id)) {
resolve()
}
})
})
const response = await conn.sendMessage(testJid, 'My Name Jeff', MessageType.text)
await promiseTimeout(15000, waitForUpdate())
it('should update presence', async () => {
const presences = Object.values(Presence)
for (const i in presences) {
const response = await conn.updatePresence(testJid, presences[i])
assert.strictEqual(response.status, 200)
await delay(1500)
}
})
})
})