@@ -165,34 +165,100 @@ describe('Streaming', () => {
165165
166166 const buckets : BucketChecksum [ ] = [ ] ;
167167 for ( let prio = 0 ; prio <= 3 ; prio ++ ) {
168- buckets . push ( { bucket : `prio${ prio } ` , priority : prio , checksum : 0 } ) ;
168+ buckets . push ( { bucket : `prio${ prio } ` , priority : prio , checksum : 10 + prio } ) ;
169169 }
170170 remote . enqueueLine ( {
171171 checkpoint : {
172- last_op_id : '0 ' ,
172+ last_op_id : '4 ' ,
173173 buckets,
174174 } ,
175175 } ) ;
176176
177+ let operationId = 1 ;
178+ const addRow = ( prio : number ) => {
179+ remote . enqueueLine ( {
180+ data : {
181+ bucket : `prio${ prio } ` ,
182+ data : [ {
183+ checksum : prio + 10 ,
184+ data : JSON . stringify ( { 'name' : 'row' } ) ,
185+ op : 'PUT' ,
186+ op_id : ( operationId ++ ) . toString ( ) ,
187+ object_id : `prio${ prio } ` ,
188+ object_type : 'users'
189+ } ]
190+ } ,
191+ } ) ;
192+ }
193+
194+ const syncCompleted = vi . fn ( ) ;
195+ powersync . waitForFirstSync ( ) . then ( syncCompleted ) ;
196+
177197 // Emit partial sync complete for each priority but the last.
178198 for ( var prio = 0 ; prio < 3 ; prio ++ ) {
199+ const partialSyncCompleted = vi . fn ( ) ;
200+ powersync . waitForFirstSync ( { priority : prio } ) . then ( partialSyncCompleted ) ;
179201 expect ( powersync . currentStatus . statusForPriority ( prio ) . hasSynced ) . toBe ( false ) ;
202+ expect ( partialSyncCompleted ) . not . toHaveBeenCalled ( ) ;
203+ expect ( syncCompleted ) . not . toHaveBeenCalled ( ) ;
180204
205+ addRow ( prio ) ;
181206 remote . enqueueLine ( {
182207 partial_checkpoint_complete : {
183- last_op_id : '0' ,
208+ last_op_id : operationId . toString ( ) ,
184209 priority : prio ,
185210 }
186211 } ) ;
187212
188213 await powersync . syncStreamImplementation ! . waitUntilStatusMatches ( ( status ) => {
189214 return status . statusForPriority ( prio ) . hasSynced === true ;
190215 } ) ;
216+ await new Promise ( r => setTimeout ( r ) ) ;
217+ expect ( partialSyncCompleted ) . toHaveBeenCalledOnce ( ) ;
218+
219+ expect ( await powersync . getAll ( 'select * from users' ) ) . toHaveLength ( prio + 1 ) ;
191220 }
192221
193222 // Then, complete the sync.
194- remote . enqueueLine ( { checkpoint_complete : { last_op_id : '0' } } ) ;
195- await powersync . waitForFirstSync ( ) ;
223+ addRow ( 3 ) ;
224+ remote . enqueueLine ( { checkpoint_complete : { last_op_id : operationId . toString ( ) } } ) ;
225+ await vi . waitFor ( ( ) => expect ( syncCompleted ) . toHaveBeenCalledOnce ( ) , 500 ) ;
226+ expect ( await powersync . getAll ( 'select * from users' ) ) . toHaveLength ( 4 ) ;
227+ } ) ;
228+
229+ itWithGenerators ( 'Should remember sync state' , async ( createConnectedDatabase ) => {
230+ const { powersync, remote, openAnother } = await createConnectedDatabase ( ) ;
231+ expect ( powersync . currentStatus . dataFlowStatus . downloading ) . toBe ( false ) ;
232+
233+ const buckets : BucketChecksum [ ] = [ ] ;
234+ for ( let prio = 0 ; prio <= 3 ; prio ++ ) {
235+ buckets . push ( { bucket : `prio${ prio } ` , priority : prio , checksum : 0 } ) ;
236+ }
237+ remote . enqueueLine ( {
238+ checkpoint : {
239+ last_op_id : '0' ,
240+ buckets,
241+ } ,
242+ } ) ;
243+ remote . enqueueLine ( {
244+ partial_checkpoint_complete : {
245+ last_op_id : '0' ,
246+ priority : 0 ,
247+ }
248+ } ) ;
249+
250+ await powersync . waitForFirstSync ( { priority : 0 } ) ;
251+
252+ // Open another database instance.
253+ const another = openAnother ( ) ;
254+ onTestFinished ( async ( ) => {
255+ await another . close ( ) ;
256+ } ) ;
257+ await another . init ( ) ;
258+
259+ expect ( another . currentStatus . statusInPriority ) . toHaveLength ( 1 ) ;
260+ expect ( another . currentStatus . statusForPriority ( 0 ) . hasSynced ) . toBeTruthy ( ) ;
261+ await another . waitForFirstSync ( { priority : 0 } ) ;
196262 } ) ;
197263 } ) ;
198264} ) ;
0 commit comments