|
| 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