logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

chat_service.spec.js (3525B)


  1. import chatService from '../../../../../src/services/chat_service/chat_service.js'
  2. const message1 = {
  3. id: '9wLkdcmQXD21Oy8lEX',
  4. idempotency_key: '1',
  5. created_at: (new Date('2020-06-22T18:45:53.000Z'))
  6. }
  7. const message2 = {
  8. id: '9wLkdp6ihaOVdNj8Wu',
  9. idempotency_key: '2',
  10. account_id: '9vmRb29zLQReckr5ay',
  11. created_at: (new Date('2020-06-22T18:45:56.000Z'))
  12. }
  13. const message3 = {
  14. id: '9wLke9zL4Dy4OZR2RM',
  15. idempotency_key: '3',
  16. account_id: '9vmRb29zLQReckr5ay',
  17. created_at: (new Date('2020-07-22T18:45:59.000Z'))
  18. }
  19. describe('chatService', () => {
  20. describe('.add', () => {
  21. it("Doesn't add duplicates", () => {
  22. const chat = chatService.empty()
  23. chatService.add(chat, { messages: [message1] })
  24. chatService.add(chat, { messages: [message1] })
  25. expect(chat.messages.length).to.eql(1)
  26. chatService.add(chat, { messages: [message2] })
  27. expect(chat.messages.length).to.eql(2)
  28. })
  29. it('Updates minId and lastMessage and newMessageCount', () => {
  30. const chat = chatService.empty()
  31. chatService.add(chat, { messages: [message1] })
  32. expect(chat.maxId).to.eql(message1.id)
  33. expect(chat.minId).to.eql(message1.id)
  34. expect(chat.newMessageCount).to.eql(1)
  35. chatService.add(chat, { messages: [message2] })
  36. expect(chat.maxId).to.eql(message2.id)
  37. expect(chat.minId).to.eql(message1.id)
  38. expect(chat.newMessageCount).to.eql(2)
  39. chatService.resetNewMessageCount(chat)
  40. expect(chat.newMessageCount).to.eql(0)
  41. expect(chat.lastSeenMessageId).to.eql(message2.id)
  42. // Add message with higher id
  43. chatService.add(chat, { messages: [message3] })
  44. expect(chat.newMessageCount).to.eql(1)
  45. })
  46. })
  47. describe('.delete', () => {
  48. it('Updates minId and lastMessage', () => {
  49. const chat = chatService.empty()
  50. chatService.add(chat, { messages: [message1] })
  51. chatService.add(chat, { messages: [message2] })
  52. chatService.add(chat, { messages: [message3] })
  53. expect(chat.maxId).to.eql(message3.id)
  54. expect(chat.minId).to.eql(message1.id)
  55. chatService.deleteMessage(chat, message3.id)
  56. expect(chat.maxId).to.eql(message2.id)
  57. expect(chat.minId).to.eql(message1.id)
  58. chatService.deleteMessage(chat, message1.id)
  59. expect(chat.maxId).to.eql(message2.id)
  60. expect(chat.minId).to.eql(message2.id)
  61. })
  62. })
  63. describe('.getView', () => {
  64. it('Inserts date separators', () => {
  65. const chat = chatService.empty()
  66. chatService.add(chat, { messages: [message1] })
  67. chatService.add(chat, { messages: [message2] })
  68. chatService.add(chat, { messages: [message3] })
  69. const view = chatService.getView(chat)
  70. expect(view.map(i => i.type)).to.eql(['date', 'message', 'message', 'date', 'message'])
  71. })
  72. })
  73. describe('.cullOlderMessages', () => {
  74. it('keeps 50 newest messages and idIndex matches', () => {
  75. const chat = chatService.empty()
  76. for (let i = 100; i > 0; i--) {
  77. // Use decimal values with toFixed to hack together constant length predictable strings
  78. chatService.add(chat, { messages: [{ ...message1, id: 'a' + (i / 1000).toFixed(3), idempotency_key: i }] })
  79. }
  80. chatService.cullOlderMessages(chat)
  81. expect(chat.messages.length).to.eql(50)
  82. expect(chat.messages[0].id).to.eql('a0.051')
  83. expect(chat.minId).to.eql('a0.051')
  84. expect(chat.messages[49].id).to.eql('a0.100')
  85. expect(Object.keys(chat.idIndex).length).to.eql(50)
  86. })
  87. })
  88. })