| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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;
- const date = new Date();
- const ONE_HOUR = 60 * 60 * 1000;
- const ONE_MINUTE = 1 * 60;
- const targetPageId = new mongoose.Types.ObjectId().toString();
- 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();
- 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 () => {
- await Activity.insertMany([
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_CREATE,
- createdAt: new Date(),
- target: targetPageId,
- _id: currentActivityId,
- },
- {
- user: otherUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- createdAt: new Date(date.getTime() - ONE_HOUR),
- target: targetPageId,
- _id: olderActivityId,
- },
- ]);
- const result = await shouldGenerateUpdate({
- targetPageId,
- currentUserId,
- currentActivityId,
- });
- expect(result).toBe(true);
- });
- it('should not generate update activity if it is the first update activity by the creator', async () => {
- await Activity.insertMany([
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_CREATE,
- createdAt: new Date(date.getTime() - ONE_HOUR),
- target: targetPageId,
- _id: olderActivityId,
- },
- {
- 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: 'Newer content',
- format: 'markdown',
- author: currentUserId,
- },
- ]);
- const result = await shouldGenerateUpdate({
- targetPageId,
- currentUserId,
- currentActivityId,
- });
- expect(result).toBe(false);
- });
- it('should generate update activity if update is made by the same user and outside the suppression window', async () => {
- await Activity.insertMany([
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_CREATE,
- createdAt: new Date(date.getTime() - ONE_HOUR),
- target: targetPageId,
- _id: olderActivityId,
- },
- {
- 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: 'Newer content',
- format: 'markdown',
- author: currentUserId,
- },
- ]);
- const result = await shouldGenerateUpdate({
- targetPageId,
- currentUserId,
- currentActivityId,
- });
- expect(result).toBe(true);
- });
- it('should not generate update activity if update is made by the same user and within the suppression window', async () => {
- await Activity.insertMany([
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_CREATE,
- createdAt: new Date(date.getTime() - ONE_MINUTE),
- target: targetPageId,
- _id: olderActivityId,
- },
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- createdAt: new Date(),
- target: targetPageId,
- _id: currentActivityId,
- },
- ]);
- const result = await shouldGenerateUpdate({
- targetPageId,
- currentUserId,
- currentActivityId,
- });
- expect(result).toBe(false);
- });
- });
|