transcripts.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*global _converse */
  2. (function (root, factory) {
  3. define([
  4. "converse_api",
  5. "mock",
  6. "test_utils",
  7. "utils",
  8. "transcripts"
  9. ], factory
  10. );
  11. } (this, function (converse, mock, test_utils, utils, transcripts) {
  12. var Strophe = converse.env.Strophe;
  13. var _ = converse.env._;
  14. var $ = converse.env.jQuery;
  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. beforeEach(function () {
  49. test_utils.openChatRoom("discuss", 'conference.conversejs.org', 'jc');
  50. test_utils.openChatRoom("dummy", 'rooms.localhost', 'jc');
  51. test_utils.openChatRoom("prosody", 'conference.prosody.im', 'jc');
  52. });
  53. it("can be used to replay conversations", function () {
  54. spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true);
  55. _.each(transcripts, function (transcript) {
  56. var text = transcript();
  57. var xml = Strophe.xmlHtmlNode(text);
  58. $(xml).children('log').children('body').each(function (i, el) {
  59. $(el).children().each(function (i, 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. });
  72. });
  73. }));