1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
use serde::{Deserialize, Serialize};

use crate::contexts::attribute_reference::AttributeName;
use crate::util::is_false;
use crate::{
    contexts::context::{BucketPrefix, Kind},
    Context, Reference,
};
use serde_with::skip_serializing_none;

/// A type representing the index into the [crate::Flag]'s variations.
pub type VariationIndex = isize;

#[derive(Debug, PartialEq)]
pub(crate) struct BucketResult {
    pub variation_index: VariationIndex,
    pub in_experiment: bool,
}

impl From<&VariationIndex> for BucketResult {
    fn from(variation_index: &VariationIndex) -> Self {
        BucketResult {
            variation_index: *variation_index,
            in_experiment: false, // single variations are never in an experiment
        }
    }
}

/// RolloutKind describes whether a rollout is a simple percentage rollout or represents an
/// experiment. Experiments have different behaviour for tracking and variation bucketing.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum RolloutKind {
    /// Represents a simple percentage rollout. This is the default rollout kind, and will be assumed if
    /// not otherwise specified.
    Rollout,

    /// Represents an experiment. Experiments have different behaviour for tracking and variation
    /// bucketing.
    Experiment,
}

impl Default for RolloutKind {
    fn default() -> Self {
        RolloutKind::Rollout
    }
}

/// Rollout describes how contexts will be bucketed into variations during a percentage rollout.
// Rollout is deserialized via a helper, IntermediateRollout, because of semantic ambiguity
// of the bucketBy Reference field.
//
// Rollout implements Serialize directly without a helper because References can serialize
// themselves without any ambiguity.
#[skip_serializing_none]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", from = "IntermediateRollout")]
pub struct Rollout {
    // Specifies if this rollout is a simple rollout or an experiment. Should default to rollout
    // if absent.
    kind: Option<RolloutKind>,
    // Context kind associated with this rollout.
    context_kind: Option<Kind>,
    // Specifies which attribute should be used to distinguish between Contexts in a rollout.
    // Can be omitted; evaluation should treat absence as 'key'.
    bucket_by: Option<Reference>,
    // Which variations should be included in the rollout, and associated weight.
    variations: Vec<WeightedVariation>,
    // Specifies the seed to be used by the hashing algorithm.
    seed: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RolloutWithContextKind {
    kind: Option<RolloutKind>,
    context_kind: Kind,
    // bucketBy deserializes directly into a reference.
    bucket_by: Option<Reference>,
    variations: Vec<WeightedVariation>,
    seed: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RolloutWithoutContextKind {
    kind: Option<RolloutKind>,
    bucket_by: Option<AttributeName>,
    variations: Vec<WeightedVariation>,
    seed: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum IntermediateRollout {
    // RolloutWithContextKind must be listed first in the enum because otherwise
    // RolloutWithoutContextKind could match the input (by ignoring/discarding
    // the context_kind field).
    ContextAware(RolloutWithContextKind),
    ContextOblivious(RolloutWithoutContextKind),
}

impl From<IntermediateRollout> for Rollout {
    fn from(rollout: IntermediateRollout) -> Self {
        match rollout {
            IntermediateRollout::ContextAware(fields) => Self {
                kind: fields.kind,
                context_kind: Some(fields.context_kind),
                bucket_by: fields.bucket_by,
                variations: fields.variations,
                seed: fields.seed,
            },
            IntermediateRollout::ContextOblivious(fields) => Self {
                kind: fields.kind,
                context_kind: None,
                bucket_by: fields.bucket_by.map(Reference::from),
                variations: fields.variations,
                seed: fields.seed,
            },
        }
    }
}

#[cfg(test)]
pub(crate) mod proptest_generators {
    use super::*;
    use crate::proptest_generators::{any_kind, any_ref};
    use proptest::{collection::vec, option::of, prelude::*};

    prop_compose! {
        fn any_weighted_variation() (
            variation in any::<isize>(),
            weight in any::<f32>(),
            untracked in any::<bool>()
        ) -> WeightedVariation {
            WeightedVariation {
                variation,
                weight,
                untracked
            }
        }
    }

    fn any_rollout_kind() -> BoxedStrategy<RolloutKind> {
        prop_oneof![Just(RolloutKind::Rollout), Just(RolloutKind::Experiment)].boxed()
    }

    prop_compose! {
        pub(crate) fn any_rollout() (
            kind in of(any_rollout_kind()),
            context_kind in any_kind(),
            bucket_by in of(any_ref()),
            seed in of(any::<i64>()),
            variations in vec(any_weighted_variation(), 0..2)
        ) -> Rollout {
            Rollout {
                kind,
                context_kind: Some(context_kind),
                bucket_by,
                seed,
                variations,
            }
        }
    }
}

impl Rollout {
    #[cfg(test)]
    fn with_variations<V: Into<Vec<WeightedVariation>>>(variations: V) -> Self {
        Rollout {
            kind: None,
            context_kind: None,
            bucket_by: None,
            seed: None,
            variations: variations.into(),
        }
    }

    #[cfg(test)]
    fn bucket_by(self, bucket_by: &str) -> Self {
        Rollout {
            bucket_by: Some(Reference::new(bucket_by)),
            ..self
        }
    }
}

/// VariationOrRollout describes either a fixed variation or a percentage rollout.
///
/// There is a VariationOrRollout for every [crate::FlagRule], and also one in [crate::eval::Reason::Fallthrough] which is
/// used if no rules match.
///
/// Invariant: one of the variation or rollout must be non-nil.
// This enum is a bit oddly-shaped because data errors may cause the server to emit rules with neither or both of a
// variation or rollout, and we need to treat invalid states with grace (i.e. don't throw a 500 on deserialization, and
// prefer variation if both are present)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum VariationOrRollout {
    /// Represents a fixed variation.
    Variation {
        /// The index of the variation to return. It is undefined if no specific variation is defined.
        variation: VariationIndex,
    },
    /// Represents a percentage rollout.
    Rollout {
        /// Specifies the rollout definition. See [Rollout].
        rollout: Rollout,
    },
    /// Represents a malformed VariationOrRollout payload. This is done to deal with potential
    /// data errors that may occur server-side. Generally speaking this should not occur.
    Malformed(serde_json::Value),
}

pub(crate) type VariationWeight = f32;

/// WeightedVariation describes a fraction of contexts which will receive a specific variation.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct WeightedVariation {
    /// The index of the variation to be returned if the context is in this bucket. This is always a
    /// real variation index; it cannot be undefined.
    pub variation: VariationIndex,

    /// The proportion of contexts which should go into this bucket, as an integer from 0 to 100000.
    pub weight: VariationWeight,

    /// Untracked means that contexts allocated to this variation should not have tracking events sent.
    #[serde(default, skip_serializing_if = "is_false")]
    pub untracked: bool,
}

impl WeightedVariation {
    #[cfg(test)]
    fn new(variation: VariationIndex, weight: VariationWeight) -> Self {
        WeightedVariation {
            variation,
            weight,
            untracked: false,
        }
    }

    fn as_bucket_result(&self, is_experiment: bool) -> BucketResult {
        BucketResult {
            variation_index: self.variation,
            in_experiment: is_experiment && !self.untracked,
        }
    }
}

impl VariationOrRollout {
    pub(crate) fn variation(
        &self,
        flag_key: &str,
        context: &Context,
        salt: &str,
    ) -> Result<Option<BucketResult>, String> {
        match self {
            VariationOrRollout::Variation { variation: var } => Ok(Some(var.into())),
            VariationOrRollout::Rollout {
                rollout:
                    Rollout {
                        kind,
                        bucket_by,
                        variations,
                        seed,
                        context_kind,
                    },
            } => {
                let is_experiment =
                    kind.as_ref().unwrap_or(&RolloutKind::default()) == &RolloutKind::Experiment;

                let prefix = match seed {
                    Some(seed) => BucketPrefix::Seed(*seed),
                    None => BucketPrefix::KeyAndSalt(flag_key, salt),
                };

                let (bucket, was_missing_context) = context.bucket(
                    bucket_by,
                    prefix,
                    is_experiment,
                    context_kind.as_ref().unwrap_or(&Kind::default()),
                )?;

                let mut sum = 0.0;
                for variation in variations {
                    sum += variation.weight / 100_000.0;
                    if bucket < sum {
                        return Ok(Some(
                            variation.as_bucket_result(is_experiment && !was_missing_context),
                        ));
                    }
                }
                return Ok(variations
                    .last()
                    .map(|var| var.as_bucket_result(is_experiment && !was_missing_context)));
            }
            VariationOrRollout::Malformed(_) => Ok(None),
        }
    }
}

// Note: These tests are meant to be exact duplicates of tests
// in other SDKs. Do not change any of the values unless they
// are also changed in other SDKs. These are not traditional behavioral
// tests so much as consistency tests to guarantee that the implementation
// is identical across SDKs.

#[cfg(test)]
mod consistency_tests {
    use super::*;
    use crate::ContextBuilder;
    use serde_json::json;
    use spectral::prelude::*;
    use test_case::test_case;

    const BUCKET_TOLERANCE: f32 = 0.0000001;

    #[test]
    fn variation_index_for_context() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 60_000.0);
        let wv1 = WeightedVariation::new(1, 40_000.0);
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout::with_variations(vec![wv0, wv1]),
        };

        asserting!("userKeyA (bucket 0.42157587) should get variation 0")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyA").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 0,
                in_experiment: false,
            });
        asserting!("userKeyB (bucket 0.6708485) should get variation 1")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyB").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 1,
                in_experiment: false,
            });
        asserting!("userKeyC (bucket 0.10343106) should get variation 0")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyC").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 0,
                in_experiment: false,
            });
    }

    #[test_case(None, "userKeyA", 2)] // 0.42157587,
    #[test_case(None, "userKeyB", 2)] // 0.6708485,
    #[test_case(None, "userKeyC", 1)] // 0.10343106,
    #[test_case(Some(61), "userKeyA", 0)] // 0.09801207,
    #[test_case(Some(61), "userKeyB", 1)] // 0.14483777,
    #[test_case(Some(61), "userKeyC", 2)] // 0.9242641,
    fn testing_experiment_bucketing(
        seed: Option<i64>,
        key: &str,
        expected_variation_index: VariationIndex,
    ) {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 10_000.0);
        let wv1 = WeightedVariation::new(1, 20_000.0);
        let wv2 = WeightedVariation::new(2, 70_000.0);

        let mut rollout = Rollout::with_variations(vec![wv0, wv1, wv2]).bucket_by("intAttr");
        rollout.kind = Some(RolloutKind::Experiment);
        rollout.seed = seed;
        let rollout = VariationOrRollout::Rollout { rollout };

        let result = rollout
            .variation(
                HASH_KEY,
                &ContextBuilder::new(key)
                    .set_value("intAttr", 0.6708485.into())
                    .build()
                    .unwrap(),
                SALT,
            )
            .unwrap()
            .unwrap();

        assert_eq!(result.variation_index, expected_variation_index);
    }

    #[test]
    fn variation_index_for_context_with_custom_attribute() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 60_000.0);
        let wv1 = WeightedVariation::new(1, 40_000.0);
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout::with_variations(vec![wv0, wv1]).bucket_by("intAttr"),
        };

        asserting!("userKeyD (bucket 0.54771423) should get variation 0")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyA")
                            .set_value("intAttr", 33_333.into())
                            .build()
                            .unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 0,
                in_experiment: false,
            });

        asserting!("userKeyD (bucket 0.7309658) should get variation 0")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyA")
                            .set_value("intAttr", 99_999.into())
                            .build()
                            .unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 1,
                in_experiment: false,
            });
    }

    #[test]
    fn variation_index_for_context_in_experiment() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation {
            variation: 0,
            weight: 10_000.0,
            untracked: false,
        };
        let wv1 = WeightedVariation {
            variation: 1,
            weight: 20_000.0,
            untracked: false,
        };
        let wv0_untracked = WeightedVariation {
            variation: 0,
            weight: 70_000.0,
            untracked: true,
        };
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout {
                seed: Some(61),
                kind: Some(RolloutKind::Experiment),
                ..Rollout::with_variations(vec![wv0, wv1, wv0_untracked])
            },
        };

        asserting!("userKeyA (bucket 0.09801207) should get variation 0 and be in the experiment")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyA").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 0,
                in_experiment: true,
            });
        asserting!("userKeyB (bucket 0.14483777) should get variation 1 and be in the experiment")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyB").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 1,
                in_experiment: true,
            });
        asserting!(
            "userKeyC (bucket 0.9242641) should get variation 0 and not be in the experiment"
        )
        .that(
            &rollout
                .variation(
                    HASH_KEY,
                    &ContextBuilder::new("userKeyC").build().unwrap(),
                    SALT,
                )
                .unwrap(),
        )
        .contains_value(BucketResult {
            variation_index: 0,
            in_experiment: false,
        });
    }

    #[test]
    fn bucket_context_by_key() {
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "saltyA");

        let context = ContextBuilder::new("userKeyA").build().unwrap();
        let (bucket, _) = context.bucket(&None, PREFIX, false, &Kind::user()).unwrap();
        assert_that!(bucket).is_close_to(0.42157587, BUCKET_TOLERANCE);

        let context = ContextBuilder::new("userKeyB").build().unwrap();
        let (bucket, _) = context.bucket(&None, PREFIX, false, &Kind::user()).unwrap();
        assert_that!(bucket).is_close_to(0.6708485, BUCKET_TOLERANCE);

        let context = ContextBuilder::new("userKeyC").build().unwrap();
        let (bucket, _) = context.bucket(&None, PREFIX, false, &Kind::user()).unwrap();
        assert_that!(bucket).is_close_to(0.10343106, BUCKET_TOLERANCE);

        let result = context.bucket(&Some(Reference::new("")), PREFIX, false, &Kind::user());
        assert!(result.is_err());
    }

    #[test]
    fn bucket_context_by_key_with_seed() {
        const PREFIX: BucketPrefix = BucketPrefix::Seed(61);

        let context_a = ContextBuilder::new("userKeyA").build().unwrap();
        let (bucket, _) = context_a
            .bucket(&None, PREFIX, false, &Kind::user())
            .unwrap();
        assert_that!(bucket).is_close_to(0.09801207, BUCKET_TOLERANCE);

        let context_b = ContextBuilder::new("userKeyB").build().unwrap();
        let (bucket, _) = context_b
            .bucket(&None, PREFIX, false, &Kind::user())
            .unwrap();
        assert_that!(bucket).is_close_to(0.14483777, BUCKET_TOLERANCE);

        let context_c = ContextBuilder::new("userKeyC").build().unwrap();
        let (bucket, _) = context_c
            .bucket(&None, PREFIX, false, &Kind::user())
            .unwrap();
        assert_that!(bucket).is_close_to(0.9242641, BUCKET_TOLERANCE);

        // changing seed produces different bucket value
        let (bucket, _) = context_a
            .bucket(&None, BucketPrefix::Seed(60), false, &Kind::user())
            .unwrap();
        assert_that!(bucket).is_close_to(0.7008816, BUCKET_TOLERANCE)
    }

    #[test]
    #[cfg_attr(not(feature = "secondary_key_bucketing"), ignore)]
    fn bucket_context_with_secondary_key_only_when_feature_enabled() {
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "salt");

        let context1 = ContextBuilder::new("userKey").build().unwrap();

        // can only construct a context w/ secondary by deserializing from implicit user format.
        let context2: Context = serde_json::from_value(json!({
            "key" : "userKey",
            "secondary" : "mySecondaryKey"
        }))
        .unwrap();

        let result1 = context1.bucket(&None, PREFIX, false, &Kind::user());
        let result2 = context2.bucket(&None, PREFIX, false, &Kind::user());
        assert_that!(result1).is_not_equal_to(result2);
    }

    #[test]
    #[cfg_attr(feature = "secondary_key_bucketing", ignore)]
    fn bucket_context_with_secondary_key_does_not_change_result() {
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "salt");

        let context1: Context = ContextBuilder::new("userKey").build().unwrap();

        // can only construct a context w/ secondary by deserializing from implicit user format.
        let context2: Context = serde_json::from_value(json!({
            "key" : "userKey",
            "secondary" : "mySecondaryKey"
        }))
        .unwrap();

        let result1 = context1.bucket(&None, PREFIX, false, &Kind::user());
        let result2 = context2.bucket(&None, PREFIX, false, &Kind::user());
        assert_that!(result1).is_equal_to(result2);
    }

    #[test]
    fn bucket_context_by_int_attr() {
        const USER_KEY: &str = "userKeyD";
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "saltyA");

        let context = ContextBuilder::new(USER_KEY)
            .set_value("intAttr", 33_333.into())
            .build()
            .unwrap();
        let (bucket, _) = context
            .bucket(
                &Some(Reference::new("intAttr")),
                PREFIX,
                false,
                &Kind::user(),
            )
            .unwrap();
        assert_that!(bucket).is_close_to(0.54771423, BUCKET_TOLERANCE);

        let context = ContextBuilder::new(USER_KEY)
            .set_value("stringAttr", "33333".into())
            .build()
            .unwrap();
        let (bucket2, _) = context
            .bucket(
                &Some(Reference::new("stringAttr")),
                PREFIX,
                false,
                &Kind::user(),
            )
            .unwrap();
        assert_that!(bucket).is_close_to(bucket2, BUCKET_TOLERANCE);
    }

    #[test]
    fn bucket_context_by_float_attr_not_allowed() {
        const USER_KEY: &str = "userKeyE";
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "saltyA");

        let context = ContextBuilder::new(USER_KEY)
            .set_value("floatAttr", 999.999.into())
            .build()
            .unwrap();
        let (bucket, _) = context
            .bucket(
                &Some(Reference::new("floatAttr")),
                PREFIX,
                false,
                &Kind::user(),
            )
            .unwrap();
        assert_that!(bucket).is_close_to(0.0, BUCKET_TOLERANCE);
    }

    #[test]
    fn bucket_context_by_float_attr_that_is_really_an_int_is_allowed() {
        const PREFIX: BucketPrefix = BucketPrefix::KeyAndSalt("hashKey", "saltyA");

        let context = ContextBuilder::new("userKeyE")
            .set_value("floatAttr", f64::from(33_333).into())
            .build()
            .unwrap();
        let (bucket, _) = context
            .bucket(
                &Some(Reference::new("floatAttr")),
                PREFIX,
                false,
                &Kind::user(),
            )
            .unwrap();
        assert_that!(bucket).is_close_to(0.54771423, BUCKET_TOLERANCE);
    }

    #[test]
    fn test_bucket_value_beyond_last_bucket_is_pinned_to_last_bucket() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 5_000.0);
        let wv1 = WeightedVariation::new(1, 5_000.0);
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout {
                seed: Some(61),
                kind: Some(RolloutKind::Rollout),
                ..Rollout::with_variations(vec![wv0, wv1])
            },
        };
        let context = ContextBuilder::new("userKeyD")
            .set_value("intAttr", 99_999.into())
            .build()
            .unwrap();
        asserting!("userKeyD should get variation 1 and not be in the experiment")
            .that(&rollout.variation(HASH_KEY, &context, SALT).unwrap())
            .contains_value(BucketResult {
                variation_index: 1,
                in_experiment: false,
            });
    }

    #[test]
    fn test_bucket_value_beyond_last_bucket_is_pinned_to_last_bucket_for_experiment() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 5_000.0);
        let wv1 = WeightedVariation::new(1, 5_000.0);
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout {
                seed: Some(61),
                kind: Some(RolloutKind::Experiment),
                ..Rollout::with_variations(vec![wv0, wv1])
            },
        };
        let context = ContextBuilder::new("userKeyD")
            .set_value("intAttr", 99_999.into())
            .build()
            .unwrap();
        asserting!("userKeyD should get variation 1 and be in the experiment")
            .that(&rollout.variation(HASH_KEY, &context, SALT).unwrap())
            .contains_value(BucketResult {
                variation_index: 1,
                in_experiment: true,
            });
    }
}

#[cfg(test)]
mod tests {
    use crate::ContextBuilder;
    use assert_json_diff::assert_json_eq;

    use super::*;
    use crate::proptest_generators::*;
    use proptest::prelude::*;
    use serde_json::json;
    use spectral::prelude::*;

    proptest! {
        #[test]
         fn arbitrary_rollout_serialization_roundtrip(rollout in any_rollout()) {
            let json = serde_json::to_value(&rollout).expect("a rollout should serialize");
            let parsed: Rollout = serde_json::from_value(json.clone()).expect("a rollout should parse");
            assert_json_eq!(json, parsed);
        }
    }

    #[test]
    fn rollout_serialize_omits_optional_fields() {
        let json = json!({"variations" : []});
        let parsed: Rollout = serde_json::from_value(json.clone()).expect("should parse");
        assert_json_eq!(json, parsed);
    }

    #[test]
    fn test_parse_variation_or_rollout() {
        let variation: VariationOrRollout =
            serde_json::from_str(r#"{"variation":4}"#).expect("should parse");

        assert_that!(variation).is_equal_to(&VariationOrRollout::Variation { variation: 4 });

        let rollout: VariationOrRollout =
            serde_json::from_str(r#"{"rollout":{"variations":[{"variation":1,"weight":100000}]}}"#)
                .expect("should parse");
        assert_that!(rollout).is_equal_to(&VariationOrRollout::Rollout {
            rollout: Rollout::with_variations(vec![WeightedVariation::new(1, 100_000.0)]),
        });

        let rollout_bucket_by: VariationOrRollout = serde_json::from_str(
            r#"{"rollout":{"bucketBy":"bucket","variations":[{"variation":1,"weight":100000}]}}"#,
        )
        .expect("should parse");
        assert_that!(rollout_bucket_by).is_equal_to(&VariationOrRollout::Rollout {
            rollout: Rollout {
                bucket_by: Some(Reference::new("bucket")),
                ..Rollout::with_variations(vec![WeightedVariation::new(1, 100_000.0)])
            },
        });

        let rollout_seed: VariationOrRollout = serde_json::from_str(
            r#"{"rollout":{"variations":[{"variation":1,"weight":100000}],"seed":42}}"#,
        )
        .expect("should parse");
        assert_that!(rollout_seed).is_equal_to(&VariationOrRollout::Rollout {
            rollout: Rollout {
                seed: Some(42),
                ..Rollout::with_variations(vec![WeightedVariation::new(1, 100_000.0)])
            },
        });

        let rollout_experiment: VariationOrRollout = serde_json::from_str(
            r#"{
                 "rollout":
                   {
                     "kind": "experiment",
                     "variations": [
                       {"variation":1, "weight":20000},
                       {"variation":0, "weight":20000},
                       {"variation":0, "weight":60000, "untracked": true}
                     ],
                     "seed":42
                   }
            }"#,
        )
        .expect("should parse");
        assert_that!(rollout_experiment).is_equal_to(&VariationOrRollout::Rollout {
            rollout: Rollout {
                kind: Some(RolloutKind::Experiment),
                seed: Some(42),
                ..Rollout::with_variations(vec![
                    WeightedVariation::new(1, 20_000.0),
                    WeightedVariation::new(0, 20_000.0),
                    WeightedVariation {
                        untracked: true,
                        ..WeightedVariation::new(0, 60_000.0)
                    },
                ])
            },
        });

        let malformed: VariationOrRollout = serde_json::from_str(r#"{}"#).expect("should parse");
        assert_that!(malformed).is_equal_to(VariationOrRollout::Malformed(json!({})));

        let overspecified: VariationOrRollout = serde_json::from_str(
            r#"{
                "variation": 1,
                "rollout": {"variations": [{"variation": 1, "weight": 100000}], "seed": 42}
            }"#,
        )
        .expect("should parse");
        assert_that!(overspecified).is_equal_to(VariationOrRollout::Variation { variation: 1 });
    }

    #[test]
    fn incomplete_weighting_defaults_to_last_variation() {
        const HASH_KEY: &str = "hashKey";
        const SALT: &str = "saltyA";

        let wv0 = WeightedVariation::new(0, 1.0);
        let wv1 = WeightedVariation::new(1, 2.0);
        let wv2 = WeightedVariation::new(2, 3.0);
        let rollout = VariationOrRollout::Rollout {
            rollout: Rollout::with_variations(vec![wv0, wv1, wv2]),
        };

        asserting!("userKeyD (bucket 0.7816281) should get variation 2")
            .that(
                &rollout
                    .variation(
                        HASH_KEY,
                        &ContextBuilder::new("userKeyD").build().unwrap(),
                        SALT,
                    )
                    .unwrap(),
            )
            .contains_value(BucketResult {
                variation_index: 2,
                in_experiment: false,
            });
    }
}