Răsfoiți Sursa

Test that update activity generates if last updated user is different

arvid-e 3 săptămâni în urmă
părinte
comite
c4ba07bfa4

+ 4 - 4
apps/app/src/server/service/activity/update-activity-logic.ts

@@ -6,14 +6,14 @@ import { SupportedAction } from '~/interfaces/activity';
 type GenerateUpdatePayload = {
   currentUserId: string;
   targetPageId: string;
-  latestSupportedActivityId: string;
+  currentActivityId: string;
 };
 
 const MINIMUM_REVISION_FOR_ACTIVITY = 2;
-const SUPPRESION_UPDATE_WINDOW_MS = 1 * 10 * 1000; // 5 min
+const SUPPRESION_UPDATE_WINDOW_MS = 5 * 60 * 1000; // 5 min
 
 export const shouldGenerateUpdate = async (payload: GenerateUpdatePayload) => {
-  const { targetPageId, latestSupportedActivityId, currentUserId } = payload;
+  const { targetPageId, currentActivityId, currentUserId } = payload;
   const Activity = mongoose.model('Activity');
 
   // Get most recent update or create activity on the page
@@ -25,7 +25,7 @@ export const shouldGenerateUpdate = async (payload: GenerateUpdatePayload) => {
         SupportedAction.ACTION_PAGE_UPDATE,
       ],
     },
-    _id: { $ne: latestSupportedActivityId },
+    _id: { $ne: currentActivityId },
   }).sort({ createdAt: -1 });
 
   const isLastActivityByMe =

+ 60 - 0
apps/app/src/server/service/activity/update-activity.spec.ts

@@ -0,0 +1,60 @@
+import { MongoMemoryServer } from 'mongodb-memory-server-core';
+import mongoose from 'mongoose';
+
+import { SupportedAction } from '~/interfaces/activity';
+import Activity from '~/server/models/activity';
+import { Revision } from '~/server/models/revision';
+
+import { shouldGenerateUpdate } from './update-activity-logic';
+
+describe('shouldGenerateUpdate()', () => {
+  let mongoServer: MongoMemoryServer;
+
+  beforeAll(async () => {
+    mongoServer = await MongoMemoryServer.create();
+    await mongoose.connect(mongoServer.getUri());
+  });
+
+  afterAll(async () => {
+    await mongoose.disconnect();
+    await mongoServer.stop();
+  });
+
+  beforeEach(async () => {
+    await Activity.deleteMany({});
+  });
+
+  it('should generate update activity if latest update is by another user', async () => {
+    const currentUserId = new mongoose.Types.ObjectId().toString();
+    const otherUserId = new mongoose.Types.ObjectId().toString();
+    const targetPageId = new mongoose.Types.ObjectId().toString();
+
+    const currentActivityId = new mongoose.Types.ObjectId().toString();
+    const olderActivityId = new mongoose.Types.ObjectId().toString();
+
+    await Activity.insertMany([
+      {
+        user: currentUserId,
+        action: SupportedAction.ACTION_PAGE_CREATE,
+        createdAt: new Date('2025-10-31T23:59:59Z'),
+        target: targetPageId,
+        _id: currentActivityId,
+      },
+      {
+        user: otherUserId,
+        action: SupportedAction.ACTION_PAGE_CREATE,
+        createdAt: new Date('2025-10-30T23:59:59Z'),
+        target: targetPageId,
+        _id: olderActivityId,
+      },
+    ]);
+
+    const result = await shouldGenerateUpdate({
+      targetPageId,
+      currentUserId,
+      currentActivityId,
+    });
+
+    expect(result).toBe(true);
+  });
+});