EditingAMessageTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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('Editing a message', async function () {
  6. let projectId, userId, threadId
  7. before(async function () {
  8. await ChatApp.ensureRunning()
  9. })
  10. describe('in a thread', async function () {
  11. const content = 'thread message'
  12. const newContent = 'updated thread message'
  13. let messageId
  14. beforeEach(async function () {
  15. projectId = new ObjectId().toString()
  16. userId = new ObjectId().toString()
  17. threadId = new ObjectId().toString()
  18. const { response, body: message } = await ChatClient.sendMessage(
  19. projectId,
  20. threadId,
  21. userId,
  22. content
  23. )
  24. expect(response.statusCode).to.equal(201)
  25. expect(message.id).to.exist
  26. expect(message.content).to.equal(content)
  27. messageId = message.id
  28. })
  29. describe('without user', function () {
  30. beforeEach(async function () {
  31. const { response } = await ChatClient.editMessage(
  32. projectId,
  33. threadId,
  34. messageId,
  35. newContent
  36. )
  37. expect(response.statusCode).to.equal(204)
  38. })
  39. it('should then list the updated message in the threads', async function () {
  40. const { response, body: threads } =
  41. await ChatClient.getThreads(projectId)
  42. expect(response.statusCode).to.equal(200)
  43. expect(threads[threadId].messages.length).to.equal(1)
  44. expect(threads[threadId].messages[0].content).to.equal(newContent)
  45. })
  46. })
  47. describe('with the same user', function () {
  48. beforeEach(async function () {
  49. const { response } = await ChatClient.editMessageWithUser(
  50. projectId,
  51. threadId,
  52. messageId,
  53. userId,
  54. newContent
  55. )
  56. expect(response.statusCode).to.equal(204)
  57. })
  58. it('should then list the updated message in the threads', async function () {
  59. const { response, body: threads } =
  60. await ChatClient.getThreads(projectId)
  61. expect(response.statusCode).to.equal(200)
  62. expect(threads[threadId].messages.length).to.equal(1)
  63. expect(threads[threadId].messages[0].content).to.equal(newContent)
  64. })
  65. })
  66. describe('with another user', function () {
  67. beforeEach(async function () {
  68. const { response } = await ChatClient.editMessageWithUser(
  69. projectId,
  70. threadId,
  71. messageId,
  72. new ObjectId(),
  73. newContent
  74. )
  75. expect(response.statusCode).to.equal(404)
  76. })
  77. it('should then list the old message in the threads', async function () {
  78. const { response, body: threads } =
  79. await ChatClient.getThreads(projectId)
  80. expect(response.statusCode).to.equal(200)
  81. expect(threads[threadId].messages.length).to.equal(1)
  82. expect(threads[threadId].messages[0].content).to.equal(content)
  83. })
  84. })
  85. })
  86. })