chore(tests): add Chat model tests (#784)

* add search and status tests

* reset to previous status in test

* move version to its own test

* add tests for Chat model

* keep hooks at the top

* run tests recursively in default script
This commit is contained in:
Pedro S. Lopez
2022-02-05 22:05:42 -04:00
committed by GitHub
parent 2f1c894a34
commit f149516d0d
5 changed files with 246 additions and 4 deletions

View File

@@ -5,7 +5,8 @@
"main": "./index.js", "main": "./index.js",
"typings": "./index.d.ts", "typings": "./index.d.ts",
"scripts": { "scripts": {
"test": "mocha tests", "test": "mocha tests --recursive",
"test-single": "mocha",
"shell": "node --experimental-repl-await ./shell.js", "shell": "node --experimental-repl-await ./shell.js",
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose" "generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose"
}, },

View File

@@ -729,7 +729,7 @@ class Client extends EventEmitter {
return await this.pupPage.evaluate(async chatId => { return await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId); let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.archiveChat(chat, true); await window.Store.Cmd.archiveChat(chat, true);
return chat.archive; return true;
}, chatId); }, chatId);
} }
@@ -741,7 +741,7 @@ class Client extends EventEmitter {
return await this.pupPage.evaluate(async chatId => { return await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId); let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.archiveChat(chat, false); await window.Store.Cmd.archiveChat(chat, false);
return chat.archive; return false;
}, chatId); }, chatId);
} }

View File

@@ -65,7 +65,7 @@ class Chat extends Base {
/** /**
* Indicates if the chat is muted or not * Indicates if the chat is muted or not
* @type {number} * @type {boolean}
*/ */
this.isMuted = data.isMuted; this.isMuted = data.isMuted;

View File

@@ -200,6 +200,12 @@ describe('Client', function() {
await client.destroy(); await client.destroy();
}); });
it('can get current WhatsApp Web version', async function () {
const version = await client.getWWebVersion();
expect(typeof version).to.equal('string');
console.log(`WA Version: ${version}`);
});
describe('Expose Store', function() { describe('Expose Store', function() {
it('exposes the store', async function() { it('exposes the store', async function() {
const exposed = await client.pupPage.evaluate(() => { const exposed = await client.pupPage.evaluate(() => {
@@ -526,5 +532,53 @@ END:VCARD`;
expect(formatted).to.eql('+1 (809) 220-1111'); expect(formatted).to.eql('+1 (809) 220-1111');
}); });
}); });
describe('Search messages', function () {
it('can search for messages', async function () {
this.timeout(5000);
const m1 = await client.sendMessage(remoteId, 'I\'m searching for Super Mario Brothers');
const m2 = await client.sendMessage(remoteId, 'This also contains Mario');
const m3 = await client.sendMessage(remoteId, 'Nothing of interest here, just Luigi');
// wait for search index to catch up
await helper.sleep(1000);
const msgs = await client.searchMessages('Mario', {chatId: remoteId});
expect(msgs.length).to.be.greaterThanOrEqual(2);
const msgIds = msgs.map(m => m.id._serialized);
expect(msgIds).to.include.members([
m1.id._serialized, m2.id._serialized
]);
expect(msgIds).to.not.include.members([m3.id._serialized]);
});
});
describe('Status/About', function () {
let me, previousStatus;
before(async function () {
me = await client.getContactById(client.info.wid._serialized);
previousStatus = await me.getAbout();
});
after(async function () {
await client.setStatus(previousStatus);
});
it('can set the status text', async function () {
await client.setStatus('My shiny new status');
const status = await me.getAbout();
expect(status).to.eql('My shiny new status');
});
it('can set the status text to something else', async function () {
await client.setStatus('Busy');
const status = await me.getAbout();
expect(status).to.eql('Busy');
});
});
}); });
}); });

187
tests/structures/chat.js Normal file
View File

@@ -0,0 +1,187 @@
const { expect } = require('chai');
const helper = require('../helper');
const Message = require('../../src/structures/Message');
const { MessageTypes } = require('../../src/util/Constants');
const { Contact } = require('../../src/structures');
const remoteId = helper.remoteId;
describe('Chat', function () {
let client;
let chat;
before(async function() {
this.timeout(35000);
client = helper.createClient({ withSession: true });
await client.initialize();
chat = await client.getChatById(remoteId);
});
after(async function () {
await client.destroy();
});
it('can send a message to a chat', async function () {
const msg = await chat.sendMessage('hello world');
expect(msg).to.be.instanceOf(Message);
expect(msg.type).to.equal(MessageTypes.TEXT);
expect(msg.fromMe).to.equal(true);
expect(msg.body).to.equal('hello world');
expect(msg.to).to.equal(remoteId);
});
it('can fetch messages sent in a chat', async function () {
this.timeout(5000);
await helper.sleep(1000);
const msg = await chat.sendMessage('another message');
const messages = await chat.fetchMessages();
expect(messages.length).to.be.greaterThanOrEqual(2);
const fetchedMsg = messages[messages.length-1];
expect(fetchedMsg).to.be.instanceOf(Message);
expect(fetchedMsg.type).to.equal(MessageTypes.TEXT);
expect(fetchedMsg.id._serialized).to.equal(msg.id._serialized);
expect(fetchedMsg.body).to.equal(msg.body);
});
it('can use a limit when fetching messages sent in a chat', async function () {
await helper.sleep(1000);
const msg = await chat.sendMessage('yet another message');
const messages = await chat.fetchMessages({limit: 1});
expect(messages).to.have.lengthOf(1);
const fetchedMsg = messages[0];
expect(fetchedMsg).to.be.instanceOf(Message);
expect(fetchedMsg.type).to.equal(MessageTypes.TEXT);
expect(fetchedMsg.id._serialized).to.equal(msg.id._serialized);
expect(fetchedMsg.body).to.equal(msg.body);
});
it('can get the related contact', async function () {
const contact = await chat.getContact();
expect(contact).to.be.instanceOf(Contact);
expect(contact.id._serialized).to.equal(chat.id._serialized);
});
describe('Seen', function () {
it('can mark a chat as unread', async function () {
await chat.markUnread();
await helper.sleep(500);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.unreadCount).to.equal(-1);
});
it('can mark a chat as seen', async function () {
const res = await chat.sendSeen();
expect(res).to.equal(true);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.unreadCount).to.equal(0);
});
});
describe('Archiving', function (){
it('can archive a chat', async function () {
const res = await chat.archive();
expect(res).to.equal(true);
await helper.sleep(1000);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.archived).to.equal(true);
});
it('can unarchive a chat', async function () {
const res = await chat.unarchive();
expect(res).to.equal(false);
await helper.sleep(1000);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.archived).to.equal(false);
});
});
describe('Pinning', function () {
it('can pin a chat', async function () {
const res = await chat.pin();
expect(res).to.equal(true);
await helper.sleep(1000);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.pinned).to.equal(true);
});
it('can unpin a chat', async function () {
const res = await chat.unpin();
expect(res).to.equal(false);
await helper.sleep(1000);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.pinned).to.equal(false);
});
});
describe('Muting', function () {
it('can mute a chat forever', async function() {
await chat.mute();
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.isMuted).to.equal(true);
expect(chat.muteExpiration).to.equal(-1);
});
it('can mute a chat until a specific date', async function() {
const unmuteDate = new Date(new Date().getTime() + (1000*60*60));
await chat.mute(unmuteDate);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.isMuted).to.equal(true);
expect(chat.muteExpiration).to.equal(
Math.round(unmuteDate.getTime() / 1000)
);
});
it('can unmute a chat', async function () {
await chat.unmute();
await helper.sleep(500);
// refresh chat
chat = await client.getChatById(remoteId);
expect(chat.isMuted).to.equal(false);
expect(chat.muteExpiration).to.equal(0);
});
});
describe.skip('Destructive operations', function () {
it('can clear all messages from chat', async function () {
this.timeout(5000);
const res = await chat.clearMessages();
expect(res).to.equal(true);
await helper.sleep(3000);
const msgs = await chat.fetchMessages();
expect(msgs).to.have.lengthOf(0);
});
it('can delete a chat', async function () {
const res = await chat.delete();
expect(res).to.equal(true);
});
});
});