update-activity.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { MongoMemoryServer } from 'mongodb-memory-server-core';
  2. import mongoose from 'mongoose';
  3. import { SupportedAction } from '~/interfaces/activity';
  4. import Activity from '~/server/models/activity';
  5. import { Revision } from '~/server/models/revision';
  6. import { shouldGenerateUpdate } from './update-activity-logic';
  7. describe('shouldGenerateUpdate()', () => {
  8. let mongoServer: MongoMemoryServer;
  9. const date = new Date();
  10. const ONE_HOUR = 60 * 60 * 1000;
  11. const ONE_MINUTE = 1 * 60;
  12. const targetPageId = new mongoose.Types.ObjectId().toString();
  13. const currentUserId = new mongoose.Types.ObjectId().toString();
  14. const otherUserId = new mongoose.Types.ObjectId().toString();
  15. const currentActivityId = new mongoose.Types.ObjectId().toString();
  16. const olderActivityId = new mongoose.Types.ObjectId().toString();
  17. beforeAll(async () => {
  18. mongoServer = await MongoMemoryServer.create();
  19. await mongoose.connect(mongoServer.getUri());
  20. });
  21. afterAll(async () => {
  22. await mongoose.disconnect();
  23. await mongoServer.stop();
  24. });
  25. beforeEach(async () => {
  26. await Activity.deleteMany({});
  27. });
  28. it('should generate update activity if latest update is by another user', async () => {
  29. await Activity.insertMany([
  30. {
  31. user: currentUserId,
  32. action: SupportedAction.ACTION_PAGE_CREATE,
  33. createdAt: new Date(),
  34. target: targetPageId,
  35. _id: currentActivityId,
  36. },
  37. {
  38. user: otherUserId,
  39. action: SupportedAction.ACTION_PAGE_UPDATE,
  40. createdAt: new Date(date.getTime() - ONE_HOUR),
  41. target: targetPageId,
  42. _id: olderActivityId,
  43. },
  44. ]);
  45. const result = await shouldGenerateUpdate({
  46. targetPageId,
  47. currentUserId,
  48. currentActivityId,
  49. });
  50. expect(result).toBe(true);
  51. });
  52. it('should not generate update activity if it is the first update activity by the creator', async () => {
  53. await Activity.insertMany([
  54. {
  55. user: currentUserId,
  56. action: SupportedAction.ACTION_PAGE_CREATE,
  57. createdAt: new Date(date.getTime() - ONE_HOUR),
  58. target: targetPageId,
  59. _id: olderActivityId,
  60. },
  61. {
  62. user: currentUserId,
  63. action: SupportedAction.ACTION_PAGE_UPDATE,
  64. createdAt: new Date(),
  65. target: targetPageId,
  66. _id: currentActivityId,
  67. },
  68. ]);
  69. await Revision.insertMany([
  70. {
  71. _id: new mongoose.Types.ObjectId(),
  72. pageId: targetPageId,
  73. body: 'Old content',
  74. format: 'markdown',
  75. author: currentUserId,
  76. },
  77. {
  78. _id: new mongoose.Types.ObjectId(),
  79. pageId: targetPageId,
  80. body: 'Newer content',
  81. format: 'markdown',
  82. author: currentUserId,
  83. },
  84. ]);
  85. const result = await shouldGenerateUpdate({
  86. targetPageId,
  87. currentUserId,
  88. currentActivityId,
  89. });
  90. expect(result).toBe(false);
  91. });
  92. it('should generate update activity if update is made by the same user and outside the suppression window', async () => {
  93. await Activity.insertMany([
  94. {
  95. user: currentUserId,
  96. action: SupportedAction.ACTION_PAGE_CREATE,
  97. createdAt: new Date(date.getTime() - ONE_HOUR),
  98. target: targetPageId,
  99. _id: olderActivityId,
  100. },
  101. {
  102. user: currentUserId,
  103. action: SupportedAction.ACTION_PAGE_UPDATE,
  104. createdAt: new Date(),
  105. target: targetPageId,
  106. _id: currentActivityId,
  107. },
  108. ]);
  109. await Revision.insertMany([
  110. {
  111. _id: new mongoose.Types.ObjectId(),
  112. pageId: targetPageId,
  113. body: 'Old content',
  114. format: 'markdown',
  115. author: currentUserId,
  116. },
  117. {
  118. _id: new mongoose.Types.ObjectId(),
  119. pageId: targetPageId,
  120. body: 'Newer content',
  121. format: 'markdown',
  122. author: currentUserId,
  123. },
  124. ]);
  125. const result = await shouldGenerateUpdate({
  126. targetPageId,
  127. currentUserId,
  128. currentActivityId,
  129. });
  130. expect(result).toBe(true);
  131. });
  132. it('should not generate update activity if update is made by the same user and within the suppression window', async () => {
  133. await Activity.insertMany([
  134. {
  135. user: currentUserId,
  136. action: SupportedAction.ACTION_PAGE_CREATE,
  137. createdAt: new Date(date.getTime() - ONE_MINUTE),
  138. target: targetPageId,
  139. _id: olderActivityId,
  140. },
  141. {
  142. user: currentUserId,
  143. action: SupportedAction.ACTION_PAGE_UPDATE,
  144. createdAt: new Date(),
  145. target: targetPageId,
  146. _id: currentActivityId,
  147. },
  148. ]);
  149. const result = await shouldGenerateUpdate({
  150. targetPageId,
  151. currentUserId,
  152. currentActivityId,
  153. });
  154. expect(result).toBe(false);
  155. });
  156. });