@powersync/service-core-tests 0.11.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ import {
7
7
  utils
8
8
  } from '@powersync/service-core';
9
9
  import { JSONBig } from '@powersync/service-jsonbig';
10
- import { RequestParameters } from '@powersync/service-sync-rules';
10
+ import { BucketSourceType, RequestParameters } from '@powersync/service-sync-rules';
11
11
  import path from 'path';
12
12
  import * as timers from 'timers/promises';
13
13
  import { fileURLToPath } from 'url';
@@ -82,15 +82,18 @@ export function registerSyncTests(factory: storage.TestStorageFactory) {
82
82
  const stream = sync.streamResponse({
83
83
  syncContext,
84
84
  bucketStorage: bucketStorage,
85
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
85
+ syncRules: {
86
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
87
+ version: bucketStorage.group_id
88
+ },
86
89
  params: {
87
90
  buckets: [],
88
91
  include_checksum: true,
89
92
  raw_data: true
90
93
  },
91
94
  tracker,
92
- syncParams: new RequestParameters({ sub: '' }, {}),
93
- token: { exp: Date.now() / 1000 + 10 } as any
95
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
96
+ isEncodingAsBson: false
94
97
  });
95
98
 
96
99
  const lines = await consumeCheckpointLines(stream);
@@ -143,15 +146,18 @@ bucket_definitions:
143
146
  const stream = sync.streamResponse({
144
147
  syncContext,
145
148
  bucketStorage,
146
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
149
+ syncRules: {
150
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
151
+ version: bucketStorage.group_id
152
+ },
147
153
  params: {
148
154
  buckets: [],
149
155
  include_checksum: true,
150
156
  raw_data: true
151
157
  },
152
158
  tracker,
153
- syncParams: new RequestParameters({ sub: '' }, {}),
154
- token: { exp: Date.now() / 1000 + 10 } as any
159
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
160
+ isEncodingAsBson: false
155
161
  });
156
162
 
157
163
  const lines = await consumeCheckpointLines(stream);
@@ -206,15 +212,18 @@ bucket_definitions:
206
212
  const stream = sync.streamResponse({
207
213
  syncContext,
208
214
  bucketStorage,
209
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
215
+ syncRules: {
216
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
217
+ version: bucketStorage.group_id
218
+ },
210
219
  params: {
211
220
  buckets: [],
212
221
  include_checksum: true,
213
222
  raw_data: true
214
223
  },
215
224
  tracker,
216
- syncParams: new RequestParameters({ sub: '' }, {}),
217
- token: { exp: Date.now() / 1000 + 10 } as any
225
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
226
+ isEncodingAsBson: false
218
227
  });
219
228
 
220
229
  let sentCheckpoints = 0;
@@ -316,15 +325,18 @@ bucket_definitions:
316
325
  const stream = sync.streamResponse({
317
326
  syncContext,
318
327
  bucketStorage,
319
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
328
+ syncRules: {
329
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
330
+ version: bucketStorage.group_id
331
+ },
320
332
  params: {
321
333
  buckets: [],
322
334
  include_checksum: true,
323
335
  raw_data: true
324
336
  },
325
337
  tracker,
326
- syncParams: new RequestParameters({ sub: 'user_one' }, {}),
327
- token: { sub: 'user_one', exp: Date.now() / 1000 + 100000 } as any
338
+ token: { sub: 'user_one', exp: Date.now() / 1000 + 100000 } as any,
339
+ isEncodingAsBson: false
328
340
  });
329
341
 
330
342
  let sentCheckpoints = 0;
@@ -457,15 +469,18 @@ bucket_definitions:
457
469
  const stream = sync.streamResponse({
458
470
  syncContext,
459
471
  bucketStorage,
460
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
472
+ syncRules: {
473
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
474
+ version: bucketStorage.group_id
475
+ },
461
476
  params: {
462
477
  buckets: [],
463
478
  include_checksum: true,
464
479
  raw_data: true
465
480
  },
466
481
  tracker,
467
- syncParams: new RequestParameters({ sub: '' }, {}),
468
- token: { exp: Date.now() / 1000 + 10 } as any
482
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
483
+ isEncodingAsBson: false
469
484
  });
470
485
 
471
486
  let sentRows = 0;
@@ -573,15 +588,18 @@ bucket_definitions:
573
588
  const stream = sync.streamResponse({
574
589
  syncContext,
575
590
  bucketStorage,
576
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
591
+ syncRules: {
592
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
593
+ version: bucketStorage.group_id
594
+ },
577
595
  params: {
578
596
  buckets: [],
579
597
  include_checksum: true,
580
598
  raw_data: true
581
599
  },
582
600
  tracker,
583
- syncParams: new RequestParameters({ sub: '' }, {}),
584
- token: { exp: Date.now() / 1000 + 100000 } as any
601
+ token: { sub: '', exp: Date.now() / 1000 + 100000 } as any,
602
+ isEncodingAsBson: false
585
603
  });
586
604
 
587
605
  const lines: any[] = [];
@@ -639,15 +657,18 @@ bucket_definitions:
639
657
  const stream = sync.streamResponse({
640
658
  syncContext,
641
659
  bucketStorage,
642
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
660
+ syncRules: {
661
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
662
+ version: bucketStorage.group_id
663
+ },
643
664
  params: {
644
665
  buckets: [],
645
666
  include_checksum: true,
646
667
  raw_data: false
647
668
  },
648
669
  tracker,
649
- syncParams: new RequestParameters({ sub: '' }, {}),
650
- token: { exp: Date.now() / 1000 + 10 } as any
670
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
671
+ isEncodingAsBson: false
651
672
  });
652
673
 
653
674
  const lines = await consumeCheckpointLines(stream);
@@ -668,15 +689,18 @@ bucket_definitions:
668
689
  const stream = sync.streamResponse({
669
690
  syncContext,
670
691
  bucketStorage,
671
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
692
+ syncRules: {
693
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
694
+ version: bucketStorage.group_id
695
+ },
672
696
  params: {
673
697
  buckets: [],
674
698
  include_checksum: true,
675
699
  raw_data: true
676
700
  },
677
701
  tracker,
678
- syncParams: new RequestParameters({ sub: '' }, {}),
679
- token: { exp: 0 } as any
702
+ token: { sub: '', exp: 0 } as any,
703
+ isEncodingAsBson: false
680
704
  });
681
705
 
682
706
  const lines = await consumeCheckpointLines(stream);
@@ -699,15 +723,18 @@ bucket_definitions:
699
723
  const stream = sync.streamResponse({
700
724
  syncContext,
701
725
  bucketStorage,
702
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
726
+ syncRules: {
727
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
728
+ version: bucketStorage.group_id
729
+ },
703
730
  params: {
704
731
  buckets: [],
705
732
  include_checksum: true,
706
733
  raw_data: true
707
734
  },
708
735
  tracker,
709
- syncParams: new RequestParameters({ sub: '' }, {}),
710
- token: { exp: Date.now() / 1000 + 10 } as any
736
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
737
+ isEncodingAsBson: false
711
738
  });
712
739
  const iter = stream[Symbol.asyncIterator]();
713
740
  context.onTestFinished(() => {
@@ -773,15 +800,18 @@ bucket_definitions:
773
800
  const stream = sync.streamResponse({
774
801
  syncContext,
775
802
  bucketStorage,
776
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
803
+ syncRules: {
804
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
805
+ version: bucketStorage.group_id
806
+ },
777
807
  params: {
778
808
  buckets: [],
779
809
  include_checksum: true,
780
810
  raw_data: true
781
811
  },
782
812
  tracker,
783
- syncParams: new RequestParameters({ sub: 'user1' }, {}),
784
- token: { exp: Date.now() / 1000 + 100 } as any
813
+ token: { sub: 'user1', exp: Date.now() / 1000 + 100 } as any,
814
+ isEncodingAsBson: false
785
815
  });
786
816
  const iter = stream[Symbol.asyncIterator]();
787
817
  context.onTestFinished(() => {
@@ -849,15 +879,18 @@ bucket_definitions:
849
879
  const stream = sync.streamResponse({
850
880
  syncContext,
851
881
  bucketStorage,
852
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
882
+ syncRules: {
883
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
884
+ version: bucketStorage.group_id
885
+ },
853
886
  params: {
854
887
  buckets: [],
855
888
  include_checksum: true,
856
889
  raw_data: true
857
890
  },
858
891
  tracker,
859
- syncParams: new RequestParameters({ sub: 'user1' }, {}),
860
- token: { exp: Date.now() / 1000 + 100 } as any
892
+ token: { sub: 'user1', exp: Date.now() / 1000 + 100 } as any,
893
+ isEncodingAsBson: false
861
894
  });
862
895
  const iter = stream[Symbol.asyncIterator]();
863
896
  context.onTestFinished(() => {
@@ -916,15 +949,18 @@ bucket_definitions:
916
949
  const stream = sync.streamResponse({
917
950
  syncContext,
918
951
  bucketStorage,
919
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
952
+ syncRules: {
953
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
954
+ version: bucketStorage.group_id
955
+ },
920
956
  params: {
921
957
  buckets: [],
922
958
  include_checksum: true,
923
959
  raw_data: true
924
960
  },
925
961
  tracker,
926
- syncParams: new RequestParameters({ sub: 'user1' }, {}),
927
- token: { exp: Date.now() / 1000 + 100 } as any
962
+ token: { sub: 'user1', exp: Date.now() / 1000 + 100 } as any,
963
+ isEncodingAsBson: false
928
964
  });
929
965
  const iter = stream[Symbol.asyncIterator]();
930
966
  context.onTestFinished(() => {
@@ -984,15 +1020,18 @@ bucket_definitions:
984
1020
  const stream = sync.streamResponse({
985
1021
  syncContext,
986
1022
  bucketStorage,
987
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1023
+ syncRules: {
1024
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1025
+ version: bucketStorage.group_id
1026
+ },
988
1027
  params: {
989
1028
  buckets: [],
990
1029
  include_checksum: true,
991
1030
  raw_data: true
992
1031
  },
993
1032
  tracker,
994
- syncParams: new RequestParameters({ sub: '' }, {}),
995
- token: { exp: exp } as any
1033
+ token: { sub: '', exp: exp } as any,
1034
+ isEncodingAsBson: false
996
1035
  });
997
1036
  const iter = stream[Symbol.asyncIterator]();
998
1037
  context.onTestFinished(() => {
@@ -1047,15 +1086,18 @@ bucket_definitions:
1047
1086
  const stream = sync.streamResponse({
1048
1087
  syncContext,
1049
1088
  bucketStorage,
1050
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1089
+ syncRules: {
1090
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1091
+ version: bucketStorage.group_id
1092
+ },
1051
1093
  params: {
1052
1094
  buckets: [],
1053
1095
  include_checksum: true,
1054
1096
  raw_data: true
1055
1097
  },
1056
1098
  tracker,
1057
- syncParams: new RequestParameters({ sub: '' }, {}),
1058
- token: { exp: Date.now() / 1000 + 10 } as any
1099
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
1100
+ isEncodingAsBson: false
1059
1101
  });
1060
1102
 
1061
1103
  const iter = stream[Symbol.asyncIterator]();
@@ -1173,15 +1215,18 @@ bucket_definitions:
1173
1215
  const params: sync.SyncStreamParameters = {
1174
1216
  syncContext,
1175
1217
  bucketStorage,
1176
- syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1218
+ syncRules: {
1219
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1220
+ version: bucketStorage.group_id
1221
+ },
1177
1222
  params: {
1178
1223
  buckets: [],
1179
1224
  include_checksum: true,
1180
1225
  raw_data: true
1181
1226
  },
1182
1227
  tracker,
1183
- syncParams: new RequestParameters({ sub: 'test' }, {}),
1184
- token: { sub: 'test', exp: Date.now() / 1000 + 10 } as any
1228
+ token: { sub: 'test', exp: Date.now() / 1000 + 10 } as any,
1229
+ isEncodingAsBson: false
1185
1230
  };
1186
1231
  const stream1 = sync.streamResponse(params);
1187
1232
  const lines1 = await consumeCheckpointLines(stream1);
@@ -1211,6 +1256,59 @@ bucket_definitions:
1211
1256
  })
1212
1257
  });
1213
1258
  });
1259
+
1260
+ test('encodes sync rules id in buckes for streams', async () => {
1261
+ await using f = await factory();
1262
+ const rules = `
1263
+ streams:
1264
+ test:
1265
+ auto_subscribe: true
1266
+ query: SELECT * FROM test;
1267
+
1268
+ config:
1269
+ edition: 2
1270
+ `;
1271
+
1272
+ for (let i = 0; i < 2; i++) {
1273
+ const syncRules = await f.updateSyncRules({
1274
+ content: rules
1275
+ });
1276
+ const bucketStorage = f.getInstance(syncRules);
1277
+
1278
+ await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1279
+ await batch.save({
1280
+ sourceTable: TEST_TABLE,
1281
+ tag: storage.SaveOperationTag.INSERT,
1282
+ after: {
1283
+ id: 't1',
1284
+ description: 'Test 1'
1285
+ },
1286
+ afterReplicaId: 't1'
1287
+ });
1288
+ await batch.commit('0/1');
1289
+ });
1290
+
1291
+ const stream = sync.streamResponse({
1292
+ syncContext,
1293
+ bucketStorage: bucketStorage,
1294
+ syncRules: {
1295
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
1296
+ version: bucketStorage.group_id
1297
+ },
1298
+ params: {
1299
+ buckets: [],
1300
+ include_checksum: true,
1301
+ raw_data: true
1302
+ },
1303
+ tracker,
1304
+ token: { sub: '', exp: Date.now() / 1000 + 10 } as any,
1305
+ isEncodingAsBson: false
1306
+ });
1307
+
1308
+ const lines = await consumeCheckpointLines(stream);
1309
+ expect(lines).toMatchSnapshot();
1310
+ }
1311
+ });
1214
1312
  }
1215
1313
 
1216
1314
  /**