ChatManagerTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/UserGetter': (this.UserGetter = { promises: {} }),
  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. _id: 'user_id_1',
  34. },
  35. user_id_2: {
  36. _id: 'user_id_2',
  37. },
  38. }
  39. this.UserGetter.promises.getUsers = userIds =>
  40. Promise.resolve(
  41. Array.from(userIds)
  42. .map(id => this.users[id])
  43. .filter(u => !!u)
  44. )
  45. sinon.spy(this.UserGetter.promises, 'getUsers')
  46. return (this.UserInfoController.formatPersonalInfo = user => ({
  47. formatted: { id: user._id.toString() },
  48. }))
  49. })
  50. it('should inject a user object into messaged and resolved data', async function () {
  51. const threads = await this.ChatManager.promises.injectUserInfoIntoThreads(
  52. {
  53. thread1: {
  54. resolved: true,
  55. resolved_by_user_id: 'user_id_1',
  56. messages: [
  57. {
  58. user_id: 'user_id_1',
  59. content: 'foo',
  60. },
  61. {
  62. user_id: 'user_id_2',
  63. content: 'bar',
  64. },
  65. ],
  66. },
  67. thread2: {
  68. messages: [
  69. {
  70. user_id: 'user_id_1',
  71. content: 'baz',
  72. },
  73. ],
  74. },
  75. }
  76. )
  77. expect(threads).to.deep.equal({
  78. thread1: {
  79. resolved: true,
  80. resolved_by_user_id: 'user_id_1',
  81. resolved_by_user: { formatted: { id: 'user_id_1' } },
  82. messages: [
  83. {
  84. user_id: 'user_id_1',
  85. user: { formatted: { id: 'user_id_1' } },
  86. content: 'foo',
  87. },
  88. {
  89. user_id: 'user_id_2',
  90. user: { formatted: { id: 'user_id_2' } },
  91. content: 'bar',
  92. },
  93. ],
  94. },
  95. thread2: {
  96. messages: [
  97. {
  98. user_id: 'user_id_1',
  99. user: { formatted: { id: 'user_id_1' } },
  100. content: 'baz',
  101. },
  102. ],
  103. },
  104. })
  105. })
  106. it('should lookup all users in a single batch', async function () {
  107. await this.ChatManager.promises.injectUserInfoIntoThreads([
  108. {
  109. messages: [
  110. {
  111. user_id: 'user_id_1',
  112. content: 'foo',
  113. },
  114. {
  115. user_id: 'user_id_1',
  116. content: 'bar',
  117. },
  118. ],
  119. },
  120. ])
  121. this.UserGetter.promises.getUsers.should.have.been.calledOnce
  122. })
  123. })
  124. })