From 2f1c894a34e574073c54a65e659a63aa73559617 Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sat, 5 Feb 2022 20:04:00 -0400 Subject: [PATCH] add tests for `takeoverOnConflict` (#1170) * add tests * fix module test, remove unused CryptoLib --- src/util/Injected.js | 1 - tests/client.js | 108 +++++++++++++++++++++++++++++-------------- 2 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/util/Injected.js b/src/util/Injected.js index 8e7b5db..06b7049 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -8,7 +8,6 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store = Object.assign({}, window.mR.findModule(m => m.default && m.default.Chat)[0].default); window.Store.AppState = window.mR.findModule('STREAM')[0].Socket; window.Store.Conn = window.mR.findModule('Conn')[0].Conn; - window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0]; window.Store.Wap = window.mR.findModule('queryLinkPreview')[0].default; window.Store.SendSeen = window.mR.findModule('sendSeen')[0]; window.Store.SendClear = window.mR.findModule('sendClear')[0]; diff --git a/tests/client.js b/tests/client.js index 0ad8e72..78c0852 100644 --- a/tests/client.js +++ b/tests/client.js @@ -7,7 +7,7 @@ const Contact = require('../src/structures/Contact'); const Message = require('../src/structures/Message'); const MessageMedia = require('../src/structures/MessageMedia'); const Location = require('../src/structures/Location'); -const { MessageTypes } = require('../src/util/Constants'); +const { MessageTypes, WAState } = require('../src/util/Constants'); const remoteId = helper.remoteId; @@ -145,6 +145,46 @@ describe('Client', function() { await client.destroy(); }); + + it('can take over if client was logged in somewhere else with takeoverOnConflict=true', async function() { + this.timeout(40000); + + const readyCallback1 = sinon.spy(); + const readyCallback2 = sinon.spy(); + const disconnectedCallback1 = sinon.spy(); + const disconnectedCallback2 = sinon.spy(); + + const client1 = helper.createClient({ + withSession: true, + options: { takeoverOnConflict: true, takeoverTimeoutMs: 5000 } + }); + const client2 = helper.createClient({withSession: true}); + + client1.on('ready', readyCallback1); + client2.on('ready', readyCallback2); + client1.on('disconnected', disconnectedCallback1); + client2.on('disconnected', disconnectedCallback2); + + await client1.initialize(); + expect(readyCallback1.called).to.equal(true); + expect(readyCallback2.called).to.equal(false); + expect(disconnectedCallback1.called).to.equal(false); + expect(disconnectedCallback2.called).to.equal(false); + + await client2.initialize(); + expect(readyCallback2.called).to.equal(true); + expect(disconnectedCallback1.called).to.equal(false); + expect(disconnectedCallback2.called).to.equal(false); + + // wait for takeoverTimeoutMs to kick in + await helper.sleep(5200); + expect(disconnectedCallback1.called).to.equal(false); + expect(disconnectedCallback2.called).to.equal(true); + expect(disconnectedCallback2.calledWith(WAState.CONFLICT)).to.equal(true); + + await client1.destroy(); + + }); }); describe('Authenticated', function() { @@ -171,46 +211,46 @@ describe('Client', function() { it('exposes all required WhatsApp Web internal models', async function() { const expectedModules = [ - 'Chat', - 'Msg', - 'Contact', - 'Conn', 'AppState', - 'CryptoLib', - 'Wap', - 'SendSeen', - 'SendClear', - 'SendDelete', - 'genId', - 'SendMessage', - 'MsgKey', - 'Invite', - 'OpaqueData', - 'MediaPrep', - 'MediaObject', - 'MediaUpload', - 'Cmd', - 'MediaTypes', - 'VCard', - 'UserConstructor', - 'Validators', - 'WidFactory', 'BlockContact', - 'GroupMetadata', - 'Sticker', - 'UploadUtils', - 'Label', + 'Call', + 'Chat', + 'Cmd', + 'Conn', + 'Contact', + 'DownloadManager', 'Features', + 'GroupMetadata', + 'Invite', + 'Label', + 'MediaObject', + 'MediaPrep', + 'MediaTypes', + 'MediaUpload', + 'Msg', + 'MsgKey', + 'OpaqueData', 'QueryOrder', 'QueryProduct', - 'DownloadManager' - ]; + 'SendClear', + 'SendDelete', + 'SendMessage', + 'SendSeen', + 'Sticker', + 'UploadUtils', + 'UserConstructor', + 'VCard', + 'Validators', + 'Wap', + 'WidFactory', + 'genId' + ]; - const loadedModules = await client.pupPage.evaluate(() => { - return Object.keys(window.Store); - }); + const loadedModules = await client.pupPage.evaluate((expectedModules) => { + return expectedModules.filter(m => Boolean(window.Store[m])); + }, expectedModules); - expect(loadedModules).to.include.members(expectedModules); + expect(loadedModules).to.have.members(expectedModules); }); });