(function (root, factory) { define([ "jasmine", "mock", "test-utils" ], factory); } (this, function (jasmine, mock, test_utils) { "use strict"; const { Backbone, Promise, Strophe, $iq, $msg, $pres, b64_sha1, dayjs, sizzle, _ } = converse.env; const u = converse.env.utils; describe("A Chat Message", function () { it("can be sent as a correction by clicking the pencil icon", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, async function (done, _converse) { await test_utils.waitForRoster(_converse, 'current', 1); test_utils.openControlBox(); const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; await test_utils.openChatBoxFor(_converse, contact_jid); const view = _converse.api.chatviews.get(contact_jid); const textarea = view.el.querySelector('textarea.chat-textarea'); textarea.value = 'But soft, what light through yonder airlock breaks?'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); expect(view.el.querySelector('.chat-msg__text').textContent) .toBe('But soft, what light through yonder airlock breaks?'); expect(textarea.value).toBe(''); const first_msg = view.model.messages.findWhere({'message': 'But soft, what light through yonder airlock breaks?'}); expect(view.el.querySelectorAll('.chat-msg .chat-msg__action').length).toBe(1); let action = view.el.querySelector('.chat-msg .chat-msg__action'); expect(action.getAttribute('title')).toBe('Edit this message'); action.style.opacity = 1; action.click(); expect(textarea.value).toBe('But soft, what light through yonder airlock breaks?'); expect(view.model.messages.at(0).get('correcting')).toBe(true); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await new Promise((resolve, reject) => view.model.messages.once('rendered', resolve)); expect(u.hasClass('correcting', view.el.querySelector('.chat-msg'))).toBe(true); spyOn(_converse.connection, 'send'); textarea.value = 'But soft, what light through yonder window breaks?'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); expect(_converse.connection.send).toHaveBeenCalled(); await new Promise((resolve, reject) => view.model.messages.once('rendered', resolve)); const msg = _converse.connection.send.calls.all()[0].args[0]; expect(msg.toLocaleString()) .toBe(``+ `But soft, what light through yonder window breaks?`+ ``+ ``+ ``+ ``+ ``); expect(view.model.messages.models.length).toBe(1); const corrected_message = view.model.messages.at(0); expect(corrected_message.get('msgid')).toBe(first_msg.get('msgid')); expect(corrected_message.get('correcting')).toBe(false); const older_versions = corrected_message.get('older_versions'); const keys = Object.keys(older_versions); expect(keys.length).toBe(1); expect(older_versions[keys[0]]).toBe('But soft, what light through yonder airlock breaks?'); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); expect(u.hasClass('correcting', view.el.querySelector('.chat-msg'))).toBe(false); // Test that clicking the pencil icon a second time cancels editing. action = view.el.querySelector('.chat-msg .chat-msg__action'); action.style.opacity = 1; action.click(); await new Promise((resolve, reject) => view.model.messages.once('rendered', resolve)); expect(textarea.value).toBe('But soft, what light through yonder window breaks?'); expect(view.model.messages.at(0).get('correcting')).toBe(true); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => u.hasClass('correcting', view.el.querySelector('.chat-msg')) === true); action = view.el.querySelector('.chat-msg .chat-msg__action'); action.style.opacity = 1; action.click(); expect(textarea.value).toBe(''); expect(view.model.messages.at(0).get('correcting')).toBe(false); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => (u.hasClass('correcting', view.el.querySelector('.chat-msg')) === false), 500); // Test that messages from other users don't have the pencil icon _converse.chatboxes.onMessage( $msg({ 'from': contact_jid, 'to': _converse.connection.jid, 'type': 'chat', 'id': (new Date()).getTime() }).c('body').t('Hello').up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree() ); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(view.el.querySelectorAll('.chat-msg .chat-msg__action').length).toBe(1); // Test confirmation dialog spyOn(window, 'confirm').and.returnValue(true); textarea.value = 'But soft, what light through yonder airlock breaks?'; action = view.el.querySelector('.chat-msg .chat-msg__action'); action.style.opacity = 1; action.click(); expect(window.confirm).toHaveBeenCalledWith( 'You have an unsent message which will be lost if you continue. Are you sure?'); expect(view.model.messages.at(0).get('correcting')).toBe(true); expect(textarea.value).toBe('But soft, what light through yonder window breaks?'); textarea.value = 'But soft, what light through yonder airlock breaks?' action.click(); expect(view.model.messages.at(0).get('correcting')).toBe(false); expect(window.confirm.calls.count()).toBe(2); expect(window.confirm.calls.argsFor(0)).toEqual( ['You have an unsent message which will be lost if you continue. Are you sure?']); expect(window.confirm.calls.argsFor(1)).toEqual( ['You have an unsent message which will be lost if you continue. Are you sure?']); done(); })); it("can be sent as a correction by using the up arrow", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, async function (done, _converse) { await test_utils.waitForRoster(_converse, 'current', 1); test_utils.openControlBox(); const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; await test_utils.openChatBoxFor(_converse, contact_jid) const view = _converse.chatboxviews.get(contact_jid); const textarea = view.el.querySelector('textarea.chat-textarea'); expect(textarea.value).toBe(''); view.onKeyDown({ target: textarea, keyCode: 38 // Up arrow }); expect(textarea.value).toBe(''); textarea.value = 'But soft, what light through yonder airlock breaks?'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); expect(view.el.querySelector('.chat-msg__text').textContent) .toBe('But soft, what light through yonder airlock breaks?'); const first_msg = view.model.messages.findWhere({'message': 'But soft, what light through yonder airlock breaks?'}); expect(textarea.value).toBe(''); view.onKeyDown({ target: textarea, keyCode: 38 // Up arrow }); expect(textarea.value).toBe('But soft, what light through yonder airlock breaks?'); expect(view.model.messages.at(0).get('correcting')).toBe(true); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => u.hasClass('correcting', view.el.querySelector('.chat-msg')), 500); spyOn(_converse.connection, 'send'); textarea.value = 'But soft, what light through yonder window breaks?'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); expect(_converse.connection.send).toHaveBeenCalled(); await new Promise((resolve, reject) => view.model.messages.once('rendered', resolve)); const msg = _converse.connection.send.calls.all()[0].args[0]; expect(msg.toLocaleString()) .toBe(``+ `But soft, what light through yonder window breaks?`+ ``+ ``+ ``+ ``+ ``); expect(view.model.messages.models.length).toBe(1); const corrected_message = view.model.messages.at(0); expect(corrected_message.get('msgid')).toBe(first_msg.get('msgid')); expect(corrected_message.get('correcting')).toBe(false); const older_versions = corrected_message.get('older_versions'); const keys = Object.keys(older_versions); expect(keys.length).toBe(1); expect(older_versions[keys[0]]).toBe('But soft, what light through yonder airlock breaks?'); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => (u.hasClass('correcting', view.el.querySelector('.chat-msg')) === false), 500); // Test that pressing the down arrow cancels message correction expect(textarea.value).toBe(''); view.onKeyDown({ target: textarea, keyCode: 38 // Up arrow }); expect(textarea.value).toBe('But soft, what light through yonder window breaks?'); expect(view.model.messages.at(0).get('correcting')).toBe(true); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => u.hasClass('correcting', view.el.querySelector('.chat-msg')), 500); expect(textarea.value).toBe('But soft, what light through yonder window breaks?'); view.onKeyDown({ target: textarea, keyCode: 40 // Down arrow }); expect(textarea.value).toBe(''); expect(view.model.messages.at(0).get('correcting')).toBe(false); expect(view.el.querySelectorAll('.chat-msg').length).toBe(1); await u.waitUntil(() => (u.hasClass('correcting', view.el.querySelector('.chat-msg')) === false), 500); textarea.value = 'It is the east, and Juliet is the one.'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(view.el.querySelectorAll('.chat-msg').length).toBe(2); textarea.value = 'Arise, fair sun, and kill the envious moon'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(view.el.querySelectorAll('.chat-msg').length).toBe(3); view.onKeyDown({ target: textarea, keyCode: 38 // Up arrow }); expect(textarea.value).toBe('Arise, fair sun, and kill the envious moon'); expect(view.model.messages.at(0).get('correcting')).toBeFalsy(); expect(view.model.messages.at(1).get('correcting')).toBeFalsy(); expect(view.model.messages.at(2).get('correcting')).toBe(true); await u.waitUntil(() => u.hasClass('correcting', sizzle('.chat-msg:last', view.el).pop()), 500); textarea.selectionEnd = 0; // Happens by pressing up, // but for some reason not in tests, so we set it manually. view.onKeyDown({ target: textarea, keyCode: 38 // Up arrow }); expect(textarea.value).toBe('It is the east, and Juliet is the one.'); expect(view.model.messages.at(0).get('correcting')).toBeFalsy(); expect(view.model.messages.at(1).get('correcting')).toBe(true); expect(view.model.messages.at(2).get('correcting')).toBeFalsy(); await u.waitUntil(() => u.hasClass('correcting', sizzle('.chat-msg', view.el)[1]), 500); textarea.value = 'It is the east, and Juliet is the sun.'; view.onKeyDown({ target: textarea, preventDefault: function noop () {}, keyCode: 13 // Enter }); await new Promise((resolve, reject) => view.model.messages.once('rendered', resolve)); expect(textarea.value).toBe(''); const messages = view.el.querySelectorAll('.chat-msg'); expect(messages.length).toBe(3); expect(messages[0].querySelector('.chat-msg__text').textContent) .toBe('But soft, what light through yonder window breaks?'); expect(messages[1].querySelector('.chat-msg__text').textContent) .toBe('It is the east, and Juliet is the sun.'); expect(messages[2].querySelector('.chat-msg__text').textContent) .toBe('Arise, fair sun, and kill the envious moon'); expect(view.model.messages.at(0).get('correcting')).toBeFalsy(); expect(view.model.messages.at(1).get('correcting')).toBeFalsy(); expect(view.model.messages.at(2).get('correcting')).toBeFalsy(); done(); })); it("can be received out of order, and will still be displayed in the right order", mock.initConverse( null, ['rosterGroupsFetched'], {}, async function (done, _converse) { await test_utils.waitForRoster(_converse, 'current'); test_utils.openControlBox(); let message, msg; const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; await u.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length) spyOn(_converse, 'log'); spyOn(_converse.chatboxes, 'getChatBox').and.callThrough(); _converse.filter_by_resource = true; /* * * * * Call me but love, and I'll be new baptized; Henceforth I never will be Romeo. * * * */ msg = $msg({'id': 'aeb213', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2018-01-02T13:08:25Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("message") .tree(); await _converse.chatboxes.onMessage(msg); await u.waitUntil(() => _converse.api.chats.get().length); const view = _converse.api.chatviews.get(sender_jid); msg = $msg({'id': 'aeb214', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2017-12-31T22:08:25Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("Older message") .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); msg = $msg({'id': 'aeb215', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2018-01-01T13:18:23Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("Inbetween message").up() .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); msg = $msg({'id': 'aeb216', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2018-01-01T13:18:23Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("another inbetween message") .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); msg = $msg({'id': 'aeb217', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2018-01-02T12:18:23Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("An earlier message on the next day") .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); msg = $msg({'id': 'aeb218', 'to': _converse.bare_jid}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('delay', {'xmlns': 'urn:xmpp:delay', 'stamp':'2018-01-02T22:28:23Z'}).up() .c('message', { 'xmlns': 'jabber:client', 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) .c('body').t("newer message from the next day") .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); // Insert message, to also check that // text messages are inserted correctly with // temporary chat events in the chat contents. msg = $msg({ 'id': 'aeb219', 'to': _converse.bare_jid, 'xmlns': 'jabber:client', 'from': sender_jid, 'type': 'chat'}) .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up() .tree(); await _converse.chatboxes.onMessage(msg); msg = $msg({ 'id': 'aeb220', 'to': _converse.bare_jid, 'xmlns': 'jabber:client', 'from': sender_jid, 'type': 'chat'}) .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up() .c('body').t("latest message") .tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); const chat_content = view.el.querySelector('.chat-content'); view.clearSpinner(); //cleanup expect(chat_content.querySelectorAll('.date-separator').length).toEqual(4); let day = sizzle('.date-separator:first', chat_content).pop(); expect(day.getAttribute('data-isodate')).toEqual(dayjs('2017-12-31T00:00:00').toISOString()); let time = sizzle('time:first', chat_content).pop(); expect(time.textContent).toEqual('Sunday Dec 31st 2017') day = sizzle('.date-separator:first', chat_content).pop(); expect(day.nextElementSibling.querySelector('.chat-msg__text').textContent).toBe('Older message'); let el = sizzle('.chat-msg:first', chat_content).pop().querySelector('.chat-msg__text') expect(u.hasClass('chat-msg--followup', el)).toBe(false); expect(el.textContent).toEqual('Older message'); time = sizzle('time.separator-text:eq(1)', chat_content).pop(); expect(time.textContent).toEqual("Monday Jan 1st 2018"); day = sizzle('.date-separator:eq(1)', chat_content).pop(); expect(day.getAttribute('data-isodate')).toEqual(dayjs('2018-01-01T00:00:00').toISOString()); expect(day.nextElementSibling.querySelector('.chat-msg__text').textContent).toBe('Inbetween message'); el = sizzle('.chat-msg:eq(1)', chat_content).pop(); expect(el.querySelector('.chat-msg__text').textContent).toEqual('Inbetween message'); expect(el.nextElementSibling.querySelector('.chat-msg__text').textContent).toEqual('another inbetween message'); el = sizzle('.chat-msg:eq(2)', chat_content).pop(); expect(el.querySelector('.chat-msg__text').textContent) .toEqual('another inbetween message'); expect(u.hasClass('chat-msg--followup', el)).toBe(true); time = sizzle('time.separator-text:nth(2)', chat_content).pop(); expect(time.textContent).toEqual("Tuesday Jan 2nd 2018"); day = sizzle('.date-separator:nth(2)', chat_content).pop(); expect(day.getAttribute('data-isodate')).toEqual(dayjs('2018-01-02T00:00:00').toISOString()); expect(day.nextElementSibling.querySelector('.chat-msg__text').textContent).toBe('An earlier message on the next day'); el = sizzle('.chat-msg:eq(3)', chat_content).pop(); expect(el.querySelector('.chat-msg__text').textContent).toEqual('An earlier message on the next day'); expect(u.hasClass('chat-msg--followup', el)).toBe(false); el = sizzle('.chat-msg:eq(4)', chat_content).pop(); expect(el.querySelector('.chat-msg__text').textContent).toEqual('message'); expect(el.nextElementSibling.querySelector('.chat-msg__text').textContent).toEqual('newer message from the next day'); expect(u.hasClass('chat-msg--followup', el)).toBe(false); day = sizzle('.date-separator:last', chat_content).pop(); expect(day.getAttribute('data-isodate')).toEqual(dayjs().startOf('day').toISOString()); expect(day.nextElementSibling.querySelector('.chat-msg__text').textContent).toBe('latest message'); expect(u.hasClass('chat-msg--followup', el)).toBe(false); done(); })); it("is ignored if it's a malformed headline message", mock.initConverse( null, ['rosterGroupsFetched'], {}, async function (done, _converse) { await test_utils.waitForRoster(_converse, 'current'); test_utils.openControlBox(); /* Ideally we wouldn't have to filter out headline * messages, but Prosody gives them the wrong 'type' :( */ sinon.spy(_converse, 'log'); sinon.spy(_converse.chatboxes, 'getChatBox'); sinon.spy(u, 'isHeadlineMessage'); const msg = $msg({ from: 'montague.lit', to: _converse.bare_jid, type: 'chat', id: (new Date()).getTime() }).c('body').t("This headline message will not be shown").tree(); await _converse.chatboxes.onMessage(msg); expect(_converse.log.calledWith( "onMessage: Ignoring incoming headline message from JID: montague.lit", Strophe.LogLevel.INFO )).toBeTruthy(); expect(u.isHeadlineMessage.called).toBeTruthy(); expect(u.isHeadlineMessage.returned(true)).toBeTruthy(); expect(_converse.chatboxes.getChatBox.called).toBeFalsy(); // Remove sinon spies _converse.log.restore(); _converse.chatboxes.getChatBox.restore(); u.isHeadlineMessage.restore(); done(); })); it("can be a carbon message, as defined in XEP-0280", mock.initConverse( null, ['rosterGroupsFetched'], {}, async function (done, _converse) { const include_nick = false; await test_utils.waitForRoster(_converse, 'current', 2, include_nick); test_utils.openControlBox(); // Send a message from a different resource spyOn(_converse, 'log'); const msgtext = 'This is a carbon message'; const sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const msg = $msg({ 'from': _converse.bare_jid, 'id': (new Date()).getTime(), 'to': _converse.connection.jid, 'type': 'chat', 'xmlns': 'jabber:client' }).c('received', {'xmlns': 'urn:xmpp:carbons:2'}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('message', { 'xmlns': 'jabber:client', 'from': sender_jid, 'to': _converse.bare_jid+'/another-resource', 'type': 'chat' }).c('body').t(msgtext).tree(); await _converse.chatboxes.onMessage(msg); await u.waitUntil(() => (_converse.api.chats.get().length > 1)) const chatbox = _converse.chatboxes.get(sender_jid); const view = _converse.chatboxviews.get(sender_jid); expect(chatbox).toBeDefined(); expect(view).toBeDefined(); // Check that the message was received and check the message parameters expect(chatbox.messages.length).toEqual(1); const msg_obj = chatbox.messages.models[0]; expect(msg_obj.get('message')).toEqual(msgtext); expect(msg_obj.get('fullname')).toBeUndefined(); expect(msg_obj.get('nickname')).toBe(null); expect(msg_obj.get('sender')).toEqual('them'); expect(msg_obj.get('is_delayed')).toEqual(false); // Now check that the message appears inside the chatbox in the DOM const chat_content = view.el.querySelector('.chat-content'); expect(chat_content.querySelector('.chat-msg .chat-msg__text').textContent).toEqual(msgtext); expect(chat_content.querySelector('.chat-msg__time').textContent.match(/^[0-9][0-9]:[0-9][0-9]/)).toBeTruthy(); await u.waitUntil(() => chatbox.vcard.get('fullname') === 'Juliet Capulet') expect(chat_content.querySelector('span.chat-msg__author').textContent.trim()).toBe('Juliet Capulet'); done(); })); it("can be a carbon message that this user sent from a different client, as defined in XEP-0280", mock.initConverse( null, ['rosterGroupsFetched'], {}, async function (done, _converse) { await test_utils.waitUntilDiscoConfirmed(_converse, 'montague.lit', [], ['vcard-temp']); await test_utils.waitForRoster(_converse, 'current'); test_utils.openControlBox(); // Send a message from a different resource spyOn(_converse, 'log'); const msgtext = 'This is a sent carbon message'; const recipient_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const msg = $msg({ 'from': _converse.bare_jid, 'id': (new Date()).getTime(), 'to': _converse.connection.jid, 'type': 'chat', 'xmlns': 'jabber:client' }).c('sent', {'xmlns': 'urn:xmpp:carbons:2'}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('message', { 'xmlns': 'jabber:client', 'from': _converse.bare_jid+'/another-resource', 'to': recipient_jid, 'type': 'chat' }).c('body').t(msgtext).tree(); await _converse.chatboxes.onMessage(msg); // Check that the chatbox and its view now exist const chatbox = _converse.chatboxes.get(recipient_jid); const view = _converse.chatboxviews.get(recipient_jid); expect(chatbox).toBeDefined(); expect(view).toBeDefined(); await new Promise(resolve => view.once('messageInserted', resolve)); // Check that the message was received and check the message parameters expect(chatbox.messages.length).toEqual(1); const msg_obj = chatbox.messages.models[0]; expect(msg_obj.get('message')).toEqual(msgtext); expect(msg_obj.get('fullname')).toEqual(_converse.xmppstatus.get('fullname')); expect(msg_obj.get('sender')).toEqual('me'); expect(msg_obj.get('is_delayed')).toEqual(false); // Now check that the message appears inside the chatbox in the DOM const msg_txt = view.el.querySelector('.chat-content .chat-msg .chat-msg__text').textContent; expect(msg_txt).toEqual(msgtext); done(); })); it("will be discarded if it's a malicious message meant to look like a carbon copy", mock.initConverse( null, ['rosterGroupsFetched'], {}, async function (done, _converse) { await test_utils.waitForRoster(_converse, 'current'); test_utils.openControlBox(); /* * * * * Please come to Creepy Valley tonight, alone! * * * * */ spyOn(_converse, 'log'); const msgtext = 'Please come to Creepy Valley tonight, alone!'; const sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const impersonated_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const msg = $msg({ 'from': sender_jid, 'id': (new Date()).getTime(), 'to': _converse.connection.jid, 'type': 'chat', 'xmlns': 'jabber:client' }).c('received', {'xmlns': 'urn:xmpp:carbons:2'}) .c('forwarded', {'xmlns': 'urn:xmpp:forward:0'}) .c('message', { 'xmlns': 'jabber:client', 'from': impersonated_jid, 'to': _converse.connection.jid, 'type': 'chat' }).c('body').t(msgtext).tree(); await _converse.chatboxes.onMessage(msg); // Check that chatbox for impersonated user is not created. let chatbox = _converse.chatboxes.get(impersonated_jid); expect(chatbox).not.toBeDefined(); // Check that the chatbox for the malicous user is not created chatbox = _converse.chatboxes.get(sender_jid); expect(chatbox).not.toBeDefined(); done(); })); it("received for a minimized chat box will increment a counter on its header", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, async function (done, _converse) { if (_converse.view_mode === 'fullscreen') { return done(); } await test_utils.waitForRoster(_converse, 'current'); const contact_name = mock.cur_names[0]; const contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@montague.lit'; test_utils.openControlBox(); spyOn(_converse.api, "trigger").and.callThrough(); await u.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length); await test_utils.openChatBoxFor(_converse, contact_jid); const chatview = _converse.api.chatviews.get(contact_jid); expect(u.isVisible(chatview.el)).toBeTruthy(); expect(chatview.model.get('minimized')).toBeFalsy(); chatview.el.querySelector('.toggle-chatbox-button').click(); expect(chatview.model.get('minimized')).toBeTruthy(); var message = 'This message is sent to a minimized chatbox'; var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; var msg = $msg({ from: sender_jid, to: _converse.connection.jid, type: 'chat', id: (new Date()).getTime() }).c('body').t(message).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); await _converse.chatboxes.onMessage(msg); await u.waitUntil(() => chatview.model.messages.length); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); const trimmed_chatboxes = _converse.minimized_chats; const trimmedview = trimmed_chatboxes.get(contact_jid); let count = trimmedview.el.querySelector('.message-count'); expect(u.isVisible(chatview.el)).toBeFalsy(); expect(trimmedview.model.get('minimized')).toBeTruthy(); expect(u.isVisible(count)).toBeTruthy(); expect(count.textContent).toBe('1'); _converse.chatboxes.onMessage( $msg({ from: mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit', to: _converse.connection.jid, type: 'chat', id: (new Date()).getTime() }).c('body').t('This message is also sent to a minimized chatbox').up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree() ); await u.waitUntil(() => (chatview.model.messages.length > 1)); expect(u.isVisible(chatview.el)).toBeFalsy(); expect(trimmedview.model.get('minimized')).toBeTruthy(); count = trimmedview.el.querySelector('.message-count'); expect(u.isVisible(count)).toBeTruthy(); expect(count.textContent).toBe('2'); trimmedview.el.querySelector('.restore-chat').click(); expect(trimmed_chatboxes.keys().length).toBe(0); done(); })); it("will indicate when it has a time difference of more than a day between it and its predecessor", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, async function (done, _converse) { const include_nick = false; await test_utils.waitForRoster(_converse, 'current', 2, include_nick); test_utils.openControlBox(); spyOn(_converse.api, "trigger").and.callThrough(); const contact_name = mock.cur_names[1]; const contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@montague.lit'; await u.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length); await test_utils.openChatBoxFor(_converse, contact_jid); test_utils.clearChatBoxMessages(_converse, contact_jid); const one_day_ago = dayjs().subtract(1, 'day'); const chatbox = _converse.chatboxes.get(contact_jid); const view = _converse.chatboxviews.get(contact_jid); let message = 'This is a day old message'; let msg = $msg({ from: contact_jid, to: _converse.connection.jid, type: 'chat', id: one_day_ago.toDate().getTime() }).c('body').t(message).up() .c('delay', { xmlns:'urn:xmpp:delay', from: 'montague.lit', stamp: one_day_ago.toISOString() }) .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); expect(chatbox.messages.length).toEqual(1); let msg_obj = chatbox.messages.models[0]; expect(msg_obj.get('message')).toEqual(message); expect(msg_obj.get('fullname')).toBeUndefined(); expect(msg_obj.get('nickname')).toBe(null); expect(msg_obj.get('sender')).toEqual('them'); expect(msg_obj.get('is_delayed')).toEqual(true); await u.waitUntil(() => chatbox.vcard.get('fullname') === 'Juliet Capulet') const chat_content = view.el.querySelector('.chat-content'); expect(chat_content.querySelector('.chat-msg .chat-msg__text').textContent).toEqual(message); expect(chat_content.querySelector('.chat-msg__time').textContent.match(/^[0-9][0-9]:[0-9][0-9]/)).toBeTruthy(); expect(chat_content.querySelector('span.chat-msg__author').textContent.trim()).toBe('Juliet Capulet'); expect(chat_content.querySelectorAll('.date-separator').length).toEqual(1); let day = chat_content.querySelector('.date-separator'); expect(day.getAttribute('class')).toEqual('message date-separator'); expect(day.getAttribute('data-isodate')).toEqual(dayjs(one_day_ago.startOf('day')).toISOString()); let time = chat_content.querySelector('time.separator-text'); expect(time.textContent).toEqual(dayjs(one_day_ago.startOf('day')).format("dddd MMM Do YYYY")); message = 'This is a current message'; msg = $msg({ from: contact_jid, to: _converse.connection.jid, type: 'chat', id: new Date().getTime() }).c('body').t(message).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); await _converse.chatboxes.onMessage(msg); await new Promise((resolve, reject) => view.once('messageInserted', resolve)); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); // Check that there is a