ChatManagerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const modulePath = path.join(
  5. __dirname,
  6. '../../../../app/src/Features/Chat/ChatManager'
  7. )
  8. const { expect } = require('chai')
  9. describe('ChatManager', function () {
  10. beforeEach(function () {
  11. this.user_id = 'mock-user-id'
  12. this.ChatManager = SandboxedModule.require(modulePath, {
  13. requires: {
  14. '../User/UserInfoManager': (this.UserInfoManager = {}),
  15. '../User/UserInfoController': (this.UserInfoController = {}),
  16. },
  17. })
  18. this.req = {
  19. params: {
  20. project_id: this.project_id,
  21. },
  22. }
  23. this.res = {
  24. json: sinon.stub(),
  25. send: sinon.stub(),
  26. sendStatus: sinon.stub(),
  27. }
  28. })
  29. describe('injectUserInfoIntoThreads', function () {
  30. beforeEach(function () {
  31. this.users = {
  32. user_id_1: {
  33. mock: 'user_1',
  34. },
  35. user_id_2: {
  36. mock: 'user_2',
  37. },
  38. }
  39. this.UserInfoManager.getPersonalInfo = (userId, callback) => {
  40. return callback(null, this.users[userId])
  41. }
  42. sinon.spy(this.UserInfoManager, 'getPersonalInfo')
  43. return (this.UserInfoController.formatPersonalInfo = user => ({
  44. formatted: user.mock,
  45. }))
  46. })
  47. it('should inject a user object into messaged and resolved data', function (done) {
  48. return this.ChatManager.injectUserInfoIntoThreads(
  49. {
  50. thread1: {
  51. resolved: true,
  52. resolved_by_user_id: 'user_id_1',
  53. messages: [
  54. {
  55. user_id: 'user_id_1',
  56. content: 'foo',
  57. },
  58. {
  59. user_id: 'user_id_2',
  60. content: 'bar',
  61. },
  62. ],
  63. },
  64. thread2: {
  65. messages: [
  66. {
  67. user_id: 'user_id_1',
  68. content: 'baz',
  69. },
  70. ],
  71. },
  72. },
  73. (error, threads) => {
  74. expect(error).to.be.null
  75. expect(threads).to.deep.equal({
  76. thread1: {
  77. resolved: true,
  78. resolved_by_user_id: 'user_id_1',
  79. resolved_by_user: { formatted: 'user_1' },
  80. messages: [
  81. {
  82. user_id: 'user_id_1',
  83. user: { formatted: 'user_1' },
  84. content: 'foo',
  85. },
  86. {
  87. user_id: 'user_id_2',
  88. user: { formatted: 'user_2' },
  89. content: 'bar',
  90. },
  91. ],
  92. },
  93. thread2: {
  94. messages: [
  95. {
  96. user_id: 'user_id_1',
  97. user: { formatted: 'user_1' },
  98. content: 'baz',
  99. },
  100. ],
  101. },
  102. })
  103. return done()
  104. }
  105. )
  106. })
  107. it('should only need to look up each user once', function (done) {
  108. return this.ChatManager.injectUserInfoIntoThreads(
  109. [
  110. {
  111. messages: [
  112. {
  113. user_id: 'user_id_1',
  114. content: 'foo',
  115. },
  116. {
  117. user_id: 'user_id_1',
  118. content: 'bar',
  119. },
  120. ],
  121. },
  122. ],
  123. (error, threads) => {
  124. expect(error).to.be.null
  125. this.UserInfoManager.getPersonalInfo.calledOnce.should.equal(true)
  126. return done()
  127. }
  128. )
  129. })
  130. })
  131. })