GettingMessagesTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. describe('Getting messages', async function () {
  6. const userId1 = ObjectId().toString()
  7. const userId2 = ObjectId().toString()
  8. const content1 = 'foo bar'
  9. const content2 = 'hello world'
  10. before(async function () {
  11. await ChatApp.ensureRunning()
  12. })
  13. describe('globally', async function () {
  14. const projectId = ObjectId().toString()
  15. before(async function () {
  16. const { response } = await ChatClient.sendGlobalMessage(
  17. projectId,
  18. userId1,
  19. content1
  20. )
  21. expect(response.statusCode).to.equal(201)
  22. const { response: response2 } = await ChatClient.sendGlobalMessage(
  23. projectId,
  24. userId2,
  25. content2
  26. )
  27. expect(response2.statusCode).to.equal(201)
  28. const { response: response3, body } = await ChatClient.checkStatus()
  29. expect(response3.statusCode).to.equal(200)
  30. expect(body).to.equal('chat is alive')
  31. })
  32. it('should contain the messages and populated users when getting the messages', async function () {
  33. const { response, body: messages } = await ChatClient.getGlobalMessages(
  34. projectId
  35. )
  36. expect(response.statusCode).to.equal(200)
  37. expect(messages.length).to.equal(2)
  38. messages.reverse()
  39. expect(messages[0].content).to.equal(content1)
  40. expect(messages[0].user_id).to.equal(userId1)
  41. expect(messages[1].content).to.equal(content2)
  42. expect(messages[1].user_id).to.equal(userId2)
  43. })
  44. })
  45. describe('from all the threads', async function () {
  46. const projectId = ObjectId().toString()
  47. const threadId1 = ObjectId().toString()
  48. const threadId2 = ObjectId().toString()
  49. before(async function () {
  50. const { response } = await ChatClient.sendMessage(
  51. projectId,
  52. threadId1,
  53. userId1,
  54. 'one'
  55. )
  56. expect(response.statusCode).to.equal(201)
  57. const { response: response2 } = await ChatClient.sendMessage(
  58. projectId,
  59. threadId2,
  60. userId2,
  61. 'two'
  62. )
  63. expect(response2.statusCode).to.equal(201)
  64. const { response: response3 } = await ChatClient.sendMessage(
  65. projectId,
  66. threadId1,
  67. userId1,
  68. 'three'
  69. )
  70. expect(response3.statusCode).to.equal(201)
  71. const { response: response4 } = await ChatClient.sendMessage(
  72. projectId,
  73. threadId2,
  74. userId2,
  75. 'four'
  76. )
  77. expect(response4.statusCode).to.equal(201)
  78. })
  79. it('should contain a dictionary of threads with messages with populated users', async function () {
  80. const { response, body: threads } = await ChatClient.getThreads(projectId)
  81. expect(response.statusCode).to.equal(200)
  82. expect(Object.keys(threads).length).to.equal(2)
  83. const thread1 = threads[threadId1]
  84. expect(thread1.messages.length).to.equal(2)
  85. const thread2 = threads[threadId2]
  86. expect(thread2.messages.length).to.equal(2)
  87. expect(thread1.messages[0].content).to.equal('one')
  88. expect(thread1.messages[0].user_id).to.equal(userId1)
  89. expect(thread1.messages[1].content).to.equal('three')
  90. expect(thread1.messages[1].user_id).to.equal(userId1)
  91. expect(thread2.messages[0].content).to.equal('two')
  92. expect(thread2.messages[0].user_id).to.equal(userId2)
  93. expect(thread2.messages[1].content).to.equal('four')
  94. expect(thread2.messages[1].user_id).to.equal(userId2)
  95. })
  96. })
  97. })