EditingAMessageTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 } = await ChatClient.getThreads(
  41. projectId
  42. )
  43. expect(response.statusCode).to.equal(200)
  44. expect(threads[threadId].messages.length).to.equal(1)
  45. expect(threads[threadId].messages[0].content).to.equal(newContent)
  46. })
  47. })
  48. describe('with the same user', function () {
  49. beforeEach(async function () {
  50. const { response } = await ChatClient.editMessageWithUser(
  51. projectId,
  52. threadId,
  53. messageId,
  54. userId,
  55. newContent
  56. )
  57. expect(response.statusCode).to.equal(204)
  58. })
  59. it('should then list the updated message in the threads', async function () {
  60. const { response, body: threads } = await ChatClient.getThreads(
  61. projectId
  62. )
  63. expect(response.statusCode).to.equal(200)
  64. expect(threads[threadId].messages.length).to.equal(1)
  65. expect(threads[threadId].messages[0].content).to.equal(newContent)
  66. })
  67. })
  68. describe('with another user', function () {
  69. beforeEach(async function () {
  70. const { response } = await ChatClient.editMessageWithUser(
  71. projectId,
  72. threadId,
  73. messageId,
  74. new ObjectId(),
  75. newContent
  76. )
  77. expect(response.statusCode).to.equal(404)
  78. })
  79. it('should then list the old message in the threads', async function () {
  80. const { response, body: threads } = await ChatClient.getThreads(
  81. projectId
  82. )
  83. expect(response.statusCode).to.equal(200)
  84. expect(threads[threadId].messages.length).to.equal(1)
  85. expect(threads[threadId].messages[0].content).to.equal(content)
  86. })
  87. })
  88. })
  89. })