smacks.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Strophe = converse.env.Strophe;
  7. const sizzle = converse.env.sizzle;
  8. const u = converse.env.utils;
  9. describe("XEP-0198 Stream Management", function () {
  10. it("gets enabled with an <enable> stanza and resumed with a <resume> stanza",
  11. mock.initConverse(
  12. null, ['connectionInitialized', 'chatBoxesInitialized'],
  13. { 'auto_login': false,
  14. 'enable_smacks': true,
  15. 'show_controlbox_by_default': true,
  16. 'smacks_max_unacked_stanzas': 2
  17. },
  18. async function (done, _converse) {
  19. const view = _converse.chatboxviews.get('controlbox');
  20. spyOn(view, 'renderControlBoxPane').and.callThrough();
  21. _converse.api.user.login('romeo@montague.lit/orchard', 'secret');
  22. const sent_stanzas = _converse.connection.sent_stanzas;
  23. let stanza = await test_utils.waitUntil(() =>
  24. sent_stanzas.filter(s => (s.tagName === 'enable')).pop());
  25. expect(_converse.session.get('smacks_enabled')).toBe(false);
  26. expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
  27. let result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true"/>`);
  28. _converse.connection._dataRecv(test_utils.createRequest(result));
  29. expect(_converse.session.get('smacks_enabled')).toBe(true);
  30. await test_utils.waitUntil(() => view.renderControlBoxPane.calls.count());
  31. let IQ_stanzas = _converse.connection.IQ_stanzas;
  32. await test_utils.waitUntil(() => IQ_stanzas.length === 4);
  33. let iq = IQ_stanzas.pop();
  34. expect(Strophe.serialize(iq)).toBe(
  35. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  36. `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  37. iq = IQ_stanzas.pop();
  38. expect(Strophe.serialize(iq)).toBe(
  39. `<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
  40. iq = IQ_stanzas.pop();
  41. expect(Strophe.serialize(iq)).toBe(
  42. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+
  43. `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  44. const disco_iq = IQ_stanzas.pop();
  45. expect(Strophe.serialize(disco_iq)).toBe(
  46. `<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  47. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`);
  48. expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2);
  49. expect(_converse.session.get('unacked_stanzas').length).toBe(4);
  50. // test handling of acks
  51. let ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="1"/>`);
  52. _converse.connection._dataRecv(test_utils.createRequest(ack));
  53. expect(_converse.session.get('unacked_stanzas').length).toBe(3);
  54. // test handling of ack requests
  55. let r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
  56. _converse.connection._dataRecv(test_utils.createRequest(r));
  57. ack = await test_utils.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a')).pop());
  58. expect(Strophe.serialize(ack)).toBe('<a h="0" xmlns="urn:xmpp:sm:3"/>');
  59. const disco_result = $iq({
  60. 'type': 'result',
  61. 'from': 'montague.lit',
  62. 'to': 'romeo@montague.lit/orchard',
  63. 'id': disco_iq.getAttribute('id'),
  64. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  65. .c('identity', {
  66. 'category': 'server',
  67. 'type': 'im'
  68. }).up()
  69. .c('feature', {'var': 'http://jabber.org/protocol/disco#info'}).up()
  70. .c('feature', {'var': 'http://jabber.org/protocol/disco#items'});
  71. _converse.connection._dataRecv(test_utils.createRequest(disco_result));
  72. ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="2"/>`);
  73. _converse.connection._dataRecv(test_utils.createRequest(ack));
  74. expect(_converse.session.get('unacked_stanzas').length).toBe(2);
  75. r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
  76. _converse.connection._dataRecv(test_utils.createRequest(r));
  77. ack = await test_utils.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a' && s.getAttribute('h') === '1')).pop());
  78. expect(Strophe.serialize(ack)).toBe('<a h="1" xmlns="urn:xmpp:sm:3"/>');
  79. // test session resumption
  80. _converse.connection.IQ_stanzas = [];
  81. IQ_stanzas = _converse.connection.IQ_stanzas;
  82. _converse.api.connection.reconnect();
  83. stanza = await test_utils.waitUntil(() =>
  84. sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
  85. expect(Strophe.serialize(stanza)).toEqual('<resume h="1" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
  86. result = u.toStanza(`<resumed xmlns="urn:xmpp:sm:3" h="another-sequence-number" previd="some-long-sm-id"/>`);
  87. _converse.connection._dataRecv(test_utils.createRequest(result));
  88. // Another <enable> stanza doesn't get sent out
  89. expect(sizzle('enable', sent_stanzas).length).toBe(0);
  90. expect(_converse.session.get('smacks_enabled')).toBe(true);
  91. await test_utils.waitUntil(() => IQ_stanzas.length === 2);
  92. // Test that unacked stanzas get resent out
  93. iq = IQ_stanzas.pop();
  94. expect(Strophe.serialize(iq)).toBe(
  95. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  96. `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  97. iq = IQ_stanzas.pop();
  98. expect(Strophe.serialize(iq)).toBe(
  99. `<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
  100. await _converse.api.waitUntil('statusInitialized');
  101. done();
  102. }));
  103. });
  104. }));