فهرست منبع

Add test case and refactor

arvid-e 3 هفته پیش
والد
کامیت
bbb2efef06
1فایلهای تغییر یافته به همراه207 افزوده شده و 29 حذف شده
  1. 207 29
      apps/app/src/server/service/activity/update-activity.spec.ts

+ 207 - 29
apps/app/src/server/service/activity/update-activity.spec.ts

@@ -10,17 +10,21 @@ import { shouldGenerateUpdate } from './update-activity-logic';
 describe('shouldGenerateUpdate()', () => {
   let mongoServer: MongoMemoryServer;
 
-  const date = new Date();
+  let date = new Date();
+  const TWO_HOURS = 2 * 60 * 60 * 1000;
   const ONE_HOUR = 60 * 60 * 1000;
-  const ONE_MINUTE = 1 * 60;
+  const ONE_MINUTE = 1 * 60 * 1000;
 
-  const targetPageId = new mongoose.Types.ObjectId().toString();
+  let targetPageId: mongoose.Types.ObjectId;
+  let currentUserId: mongoose.Types.ObjectId;
+  let otherUserId: mongoose.Types.ObjectId;
+  let currentActivityId: mongoose.Types.ObjectId;
+  let olderActivityId: mongoose.Types.ObjectId;
+  let createActivityId: mongoose.Types.ObjectId;
 
-  const currentUserId = new mongoose.Types.ObjectId().toString();
-  const otherUserId = new mongoose.Types.ObjectId().toString();
-
-  const currentActivityId = new mongoose.Types.ObjectId().toString();
-  const olderActivityId = new mongoose.Types.ObjectId().toString();
+  let targetPageIdStr: string;
+  let currentUserIdStr: string;
+  let currentActivityIdStr: string;
 
   beforeAll(async () => {
     mongoServer = await MongoMemoryServer.create();
@@ -34,17 +38,33 @@ describe('shouldGenerateUpdate()', () => {
 
   beforeEach(async () => {
     await Activity.deleteMany({});
+    await Revision.deleteMany({});
+
+    // Reset date and IDs between tests
+    date = new Date();
+    targetPageId = new mongoose.Types.ObjectId();
+    currentUserId = new mongoose.Types.ObjectId();
+    otherUserId = new mongoose.Types.ObjectId();
+    currentActivityId = new mongoose.Types.ObjectId();
+    olderActivityId = new mongoose.Types.ObjectId();
+    createActivityId = new mongoose.Types.ObjectId();
+
+    targetPageIdStr = targetPageId.toString();
+    currentUserIdStr = currentUserId.toString();
+    currentActivityIdStr = currentActivityId.toString();
   });
 
-  it('should generate update activity if latest update is by another user', async () => {
+  it('should generate update activity if: latest update is by another user, not first update', async () => {
     await Activity.insertMany([
+      // Create activity
       {
         user: currentUserId,
         action: SupportedAction.ACTION_PAGE_CREATE,
-        createdAt: new Date(),
+        createdAt: new Date(date.getTime() - TWO_HOURS),
         target: targetPageId,
-        _id: currentActivityId,
+        _id: createActivityId,
       },
+      // Latest activity
       {
         user: otherUserId,
         action: SupportedAction.ACTION_PAGE_UPDATE,
@@ -52,25 +72,101 @@ describe('shouldGenerateUpdate()', () => {
         target: targetPageId,
         _id: olderActivityId,
       },
+      // Current activity
+      {
+        user: currentUserId,
+        action: SupportedAction.ACTION_PAGE_UPDATE,
+        createdAt: new Date(),
+        target: targetPageId,
+        _id: currentActivityId,
+      },
+    ]);
+
+    // More than 2 revisions means it is NOT the first update
+    await Revision.insertMany([
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Newer content',
+        format: 'markdown',
+        author: currentUserId,
+      },
     ]);
 
     const result = await shouldGenerateUpdate({
-      targetPageId,
-      currentUserId,
-      currentActivityId,
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
     });
 
     expect(result).toBe(true);
   });
 
-  it('should not generate update activity if it is the first update activity by the creator', async () => {
+  it('should generate update activity if: page created by another user, first update', async () => {
+    await Activity.insertMany([
+      {
+        user: otherUserId,
+        action: SupportedAction.ACTION_PAGE_CREATE,
+        createdAt: new Date(date.getTime() - TWO_HOURS),
+        target: targetPageId,
+        _id: createActivityId,
+      },
+      {
+        user: currentUserId,
+        action: SupportedAction.ACTION_PAGE_UPDATE,
+        createdAt: new Date(),
+        target: targetPageId,
+        _id: currentActivityId,
+      },
+    ]);
+
+    await Revision.insertMany([
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+    ]);
+
+    const result = await shouldGenerateUpdate({
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
+    });
+    expect(result).toBe(true);
+  });
+
+  it('should not generate update activity if: update is made by the page creator, first update', async () => {
     await Activity.insertMany([
       {
         user: currentUserId,
         action: SupportedAction.ACTION_PAGE_CREATE,
         createdAt: new Date(date.getTime() - ONE_HOUR),
         target: targetPageId,
-        _id: olderActivityId,
+        _id: createActivityId,
       },
       {
         user: currentUserId,
@@ -99,22 +195,22 @@ describe('shouldGenerateUpdate()', () => {
     ]);
 
     const result = await shouldGenerateUpdate({
-      targetPageId,
-      currentUserId,
-      currentActivityId,
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
     });
 
     expect(result).toBe(false);
   });
 
-  it('should generate update activity if update is made by the same user and outside the suppression window', async () => {
+  it('should generate update activity if: update is by the same user, outside the suppression window, not first update', async () => {
     await Activity.insertMany([
       {
         user: currentUserId,
         action: SupportedAction.ACTION_PAGE_CREATE,
         createdAt: new Date(date.getTime() - ONE_HOUR),
         target: targetPageId,
-        _id: olderActivityId,
+        _id: createActivityId,
       },
       {
         user: currentUserId,
@@ -133,6 +229,13 @@ describe('shouldGenerateUpdate()', () => {
         format: 'markdown',
         author: currentUserId,
       },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
       {
         _id: new mongoose.Types.ObjectId(),
         pageId: targetPageId,
@@ -143,22 +246,22 @@ describe('shouldGenerateUpdate()', () => {
     ]);
 
     const result = await shouldGenerateUpdate({
-      targetPageId,
-      currentUserId,
-      currentActivityId,
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
     });
 
     expect(result).toBe(true);
   });
 
-  it('should not generate update activity if update is made by the same user and within the suppression window', async () => {
+  it('should not generate update activity if: update is made by the same user, within suppression window, not first update', async () => {
     await Activity.insertMany([
       {
         user: currentUserId,
         action: SupportedAction.ACTION_PAGE_CREATE,
         createdAt: new Date(date.getTime() - ONE_MINUTE),
         target: targetPageId,
-        _id: olderActivityId,
+        _id: createActivityId,
       },
       {
         user: currentUserId,
@@ -169,12 +272,87 @@ describe('shouldGenerateUpdate()', () => {
       },
     ]);
 
+    await Revision.insertMany([
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Newer content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+    ]);
+
     const result = await shouldGenerateUpdate({
-      targetPageId,
-      currentUserId,
-      currentActivityId,
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
     });
 
     expect(result).toBe(false);
   });
+
+  it('should generate update activity if: update is made by the same user, outside suppression window, not first update', async () => {
+    await Activity.insertMany([
+      {
+        user: currentUserId,
+        action: SupportedAction.ACTION_PAGE_CREATE,
+        createdAt: new Date(date.getTime() - ONE_HOUR),
+        target: targetPageId,
+        _id: createActivityId,
+      },
+      {
+        user: currentUserId,
+        action: SupportedAction.ACTION_PAGE_UPDATE,
+        createdAt: new Date(),
+        target: targetPageId,
+        _id: currentActivityId,
+      },
+    ]);
+
+    await Revision.insertMany([
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Old content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+      {
+        _id: new mongoose.Types.ObjectId(),
+        pageId: targetPageId,
+        body: 'Newer content',
+        format: 'markdown',
+        author: currentUserId,
+      },
+    ]);
+
+    const result = await shouldGenerateUpdate({
+      targetPageId: targetPageIdStr,
+      currentUserId: currentUserIdStr,
+      currentActivityId: currentActivityIdStr,
+    });
+
+    expect(result).toBe(true);
+  });
 });