@@ -607,11 +607,20 @@ bucket_definitions:
607
607
describe ( 'streams' , ( ) => {
608
608
let source : { - readonly [ P in keyof BucketSource ] : BucketSource [ P ] } ;
609
609
let storage : MockBucketChecksumStateStorage ;
610
- let staticBucketIds = [ 'stream|0[]' ] ;
611
610
612
- function checksumState ( options ?: Partial < BucketChecksumStateOptions > ) {
613
- const rules = new SqlSyncRules ( '' ) ;
614
- rules . bucketSources . push ( source ) ;
611
+ function checksumState ( source : string | boolean , options ?: Partial < BucketChecksumStateOptions > ) {
612
+ if ( typeof source == 'boolean' ) {
613
+ source = `
614
+ streams:
615
+ stream:
616
+ auto_subscribe: ${ source }
617
+ query: SELECT * FROM assets WHERE id IN ifnull(subscription.parameter('ids'), '["default"]');
618
+ ` ;
619
+ }
620
+
621
+ const rules = SqlSyncRules . fromYaml ( source , {
622
+ defaultSchema : 'public'
623
+ } ) ;
615
624
616
625
return new BucketChecksumState ( {
617
626
syncContext,
@@ -623,77 +632,15 @@ bucket_definitions:
623
632
} ) ;
624
633
}
625
634
626
- function createQuerier ( ids : string [ ] , subscription : number | null ) : BucketParameterQuerier {
627
- return {
628
- staticBuckets : ids . map ( ( bucket ) => ( {
629
- definition : 'stream' ,
630
- inclusion_reasons : subscription == null ? [ 'default' ] : [ { subscription } ] ,
631
- bucket,
632
- priority : 3
633
- } ) ) ,
634
- hasDynamicBuckets : false ,
635
- parameterQueryLookups : [ ] ,
636
- queryDynamicBucketDescriptions : function ( ) : never {
637
- throw new Error ( 'no dynamic buckets.' ) ;
638
- }
639
- } ;
640
- }
641
-
642
635
beforeEach ( ( ) => {
643
- // Currently using mocked streams before streams are actually implemented as parsable rules.
644
- source = {
645
- name : 'stream' ,
646
- type : BucketSourceType . SYNC_STREAM ,
647
- subscribedToByDefault : false ,
648
- pushBucketParameterQueriers ( result , options ) {
649
- // Create a fake querier that resolves the global stream["default"] bucket by default and allows extracting
650
- // additional buckets from parameters.
651
- const subscriptions = options . streams [ 'stream' ] ?? [ ] ;
652
- if ( ! this . subscribedToByDefault && ! subscriptions . length ) {
653
- return ;
654
- }
655
-
656
- let hasExplicitDefaultSubscription = false ;
657
- for ( const subscription of subscriptions ) {
658
- try {
659
- let subscriptionParameters = [ ] ;
660
-
661
- if ( subscription . parameters != null ) {
662
- subscriptionParameters = JSON . parse ( subscription . parameters [ 'ids' ] as string ) . map (
663
- ( e : string ) => `stream["${ e } "]`
664
- ) ;
665
- } else {
666
- hasExplicitDefaultSubscription = true ;
667
- }
668
-
669
- result . queriers . push ( createQuerier ( [ ...subscriptionParameters ] , subscription . opaque_id ) ) ;
670
- } catch ( e ) {
671
- result . errors . push ( {
672
- descriptor : 'stream' ,
673
- subscription,
674
- message : `Error evaluating bucket ids: ${ e . message } `
675
- } ) ;
676
- }
677
- }
678
-
679
- // If the stream is subscribed to by default and there is no explicit subscription that would match the default
680
- // subscription, also include the default querier.
681
- if ( this . subscribedToByDefault && ! hasExplicitDefaultSubscription ) {
682
- result . queriers . push ( createQuerier ( [ 'stream["default"]' ] , null ) ) ;
683
- }
684
- }
685
- } satisfies Partial < BucketSource > as any ;
686
-
687
636
storage = new MockBucketChecksumStateStorage ( ) ;
688
- storage . updateTestChecksum ( { bucket : 'stream["default"]' , checksum : 1 , count : 1 } ) ;
689
- storage . updateTestChecksum ( { bucket : 'stream["a"]' , checksum : 1 , count : 1 } ) ;
690
- storage . updateTestChecksum ( { bucket : 'stream["b"]' , checksum : 1 , count : 1 } ) ;
637
+ storage . updateTestChecksum ( { bucket : 'stream|0 ["default"]' , checksum : 1 , count : 1 } ) ;
638
+ storage . updateTestChecksum ( { bucket : 'stream|0 ["a"]' , checksum : 1 , count : 1 } ) ;
639
+ storage . updateTestChecksum ( { bucket : 'stream|0 ["b"]' , checksum : 1 , count : 1 } ) ;
691
640
} ) ;
692
641
693
642
test ( 'includes defaults' , async ( ) => {
694
- source . subscribedToByDefault = true ;
695
- const state = checksumState ( ) ;
696
-
643
+ const state = checksumState ( true ) ;
697
644
const line = await state . buildNextCheckpointLine ( {
698
645
base : storage . makeCheckpoint ( 1n ) ,
699
646
writeCheckpoint : null ,
@@ -703,7 +650,7 @@ bucket_definitions:
703
650
expect ( line ?. checkpointLine ) . toEqual ( {
704
651
checkpoint : {
705
652
buckets : [
706
- { bucket : 'stream["default"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { default : 0 } ] }
653
+ { bucket : 'stream|0 ["default"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { default : 0 } ] }
707
654
] ,
708
655
last_op_id : '1' ,
709
656
write_checkpoint : undefined ,
@@ -713,8 +660,7 @@ bucket_definitions:
713
660
} ) ;
714
661
715
662
test ( 'can exclude defaults' , async ( ) => {
716
- source . subscribedToByDefault = true ;
717
- const state = checksumState ( { syncRequest : { streams : { include_defaults : false , subscriptions : [ ] } } } ) ;
663
+ const state = checksumState ( true , { syncRequest : { streams : { include_defaults : false , subscriptions : [ ] } } } ) ;
718
664
719
665
const line = await state . buildNextCheckpointLine ( {
720
666
base : storage . makeCheckpoint ( 1n ) ,
@@ -733,9 +679,7 @@ bucket_definitions:
733
679
} ) ;
734
680
735
681
test ( 'custom subscriptions' , async ( ) => {
736
- source . subscribedToByDefault = true ;
737
-
738
- const state = checksumState ( {
682
+ const state = checksumState ( true , {
739
683
syncRequest : {
740
684
streams : {
741
685
subscriptions : [
@@ -755,9 +699,9 @@ bucket_definitions:
755
699
expect ( line ?. checkpointLine ) . toEqual ( {
756
700
checkpoint : {
757
701
buckets : [
758
- { bucket : 'stream["a"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { sub : 0 } ] } ,
759
- { bucket : 'stream["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 1 } ] } ,
760
- { bucket : 'stream["default"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { default : 0 } ] }
702
+ { bucket : 'stream|0 ["a"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { sub : 0 } ] } ,
703
+ { bucket : 'stream|0 ["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 1 } ] } ,
704
+ { bucket : 'stream|0 ["default"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { default : 0 } ] }
761
705
] ,
762
706
last_op_id : '1' ,
763
707
write_checkpoint : undefined ,
@@ -767,7 +711,7 @@ bucket_definitions:
767
711
} ) ;
768
712
769
713
test ( 'overlap between custom subscriptions' , async ( ) => {
770
- const state = checksumState ( {
714
+ const state = checksumState ( false , {
771
715
syncRequest : {
772
716
streams : {
773
717
subscriptions : [
@@ -787,8 +731,8 @@ bucket_definitions:
787
731
expect ( line ?. checkpointLine ) . toEqual ( {
788
732
checkpoint : {
789
733
buckets : [
790
- { bucket : 'stream["a"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { sub : 0 } ] } ,
791
- { bucket : 'stream["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } , { sub : 1 } ] }
734
+ { bucket : 'stream|0 ["a"]' , checksum : 1 , count : 1 , priority : 3 , subscriptions : [ { sub : 0 } ] } ,
735
+ { bucket : 'stream|0 ["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } , { sub : 1 } ] }
792
736
] ,
793
737
last_op_id : '1' ,
794
738
write_checkpoint : undefined ,
@@ -798,8 +742,7 @@ bucket_definitions:
798
742
} ) ;
799
743
800
744
test ( 'overlap between default and custom subscription' , async ( ) => {
801
- source . subscribedToByDefault = true ;
802
- const state = checksumState ( {
745
+ const state = checksumState ( true , {
803
746
syncRequest : {
804
747
streams : {
805
748
subscriptions : [ { stream : 'stream' , parameters : { ids : '["a", "default"]' } , override_priority : 1 } ]
@@ -816,9 +759,9 @@ bucket_definitions:
816
759
expect ( line ?. checkpointLine ) . toEqual ( {
817
760
checkpoint : {
818
761
buckets : [
819
- { bucket : 'stream["a"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
762
+ { bucket : 'stream|0 ["a"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
820
763
{
821
- bucket : 'stream["default"]' ,
764
+ bucket : 'stream|0 ["default"]' ,
822
765
checksum : 1 ,
823
766
count : 1 ,
824
767
priority : 1 ,
@@ -833,9 +776,7 @@ bucket_definitions:
833
776
} ) ;
834
777
835
778
test ( 'reports errors' , async ( ) => {
836
- source . subscribedToByDefault = true ;
837
-
838
- const state = checksumState ( {
779
+ const state = checksumState ( true , {
839
780
syncRequest : {
840
781
streams : {
841
782
subscriptions : [
@@ -855,10 +796,10 @@ bucket_definitions:
855
796
expect ( line ?. checkpointLine ) . toEqual ( {
856
797
checkpoint : {
857
798
buckets : [
858
- { bucket : 'stream["a"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
859
- { bucket : 'stream["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
799
+ { bucket : 'stream|0 ["a"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
800
+ { bucket : 'stream|0 ["b"]' , checksum : 1 , count : 1 , priority : 1 , subscriptions : [ { sub : 0 } ] } ,
860
801
{
861
- bucket : 'stream["default"]' ,
802
+ bucket : 'stream|0 ["default"]' ,
862
803
checksum : 1 ,
863
804
count : 1 ,
864
805
priority : 3 ,
0 commit comments