From c28ef3189cce36731d33bb0de291211cc3275881 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Sat, 24 Aug 2024 02:18:23 +0800 Subject: [PATCH] fix: test case --- .../server/src/core/doc/adapters/userspace.ts | 4 ++-- .../server/src/core/doc/adapters/workspace.ts | 6 +++--- .../backend/server/src/core/doc/storage/doc.ts | 6 +++--- packages/backend/server/src/core/sync/gateway.ts | 14 +++++++------- packages/backend/server/tests/doc/history.spec.ts | 6 +++--- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/backend/server/src/core/doc/adapters/userspace.ts b/packages/backend/server/src/core/doc/adapters/userspace.ts index f11c10a9dd..d59983da24 100644 --- a/packages/backend/server/src/core/doc/adapters/userspace.ts +++ b/packages/backend/server/src/core/doc/adapters/userspace.ts @@ -46,10 +46,10 @@ export class PgUserspaceDocStorageAdapter extends DocStorageAdapter { } async pushDocUpdates( - editorId: string, userId: string, docId: string, - updates: Uint8Array[] + updates: Uint8Array[], + editorId?: string ) { if (!updates.length) { return 0; diff --git a/packages/backend/server/src/core/doc/adapters/workspace.ts b/packages/backend/server/src/core/doc/adapters/workspace.ts index c4b656beab..462dd7d9d0 100644 --- a/packages/backend/server/src/core/doc/adapters/workspace.ts +++ b/packages/backend/server/src/core/doc/adapters/workspace.ts @@ -36,10 +36,10 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter { } async pushDocUpdates( - editorId: string, workspaceId: string, docId: string, - updates: Uint8Array[] + updates: Uint8Array[], + editorId?: string ) { if (!updates.length) { return 0; @@ -270,7 +270,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter { } override async rollbackDoc( - editorId: string, + editorId: string | undefined, spaceId: string, docId: string, timestamp: number diff --git a/packages/backend/server/src/core/doc/storage/doc.ts b/packages/backend/server/src/core/doc/storage/doc.ts index 49099afe7f..633cb6fb1a 100644 --- a/packages/backend/server/src/core/doc/storage/doc.ts +++ b/packages/backend/server/src/core/doc/storage/doc.ts @@ -97,10 +97,10 @@ export abstract class DocStorageAdapter extends Connection { } abstract pushDocUpdates( - editorId: string, spaceId: string, docId: string, - updates: Uint8Array[] + updates: Uint8Array[], + editorId?: string ): Promise; abstract deleteDoc(spaceId: string, docId: string): Promise; @@ -124,7 +124,7 @@ export abstract class DocStorageAdapter extends Connection { } const change = this.generateChangeUpdate(fromSnapshot.bin, toSnapshot.bin); - await this.pushDocUpdates(editorId, spaceId, docId, [change]); + await this.pushDocUpdates(spaceId, docId, [change], editorId); // force create a new history record after rollback await this.createDocHistory(fromSnapshot, true); } diff --git a/packages/backend/server/src/core/sync/gateway.ts b/packages/backend/server/src/core/sync/gateway.ts index b4279bb083..0c7a815b81 100644 --- a/packages/backend/server/src/core/sync/gateway.ts +++ b/packages/backend/server/src/core/sync/gateway.ts @@ -277,10 +277,10 @@ export class SpaceSyncGateway // TODO(@forehalo): we might need to check write permission before push updates const timestamp = await adapter.push( - user.id, spaceId, docId, - updates.map(update => Buffer.from(update, 'base64')) + updates.map(update => Buffer.from(update, 'base64')), + user.id ); // could be put in [adapter.push] @@ -601,9 +601,9 @@ abstract class SyncSocketAdapter { permission?: Permission ): Promise; - push(editorId: string, spaceId: string, docId: string, updates: Buffer[]) { + push(spaceId: string, docId: string, updates: Buffer[], editorId: string) { this.assertIn(spaceId); - return this.storage.pushDocUpdates(editorId, spaceId, docId, updates); + return this.storage.pushDocUpdates(spaceId, docId, updates, editorId); } get(spaceId: string, docId: string) { @@ -627,13 +627,13 @@ class WorkspaceSyncAdapter extends SyncSocketAdapter { } override push( - editorId: string, spaceId: string, docId: string, - updates: Buffer[] + updates: Buffer[], + editorId: string ) { const id = new DocID(docId, spaceId); - return super.push(editorId, spaceId, id.guid, updates); + return super.push(spaceId, id.guid, updates, editorId); } override get(spaceId: string, docId: string) { diff --git a/packages/backend/server/tests/doc/history.spec.ts b/packages/backend/server/tests/doc/history.spec.ts index 9e987016b8..7f1edc156b 100644 --- a/packages/backend/server/tests/doc/history.spec.ts +++ b/packages/backend/server/tests/doc/history.spec.ts @@ -1,5 +1,3 @@ -import { randomUUID } from 'node:crypto'; - import { TestingModule } from '@nestjs/testing'; import type { Snapshot } from '@prisma/client'; import { PrismaClient } from '@prisma/client'; @@ -50,6 +48,8 @@ const snapshot: Snapshot = { seq: 0, updatedAt: new Date(), createdAt: new Date(), + createdBy: null, + updatedBy: null, }; function getSnapshot(timestamp: number = Date.now()): DocRecord { @@ -264,7 +264,7 @@ test('should be able to recover from history', async t => { await adapter.createDocHistory(getSnapshot(history1Timestamp)); await adapter.rollbackDoc( - randomUUID(), + undefined, snapshot.workspaceId, snapshot.id, history1Timestamp