1- import { storage } from '@powersync/service-core' ;
1+ import { addChecksums , storage } from '@powersync/service-core' ;
22import { expect , test } from 'vitest' ;
33import * as test_utils from '../test-utils/test-utils-index.js' ;
44
@@ -59,19 +59,16 @@ bucket_definitions:
5959
6060 expect ( dataBefore ) . toMatchObject ( [
6161 {
62- checksum : 2634521662 ,
6362 object_id : 't1' ,
6463 op : 'PUT' ,
6564 op_id : '1'
6665 } ,
6766 {
68- checksum : 4243212114 ,
6967 object_id : 't2' ,
7068 op : 'PUT' ,
7169 op_id : '2'
7270 } ,
7371 {
74- checksum : 4243212114 ,
7572 object_id : 't2' ,
7673 op : 'PUT' ,
7774 op_id : '3'
@@ -96,19 +93,14 @@ bucket_definitions:
9693
9794 expect ( batchAfter . targetOp ) . toEqual ( 3n ) ;
9895 expect ( dataAfter ) . toMatchObject ( [
96+ dataBefore [ 0 ] ,
9997 {
100- checksum : 2634521662 ,
101- object_id : 't1' ,
102- op : 'PUT' ,
103- op_id : '1'
104- } ,
105- {
106- checksum : 4243212114 ,
98+ checksum : dataBefore [ 1 ] . checksum ,
10799 op : 'MOVE' ,
108100 op_id : '2'
109101 } ,
110102 {
111- checksum : 4243212114 ,
103+ checksum : dataBefore [ 2 ] . checksum ,
112104 object_id : 't2' ,
113105 op : 'PUT' ,
114106 op_id : '3'
@@ -181,30 +173,23 @@ bucket_definitions:
181173 const dataBefore = batchBefore . chunkData . data ;
182174 const checksumBefore = await bucketStorage . getChecksums ( checkpoint , [ 'global[]' ] ) ;
183175
176+ // op_id sequence depends on the storage implementation
184177 expect ( dataBefore ) . toMatchObject ( [
185178 {
186- checksum : 2634521662 ,
187179 object_id : 't1' ,
188- op : 'PUT' ,
189- op_id : '1'
180+ op : 'PUT'
190181 } ,
191182 {
192- checksum : 4243212114 ,
193183 object_id : 't2' ,
194- op : 'PUT' ,
195- op_id : '2'
184+ op : 'PUT'
196185 } ,
197186 {
198- checksum : 4228978084 ,
199187 object_id : 't1' ,
200- op : 'REMOVE' ,
201- op_id : '3'
188+ op : 'REMOVE'
202189 } ,
203190 {
204- checksum : 4243212114 ,
205191 object_id : 't2' ,
206192 op : 'PUT'
207- // op_id is 4 or 5, depending on storage implementation
208193 }
209194 ] ) ;
210195
@@ -225,15 +210,16 @@ bucket_definitions:
225210 expect ( batchAfter . targetOp ) . toBeLessThanOrEqual ( checkpoint ) ;
226211 expect ( dataAfter ) . toMatchObject ( [
227212 {
228- checksum : - 1778190028 ,
229- op : 'CLEAR' ,
230- op_id : '3'
213+ checksum : addChecksums (
214+ addChecksums ( dataBefore [ 0 ] . checksum as number , dataBefore [ 1 ] . checksum as number ) ,
215+ dataBefore [ 2 ] . checksum as number
216+ ) ,
217+ op : 'CLEAR'
231218 } ,
232219 {
233- checksum : 4243212114 ,
220+ checksum : dataBefore [ 3 ] . checksum ,
234221 object_id : 't2' ,
235222 op : 'PUT'
236- // op_id is 4 or 5, depending on storage implementation
237223 }
238224 ] ) ;
239225 expect ( checksumAfter . get ( 'global[]' ) ) . toEqual ( {
@@ -288,7 +274,7 @@ bucket_definitions:
288274 } ) ;
289275
290276 const checkpoint1 = result ! . flushed_op ;
291- const checksumBefore = await bucketStorage . getChecksums ( checkpoint1 , [ 'global[]' ] ) ;
277+ await bucketStorage . getChecksums ( checkpoint1 , [ 'global[]' ] ) ;
292278
293279 const result2 = await bucketStorage . startBatch ( test_utils . BATCH_OPTIONS , async ( batch ) => {
294280 await batch . save ( {
@@ -317,18 +303,15 @@ bucket_definitions:
317303 await bucketStorage . clearChecksumCache ( ) ;
318304 const checksumAfter = await bucketStorage . getChecksums ( checkpoint2 , [ 'global[]' ] ) ;
319305
320- expect ( batchAfter . targetOp ) . toEqual ( 5n ) ;
321306 expect ( dataAfter ) . toMatchObject ( [
322307 {
323- checksum : 1874612650 ,
324- op : 'CLEAR' ,
325- op_id : '5'
308+ op : 'CLEAR'
326309 }
327310 ] ) ;
328311 expect ( checksumAfter . get ( 'global[]' ) ) . toEqual ( {
329312 bucket : 'global[]' ,
330313 count : 1 ,
331- checksum : 1874612650
314+ checksum : dataAfter [ 0 ] . checksum
332315 } ) ;
333316 } ) ;
334317
@@ -526,10 +509,9 @@ bucket_definitions:
526509 const checkpoint2 = result2 ! . flushed_op ;
527510 await bucketStorage . clearChecksumCache ( ) ;
528511 const checksumAfter = await bucketStorage . getChecksums ( checkpoint2 , [ 'global[]' ] ) ;
529- expect ( checksumAfter . get ( 'global[]' ) ) . toEqual ( {
512+ expect ( checksumAfter . get ( 'global[]' ) ) . toMatchObject ( {
530513 bucket : 'global[]' ,
531- count : 4 ,
532- checksum : 1874612650
514+ count : 4
533515 } ) ;
534516 } ) ;
535517
@@ -589,12 +571,10 @@ bucket_definitions:
589571 } ) ;
590572
591573 const checkpoint2 = result2 ! . flushed_op ;
592- // Check that the checksum was correctly updated with the clear operation after having a cached checksum
593574 const checksumAfter = await bucketStorage . getChecksums ( checkpoint2 , [ 'global[]' ] ) ;
594575 expect ( checksumAfter . get ( 'global[]' ) ) . toMatchObject ( {
595576 bucket : 'global[]' ,
596- count : 1 ,
597- checksum : - 1481659821
577+ count : 1
598578 } ) ;
599579 } ) ;
600580}
0 commit comments