| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- 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;
- let date = new Date();
- const TWO_HOURS = 2 * 60 * 60 * 1000;
- const ONE_HOUR = 60 * 60 * 1000;
- const ONE_MINUTE = 1 * 60 * 1000;
- 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;
- let targetPageIdStr: string;
- let currentUserIdStr: string;
- let currentActivityIdStr: string;
- beforeAll(async () => {
- mongoServer = await MongoMemoryServer.create();
- await mongoose.connect(mongoServer.getUri());
- });
- afterAll(async () => {
- await mongoose.disconnect();
- await mongoServer.stop();
- });
- 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, not first update', async () => {
- await Activity.insertMany([
- // Create activity
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_CREATE,
- createdAt: new Date(date.getTime() - TWO_HOURS),
- target: targetPageId,
- _id: createActivityId,
- },
- // Latest activity
- {
- user: otherUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- createdAt: new Date(date.getTime() - ONE_HOUR),
- 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: targetPageIdStr,
- currentUserId: currentUserIdStr,
- currentActivityId: currentActivityIdStr,
- });
- expect(result).toBe(true);
- });
- 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: 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: 'Newer content',
- format: 'markdown',
- author: currentUserId,
- },
- ]);
- const result = await shouldGenerateUpdate({
- targetPageId: targetPageIdStr,
- currentUserId: currentUserIdStr,
- currentActivityId: currentActivityIdStr,
- });
- expect(result).toBe(false);
- });
- 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() - TWO_HOURS),
- target: targetPageId,
- _id: createActivityId,
- },
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- 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: '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);
- });
- 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() - TWO_HOURS),
- target: targetPageId,
- _id: createActivityId,
- },
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- 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,
- },
- ]);
- 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(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() - TWO_HOURS),
- target: targetPageId,
- _id: createActivityId,
- },
- {
- user: currentUserId,
- action: SupportedAction.ACTION_PAGE_UPDATE,
- 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: '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);
- });
- });
|