update-activity.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. let date = new Date();
  10. const TWO_HOURS = 2 * 60 * 60 * 1000;
  11. const ONE_HOUR = 60 * 60 * 1000;
  12. const ONE_MINUTE = 1 * 60 * 1000;
  13. let targetPageId: mongoose.Types.ObjectId;
  14. let currentUserId: mongoose.Types.ObjectId;
  15. let otherUserId: mongoose.Types.ObjectId;
  16. let currentActivityId: mongoose.Types.ObjectId;
  17. let olderActivityId: mongoose.Types.ObjectId;
  18. let createActivityId: mongoose.Types.ObjectId;
  19. let targetPageIdStr: string;
  20. let currentUserIdStr: string;
  21. let currentActivityIdStr: string;
  22. beforeAll(async () => {
  23. mongoServer = await MongoMemoryServer.create();
  24. await mongoose.connect(mongoServer.getUri());
  25. });
  26. afterAll(async () => {
  27. await mongoose.disconnect();
  28. await mongoServer.stop();
  29. });
  30. beforeEach(async () => {
  31. await Activity.deleteMany({});
  32. await Revision.deleteMany({});
  33. // Reset date and IDs between tests
  34. date = new Date();
  35. targetPageId = new mongoose.Types.ObjectId();
  36. currentUserId = new mongoose.Types.ObjectId();
  37. otherUserId = new mongoose.Types.ObjectId();
  38. currentActivityId = new mongoose.Types.ObjectId();
  39. olderActivityId = new mongoose.Types.ObjectId();
  40. createActivityId = new mongoose.Types.ObjectId();
  41. targetPageIdStr = targetPageId.toString();
  42. currentUserIdStr = currentUserId.toString();
  43. currentActivityIdStr = currentActivityId.toString();
  44. });
  45. it('should generate update activity if: latest update is by another user, not first update', async () => {
  46. await Activity.insertMany([
  47. // Create activity
  48. {
  49. user: currentUserId,
  50. action: SupportedAction.ACTION_PAGE_CREATE,
  51. createdAt: new Date(date.getTime() - TWO_HOURS),
  52. target: targetPageId,
  53. _id: createActivityId,
  54. },
  55. // Latest activity
  56. {
  57. user: otherUserId,
  58. action: SupportedAction.ACTION_PAGE_UPDATE,
  59. createdAt: new Date(date.getTime() - ONE_HOUR),
  60. target: targetPageId,
  61. _id: olderActivityId,
  62. },
  63. // Current activity
  64. {
  65. user: currentUserId,
  66. action: SupportedAction.ACTION_PAGE_UPDATE,
  67. createdAt: new Date(),
  68. target: targetPageId,
  69. _id: currentActivityId,
  70. },
  71. ]);
  72. // More than 2 revisions means it is NOT the first update
  73. await Revision.insertMany([
  74. {
  75. _id: new mongoose.Types.ObjectId(),
  76. pageId: targetPageId,
  77. body: 'Old content',
  78. format: 'markdown',
  79. author: currentUserId,
  80. },
  81. {
  82. _id: new mongoose.Types.ObjectId(),
  83. pageId: targetPageId,
  84. body: 'Old content',
  85. format: 'markdown',
  86. author: currentUserId,
  87. },
  88. {
  89. _id: new mongoose.Types.ObjectId(),
  90. pageId: targetPageId,
  91. body: 'Newer content',
  92. format: 'markdown',
  93. author: currentUserId,
  94. },
  95. ]);
  96. const result = await shouldGenerateUpdate({
  97. targetPageId: targetPageIdStr,
  98. currentUserId: currentUserIdStr,
  99. currentActivityId: currentActivityIdStr,
  100. });
  101. expect(result).toBe(true);
  102. });
  103. it('should generate update activity if: page created by another user, first update', async () => {
  104. await Activity.insertMany([
  105. {
  106. user: otherUserId,
  107. action: SupportedAction.ACTION_PAGE_CREATE,
  108. createdAt: new Date(date.getTime() - TWO_HOURS),
  109. target: targetPageId,
  110. _id: createActivityId,
  111. },
  112. {
  113. user: currentUserId,
  114. action: SupportedAction.ACTION_PAGE_UPDATE,
  115. createdAt: new Date(),
  116. target: targetPageId,
  117. _id: currentActivityId,
  118. },
  119. ]);
  120. await Revision.insertMany([
  121. {
  122. _id: new mongoose.Types.ObjectId(),
  123. pageId: targetPageId,
  124. body: 'Old content',
  125. format: 'markdown',
  126. author: currentUserId,
  127. },
  128. {
  129. _id: new mongoose.Types.ObjectId(),
  130. pageId: targetPageId,
  131. body: 'Old content',
  132. format: 'markdown',
  133. author: currentUserId,
  134. },
  135. ]);
  136. const result = await shouldGenerateUpdate({
  137. targetPageId: targetPageIdStr,
  138. currentUserId: currentUserIdStr,
  139. currentActivityId: currentActivityIdStr,
  140. });
  141. expect(result).toBe(true);
  142. });
  143. it('should not generate update activity if: update is made by the page creator, first update', async () => {
  144. await Activity.insertMany([
  145. {
  146. user: currentUserId,
  147. action: SupportedAction.ACTION_PAGE_CREATE,
  148. createdAt: new Date(date.getTime() - ONE_HOUR),
  149. target: targetPageId,
  150. _id: createActivityId,
  151. },
  152. {
  153. user: currentUserId,
  154. action: SupportedAction.ACTION_PAGE_UPDATE,
  155. createdAt: new Date(),
  156. target: targetPageId,
  157. _id: currentActivityId,
  158. },
  159. ]);
  160. await Revision.insertMany([
  161. {
  162. _id: new mongoose.Types.ObjectId(),
  163. pageId: targetPageId,
  164. body: 'Old content',
  165. format: 'markdown',
  166. author: currentUserId,
  167. },
  168. {
  169. _id: new mongoose.Types.ObjectId(),
  170. pageId: targetPageId,
  171. body: 'Newer content',
  172. format: 'markdown',
  173. author: currentUserId,
  174. },
  175. ]);
  176. const result = await shouldGenerateUpdate({
  177. targetPageId: targetPageIdStr,
  178. currentUserId: currentUserIdStr,
  179. currentActivityId: currentActivityIdStr,
  180. });
  181. expect(result).toBe(false);
  182. });
  183. it('should generate update activity if: update is by the same user, outside the suppression window, not first update', async () => {
  184. await Activity.insertMany([
  185. {
  186. user: currentUserId,
  187. action: SupportedAction.ACTION_PAGE_CREATE,
  188. createdAt: new Date(date.getTime() - ONE_HOUR),
  189. target: targetPageId,
  190. _id: createActivityId,
  191. },
  192. {
  193. user: currentUserId,
  194. action: SupportedAction.ACTION_PAGE_UPDATE,
  195. createdAt: new Date(),
  196. target: targetPageId,
  197. _id: currentActivityId,
  198. },
  199. ]);
  200. await Revision.insertMany([
  201. {
  202. _id: new mongoose.Types.ObjectId(),
  203. pageId: targetPageId,
  204. body: 'Old content',
  205. format: 'markdown',
  206. author: currentUserId,
  207. },
  208. {
  209. _id: new mongoose.Types.ObjectId(),
  210. pageId: targetPageId,
  211. body: 'Old content',
  212. format: 'markdown',
  213. author: currentUserId,
  214. },
  215. {
  216. _id: new mongoose.Types.ObjectId(),
  217. pageId: targetPageId,
  218. body: 'Newer content',
  219. format: 'markdown',
  220. author: currentUserId,
  221. },
  222. ]);
  223. const result = await shouldGenerateUpdate({
  224. targetPageId: targetPageIdStr,
  225. currentUserId: currentUserIdStr,
  226. currentActivityId: currentActivityIdStr,
  227. });
  228. expect(result).toBe(true);
  229. });
  230. it('should not generate update activity if: update is made by the same user, within suppression window, not first update', async () => {
  231. await Activity.insertMany([
  232. {
  233. user: currentUserId,
  234. action: SupportedAction.ACTION_PAGE_CREATE,
  235. createdAt: new Date(date.getTime() - ONE_MINUTE),
  236. target: targetPageId,
  237. _id: createActivityId,
  238. },
  239. {
  240. user: currentUserId,
  241. action: SupportedAction.ACTION_PAGE_UPDATE,
  242. createdAt: new Date(),
  243. target: targetPageId,
  244. _id: currentActivityId,
  245. },
  246. ]);
  247. await Revision.insertMany([
  248. {
  249. _id: new mongoose.Types.ObjectId(),
  250. pageId: targetPageId,
  251. body: 'Old content',
  252. format: 'markdown',
  253. author: currentUserId,
  254. },
  255. {
  256. _id: new mongoose.Types.ObjectId(),
  257. pageId: targetPageId,
  258. body: 'Old content',
  259. format: 'markdown',
  260. author: currentUserId,
  261. },
  262. {
  263. _id: new mongoose.Types.ObjectId(),
  264. pageId: targetPageId,
  265. body: 'Newer content',
  266. format: 'markdown',
  267. author: currentUserId,
  268. },
  269. ]);
  270. const result = await shouldGenerateUpdate({
  271. targetPageId: targetPageIdStr,
  272. currentUserId: currentUserIdStr,
  273. currentActivityId: currentActivityIdStr,
  274. });
  275. expect(result).toBe(false);
  276. });
  277. it('should generate update activity if: update is made by the same user, outside suppression window, not first update', async () => {
  278. await Activity.insertMany([
  279. {
  280. user: currentUserId,
  281. action: SupportedAction.ACTION_PAGE_CREATE,
  282. createdAt: new Date(date.getTime() - ONE_HOUR),
  283. target: targetPageId,
  284. _id: createActivityId,
  285. },
  286. {
  287. user: currentUserId,
  288. action: SupportedAction.ACTION_PAGE_UPDATE,
  289. createdAt: new Date(),
  290. target: targetPageId,
  291. _id: currentActivityId,
  292. },
  293. ]);
  294. await Revision.insertMany([
  295. {
  296. _id: new mongoose.Types.ObjectId(),
  297. pageId: targetPageId,
  298. body: 'Old content',
  299. format: 'markdown',
  300. author: currentUserId,
  301. },
  302. {
  303. _id: new mongoose.Types.ObjectId(),
  304. pageId: targetPageId,
  305. body: 'Old content',
  306. format: 'markdown',
  307. author: currentUserId,
  308. },
  309. {
  310. _id: new mongoose.Types.ObjectId(),
  311. pageId: targetPageId,
  312. body: 'Newer content',
  313. format: 'markdown',
  314. author: currentUserId,
  315. },
  316. ]);
  317. const result = await shouldGenerateUpdate({
  318. targetPageId: targetPageIdStr,
  319. currentUserId: currentUserIdStr,
  320. currentActivityId: currentActivityIdStr,
  321. });
  322. expect(result).toBe(true);
  323. });
  324. });