mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
remove all files
This commit is contained in:
@@ -39,7 +39,7 @@ export async function sendAndRetrieveMessage(conn: WAConnection, content, type:
|
||||
const chat = conn.chats.get(recipientJid)
|
||||
|
||||
assert.ok (chat.messages.get(GET_MESSAGE_ID(message.key)))
|
||||
assert.ok (chat.t >= (unixTimestampSeconds()-5), `expected: ${chat.t} > ${(unixTimestampSeconds()-5)}`)
|
||||
assert.ok (chat.t >= (unixTimestampSeconds()-5) )
|
||||
return message
|
||||
}
|
||||
export const WAConnectionTest = (name: string, func: (conn: WAConnection) => void) => (
|
||||
|
||||
@@ -72,7 +72,7 @@ WAConnectionTest('Messages', conn => {
|
||||
assert.ok (message.message.audioMessage.seconds > 0)
|
||||
await conn.downloadAndSaveMediaMessage(message,'./Media/received_aud')
|
||||
})
|
||||
it('should send a voice note', async () => {
|
||||
it('should send an audio as a voice note', async () => {
|
||||
const content = await fs.readFile('./Media/sonata.mp3')
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.audio, { mimetype: Mimetype.mp4Audio, ptt: true })
|
||||
|
||||
|
||||
95
src/Tests/test.binary.ts
Normal file
95
src/Tests/test.binary.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import BinaryNode from '../BinaryNode'
|
||||
|
||||
describe('Binary Coding Tests', () => {
|
||||
|
||||
const TEST_VECTORS: [string, BinaryNode][] = [
|
||||
[
|
||||
'f806092f5a0a10f804f80234fc6c0a350a1b39313735323938373131313740732e77686174736170702e6e657410011a143345423030393637354537454433374141424632122b0a292a7069616e6f20726f6f6d2074696d696e6773206172653a2a0a20363a3030414d2d31323a3030414d18b3faa7f3052003f80234fc4c0a410a1b39313735323938373131313740732e77686174736170702e6e657410001a20304643454335333330463634393239433645394132434646443242433845414418bdfaa7f305c00101f80234fc930a350a1b39313735323938373131313740732e77686174736170702e6e657410011a14334542303033433742353339414644303937353312520a50536f727279206672656e2c204920636f756c646e277420756e6465727374616e6420274c69627261272e2054797065202768656c702720746f206b6e6f77207768617420616c6c20492063616e20646f18c1faa7f3052003f80234fc540a410a1b39313735323938373131313740732e77686174736170702e6e657410001a20413132333042384436423041314437393345433241453245413043313638443812090a076c69627261727918c2faa7f305',
|
||||
new BinaryNode(
|
||||
'action',
|
||||
{ last: 'true', add: 'before' },
|
||||
[
|
||||
new BinaryNode(
|
||||
'message',
|
||||
{},
|
||||
{
|
||||
key: { remoteJid: '917529871117@s.whatsapp.net', fromMe: true, id: '3EB009675E7ED37AABF2' },
|
||||
message: { conversation: '*piano room timings are:*\n 6:00AM-12:00AM' },
|
||||
messageTimestamp: '1584004403',
|
||||
status: 'DELIVERY_ACK',
|
||||
} as any
|
||||
),
|
||||
new BinaryNode(
|
||||
'message',
|
||||
{},
|
||||
{
|
||||
key: {
|
||||
remoteJid: '917529871117@s.whatsapp.net',
|
||||
fromMe: false,
|
||||
id: '0FCEC5330F64929C6E9A2CFFD2BC8EAD',
|
||||
},
|
||||
messageTimestamp: '1584004413',
|
||||
messageStubType: 'REVOKE',
|
||||
} as any
|
||||
),
|
||||
new BinaryNode(
|
||||
'message',
|
||||
{},
|
||||
{
|
||||
key: { remoteJid: '917529871117@s.whatsapp.net', fromMe: true, id: '3EB003C7B539AFD09753' },
|
||||
message: {
|
||||
conversation:
|
||||
"Sorry fren, I couldn't understand 'Libra'. Type 'help' to know what all I can do",
|
||||
},
|
||||
messageTimestamp: '1584004417',
|
||||
status: 'DELIVERY_ACK',
|
||||
} as any
|
||||
),
|
||||
new BinaryNode(
|
||||
'message',
|
||||
{},
|
||||
{
|
||||
key: {
|
||||
remoteJid: '917529871117@s.whatsapp.net',
|
||||
fromMe: false,
|
||||
id: 'A1230B8D6B0A1D793EC2AE2EA0C168D8',
|
||||
},
|
||||
message: { conversation: 'library' },
|
||||
messageTimestamp: '1584004418',
|
||||
} as any
|
||||
),
|
||||
]
|
||||
)
|
||||
],
|
||||
[
|
||||
'f8063f2dfafc0831323334353637385027fc0431323334f801f80228fc0701020304050607',
|
||||
new BinaryNode(
|
||||
'picture',
|
||||
{jid: '12345678@s.whatsapp.net', id: '1234'},
|
||||
[
|
||||
new BinaryNode(
|
||||
'image',
|
||||
{},
|
||||
Buffer.from([1,2,3,4,5,6,7])
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
]
|
||||
it('should encode/decode strings', () => {
|
||||
for(const [input, output] of TEST_VECTORS) {
|
||||
const buff = Buffer.from(input, 'hex')
|
||||
const node = BinaryNode.from(buff)
|
||||
expect(
|
||||
JSON.parse(JSON.stringify(node))
|
||||
).toStrictEqual(
|
||||
JSON.parse(JSON.stringify(output))
|
||||
)
|
||||
expect(
|
||||
node.toBuffer().toString('hex')
|
||||
).toStrictEqual(
|
||||
input
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
200
src/Tests/test.connect.ts
Normal file
200
src/Tests/test.connect.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import Boom from 'boom'
|
||||
import P from 'pino'
|
||||
import BinaryNode from '../BinaryNode'
|
||||
import makeConnection, { Connection, DisconnectReason } from '../makeConnection'
|
||||
import { delay } from '../WAConnection/Utils'
|
||||
|
||||
describe('QR Generation', () => {
|
||||
it('should generate QR', async () => {
|
||||
const QR_GENS = 1
|
||||
const {ev, open} = makeConnection({
|
||||
maxRetries: 0,
|
||||
maxQRCodes: QR_GENS,
|
||||
logger: P({ level: 'trace' })
|
||||
})
|
||||
let calledQR = 0
|
||||
ev.removeAllListeners('qr')
|
||||
ev.on('state.update', ({ qr }) => {
|
||||
if(qr) calledQR += 1
|
||||
})
|
||||
|
||||
await expect(open()).rejects.toThrowError('Too many QR codes')
|
||||
expect(
|
||||
Object.keys(ev.eventNames()).filter(key => key.startsWith('TAG:'))
|
||||
).toHaveLength(0)
|
||||
expect(calledQR).toBeGreaterThanOrEqual(QR_GENS)
|
||||
}, 60_000)
|
||||
})
|
||||
|
||||
describe('Test Connect', () => {
|
||||
const logger = P({ level: 'trace' })
|
||||
it('should connect', async () => {
|
||||
|
||||
logger.info('please be ready to scan with your phone')
|
||||
|
||||
const conn = makeConnection({
|
||||
logger,
|
||||
printQRInTerminal: true
|
||||
})
|
||||
await conn.open()
|
||||
const { user, isNewLogin } = await conn.getState()
|
||||
expect(user).toHaveProperty('jid')
|
||||
expect(user).toHaveProperty('name')
|
||||
expect(isNewLogin).toBe(true)
|
||||
|
||||
conn.close()
|
||||
}, 65_000)
|
||||
|
||||
it('should restore session', async () => {
|
||||
const conn = makeConnection({
|
||||
printQRInTerminal: true,
|
||||
logger,
|
||||
})
|
||||
await conn.open()
|
||||
conn.close()
|
||||
|
||||
await delay(2500)
|
||||
|
||||
await conn.open()
|
||||
const { user, isNewLogin, qr } = await conn.getState()
|
||||
expect(user).toHaveProperty('jid')
|
||||
expect(user).toHaveProperty('name')
|
||||
expect(isNewLogin).toBe(false)
|
||||
expect(qr).toBe(undefined)
|
||||
|
||||
conn.close()
|
||||
}, 65_000)
|
||||
|
||||
it('should logout', async () => {
|
||||
let conn = makeConnection({
|
||||
printQRInTerminal: true,
|
||||
logger,
|
||||
})
|
||||
await conn.open()
|
||||
const { user, qr } = await conn.getState()
|
||||
expect(user).toHaveProperty('jid')
|
||||
expect(user).toHaveProperty('name')
|
||||
expect(qr).toBe(undefined)
|
||||
|
||||
const credentials = conn.getAuthInfo()
|
||||
await conn.logout()
|
||||
|
||||
conn = makeConnection({
|
||||
credentials,
|
||||
logger
|
||||
})
|
||||
await expect(conn.open()).rejects.toThrowError('Unexpected error in login')
|
||||
}, 65_000)
|
||||
})
|
||||
|
||||
describe ('Reconnects', () => {
|
||||
const verifyConnectionOpen = async (conn: Connection) => {
|
||||
expect((await conn.getState()).user).toBeDefined()
|
||||
let failed = false
|
||||
// check that the connection stays open
|
||||
conn.ev.on('state.update', ({ connection, lastDisconnect }) => {
|
||||
if(connection === 'close' && !!lastDisconnect.error) {
|
||||
failed = true
|
||||
}
|
||||
})
|
||||
await delay (60*1000)
|
||||
conn.close ()
|
||||
|
||||
expect(failed).toBe(false)
|
||||
}
|
||||
it('should dispose correctly on bad_session', async () => {
|
||||
const conn = makeConnection({
|
||||
reconnectMode: 'on-any-error',
|
||||
credentials: './auth_info.json',
|
||||
maxRetries: 2,
|
||||
connectCooldownMs: 500
|
||||
})
|
||||
let gotClose0 = false
|
||||
let gotClose1 = false
|
||||
|
||||
const openPromise = conn.open()
|
||||
|
||||
conn.getSocket().ev.once('ws-close', () => {
|
||||
gotClose0 = true
|
||||
})
|
||||
conn.ev.on('state.update', ({ lastDisconnect }) => {
|
||||
//@ts-ignore
|
||||
if(lastDisconnect?.error?.output?.statusCode === DisconnectReason.badSession) {
|
||||
gotClose1 = true
|
||||
}
|
||||
})
|
||||
setTimeout (() => conn.getSocket().ws.emit ('message', Buffer.from('some-tag,sdjjij1jo2ejo1je')), 1500)
|
||||
await openPromise
|
||||
|
||||
console.log('opened connection')
|
||||
|
||||
await delay(1000)
|
||||
conn.getSocket().ws.emit ('message', Buffer.from('some-tag,sdjjij1jo2ejo1je'))
|
||||
|
||||
await delay(2000)
|
||||
await conn.waitForConnection()
|
||||
|
||||
conn.close()
|
||||
|
||||
expect(gotClose0).toBe(true)
|
||||
expect(gotClose1).toBe(true)
|
||||
}, 20_000)
|
||||
/**
|
||||
* the idea is to test closing the connection at multiple points in the connection
|
||||
* and see if the library cleans up resources correctly
|
||||
*/
|
||||
it('should cleanup correctly', async () => {
|
||||
const conn = makeConnection({
|
||||
reconnectMode: 'on-any-error',
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
let timeoutMs = 100
|
||||
while (true) {
|
||||
let tmout = setTimeout (() => {
|
||||
conn.close()
|
||||
}, timeoutMs)
|
||||
try {
|
||||
await conn.open()
|
||||
clearTimeout (tmout)
|
||||
break
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
// exponentially increase the timeout disconnect
|
||||
timeoutMs *= 2
|
||||
}
|
||||
await verifyConnectionOpen(conn)
|
||||
}, 120_000)
|
||||
/**
|
||||
* the idea is to test closing the connection at multiple points in the connection
|
||||
* and see if the library cleans up resources correctly
|
||||
*/
|
||||
it('should disrupt connect loop', async () => {
|
||||
const conn = makeConnection({
|
||||
reconnectMode: 'on-any-error',
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
|
||||
let timeout = 1000
|
||||
let tmout
|
||||
const endConnection = async () => {
|
||||
while (!conn.getSocket()) {
|
||||
await delay(100)
|
||||
}
|
||||
conn.getSocket().end(Boom.preconditionRequired('conn close'))
|
||||
|
||||
while (conn.getSocket()) {
|
||||
await delay(100)
|
||||
}
|
||||
|
||||
timeout *= 2
|
||||
tmout = setTimeout (endConnection, timeout)
|
||||
}
|
||||
tmout = setTimeout (endConnection, timeout)
|
||||
|
||||
await conn.open()
|
||||
clearTimeout (tmout)
|
||||
|
||||
await verifyConnectionOpen(conn)
|
||||
}, 120_000)
|
||||
})
|
||||
165
src/Tests/test.queries.ts
Normal file
165
src/Tests/test.queries.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import BinaryNode from '../BinaryNode'
|
||||
import makeConnection from '../makeConnection'
|
||||
import { delay } from '../WAConnection/Utils'
|
||||
|
||||
describe('Queries', () => {
|
||||
/*it ('should correctly send updates for chats', async () => {
|
||||
const conn = makeConnection({
|
||||
pendingRequestTimeoutMs: undefined,
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
const task = new Promise(resolve => conn.once('chats-received', resolve))
|
||||
await conn.connect ()
|
||||
await task
|
||||
|
||||
conn.close ()
|
||||
|
||||
const oldChat = conn.chats.all()[0]
|
||||
oldChat.archive = 'true' // mark the first chat as archived
|
||||
oldChat.modify_tag = '1234' // change modify tag to detect change
|
||||
|
||||
const promise = new Promise(resolve => conn.once('chats-update', resolve))
|
||||
|
||||
const result = await conn.connect ()
|
||||
assert.ok (!result.newConnection)
|
||||
|
||||
const chats = await promise as Partial<WAChat>[]
|
||||
const chat = chats.find (c => c.jid === oldChat.jid)
|
||||
assert.ok (chat)
|
||||
|
||||
assert.ok ('archive' in chat)
|
||||
assert.strictEqual (Object.keys(chat).length, 3)
|
||||
assert.strictEqual (Object.keys(chats).length, 1)
|
||||
|
||||
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({
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
await conn.open()
|
||||
await delay(2000)
|
||||
|
||||
conn.close()
|
||||
const { user: { jid } } = await conn.getState()
|
||||
const task: Promise<any> = conn.query({
|
||||
json: ['query', 'Status', jid]
|
||||
})
|
||||
|
||||
await delay(2000)
|
||||
|
||||
conn.open()
|
||||
const json = await task
|
||||
|
||||
expect(json.status).toBeDefined()
|
||||
|
||||
conn.close()
|
||||
}, 65_000)
|
||||
|
||||
it('[MANUAL] should recieve query response after phone disconnect', async () => {
|
||||
const conn = makeConnection ({
|
||||
printQRInTerminal: true,
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
await conn.open()
|
||||
const { phoneConnected } = await conn.getState()
|
||||
expect(phoneConnected).toBe(true)
|
||||
|
||||
try {
|
||||
const waitForEvent = expect => new Promise (resolve => {
|
||||
conn.ev.on('state.update', ({phoneConnected}) => {
|
||||
if (phoneConnected === expect) {
|
||||
conn.ev.removeAllListeners('state.update')
|
||||
resolve(undefined)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
console.log('disconnect your phone from the internet')
|
||||
await delay(10_000)
|
||||
console.log('phone should be disconnected now, testing...')
|
||||
|
||||
const query = conn.query({
|
||||
json: new BinaryNode(
|
||||
'query',
|
||||
{
|
||||
epoch: conn.getSocket().currentEpoch().toString(),
|
||||
type: 'message',
|
||||
jid: '1234@s.whatsapp.net',
|
||||
kind: 'before',
|
||||
count: '10',
|
||||
}
|
||||
),
|
||||
requiresPhoneConnection: true,
|
||||
expect200: false
|
||||
})
|
||||
await waitForEvent(false)
|
||||
|
||||
console.log('reconnect your phone to the internet')
|
||||
await waitForEvent(true)
|
||||
|
||||
console.log('reconnected successfully')
|
||||
|
||||
await expect(query).resolves.toBeDefined()
|
||||
} finally {
|
||||
conn.close()
|
||||
}
|
||||
}, 65_000)
|
||||
|
||||
it('should re-execute query on connection closed error', async () => {
|
||||
const conn = makeConnection({
|
||||
credentials: './auth_info.json'
|
||||
})
|
||||
await conn.open()
|
||||
const { user: { jid } } = await conn.getState()
|
||||
const task: Promise<any> = conn.query({ json: ['query', 'Status', jid], waitForOpen: true })
|
||||
|
||||
await delay(20)
|
||||
// fake cancel the connection
|
||||
conn.getSocket().ev.emit('message', '1234,["Pong",false]')
|
||||
|
||||
await delay(2000)
|
||||
|
||||
const json = await task
|
||||
|
||||
expect(json.status).toBeDefined()
|
||||
|
||||
conn.close()
|
||||
}, 65_000)
|
||||
})
|
||||
Reference in New Issue
Block a user