GettingAThreadTests.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 a thread', async function () {
  6. const userId1 = new ObjectId().toString()
  7. const userId2 = new ObjectId().toString()
  8. const content1 = 'first message'
  9. const content2 = 'second message'
  10. const projectId = new ObjectId().toString()
  11. const threadId = new ObjectId().toString()
  12. before(async function () {
  13. await ChatApp.ensureRunning()
  14. })
  15. describe('when thread exists', async function () {
  16. before(async function () {
  17. // Send first message to create thread
  18. const { response } = await ChatClient.sendMessage(
  19. projectId,
  20. threadId,
  21. userId1,
  22. content1
  23. )
  24. expect(response.statusCode).to.equal(201)
  25. // Send second message to the same thread
  26. const { response: response2 } = await ChatClient.sendMessage(
  27. projectId,
  28. threadId,
  29. userId2,
  30. content2
  31. )
  32. expect(response2.statusCode).to.equal(201)
  33. })
  34. it('should return the thread with all messages', async function () {
  35. const { response, body: thread } = await ChatClient.getThread(
  36. projectId,
  37. threadId
  38. )
  39. expect(response.statusCode).to.equal(200)
  40. expect(thread).to.exist
  41. expect(thread.messages).to.exist
  42. expect(thread.messages.length).to.equal(2)
  43. expect(thread.messages[0].content).to.equal(content1)
  44. expect(thread.messages[0].user_id).to.equal(userId1)
  45. expect(thread.messages[1].content).to.equal(content2)
  46. expect(thread.messages[1].user_id).to.equal(userId2)
  47. })
  48. })
  49. describe('when thread does not exist', async function () {
  50. it('should return 404', async function () {
  51. const nonExistentThreadId = new ObjectId().toString()
  52. const { response } = await ChatClient.getThread(
  53. projectId,
  54. nonExistentThreadId
  55. )
  56. expect(response.statusCode).to.equal(404)
  57. })
  58. })
  59. })