@@ -560,6 +560,58 @@ def boolean_flag_with_clauses(clauses)
560560 end
561561 end
562562
563+ describe "variation_index_for_user" do
564+ it "matches bucket" do
565+ user = { key : "userkey" }
566+ flag_key = "flagkey"
567+ salt = "salt"
568+
569+ # First verify that with our test inputs, the bucket value will be greater than zero and less than 100000,
570+ # so we can construct a rollout whose second bucket just barely contains that value
571+ bucket_value = ( bucket_user ( user , flag_key , "key" , salt ) * 100000 ) . truncate ( )
572+ expect ( bucket_value ) . to be > 0
573+ expect ( bucket_value ) . to be < 100000
574+
575+ bad_variation_a = 0
576+ matched_variation = 1
577+ bad_variation_b = 2
578+ rule = {
579+ rollout : {
580+ variations : [
581+ { variation : bad_variation_a , weight : bucket_value } , # end of bucket range is not inclusive, so it will *not* match the target value
582+ { variation : matched_variation , weight : 1 } , # size of this bucket is 1, so it only matches that specific value
583+ { variation : bad_variation_b , weight : 100000 - ( bucket_value + 1 ) }
584+ ]
585+ }
586+ }
587+ flag = { key : flag_key , salt : salt }
588+
589+ result_variation = variation_index_for_user ( flag , rule , user )
590+ expect ( result_variation ) . to be matched_variation
591+ end
592+
593+ it "uses last bucket if bucket value is equal to total weight" do
594+ user = { key : "userkey" }
595+ flag_key = "flagkey"
596+ salt = "salt"
597+
598+ bucket_value = ( bucket_user ( user , flag_key , "key" , salt ) * 100000 ) . truncate ( )
599+
600+ # We'll construct a list of variations that stops right at the target bucket value
601+ rule = {
602+ rollout : {
603+ variations : [
604+ { variation : 0 , weight : bucket_value }
605+ ]
606+ }
607+ }
608+ flag = { key : flag_key , salt : salt }
609+
610+ result_variation = variation_index_for_user ( flag , rule , user )
611+ expect ( result_variation ) . to be 0
612+ end
613+ end
614+
563615 describe "bucket_user" do
564616 it "gets expected bucket values for specific keys" do
565617 user = { key : "userKeyA" }
0 commit comments