update-activity.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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() - TWO_HOURS),
  189. target: targetPageId,
  190. _id: createActivityId,
  191. },
  192. {
  193. user: currentUserId,
  194. action: SupportedAction.ACTION_PAGE_UPDATE,
  195. createdAt: new Date(date.getTime() - ONE_HOUR),
  196. target: targetPageId,
  197. _id: olderActivityId,
  198. },
  199. {
  200. user: currentUserId,
  201. action: SupportedAction.ACTION_PAGE_UPDATE,
  202. createdAt: new Date(),
  203. target: targetPageId,
  204. _id: currentActivityId,
  205. },
  206. ]);
  207. await Revision.insertMany([
  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: 'Old content',
  219. format: 'markdown',
  220. author: currentUserId,
  221. },
  222. {
  223. _id: new mongoose.Types.ObjectId(),
  224. pageId: targetPageId,
  225. body: 'Newer content',
  226. format: 'markdown',
  227. author: currentUserId,
  228. },
  229. ]);
  230. const result = await shouldGenerateUpdate({
  231. targetPageId: targetPageIdStr,
  232. currentUserId: currentUserIdStr,
  233. currentActivityId: currentActivityIdStr,
  234. });
  235. expect(result).toBe(true);
  236. });
  237. it('should not generate update activity if: update is made by the same user, within suppression window, not first update', async () => {
  238. await Activity.insertMany([
  239. {
  240. user: currentUserId,
  241. action: SupportedAction.ACTION_PAGE_CREATE,
  242. createdAt: new Date(date.getTime() - TWO_HOURS),
  243. target: targetPageId,
  244. _id: createActivityId,
  245. },
  246. {
  247. user: currentUserId,
  248. action: SupportedAction.ACTION_PAGE_UPDATE,
  249. createdAt: new Date(date.getTime() - ONE_MINUTE),
  250. target: targetPageId,
  251. _id: olderActivityId,
  252. },
  253. {
  254. user: currentUserId,
  255. action: SupportedAction.ACTION_PAGE_UPDATE,
  256. createdAt: new Date(),
  257. target: targetPageId,
  258. _id: currentActivityId,
  259. },
  260. ]);
  261. await Revision.insertMany([
  262. {
  263. _id: new mongoose.Types.ObjectId(),
  264. pageId: targetPageId,
  265. body: 'Old content',
  266. format: 'markdown',
  267. author: currentUserId,
  268. },
  269. {
  270. _id: new mongoose.Types.ObjectId(),
  271. pageId: targetPageId,
  272. body: 'Old content',
  273. format: 'markdown',
  274. author: currentUserId,
  275. },
  276. {
  277. _id: new mongoose.Types.ObjectId(),
  278. pageId: targetPageId,
  279. body: 'Newer content',
  280. format: 'markdown',
  281. author: currentUserId,
  282. },
  283. ]);
  284. const result = await shouldGenerateUpdate({
  285. targetPageId: targetPageIdStr,
  286. currentUserId: currentUserIdStr,
  287. currentActivityId: currentActivityIdStr,
  288. });
  289. expect(result).toBe(false);
  290. });
  291. it('should generate update activity if: update is made by the same user, outside suppression window, not first update', async () => {
  292. await Activity.insertMany([
  293. {
  294. user: currentUserId,
  295. action: SupportedAction.ACTION_PAGE_CREATE,
  296. createdAt: new Date(date.getTime() - TWO_HOURS),
  297. target: targetPageId,
  298. _id: createActivityId,
  299. },
  300. {
  301. user: currentUserId,
  302. action: SupportedAction.ACTION_PAGE_UPDATE,
  303. createdAt: new Date(date.getTime() - ONE_HOUR),
  304. target: targetPageId,
  305. _id: olderActivityId,
  306. },
  307. {
  308. user: currentUserId,
  309. action: SupportedAction.ACTION_PAGE_UPDATE,
  310. createdAt: new Date(),
  311. target: targetPageId,
  312. _id: currentActivityId,
  313. },
  314. ]);
  315. await Revision.insertMany([
  316. {
  317. _id: new mongoose.Types.ObjectId(),
  318. pageId: targetPageId,
  319. body: 'Old content',
  320. format: 'markdown',
  321. author: currentUserId,
  322. },
  323. {
  324. _id: new mongoose.Types.ObjectId(),
  325. pageId: targetPageId,
  326. body: 'Old content',
  327. format: 'markdown',
  328. author: currentUserId,
  329. },
  330. {
  331. _id: new mongoose.Types.ObjectId(),
  332. pageId: targetPageId,
  333. body: 'Newer content',
  334. format: 'markdown',
  335. author: currentUserId,
  336. },
  337. ]);
  338. const result = await shouldGenerateUpdate({
  339. targetPageId: targetPageIdStr,
  340. currentUserId: currentUserIdStr,
  341. currentActivityId: currentActivityIdStr,
  342. });
  343. expect(result).toBe(true);
  344. });
  345. });