(function (root, factory) {
define(["jasmine", "mock", "test-utils"], factory);
} (this, function (jasmine, mock, test_utils) {
"use strict";
var _ = converse.env._;
var Backbone = converse.env.Backbone;
var Strophe = converse.env.Strophe;
var $iq = converse.env.$iq;
var $msg = converse.env.$msg;
var moment = converse.env.moment;
// See: https://xmpp.org/rfcs/rfc3921.html
describe("Message Archive Management", function () {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
describe("Archived Messages", function () {
it("aren't shown as duplicates",
mock.initConverseWithPromises(
null, ['discoInitialized'], {},
function (done, _converse) {
test_utils.openAndEnterChatRoom(_converse, 'trek-radio', 'conference.lightwitch.org', 'jcbrand').then(function () {
var chatroomview = _converse.chatboxviews.get('trek-radio@conference.lightwitch.org');
var stanza = Strophe.xmlHtmlNode(
`
negan
`).firstElementChild;
_converse.connection._dataRecv(test_utils.createRequest(stanza));
stanza = Strophe.xmlHtmlNode(
`
negan
`).firstElementChild;
chatroomview.model.onMessage(stanza);
expect(chatroomview.content.querySelectorAll('.chat-msg').length).toBe(1);
done();
});
}))
});
describe("The archive.query API", function () {
it("can be used to query for all archived messages",
mock.initConverseWithPromises(
null, ['discoInitialized'], {},
function (done, _converse) {
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!_converse.disco_entities.get(_converse.domain).features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
_converse.api.archive.query();
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
"");
done();
}));
it("can be used to query for all messages to/from a particular JID",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
_converse.api.archive.query({'with':'juliet@capulet.lit'});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
"juliet@capulet.lit"+
""+
""+
""+
""
);
done();
});
}));
it("can be used to query for archived messages from a chat room",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
var callback = jasmine.createSpy('callback');
_converse.api.archive.query({'with': 'coven@chat.shakespeare.lit', 'groupchat': true}, callback);
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+
"");
done();
});
}));
it("checks whether returned MAM messages from a MUC room are from the right JID",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
var callback = jasmine.createSpy('callback');
_converse.api.archive.query({'with': 'coven@chat.shakespear.lit', 'groupchat': true, 'max':'10'}, callback);
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
/*
*
*
*
*
* Thrice the brinded cat hath mew'd.
*
*
*
*
*
*
*
*/
var msg1 = $msg({'id':'iasd207', 'from': 'other@chat.shakespear.lit', 'to': 'dummy@localhost'})
.c('result', {'xmlns': 'urn:xmpp:mam:2', 'queryid':queryid, 'id':'34482-21985-73620'})
.c('forwarded', {'xmlns':'urn:xmpp:forward:0'})
.c('delay', {'xmlns':'urn:xmpp:delay', 'stamp':'2010-07-10T23:08:25Z'}).up()
.c('message', {
'xmlns':'jabber:client',
'to':'dummy@localhost',
'id':'162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2',
'from':'coven@chat.shakespeare.lit/firstwitch',
'type':'groupchat' })
.c('body').t("Thrice the brinded cat hath mew'd.");
_converse.connection._dataRecv(test_utils.createRequest(msg1));
/* Send an stanza to indicate the end of the result set.
*
*
*
*
* 28482-98726-73623
* 09af3-cc343-b409f
* 20
*
*
*/
var stanza = $iq({'type': 'result', 'id': IQ_id})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t('23452-4534-1').up()
.c('last').t('09af3-cc343-b409f').up()
.c('count').t('16');
_converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(callback).toHaveBeenCalled();
var args = callback.calls.argsFor(0);
expect(args[0].length).toBe(0);
done();
});
}));
it("can be used to query for all messages in a certain timespan",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
_converse.api.disco.entities.get().then(function (entities) {
if (!entities.get(_converse.domain).features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var start = '2010-06-07T00:00:00Z';
var end = '2010-07-07T13:23:54Z';
_converse.api.archive.query({
'start': start,
'end': end
});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+moment(start).format()+""+
""+
""+
""+moment(end).format()+""+
""+
""+
""+
""
);
done();
});
}));
it("throws a TypeError if an invalid date is provided",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
expect(_.partial(_converse.api.archive.query, {'start': 'not a real date'})).toThrow(
new TypeError('archive.query: invalid date provided for: start')
);
done();
});
}));
it("can be used to query for all messages after a certain time",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!_converse.disco_entities.get(_converse.domain).features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var start = '2010-06-07T00:00:00Z';
_converse.api.archive.query({'start': start});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+moment(start).format()+""+
""+
""+
""+
""
);
done();
});
}));
it("can be used to query for a limited set of results",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
var start = '2010-06-07T00:00:00Z';
_converse.api.archive.query({'start': start, 'max':10});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+moment(start).format()+""+
""+
""+
""+
"10"+
""+
""+
""
);
done();
});
}));
it("can be used to page through results",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
var start = '2010-06-07T00:00:00Z';
_converse.api.archive.query({
'start': start,
'after': '09af3-cc343-b409f',
'max':10
});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+moment(start).format()+""+
""+
""+
""+
"10"+
"09af3-cc343-b409f"+
""+
""+
""
);
done();
});
}));
it("accepts \"before\" with an empty string as value to reverse the order",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
_converse.api.archive.query({'before': '', 'max':10});
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
""+
"10"+
""+
""+
""+
""
);
done();
});
}));
it("accepts a Strophe.RSM object for the query options",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
// Normally the user wouldn't manually make a Strophe.RSM object
// and pass it in. However, in the callback method an RSM object is
// returned which can be reused for easy paging. This test is
// more for that usecase.
var rsm = new Strophe.RSM({'max': '10'});
rsm['with'] = 'romeo@montague.lit'; // eslint-disable-line dot-notation
rsm.start = '2010-06-07T00:00:00Z';
_converse.api.archive.query(rsm);
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
""+
""+
""+
""+
"urn:xmpp:mam:2"+
""+
""+
"romeo@montague.lit"+
""+
""+
""+moment(rsm.start).format()+""+
""+
""+
""+
"10"+
""+
""+
""
);
done();
});
}));
it("accepts a callback function, which it passes the messages and a Strophe.RSM object",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
if (!entity.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.disco_entities.get(_converse.domain).features.create({'var': Strophe.NS.MAM});
}
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
var callback = jasmine.createSpy('callback');
_converse.api.archive.query({'with': 'romeo@capulet.lit', 'max':'10'}, callback);
var queryid = sent_stanza.nodeTree.querySelector('query').getAttribute('queryid');
/*
*
*
*
*
* Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.
*
*
*
*
*/
var msg1 = $msg({'id':'aeb213', 'to':'juliet@capulet.lit/chamber'})
.c('result', {'xmlns': 'urn:xmpp:mam:2', 'queryid':queryid, 'id':'28482-98726-73623'})
.c('forwarded', {'xmlns':'urn:xmpp:forward:0'})
.c('delay', {'xmlns':'urn:xmpp:delay', 'stamp':'2010-07-10T23:08:25Z'}).up()
.c('message', {
'xmlns':'jabber:client',
'to':'juliet@capulet.lit/balcony',
'from':'romeo@montague.lit/orchard',
'type':'chat' })
.c('body').t("Call me but love, and I'll be new baptized;");
_converse.connection._dataRecv(test_utils.createRequest(msg1));
var msg2 = $msg({'id':'aeb213', 'to':'juliet@capulet.lit/chamber'})
.c('result', {'xmlns': 'urn:xmpp:mam:2', 'queryid':queryid, 'id':'28482-98726-73624'})
.c('forwarded', {'xmlns':'urn:xmpp:forward:0'})
.c('delay', {'xmlns':'urn:xmpp:delay', 'stamp':'2010-07-10T23:08:25Z'}).up()
.c('message', {
'xmlns':'jabber:client',
'to':'juliet@capulet.lit/balcony',
'from':'romeo@montague.lit/orchard',
'type':'chat' })
.c('body').t("Henceforth I never will be Romeo.");
_converse.connection._dataRecv(test_utils.createRequest(msg2));
/* Send an stanza to indicate the end of the result set.
*
*
*
*
* 28482-98726-73623
* 09af3-cc343-b409f
* 20
*
*
*/
var stanza = $iq({'type': 'result', 'id': IQ_id})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t('23452-4534-1').up()
.c('last').t('09af3-cc343-b409f').up()
.c('count').t('16');
_converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(callback).toHaveBeenCalled();
var args = callback.calls.argsFor(0);
expect(args[0].length).toBe(2);
expect(args[0][0].outerHTML).toBe(msg1.nodeTree.outerHTML);
expect(args[0][1].outerHTML).toBe(msg2.nodeTree.outerHTML);
expect(args[1]['with']).toBe('romeo@capulet.lit'); // eslint-disable-line dot-notation
expect(args[1].max).toBe('10');
expect(args[1].count).toBe('16');
expect(args[1].first).toBe('23452-4534-1');
expect(args[1].last).toBe('09af3-cc343-b409f');
done()
});
}));
});
describe("The default preference", function () {
it("is set once server support for MAM has been confirmed",
mock.initConverseWithPromises(
null, [], {},
function (done, _converse) {
_converse.api.disco.entities.get(_converse.domain).then(function (entity) {
var sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
spyOn(_converse, 'onMAMPreferences').and.callThrough();
_converse.message_archiving = 'never';
var feature = new Backbone.Model({
'var': Strophe.NS.MAM
});
spyOn(feature, 'save').and.callFake(feature.set); // Save will complain about a url not being set
entity.onFeatureAdded(feature);
expect(_converse.connection.sendIQ).toHaveBeenCalled();
expect(sent_stanza.toLocaleString()).toBe(
""+
""+
""
);
/* Example 20. Server responds with current preferences
*
*
*
*
*
*
*
*/
var stanza = $iq({'type': 'result', 'id': IQ_id})
.c('prefs', {'xmlns': Strophe.NS.MAM, 'default':'roster'})
.c('always').c('jid').t('romeo@montague.lit').up().up()
.c('never').c('jid').t('montague@montague.lit');
_converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(_converse.onMAMPreferences).toHaveBeenCalled();
expect(_converse.connection.sendIQ.calls.count()).toBe(2);
expect(sent_stanza.toString()).toBe(
""+
""+
"romeo@montague.lit"+
"montague@montague.lit"+
""+
""
);
expect(feature.get('preference')).toBe(undefined);
/*
*
*
* romeo@montague.lit
*
*
* montague@montague.lit
*
*
*
*/
stanza = $iq({'type': 'result', 'id': IQ_id})
.c('prefs', {'xmlns': Strophe.NS.MAM, 'default':'always'})
.c('always').up()
.c('never').up();
_converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(feature.save).toHaveBeenCalled();
expect(feature.get('preferences')['default']).toBe('never'); // eslint-disable-line dot-notation
done();
});
}));
});
});
}));