add tests for takeoverOnConflict (#1170)

* add tests

* fix module test, remove unused CryptoLib
This commit is contained in:
Pedro S. Lopez
2022-02-05 20:04:00 -04:00
committed by GitHub
parent 32e47d818a
commit 2f1c894a34
2 changed files with 74 additions and 35 deletions

View File

@@ -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];

View File

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