diff --git a/package.json b/package.json index 117a118..db791e6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "./index.js", "typings": "./index.d.ts", "scripts": { - "test": "mocha tests", + "test": "mocha tests --recursive", + "test-single": "mocha", "shell": "node --experimental-repl-await ./shell.js", "generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose" }, diff --git a/src/Client.js b/src/Client.js index dd9b819..a6bd6da 100644 --- a/src/Client.js +++ b/src/Client.js @@ -729,7 +729,7 @@ class Client extends EventEmitter { return await this.pupPage.evaluate(async chatId => { let chat = await window.Store.Chat.get(chatId); await window.Store.Cmd.archiveChat(chat, true); - return chat.archive; + return true; }, chatId); } @@ -741,7 +741,7 @@ class Client extends EventEmitter { return await this.pupPage.evaluate(async chatId => { let chat = await window.Store.Chat.get(chatId); await window.Store.Cmd.archiveChat(chat, false); - return chat.archive; + return false; }, chatId); } diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 32bb486..b8aacb1 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -65,7 +65,7 @@ class Chat extends Base { /** * Indicates if the chat is muted or not - * @type {number} + * @type {boolean} */ this.isMuted = data.isMuted; diff --git a/tests/client.js b/tests/client.js index 78c0852..49c69ec 100644 --- a/tests/client.js +++ b/tests/client.js @@ -200,6 +200,12 @@ describe('Client', function() { 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() { it('exposes the store', async function() { const exposed = await client.pupPage.evaluate(() => { @@ -526,5 +532,53 @@ END:VCARD`; 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'); + }); + }); }); }); \ No newline at end of file diff --git a/tests/structures/chat.js b/tests/structures/chat.js new file mode 100644 index 0000000..92d18a0 --- /dev/null +++ b/tests/structures/chat.js @@ -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); + }); + }); +}); \ No newline at end of file