@@ -22,6 +22,88 @@ describe('Sync', () => {
2222 describe ( 'rust client' , ( ) => {
2323 defineSyncTests ( SyncClientImplementation . RUST ) ;
2424 } ) ;
25+
26+ mockSyncServiceTest ( 'can migrate between sync implementations' , async ( { syncService } ) => {
27+ function addData ( id : string ) {
28+ syncService . pushLine ( {
29+ data : {
30+ bucket : 'a' ,
31+ data : [
32+ {
33+ checksum : 0 ,
34+ op_id : id ,
35+ op : 'PUT' ,
36+ object_id : id ,
37+ object_type : 'lists' ,
38+ subkey : `subkey_${ id } ` ,
39+ data : '{}'
40+ }
41+ ]
42+ }
43+ } ) ;
44+ }
45+ const checkpoint = {
46+ checkpoint : {
47+ last_op_id : '3' ,
48+ buckets : [ bucket ( 'a' , 3 ) ]
49+ }
50+ } ;
51+
52+ let database = await syncService . createDatabase ( ) ;
53+ database . connect ( new TestConnector ( ) , {
54+ clientImplementation : SyncClientImplementation . JAVASCRIPT ,
55+ connectionMethod : SyncStreamConnectionMethod . HTTP
56+ } ) ;
57+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
58+ syncService . pushLine ( checkpoint ) ;
59+ addData ( '1' ) ;
60+
61+ await vi . waitFor ( async ( ) => {
62+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toHaveLength ( 1 ) ;
63+ } ) ;
64+ await database . disconnect ( ) ;
65+ // The JavaScript client encodes subkeys to JSON when it shouldn't...
66+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toEqual ( [
67+ expect . objectContaining ( { key : 'lists/1/"subkey_1"' } )
68+ ] ) ;
69+
70+ // Connecting again with the new client should fix the format
71+ database . connect ( new TestConnector ( ) , {
72+ clientImplementation : SyncClientImplementation . RUST ,
73+ connectionMethod : SyncStreamConnectionMethod . HTTP
74+ } ) ;
75+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
76+ syncService . pushLine ( checkpoint ) ;
77+ addData ( '2' ) ;
78+ await vi . waitFor ( async ( ) => {
79+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toHaveLength ( 2 ) ;
80+ } ) ;
81+ await database . disconnect ( ) ;
82+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toEqual ( [
83+ // Existing entry should be fixed too!
84+ expect . objectContaining ( { key : 'lists/1/subkey_1' } ) ,
85+ expect . objectContaining ( { key : 'lists/2/subkey_2' } )
86+ ] ) ;
87+
88+ // Finally, connecting with JS again should keep the fixed subkey format.
89+ database . connect ( new TestConnector ( ) , {
90+ clientImplementation : SyncClientImplementation . RUST ,
91+ connectionMethod : SyncStreamConnectionMethod . HTTP
92+ } ) ;
93+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
94+ syncService . pushLine ( checkpoint ) ;
95+ addData ( '3' ) ;
96+ await vi . waitFor ( async ( ) => {
97+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toHaveLength ( 3 ) ;
98+ } ) ;
99+ await database . disconnect ( ) ;
100+ expect ( await database . getAll ( 'SELECT * FROM ps_oplog' ) ) . toEqual ( [
101+ // Existing entry should be fixed too!
102+ expect . objectContaining ( { key : 'lists/1/subkey_1' } ) ,
103+ expect . objectContaining ( { key : 'lists/2/subkey_2' } ) ,
104+ expect . objectContaining ( { key : 'lists/3/subkey_3' } )
105+ ] ) ;
106+ } ) ;
25107} ) ;
26108
27109function defineSyncTests ( impl : SyncClientImplementation ) {
0 commit comments