|
@@ -1,3 +1,4 @@
|
|
|
|
|
+import { YDocStatus } from '@growi/editor/dist/consts';
|
|
|
import { Types } from 'mongoose';
|
|
import { Types } from 'mongoose';
|
|
|
import type { Server } from 'socket.io';
|
|
import type { Server } from 'socket.io';
|
|
|
import { mock } from 'vitest-mock-extended';
|
|
import { mock } from 'vitest-mock-extended';
|
|
@@ -11,6 +12,7 @@ import { getYjsService, initializeYjsService } from './yjs';
|
|
|
|
|
|
|
|
vi.mock('y-socket.io/dist/server', () => {
|
|
vi.mock('y-socket.io/dist/server', () => {
|
|
|
const YSocketIO = vi.fn();
|
|
const YSocketIO = vi.fn();
|
|
|
|
|
+ YSocketIO.prototype.on = vi.fn();
|
|
|
YSocketIO.prototype.initialize = vi.fn();
|
|
YSocketIO.prototype.initialize = vi.fn();
|
|
|
return { YSocketIO };
|
|
return { YSocketIO };
|
|
|
});
|
|
});
|
|
@@ -26,7 +28,7 @@ const getPrivateMdbInstance = (yjsService: IYjsService): MongodbPersistence => {
|
|
|
|
|
|
|
|
describe('YjsService', () => {
|
|
describe('YjsService', () => {
|
|
|
|
|
|
|
|
- describe('hasYdocsNewerThanLatestRevision()', () => {
|
|
|
|
|
|
|
+ describe('getYDocStatus()', () => {
|
|
|
|
|
|
|
|
beforeAll(async() => {
|
|
beforeAll(async() => {
|
|
|
const ioMock = mock<Server>();
|
|
const ioMock = mock<Server>();
|
|
@@ -45,20 +47,20 @@ describe('YjsService', () => {
|
|
|
await privateMdb.flushDB();
|
|
await privateMdb.flushDB();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('returns false when neither revisions nor YDocs exists', async() => {
|
|
|
|
|
|
|
+ it('returns ISOLATED when neither revisions nor YDocs exists', async() => {
|
|
|
// arrange
|
|
// arrange
|
|
|
const yjsService = getYjsService();
|
|
const yjsService = getYjsService();
|
|
|
|
|
|
|
|
const pageId = new ObjectId();
|
|
const pageId = new ObjectId();
|
|
|
|
|
|
|
|
// act
|
|
// act
|
|
|
- const result = await yjsService.hasYdocsNewerThanLatestRevision(pageId.toString());
|
|
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
|
|
|
// assert
|
|
// assert
|
|
|
- expect(result).toBe(false);
|
|
|
|
|
|
|
+ expect(result).toBe(YDocStatus.ISOLATED);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('returns true when no revisions exist', async() => {
|
|
|
|
|
|
|
+ it('returns ISOLATED when no revisions exist', async() => {
|
|
|
// arrange
|
|
// arrange
|
|
|
const yjsService = getYjsService();
|
|
const yjsService = getYjsService();
|
|
|
|
|
|
|
@@ -68,13 +70,30 @@ describe('YjsService', () => {
|
|
|
await privateMdb.setMeta(pageId.toString(), 'updatedAt', 1000);
|
|
await privateMdb.setMeta(pageId.toString(), 'updatedAt', 1000);
|
|
|
|
|
|
|
|
// act
|
|
// act
|
|
|
- const result = await yjsService.hasYdocsNewerThanLatestRevision(pageId.toString());
|
|
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
|
|
|
// assert
|
|
// assert
|
|
|
- expect(result).toBe(true);
|
|
|
|
|
|
|
+ expect(result).toBe(YDocStatus.ISOLATED);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('returns false when the latest revision is newer than meta data', async() => {
|
|
|
|
|
|
|
+ it('returns NEW when no YDocs exist', async() => {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ const yjsService = getYjsService();
|
|
|
|
|
+
|
|
|
|
|
+ const pageId = new ObjectId();
|
|
|
|
|
+
|
|
|
|
|
+ await Revision.insertMany([
|
|
|
|
|
+ { pageId, body: '' },
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ expect(result).toBe(YDocStatus.NEW);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('returns DRAFT when the newer YDocs exist', async() => {
|
|
|
// arrange
|
|
// arrange
|
|
|
const yjsService = getYjsService();
|
|
const yjsService = getYjsService();
|
|
|
|
|
|
|
@@ -85,33 +104,36 @@ describe('YjsService', () => {
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
const privateMdb = getPrivateMdbInstance(yjsService);
|
|
const privateMdb = getPrivateMdbInstance(yjsService);
|
|
|
- await privateMdb.setMeta(pageId.toString(), 'updatedAt', (new Date(2024, 1, 1)).getTime());
|
|
|
|
|
|
|
+ await privateMdb.setMeta(pageId.toString(), 'updatedAt', (new Date(2034, 1, 1)).getTime());
|
|
|
|
|
|
|
|
// act
|
|
// act
|
|
|
- const result = await yjsService.hasYdocsNewerThanLatestRevision(pageId.toString());
|
|
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
|
|
|
// assert
|
|
// assert
|
|
|
- expect(result).toBe(false);
|
|
|
|
|
|
|
+ expect(result).toBe(YDocStatus.DRAFT);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('returns false when no YDocs exist', async() => {
|
|
|
|
|
|
|
+ it('returns SYNCED', async() => {
|
|
|
// arrange
|
|
// arrange
|
|
|
const yjsService = getYjsService();
|
|
const yjsService = getYjsService();
|
|
|
|
|
|
|
|
const pageId = new ObjectId();
|
|
const pageId = new ObjectId();
|
|
|
|
|
|
|
|
await Revision.insertMany([
|
|
await Revision.insertMany([
|
|
|
- { pageId, body: '' },
|
|
|
|
|
|
|
+ { pageId, body: '', createdAt: new Date(2025, 1, 1) },
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
|
|
+ const privateMdb = getPrivateMdbInstance(yjsService);
|
|
|
|
|
+ await privateMdb.setMeta(pageId.toString(), 'updatedAt', (new Date(2025, 1, 1)).getTime());
|
|
|
|
|
+
|
|
|
// act
|
|
// act
|
|
|
- const result = await yjsService.hasYdocsNewerThanLatestRevision(pageId.toString());
|
|
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
|
|
|
// assert
|
|
// assert
|
|
|
- expect(result).toBe(false);
|
|
|
|
|
|
|
+ expect(result).toBe(YDocStatus.SYNCED);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('returns true when the newer YDocs exist', async() => {
|
|
|
|
|
|
|
+ it('returns OUTDATED when the latest revision is newer than meta data', async() => {
|
|
|
// arrange
|
|
// arrange
|
|
|
const yjsService = getYjsService();
|
|
const yjsService = getYjsService();
|
|
|
|
|
|
|
@@ -122,13 +144,13 @@ describe('YjsService', () => {
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
const privateMdb = getPrivateMdbInstance(yjsService);
|
|
const privateMdb = getPrivateMdbInstance(yjsService);
|
|
|
- await privateMdb.setMeta(pageId.toString(), 'updatedAt', (new Date(2034, 1, 1)).getTime());
|
|
|
|
|
|
|
+ await privateMdb.setMeta(pageId.toString(), 'updatedAt', (new Date(2024, 1, 1)).getTime());
|
|
|
|
|
|
|
|
// act
|
|
// act
|
|
|
- const result = await yjsService.hasYdocsNewerThanLatestRevision(pageId.toString());
|
|
|
|
|
|
|
+ const result = await yjsService.getYDocStatus(pageId.toString());
|
|
|
|
|
|
|
|
// assert
|
|
// assert
|
|
|
- expect(result).toBe(true);
|
|
|
|
|
|
|
+ expect(result).toBe(YDocStatus.OUTDATED);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
});
|
|
});
|