Skip to content

Commit 58e1763

Browse files
committed
test: 💍 add background sync with remote test
1 parent 88e51e3 commit 58e1763

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/json-crdt-repo/__tests__/testbed.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {MemoryLevel} from 'memory-level';
66
import {pubsub as createPubsub} from '../pubsub';
77
import {BinStrLevel, LevelLocalRepoPubSub} from '../local/level/types';
88
import {EditSessionFactory} from '../session/EditSessionFactory';
9+
import {Model, Patch} from 'json-joy/lib/json-crdt';
910

1011
/* tslint:disable:no-console */
1112

@@ -26,6 +27,15 @@ export class Testbed {
2627
public createBrowser(): BrowserTestbed {
2728
return new BrowserTestbed(this);
2829
}
30+
31+
public readonly getModelFromRemote = async (id: string | string[]): Promise<Model> => {
32+
if (Array.isArray(id)) id = id.join('/');
33+
const res = await this.remote.client.call('block.get', {id});
34+
const model = Model.fromBinary(res.block.snapshot.blob);
35+
for (const batch of res.block.tip)
36+
for (const patch of batch.patches) model.applyPatch(Patch.fromBinary(patch.blob));
37+
return model;
38+
};
2939
}
3040

3141
export class BrowserTestbed {
@@ -94,6 +104,10 @@ export class LocalRepoTestbed {
94104
});
95105
}
96106

107+
public readonly getModelFromRemote = async (id: string | string[]): Promise<Model> => {
108+
return await this.tab.browser.global.getModelFromRemote(id);
109+
};
110+
97111
public readonly stop = async () => {
98112
await this.repo.stop();
99113
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {s} from 'json-joy/lib/json-crdt';
2+
import {BehaviorSubject} from 'rxjs';
3+
import {Testbed} from '../../../__tests__/testbed';
4+
import {tick, until} from 'thingies';
5+
6+
describe('remote sync', () => {
7+
test('can synchronize after connection resumes', async () => {
8+
const connected$ = new BehaviorSubject<boolean>(true);
9+
const repo = Testbed.createRepo({connected$});
10+
const schema = s.obj({foo: s.str('bar')});
11+
const {session: session1} = repo.sessions.make({id: repo.blockId, schema});
12+
await until(async () => {
13+
try {
14+
await repo.getModelFromRemote(repo.blockId);
15+
return true;
16+
} catch {
17+
return false;
18+
}
19+
});
20+
const model1 = await repo.getModelFromRemote(repo.blockId);
21+
expect(model1.view()).toEqual({foo: 'bar'});
22+
connected$.next(false);
23+
session1.model.api.obj([]).set({
24+
foo: 'baz',
25+
x: 'y',
26+
});
27+
const {session: session2} = await repo.sessions.make({id: repo.blockId, schema});
28+
await until(() => session2.model.view()?.foo === 'baz');
29+
await tick(123);
30+
const model2 = await repo.getModelFromRemote(repo.blockId);
31+
expect(model2.view()).toEqual({foo: 'bar'});
32+
connected$.next(true);
33+
await until(async () => {
34+
const model = await repo.getModelFromRemote(repo.blockId);
35+
return model.view()?.foo === 'baz';
36+
});
37+
const model3 = await repo.getModelFromRemote(repo.blockId);
38+
expect(model3.view()).toEqual({foo: 'baz', x: 'y'});
39+
await session1.dispose();
40+
await session2.dispose();
41+
await repo.stopTab();
42+
}, 10_000);
43+
});

0 commit comments

Comments
 (0)