smacks.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. "use strict";
  5. const $iq = converse.env.$iq;
  6. const $msg = converse.env.$msg;
  7. const Strophe = converse.env.Strophe;
  8. const sizzle = converse.env.sizzle;
  9. const u = converse.env.utils;
  10. describe("XEP-0198 Stream Management", function () {
  11. it("gets enabled with an <enable> stanza and resumed with a <resume> stanza",
  12. mock.initConverse(
  13. ['chatBoxesInitialized'],
  14. { 'auto_login': false,
  15. 'enable_smacks': true,
  16. 'show_controlbox_by_default': true,
  17. 'smacks_max_unacked_stanzas': 2
  18. },
  19. async function (done, _converse) {
  20. const view = _converse.chatboxviews.get('controlbox');
  21. spyOn(view, 'renderControlBoxPane').and.callThrough();
  22. await _converse.api.user.login('romeo@montague.lit/orchard', 'secret');
  23. const sent_stanzas = _converse.connection.sent_stanzas;
  24. let stanza = await u.waitUntil(() =>
  25. sent_stanzas.filter(s => (s.tagName === 'enable')).pop());
  26. expect(_converse.session.get('smacks_enabled')).toBe(false);
  27. expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
  28. let result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true"/>`);
  29. _converse.connection._dataRecv(test_utils.createRequest(result));
  30. expect(_converse.session.get('smacks_enabled')).toBe(true);
  31. await u.waitUntil(() => view.renderControlBoxPane.calls.count());
  32. let IQ_stanzas = _converse.connection.IQ_stanzas;
  33. await u.waitUntil(() => IQ_stanzas.length === 4);
  34. let iq = IQ_stanzas[IQ_stanzas.length-1];
  35. expect(Strophe.serialize(iq)).toBe(
  36. `<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
  37. await test_utils.waitForRoster(_converse, 'current', 1);
  38. IQ_stanzas.pop();
  39. const expected_IQs = disco_iq => ([
  40. `<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  41. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`,
  42. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  43. `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`,
  44. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+
  45. `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`]);
  46. const disco_iq = IQ_stanzas.pop();
  47. expect(expected_IQs(disco_iq).includes(Strophe.serialize(disco_iq))).toBe(true);
  48. iq = IQ_stanzas.pop();
  49. expect(expected_IQs(disco_iq).includes(Strophe.serialize(disco_iq))).toBe(true);
  50. iq = IQ_stanzas.pop();
  51. expect(expected_IQs(disco_iq).includes(Strophe.serialize(disco_iq))).toBe(true);
  52. expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2);
  53. expect(_converse.session.get('unacked_stanzas').length).toBe(5);
  54. // test handling of acks
  55. let ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="2"/>`);
  56. _converse.connection._dataRecv(test_utils.createRequest(ack));
  57. expect(_converse.session.get('unacked_stanzas').length).toBe(3);
  58. // test handling of ack requests
  59. let r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
  60. _converse.connection._dataRecv(test_utils.createRequest(r));
  61. ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a')).pop());
  62. expect(Strophe.serialize(ack)).toBe('<a h="1" xmlns="urn:xmpp:sm:3"/>');
  63. const disco_result = $iq({
  64. 'type': 'result',
  65. 'from': 'montague.lit',
  66. 'to': 'romeo@montague.lit/orchard',
  67. 'id': disco_iq.getAttribute('id'),
  68. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  69. .c('identity', {
  70. 'category': 'server',
  71. 'type': 'im'
  72. }).up()
  73. .c('feature', {'var': 'http://jabber.org/protocol/disco#info'}).up()
  74. .c('feature', {'var': 'http://jabber.org/protocol/disco#items'});
  75. _converse.connection._dataRecv(test_utils.createRequest(disco_result));
  76. ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="3"/>`);
  77. _converse.connection._dataRecv(test_utils.createRequest(ack));
  78. expect(_converse.session.get('unacked_stanzas').length).toBe(2);
  79. r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
  80. _converse.connection._dataRecv(test_utils.createRequest(r));
  81. ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a' && s.getAttribute('h') === '1')).pop());
  82. expect(Strophe.serialize(ack)).toBe('<a h="1" xmlns="urn:xmpp:sm:3"/>');
  83. await _converse.api.waitUntil('rosterInitialized');
  84. // test session resumption
  85. _converse.connection.IQ_stanzas = [];
  86. IQ_stanzas = _converse.connection.IQ_stanzas;
  87. await _converse.api.connection.reconnect();
  88. stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
  89. expect(Strophe.serialize(stanza)).toEqual('<resume h="2" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
  90. result = u.toStanza(`<resumed xmlns="urn:xmpp:sm:3" h="another-sequence-number" previd="some-long-sm-id"/>`);
  91. _converse.connection._dataRecv(test_utils.createRequest(result));
  92. // Another <enable> stanza doesn't get sent out
  93. expect(sent_stanzas.filter(s => (s.tagName === 'enable')).length).toBe(1);
  94. expect(_converse.session.get('smacks_enabled')).toBe(true);
  95. await new Promise(resolve => _converse.api.listen.once('reconnected', resolve));
  96. await u.waitUntil(() => IQ_stanzas.length === 1);
  97. // Test that unacked stanzas get resent out
  98. iq = IQ_stanzas.pop();
  99. expect(Strophe.serialize(iq)).toBe(`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
  100. expect(IQ_stanzas.filter(iq => sizzle('query[xmlns="jabber:iq:roster"]', iq).pop()).length).toBe(0);
  101. done();
  102. }));
  103. it("might not resume and the session will then be reset",
  104. mock.initConverse(
  105. ['chatBoxesInitialized'],
  106. { 'auto_login': false,
  107. 'enable_smacks': true,
  108. 'show_controlbox_by_default': true,
  109. 'smacks_max_unacked_stanzas': 2
  110. },
  111. async function (done, _converse) {
  112. await _converse.api.user.login('romeo@montague.lit/orchard', 'secret');
  113. const sent_stanzas = _converse.connection.sent_stanzas;
  114. let stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'enable')).pop());
  115. expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
  116. let result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true"/>`);
  117. _converse.connection._dataRecv(test_utils.createRequest(result));
  118. await test_utils.waitForRoster(_converse, 'current', 1);
  119. // test session resumption
  120. await _converse.api.connection.reconnect();
  121. stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
  122. expect(Strophe.serialize(stanza)).toEqual('<resume h="1" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
  123. result = u.toStanza(
  124. `<failed xmlns="urn:xmpp:sm:3" h="another-sequence-number">`+
  125. `<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>`+
  126. `</failed>`);
  127. _converse.connection._dataRecv(test_utils.createRequest(result));
  128. // Session data gets reset
  129. expect(_converse.session.get('smacks_enabled')).toBe(false);
  130. expect(_converse.session.get('num_stanzas_handled')).toBe(0);
  131. expect(_converse.session.get('num_stanzas_handled_by_server')).toBe(0);
  132. expect(_converse.session.get('num_stanzas_since_last_ack')).toBe(0);
  133. expect(_converse.session.get('unacked_stanzas').length).toBe(0);
  134. expect(_converse.session.get('roster_cached')).toBeFalsy();
  135. await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'enable')).length === 2);
  136. stanza = sent_stanzas.filter(s => (s.tagName === 'enable')).pop();
  137. expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
  138. result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="another-long-sm-id" resume="true"/>`);
  139. _converse.connection._dataRecv(test_utils.createRequest(result));
  140. expect(_converse.session.get('smacks_enabled')).toBe(true);
  141. // Check that the roster gets fetched
  142. await test_utils.waitForRoster(_converse, 'current', 1);
  143. await new Promise(resolve => _converse.api.listen.once('reconnected', resolve));
  144. done();
  145. }));
  146. it("can cause MUC messages to be received before chatboxes are initialized",
  147. mock.initConverse(
  148. ['chatBoxesInitialized'],
  149. { 'auto_login': false,
  150. 'blacklisted_plugins': 'converse-mam',
  151. 'enable_smacks': true,
  152. 'muc_fetch_members': false,
  153. 'show_controlbox_by_default': true,
  154. 'smacks_max_unacked_stanzas': 2
  155. },
  156. async function (done, _converse) {
  157. const key = "converse-test-session/converse.session-romeo@montague.lit-converse.session-romeo@montague.lit";
  158. sessionStorage.setItem(
  159. key,
  160. JSON.stringify({
  161. "id": "converse.session-romeo@montague.lit",
  162. "jid": "romeo@montague.lit/converse.js-100020907",
  163. "bare_jid": "romeo@montague.lit",
  164. "resource": "converse.js-100020907",
  165. "domain": "montague.lit",
  166. "active": false,
  167. "smacks_enabled": true,
  168. "num_stanzas_handled": 580,
  169. "num_stanzas_handled_by_server": 525,
  170. "num_stanzas_since_last_ack": 0,
  171. "unacked_stanzas": [],
  172. "smacks_stream_id": "some-long-sm-id",
  173. "push_enabled": ["romeo@montague.lit"],
  174. "carbons_enabled": true,
  175. "roster_cached": true
  176. })
  177. );
  178. const muc_jid = 'lounge@montague.lit';
  179. const chatkey = `converse.chatboxes-romeo@montague.lit-${muc_jid}`;
  180. sessionStorage.setItem('converse.chatboxes-romeo@montague.lit', JSON.stringify([chatkey]));
  181. sessionStorage.setItem(chatkey,
  182. JSON.stringify({
  183. hidden: false,
  184. message_type: "groupchat",
  185. name: "lounge",
  186. num_unread: 0,
  187. type: "chatroom",
  188. jid: muc_jid,
  189. id: muc_jid,
  190. box_id: "box-YXJnQGNvbmZlcmVuY2UuY2hhdC5leGFtcGxlLm9yZw==",
  191. nick: "romeo"
  192. })
  193. );
  194. _converse.no_connection_on_bind = true; // XXX Don't trigger CONNECTED in tests/mock.js
  195. await _converse.api.user.login('romeo@montague.lit', 'secret');
  196. delete _converse.no_connection_on_bind;
  197. const sent_stanzas = _converse.connection.sent_stanzas;
  198. const stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
  199. expect(Strophe.serialize(stanza)).toEqual('<resume h="580" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
  200. const result = u.toStanza(`<resumed xmlns="urn:xmpp:sm:3" h="another-sequence-number" previd="some-long-sm-id"/>`);
  201. _converse.connection._dataRecv(test_utils.createRequest(result));
  202. expect(_converse.session.get('smacks_enabled')).toBe(true);
  203. const nick = 'romeo';
  204. const func = _converse.chatboxes.onChatBoxesFetched;
  205. spyOn(_converse.chatboxes, 'onChatBoxesFetched').and.callFake(collection => {
  206. const muc = new _converse.ChatRoom({'jid': muc_jid, 'id': muc_jid, nick}, {'collection': _converse.chatboxes});
  207. _converse.chatboxes.add(muc);
  208. func.call(_converse.chatboxes, collection);
  209. });
  210. // A MUC message gets received
  211. const msg = $msg({
  212. from: `${muc_jid}/juliet`,
  213. id: u.getUniqueId(),
  214. to: 'romeo@montague.lit',
  215. type: 'groupchat'
  216. }).c('body').t('First message').tree();
  217. _converse.connection._dataRecv(test_utils.createRequest(msg));
  218. await _converse.api.waitUntil('chatBoxesFetched');
  219. const muc = _converse.chatboxes.get(muc_jid);
  220. await u.waitUntil(() => muc.message_queue.length === 1);
  221. const view = _converse.chatboxviews.get(muc_jid);
  222. await test_utils.getRoomFeatures(_converse, muc_jid);
  223. await test_utils.receiveOwnMUCPresence(_converse, muc_jid, nick);
  224. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  225. await view.model.messages.fetched;
  226. await u.waitUntil(() => muc.messages.length);
  227. expect(muc.messages.at(0).get('message')).toBe('First message')
  228. done();
  229. }));
  230. });
  231. }));