GettingMessagesTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 } = await ChatClient.getGlobalMessages(
  45. projectId
  46. )
  47. expect(response.statusCode).to.equal(200)
  48. expect(messages.length).to.equal(2)
  49. messages.reverse()
  50. expect(messages[0].content).to.equal(content1)
  51. expect(messages[0].user_id).to.equal(userId1)
  52. expect(messages[1].content).to.equal(content2)
  53. expect(messages[1].user_id).to.equal(userId2)
  54. })
  55. })
  56. describe('from all the threads', async function () {
  57. const projectId = new ObjectId().toString()
  58. const threadId1 = new ObjectId().toString()
  59. const threadId2 = new ObjectId().toString()
  60. before(async function () {
  61. const { response } = await ChatClient.sendMessage(
  62. projectId,
  63. threadId1,
  64. userId1,
  65. 'one'
  66. )
  67. expect(response.statusCode).to.equal(201)
  68. const { response: response2 } = await ChatClient.sendMessage(
  69. projectId,
  70. threadId2,
  71. userId2,
  72. 'two'
  73. )
  74. expect(response2.statusCode).to.equal(201)
  75. const { response: response3 } = await ChatClient.sendMessage(
  76. projectId,
  77. threadId1,
  78. userId1,
  79. 'three'
  80. )
  81. expect(response3.statusCode).to.equal(201)
  82. const { response: response4 } = await ChatClient.sendMessage(
  83. projectId,
  84. threadId2,
  85. userId2,
  86. 'four'
  87. )
  88. expect(response4.statusCode).to.equal(201)
  89. })
  90. it('should contain a dictionary of threads with messages with populated users', async function () {
  91. const { response, body: threads } = await ChatClient.getThreads(projectId)
  92. expect(response.statusCode).to.equal(200)
  93. expect(Object.keys(threads).length).to.equal(2)
  94. const thread1 = threads[threadId1]
  95. expect(thread1.messages.length).to.equal(2)
  96. const thread2 = threads[threadId2]
  97. expect(thread2.messages.length).to.equal(2)
  98. expect(thread1.messages[0].content).to.equal('one')
  99. expect(thread1.messages[0].user_id).to.equal(userId1)
  100. expect(thread1.messages[1].content).to.equal('three')
  101. expect(thread1.messages[1].user_id).to.equal(userId1)
  102. expect(thread2.messages[0].content).to.equal('two')
  103. expect(thread2.messages[0].user_id).to.equal(userId2)
  104. expect(thread2.messages[1].content).to.equal('four')
  105. expect(thread2.messages[1].user_id).to.equal(userId2)
  106. })
  107. })
  108. })