mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 03:56:23 +08:00
feat(nbstore): add doc sync frontend (#9070)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
import { test, vitest } from 'vitest';
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
|
||||
import { DocFrontend } from '../frontend/doc';
|
||||
import { IndexedDBDocStorage } from '../impls/idb';
|
||||
import { expectYjsEqual } from './utils';
|
||||
|
||||
test('doc', async () => {
|
||||
const doc1 = new YDoc({
|
||||
guid: 'test-doc',
|
||||
});
|
||||
doc1.getMap('test').set('hello', 'world');
|
||||
|
||||
const docStorage = new IndexedDBDocStorage({
|
||||
id: 'ws1',
|
||||
peer: 'a',
|
||||
type: 'workspace',
|
||||
});
|
||||
|
||||
await docStorage.connect();
|
||||
|
||||
const frontend1 = new DocFrontend(docStorage, null);
|
||||
frontend1.start();
|
||||
frontend1.addDoc(doc1);
|
||||
await vitest.waitFor(async () => {
|
||||
const doc = await docStorage.getDoc('test-doc');
|
||||
expectYjsEqual(doc!.bin, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const doc2 = new YDoc({
|
||||
guid: 'test-doc',
|
||||
});
|
||||
const frontend2 = new DocFrontend(docStorage, null);
|
||||
frontend2.start();
|
||||
frontend2.addDoc(doc2);
|
||||
|
||||
await vitest.waitFor(async () => {
|
||||
expectYjsEqual(doc2, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from '../impls/idb';
|
||||
import { SpaceStorage } from '../storage';
|
||||
import { SyncEngine } from '../sync';
|
||||
import { expectYjsEqual } from './utils';
|
||||
|
||||
test('doc', async () => {
|
||||
const doc = new YDoc();
|
||||
@@ -53,19 +54,24 @@ test('doc', async () => {
|
||||
});
|
||||
|
||||
const sync = new SyncEngine(peerA, [peerB, peerC]);
|
||||
const abort = new AbortController();
|
||||
sync.run(abort.signal);
|
||||
sync.start();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
{
|
||||
const b = await peerB.get('doc').getDoc('doc1');
|
||||
expect(b).not.toBeNull();
|
||||
expect(b?.bin).toEqual(update);
|
||||
expectYjsEqual(b!.bin, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
},
|
||||
});
|
||||
|
||||
const c = await peerC.get('doc').getDoc('doc1');
|
||||
expect(c).not.toBeNull();
|
||||
expect(c?.bin).toEqual(update);
|
||||
expectYjsEqual(c!.bin, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
doc.getMap('test').set('foo', 'bar');
|
||||
@@ -79,12 +85,20 @@ test('doc', async () => {
|
||||
|
||||
{
|
||||
const a = await peerA.get('doc').getDoc('doc1');
|
||||
expect(a).not.toBeNull();
|
||||
expect(a?.bin).toEqual(update2);
|
||||
expectYjsEqual(a!.bin, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
|
||||
const c = await peerC.get('doc').getDoc('doc1');
|
||||
expect(c).not.toBeNull();
|
||||
expect(c?.bin).toEqual(update2);
|
||||
expectYjsEqual(c!.bin, {
|
||||
test: {
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -130,8 +144,7 @@ test('blob', async () => {
|
||||
await peerC.connect();
|
||||
|
||||
const sync = new SyncEngine(peerA, [peerB, peerC]);
|
||||
const abort = new AbortController();
|
||||
sync.run(abort.signal);
|
||||
sync.start();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { expect } from 'vitest';
|
||||
import { applyUpdate, Doc as YDoc } from 'yjs';
|
||||
|
||||
export function expectYjsEqual(
|
||||
doc: Uint8Array | YDoc,
|
||||
match: Record<string, any>
|
||||
) {
|
||||
let ydoc: YDoc;
|
||||
if (doc instanceof Uint8Array) {
|
||||
ydoc = new YDoc();
|
||||
applyUpdate(ydoc, doc);
|
||||
} else {
|
||||
ydoc = doc;
|
||||
}
|
||||
|
||||
for (const key in match) {
|
||||
const value = match[key];
|
||||
if (Array.isArray(value)) {
|
||||
const actual = ydoc.getArray(key).toJSON();
|
||||
expect(actual).toEqual(value);
|
||||
} else if (typeof value === 'string') {
|
||||
const actual = ydoc.getText(key).toJSON();
|
||||
expect(actual).toEqual(value);
|
||||
} else {
|
||||
const actual = ydoc.getMap(key).toJSON();
|
||||
expect(actual).toEqual(value);
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
Reference in New Issue
Block a user