GettingMessagesTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { ObjectId } from '../../../app/js/mongodb.js'
  2. import { expect } from 'chai'
  3. import * as ChatClient from './helpers/ChatClient.js'
  4. import * as ChatApp from './helpers/ChatApp.js'
  5. async function getCount() {
  6. return await ChatClient.getMetric(line => {
  7. return (
  8. line.includes('timer_http_request_count') &&
  9. line.includes('path="project_{projectId}_messages"') &&
  10. line.includes('method="POST"')
  11. )
  12. })
  13. }
  14. describe('Getting messages', async function () {
  15. const userId1 = new ObjectId().toString()
  16. const userId2 = new ObjectId().toString()
  17. const content1 = 'foo bar'
  18. const content2 = 'hello world'
  19. before(async function () {
  20. await ChatApp.ensureRunning()
  21. })
  22. describe('globally', async function () {
  23. const projectId = new ObjectId().toString()
  24. before(async function () {
  25. const previousCount = await getCount()
  26. const { response } = await ChatClient.sendGlobalMessage(
  27. projectId,
  28. userId1,
  29. content1
  30. )
  31. expect(response.statusCode).to.equal(201)
  32. const { response: response2 } = await ChatClient.sendGlobalMessage(
  33. projectId,
  34. userId2,
  35. content2
  36. )
  37. expect(response2.statusCode).to.equal(201)
  38. const { response: response3, body } = await ChatClient.checkStatus()
  39. expect(response3.statusCode).to.equal(200)
  40. expect(body).to.equal('chat is alive')
  41. expect(await getCount()).to.equal(previousCount + 2)
  42. })
  43. it('should contain the messages and populated users when getting the messages', async function () {
  44. const { response, body: messages } =
  45. await ChatClient.getGlobalMessages(projectId)
  46. expect(response.statusCode).to.equal(200)
  47. expect(messages.length).to.equal(2)
  48. messages.reverse()
  49. expect(messages[0].content).to.equal(content1)
  50. expect(messages[0].user_id).to.equal(userId1)
  51. expect(messages[1].content).to.equal(content2)
  52. expect(messages[1].user_id).to.equal(userId2)
  53. })
  54. })
  55. describe('from all the threads', async function () {
  56. const projectId = new ObjectId().toString()
  57. const threadId1 = new ObjectId().toString()
  58. const threadId2 = new ObjectId().toString()
  59. before(async function () {
  60. const { response } = await ChatClient.sendMessage(
  61. projectId,
  62. threadId1,
  63. userId1,
  64. 'one'
  65. )
  66. expect(response.statusCode).to.equal(201)
  67. const { response: response2 } = await ChatClient.sendMessage(
  68. projectId,
  69. threadId2,
  70. userId2,
  71. 'two'
  72. )
  73. expect(response2.statusCode).to.equal(201)
  74. const { response: response3 } = await ChatClient.sendMessage(
  75. projectId,
  76. threadId1,
  77. userId1,
  78. 'three'
  79. )
  80. expect(response3.statusCode).to.equal(201)
  81. const { response: response4 } = await ChatClient.sendMessage(
  82. projectId,
  83. threadId2,
  84. userId2,
  85. 'four'
  86. )
  87. expect(response4.statusCode).to.equal(201)
  88. })
  89. it('should contain a dictionary of threads with messages with populated users', async function () {
  90. const { response, body: threads } = await ChatClient.getThreads(projectId)
  91. expect(response.statusCode).to.equal(200)
  92. expect(Object.keys(threads).length).to.equal(2)
  93. const thread1 = threads[threadId1]
  94. expect(thread1.messages.length).to.equal(2)
  95. const thread2 = threads[threadId2]
  96. expect(thread2.messages.length).to.equal(2)
  97. expect(thread1.messages[0].content).to.equal('one')
  98. expect(thread1.messages[0].user_id).to.equal(userId1)
  99. expect(thread1.messages[1].content).to.equal('three')
  100. expect(thread1.messages[1].user_id).to.equal(userId1)
  101. expect(thread2.messages[0].content).to.equal('two')
  102. expect(thread2.messages[0].user_id).to.equal(userId2)
  103. expect(thread2.messages[1].content).to.equal('four')
  104. expect(thread2.messages[1].user_id).to.equal(userId2)
  105. })
  106. })
  107. describe('from a list of threads', function () {
  108. const projectId = new ObjectId().toString()
  109. const threadId1 = new ObjectId().toString()
  110. const threadId2 = new ObjectId().toString()
  111. const threadId3 = new ObjectId().toString()
  112. before(async function () {
  113. const { response } = await ChatClient.sendMessage(
  114. projectId,
  115. threadId1,
  116. userId1,
  117. 'one'
  118. )
  119. expect(response.statusCode).to.equal(201)
  120. const { response: response2 } = await ChatClient.sendMessage(
  121. projectId,
  122. threadId2,
  123. userId2,
  124. 'two'
  125. )
  126. expect(response2.statusCode).to.equal(201)
  127. const { response: response3 } = await ChatClient.sendMessage(
  128. projectId,
  129. threadId1,
  130. userId1,
  131. 'three'
  132. )
  133. expect(response3.statusCode).to.equal(201)
  134. })
  135. it('should contain a dictionary of threads with messages with populated users', async function () {
  136. const { response, body: threads } = await ChatClient.generateThreadData(
  137. projectId,
  138. [threadId1, threadId3]
  139. )
  140. expect(response.statusCode).to.equal(200)
  141. expect(Object.keys(threads).length).to.equal(1)
  142. const thread1 = threads[threadId1]
  143. expect(thread1.messages.length).to.equal(2)
  144. expect(thread1.messages[0].content).to.equal('one')
  145. expect(thread1.messages[0].user_id).to.equal(userId1)
  146. expect(thread1.messages[1].content).to.equal('three')
  147. expect(thread1.messages[1].user_id).to.equal(userId1)
  148. })
  149. })
  150. })