transcripts.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (function (root, factory) {
  2. define([
  3. "jquery",
  4. "jasmine",
  5. "converse-core",
  6. "mock",
  7. "test-utils",
  8. "utils",
  9. "transcripts"
  10. ], factory
  11. );
  12. } (this, function ($, jasmine, converse, mock, test_utils, utils, transcripts) {
  13. var Strophe = converse.env.Strophe;
  14. var _ = converse.env._;
  15. var IGNORED_TAGS = [
  16. 'stream:features',
  17. 'auth',
  18. 'challenge',
  19. 'success',
  20. 'stream:features',
  21. 'response'
  22. ];
  23. function traverseElement (el, _stanza) {
  24. if (typeof _stanza !== 'undefined') {
  25. if (el.nodeType === 3) {
  26. _stanza.t(el.nodeValue);
  27. return _stanza;
  28. } else {
  29. _stanza = _stanza.c(el.nodeName.toLowerCase(), getAttributes(el));
  30. }
  31. } else {
  32. _stanza = new Strophe.Builder(
  33. el.nodeName.toLowerCase(),
  34. getAttributes(el)
  35. );
  36. }
  37. _.each(el.childNodes, _.partial(traverseElement, _, _stanza));
  38. return _stanza.up();
  39. }
  40. function getAttributes (el) {
  41. var attributes = {};
  42. _.each(el.attributes, function (att) {
  43. attributes[att.nodeName] = att.nodeValue;
  44. });
  45. return attributes;
  46. }
  47. return describe("Transcripts of chat logs", function () {
  48. it("can be used to replay conversations",
  49. mock.initConverseWithPromises(
  50. null, ['rosterGroupsFetched'], {},
  51. function (done, _converse) {
  52. _converse.allow_non_roster_messaging = true;
  53. test_utils.openAndEnterChatRoom(_converse, 'discuss', 'conference.conversejs.org', 'dummy').then(function () {
  54. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  55. _.each(transcripts, function (transcript) {
  56. var text = transcript();
  57. var xml = Strophe.xmlHtmlNode(text);
  58. _.each(xml.firstElementChild.children, function (el) {
  59. _.each(el.children, function (el) {
  60. if (el.nodeType === 3) {
  61. return; // Ignore text
  62. }
  63. if (_.includes(IGNORED_TAGS, el.nodeName.toLowerCase())) {
  64. return;
  65. }
  66. var _stanza = traverseElement(el);
  67. _converse.connection._dataRecv(test_utils.createRequest(_stanza));
  68. });
  69. });
  70. });
  71. done();
  72. });
  73. }));
  74. });
  75. }));