orcher-sdk 0.17.2

Rust SDK for Orcher, a durable workflow engine: workflows, tasks, actors, workers and a client
Documentation
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
//! Deterministic randomness for workflows.
//!
//! Every value is derived from the run and the order of the calls, so a replay of the
//! same workflow produces the same values in the same order.

use orcher_sdk_core::kernel::random::RunRandom;
use rand::distributions::uniform::SampleUniform;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use uuid::Uuid;

/// Deterministic random source for workflow code, available as `ctx.rand()`.
///
/// Workflow code is replayed, so it must produce the same values every time it runs.
/// `Uuid::new_v4()` or `rand::random()` return new values on replay and break that rule;
/// use this type instead.
///
/// # Determinism
///
/// The same run gets the same sequence of values on every replay, different runs get
/// different sequences, and successive calls within one run return different values.
/// Because the sequence depends on call order, workflow code must make its random calls
/// in the same order on every run. Clones continue one sequence.
///
/// A run under the kernel's step-id scheme (every run a current worker starts) draws
/// from the kernel's generator instead, seeded once from the run's history lineage: its
/// values depend on neither the Rust toolchain nor the `rand` release, a reset run draws
/// the same ones, and any SDK draws the same ones for the run. `random_range` samples
/// from that generator with `rand`'s uniform sampling.
///
/// A run this SDK started before then keeps its own seed, from the standard library's
/// `DefaultHasher` and the `rand` crate's `StdRng`, neither of which guarantees the same
/// output across releases. Upgrading Rust or `rand` can therefore change the values a
/// replay of such a run produces.
///
/// # Security
///
/// **This RNG is not cryptographically secure.** Anyone who knows the workflow ID can
/// reproduce its output. Never use it for keys, tokens, or other secrets; generate those
/// in a task with a cryptographic library.
///
/// # Examples
///
/// ```rust
/// # use orcher_sdk::prelude::*;
/// # async fn example(ctx: &WorkflowContext) -> Result<()> {
/// // Deterministic UUID (same on replay)
/// let order_id = format!("ORD-{}", ctx.rand().uuid());
/// let confirmation = format!("CONF-{}", ctx.rand().uuid());
///
/// // Deterministic random number [0.0, 1.0)
/// let priority = ctx.rand().random();
///
/// // Random value in a range
/// let discount = ctx.rand().random_range(5..=20);
///
/// // Random boolean (50/50)
/// let should_expedite = ctx.rand().bool();
///
/// // Random selection from slice
/// let shipping_method = ctx.rand().choose(&["standard", "express", "overnight"]);
/// # Ok(())
/// # }
/// ```
///
/// ## Wrong: non-deterministic
///
/// ```rust
/// use uuid::Uuid;
/// use rand::random;
///
/// // Different on every run, so replay breaks.
/// let id = Uuid::new_v4();
/// let value = random::<f64>();
/// ```
///
/// ## Correct: deterministic
///
/// ```rust
/// # use orcher_sdk::prelude::*;
/// # fn example(ctx: &WorkflowContext) {
/// // Same on every run, so replay is safe.
/// let id = ctx.rand().uuid();
/// let value = ctx.rand().random();
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct WorkflowRand {
    /// Where the values come from.
    source: Source,
    /// Number of values drawn so far; shared by clones so they continue one sequence.
    step_counter: Arc<AtomicU64>,
}

/// Where a run's values come from.
#[derive(Debug, Clone)]
enum Source {
    /// This SDK's own generator, seeded per call from the workflow ID: the
    /// values of a run under this SDK's own step-id scheme.
    Own {
        /// Workflow ID that seeds every value.
        workflow_id: String,
    },
    /// The kernel's generator, seeded once from the run's history lineage:
    /// the values of a run under the kernel's step-id scheme, the ones any
    /// SDK draws for the run. Clones draw from the one generator.
    Kernel(Arc<Mutex<RunRandom>>),
}

/// The seed of this SDK's own generator for draw `counter` of the workflow
/// `workflow_id`: a hash of the two.
fn own_seed(workflow_id: &str, counter: u64) -> u64 {
    let mut hasher = DefaultHasher::new();
    workflow_id.hash(&mut hasher);
    counter.hash(&mut hasher);
    hasher.finish()
}

/// The generator one draw uses.
enum Drawn<'a> {
    Own(Box<StdRng>),
    Kernel(MutexGuard<'a, RunRandom>),
}

/// The kernel's generator as a `rand` source, for sampling the kernel has no
/// algorithm of its own for.
struct KernelRng<'a>(&'a mut RunRandom);

impl rand::RngCore for KernelRng<'_> {
    fn next_u32(&mut self) -> u32 {
        (self.0.next_u64() >> 32) as u32
    }

    fn next_u64(&mut self) -> u64 {
        self.0.next_u64()
    }

    fn fill_bytes(&mut self, dest: &mut [u8]) {
        for chunk in dest.chunks_mut(8) {
            let bytes = self.0.next_u64().to_le_bytes();
            chunk.copy_from_slice(&bytes[..chunk.len()]);
        }
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
        self.fill_bytes(dest);
        Ok(())
    }
}

impl WorkflowRand {
    /// Creates a random source for the given workflow ID, starting at counter 0.
    ///
    /// Workflow code normally uses `ctx.rand()` rather than creating one.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    /// let uuid = rand.uuid();
    /// ```
    pub fn new(workflow_id: impl Into<String>) -> Self {
        Self {
            source: Source::Own {
                workflow_id: workflow_id.into(),
            },
            step_counter: Arc::new(AtomicU64::new(0)),
        }
    }

    /// The source of a run under the kernel's step-id scheme, from the seed
    /// its start carries: the kernel's generator and algorithms, so its values
    /// depend on neither the Rust toolchain nor the `rand` release.
    pub(crate) fn from_kernel_seed(seed: [u8; 32]) -> Self {
        Self {
            source: Source::Kernel(Arc::new(Mutex::new(RunRandom::from_seed(seed)))),
            step_counter: Arc::new(AtomicU64::new(0)),
        }
    }

    /// The generator for the next draw, advancing the counter: a fresh RNG
    /// seeded from the workflow ID and the counter, or the kernel's generator.
    fn next_rng(&self) -> Drawn<'_> {
        let counter = self.step_counter.fetch_add(1, Ordering::SeqCst);
        match &self.source {
            Source::Own { workflow_id } => Drawn::Own(Box::new(StdRng::seed_from_u64(own_seed(
                workflow_id,
                counter,
            )))),
            Source::Kernel(random) => Drawn::Kernel(
                random
                    .lock()
                    .unwrap_or_else(|poisoned| poisoned.into_inner()),
            ),
        }
    }

    /// Returns a deterministic UUID.
    ///
    /// Successive calls return different UUIDs; a replay returns the same ones in the same
    /// order. Useful for idempotency keys, order IDs, and correlation IDs.
    ///
    /// The UUID is built from 128 random bits, so its version and variant bits are not set
    /// to those of a v4 UUID.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let id1 = rand.uuid();
    /// let id2 = rand.uuid();
    ///
    /// // Successive calls differ.
    /// assert_ne!(id1, id2);
    ///
    /// // A fresh source for the same workflow repeats the sequence.
    /// let rand2 = WorkflowRand::new("wf-123");
    /// let id3 = rand2.uuid();
    /// assert_eq!(id1, id3); // Same workflow ID, and both counters started at 0
    /// ```
    ///
    /// # Example: Idempotency Key
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// // The key is the same on every replay.
    /// let idempotency_key = format!("PAY-{}", ctx.rand().uuid());
    ///
    /// // So the task receives the same key each time it is scheduled.
    /// let payment_id: String = ctx.execute_task(
    ///     "process_payment",
    ///     serde_json::json!({
    ///         "amount": 100.0,
    ///         "idempotency_key": idempotency_key
    ///     })
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn uuid(&self) -> Uuid {
        match self.next_rng() {
            Drawn::Own(mut rng) => Uuid::from_u128(rng.gen()),
            Drawn::Kernel(mut random) => Uuid::from_bytes(random.uuid_bytes()),
        }
    }

    /// Returns a deterministic random `f64` in `[0.0, 1.0)`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let value = rand.random();
    /// assert!(value >= 0.0 && value < 1.0);
    /// ```
    ///
    /// # Example: Weighted Routing
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// let priority = ctx.rand().random();
    ///
    /// let queue = if priority < 0.2 {
    ///     "high-priority"
    /// } else if priority < 0.7 {
    ///     "normal"
    /// } else {
    ///     "low-priority"
    /// };
    ///
    /// let _ticket: String = ctx.execute_task("route_to_queue", queue).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn random(&self) -> f64 {
        match self.next_rng() {
            Drawn::Own(mut rng) => rng.gen(),
            Drawn::Kernel(mut random) => random.next_f64(),
        }
    }

    /// Returns a deterministic random value from `range`, such as `5..20` or `5..=20`.
    ///
    /// # Panics
    ///
    /// Panics if `range` is empty.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// // Exclusive range
    /// let value = rand.random_range(10..20);
    /// assert!(value >= 10 && value < 20);
    ///
    /// // Inclusive range
    /// let value = rand.random_range(10..=20);
    /// assert!(value >= 10 && value <= 20);
    /// ```
    ///
    /// # Example: Random Discount
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// let discount_percentage = ctx.rand().random_range(5..=20);
    /// let discount_code = format!("SAVE{}-{}", discount_percentage, ctx.rand().uuid());
    /// # Ok(())
    /// # }
    /// ```
    pub fn random_range<T, R>(&self, range: R) -> T
    where
        T: SampleUniform,
        R: rand::distributions::uniform::SampleRange<T>,
    {
        match self.next_rng() {
            Drawn::Own(mut rng) => rng.gen_range(range),
            Drawn::Kernel(mut random) => KernelRng(&mut random).gen_range(range),
        }
    }

    /// Returns a deterministic random boolean, `true` and `false` equally likely.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let coin_flip = rand.bool();
    /// assert!(coin_flip == true || coin_flip == false);
    /// ```
    ///
    /// # Example: A/B Test
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext, order_id: String) -> Result<String> {
    /// let use_new_feature = ctx.rand().bool();
    ///
    /// let receipt: String = if use_new_feature {
    ///     ctx.execute_task("new_payment_flow", order_id).await?
    /// } else {
    ///     ctx.execute_task("old_payment_flow", order_id).await?
    /// };
    /// # Ok(receipt)
    /// # }
    /// ```
    pub fn bool(&self) -> bool {
        match self.next_rng() {
            Drawn::Own(mut rng) => rng.gen(),
            Drawn::Kernel(mut random) => random.next_bool(),
        }
    }

    /// Returns a deterministically chosen element of `items`, or `None` if it is empty.
    ///
    /// An empty slice does not advance the counter.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let items = vec!["apple", "banana", "cherry"];
    /// let choice = rand.choose(&items);
    /// assert!(choice.is_some());
    ///
    /// let empty: Vec<&str> = vec![];
    /// let choice = rand.choose(&empty);
    /// assert!(choice.is_none());
    /// ```
    ///
    /// # Example: Random Server Selection
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// let servers = vec!["server1.com", "server2.com", "server3.com"];
    /// let server = ctx
    ///     .rand()
    ///     .choose(&servers)
    ///     .ok_or_else(|| Error::Other("no servers available".to_string()))?;
    ///
    /// let _response: String = ctx
    ///     .execute_task("call_api", serde_json::json!({ "server": server }))
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn choose<'a, T>(&self, items: &'a [T]) -> Option<&'a T> {
        if items.is_empty() {
            return None;
        }
        let index = match self.next_rng() {
            Drawn::Own(mut rng) => rng.gen_range(0..items.len()),
            Drawn::Kernel(mut random) => random.below(items.len() as u64) as usize,
        };
        Some(&items[index])
    }

    /// Shuffles `items` in place; a replay produces the same order.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let mut items = vec![1, 2, 3, 4, 5];
    /// rand.shuffle(&mut items);
    /// // The order is random but the same on every replay.
    /// ```
    ///
    /// # Example: Process Items in Random Order
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// let mut items = vec!["item1", "item2", "item3", "item4"];
    /// ctx.rand().shuffle(&mut items);
    ///
    /// for item in items {
    ///     let _processed: bool = ctx.execute_task("process_item", item).await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn shuffle<T>(&self, items: &mut [T]) {
        use rand::seq::SliceRandom;
        match self.next_rng() {
            Drawn::Own(mut rng) => items.shuffle(&mut *rng),
            Drawn::Kernel(mut random) => random.shuffle(items),
        }
    }

    /// Returns `count` deterministically chosen elements of `items`, without replacement.
    ///
    /// No element position is picked twice. If `count` exceeds the slice length, every
    /// element is returned. An empty slice returns an empty vector without advancing the
    /// counter.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    ///
    /// let items = vec![1, 2, 3, 4, 5];
    /// let chosen = rand.choose_multiple(&items, 3);
    /// assert_eq!(chosen.len(), 3);
    ///
    /// // Asking for more than the slice holds returns every element.
    /// let chosen = rand.choose_multiple(&items, 10);
    /// assert_eq!(chosen.len(), 5);
    /// ```
    ///
    /// # Example: Process a Random Subset
    ///
    /// ```rust
    /// # use orcher_sdk::prelude::*;
    /// # async fn example(ctx: &WorkflowContext) -> Result<()> {
    /// let all_users = vec!["user1", "user2", "user3", "user4", "user5"];
    /// let sample = ctx.rand().choose_multiple(&all_users, 2);
    ///
    /// for user in sample {
    ///     let _sent: bool = ctx.execute_task("send_survey", user).await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn choose_multiple<T: Clone>(&self, items: &[T], count: usize) -> Vec<T> {
        use rand::seq::SliceRandom;

        if items.is_empty() {
            return vec![];
        }

        let actual_count = count.min(items.len());
        match self.next_rng() {
            Drawn::Own(mut rng) => items
                .choose_multiple(&mut *rng, actual_count)
                .cloned()
                .collect(),
            Drawn::Kernel(mut random) => random
                .sample(items.len(), actual_count)
                .into_iter()
                .map(|index| items[index].clone())
                .collect(),
        }
    }

    /// Returns how many random values have been drawn, across this source and its clones.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::workflow::WorkflowRand;
    ///
    /// let rand = WorkflowRand::new("wf-123");
    /// assert_eq!(rand.step_count(), 0);
    ///
    /// rand.uuid();
    /// assert_eq!(rand.step_count(), 1);
    ///
    /// rand.random();
    /// assert_eq!(rand.step_count(), 2);
    /// ```
    pub fn step_count(&self) -> u64 {
        self.step_counter.load(Ordering::SeqCst)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_workflow_rand_uuid_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");

        let uuid1 = rand1.uuid();
        let uuid2 = rand2.uuid();

        // Same workflow ID, same UUID.
        assert_eq!(uuid1, uuid2);
    }

    #[test]
    fn test_workflow_rand_uuid_different_workflows() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-456");

        let uuid1 = rand1.uuid();
        let uuid2 = rand2.uuid();

        // Different workflow IDs, different UUIDs.
        assert_ne!(uuid1, uuid2);
    }

    #[test]
    fn test_workflow_rand_uuid_sequence() {
        let rand = WorkflowRand::new("wf-123");

        let uuid1 = rand.uuid();
        let uuid2 = rand.uuid();
        let uuid3 = rand.uuid();

        // Each call advances the counter, so all differ.
        assert_ne!(uuid1, uuid2);
        assert_ne!(uuid2, uuid3);
        assert_ne!(uuid1, uuid3);
    }

    #[test]
    fn test_workflow_rand_random_in_range() {
        let rand = WorkflowRand::new("wf-123");

        for _ in 0..100 {
            let value = rand.random();
            assert!((0.0..1.0).contains(&value));
        }
    }

    #[test]
    fn test_workflow_rand_random_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");

        let value1 = rand1.random();
        let value2 = rand2.random();

        assert_eq!(value1, value2);
    }

    #[test]
    fn test_workflow_rand_random_range() {
        let rand = WorkflowRand::new("wf-123");

        for _ in 0..100 {
            let value = rand.random_range(10..20);
            assert!((10..20).contains(&value));
        }
    }

    #[test]
    fn test_workflow_rand_random_range_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");

        let value1 = rand1.random_range(5..15);
        let value2 = rand2.random_range(5..15);

        assert_eq!(value1, value2);
    }

    #[test]
    fn test_workflow_rand_bool() {
        let rand = WorkflowRand::new("wf-123");

        let mut true_count = 0;
        let mut false_count = 0;

        for _ in 0..100 {
            if rand.bool() {
                true_count += 1;
            } else {
                false_count += 1;
            }
        }

        // Both values occur.
        assert!(true_count > 0);
        assert!(false_count > 0);
    }

    #[test]
    fn test_workflow_rand_bool_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");

        let value1 = rand1.bool();
        let value2 = rand2.bool();

        assert_eq!(value1, value2);
    }

    #[test]
    fn test_workflow_rand_choose() {
        let rand = WorkflowRand::new("wf-123");
        let items = vec!["a", "b", "c"];

        let choice = rand.choose(&items);
        assert!(choice.is_some());
        assert!(items.contains(choice.unwrap()));
    }

    #[test]
    fn test_workflow_rand_choose_empty() {
        let rand = WorkflowRand::new("wf-123");
        let items: Vec<&str> = vec![];

        let choice = rand.choose(&items);
        assert!(choice.is_none());
    }

    #[test]
    fn test_workflow_rand_choose_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");
        let items = vec!["a", "b", "c"];

        let choice1 = rand1.choose(&items);
        let choice2 = rand2.choose(&items);

        assert_eq!(choice1, choice2);
    }

    #[test]
    fn test_workflow_rand_shuffle() {
        let rand = WorkflowRand::new("wf-123");
        let mut items1 = vec![1, 2, 3, 4, 5];
        let items2 = items1.clone();

        rand.shuffle(&mut items1);

        // A shuffle may in principle return the original order; for this fixed seed it
        // does not.
        let same_order = items1.iter().zip(items2.iter()).all(|(a, b)| a == b);
        assert!(!same_order || items1.len() <= 1);
    }

    #[test]
    fn test_workflow_rand_shuffle_deterministic() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = WorkflowRand::new("wf-123");

        let mut items1 = vec![1, 2, 3, 4, 5];
        let mut items2 = vec![1, 2, 3, 4, 5];

        rand1.shuffle(&mut items1);
        rand2.shuffle(&mut items2);

        // Same workflow ID, same shuffle.
        assert_eq!(items1, items2);
    }

    #[test]
    fn test_workflow_rand_shuffle_empty() {
        let rand = WorkflowRand::new("wf-123");
        let mut items: Vec<i32> = vec![];

        rand.shuffle(&mut items);
        assert_eq!(items.len(), 0);
    }

    #[test]
    fn test_workflow_rand_shuffle_single() {
        let rand = WorkflowRand::new("wf-123");
        let mut items = vec![42];

        rand.shuffle(&mut items);
        assert_eq!(items, vec![42]);
    }

    #[test]
    fn test_workflow_rand_choose_multiple() {
        let rand = WorkflowRand::new("wf-123");
        let items = vec![1, 2, 3, 4, 5];

        let chosen = rand.choose_multiple(&items, 3);
        assert_eq!(chosen.len(), 3);

        // Every chosen item comes from the slice.
        for item in &chosen {
            assert!(items.contains(item));
        }

        // No duplicates.
        let mut sorted = chosen.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), chosen.len());
    }

    #[test]
    fn test_workflow_rand_choose_multiple_more_than_available() {
        let rand = WorkflowRand::new("wf-123");
        let items = vec![1, 2, 3];

        let chosen = rand.choose_multiple(&items, 10);
        assert_eq!(chosen.len(), 3); // Every element is returned
    }

    #[test]
    fn test_workflow_rand_choose_multiple_empty() {
        let rand = WorkflowRand::new("wf-123");
        let items: Vec<i32> = vec![];

        let chosen = rand.choose_multiple(&items, 5);
        assert_eq!(chosen.len(), 0);
    }

    #[test]
    fn test_workflow_rand_clone() {
        let rand1 = WorkflowRand::new("wf-123");
        let rand2 = rand1.clone();

        // Clones share one counter, so the second call continues the sequence.
        let uuid1 = rand1.uuid();
        let uuid2 = rand2.uuid();

        assert_ne!(uuid1, uuid2);

        // Independent sources for the same workflow start at the same point.
        let rand3 = WorkflowRand::new("wf-123");
        let rand4 = WorkflowRand::new("wf-123");
        let uuid3 = rand3.uuid();
        let uuid4 = rand4.uuid();
        assert_eq!(uuid3, uuid4);
    }

    #[test]
    fn test_workflow_rand_compute_seed_consistency() {
        let seed1 = own_seed("wf-123", 0);
        let seed2 = own_seed("wf-123", 0);

        // Same counter, same seed.
        assert_eq!(seed1, seed2);
    }

    #[test]
    fn test_workflow_rand_compute_seed_different_counters() {
        let seed1 = own_seed("wf-123", 0);
        let seed2 = own_seed("wf-123", 1);

        // Different counters, different seeds.
        assert_ne!(seed1, seed2);
    }

    #[test]
    fn test_workflow_rand_step_count() {
        let rand = WorkflowRand::new("wf-123");

        assert_eq!(rand.step_count(), 0);

        rand.uuid();
        assert_eq!(rand.step_count(), 1);

        rand.random();
        assert_eq!(rand.step_count(), 2);

        rand.random_range(1..10);
        assert_eq!(rand.step_count(), 3);

        rand.bool();
        assert_eq!(rand.step_count(), 4);
    }
}