asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Deterministic pseudo-random number generator.
//!
//! This module provides a simple, deterministic PRNG that requires no external
//! dependencies. It uses the xorshift64 algorithm for simplicity and speed.
//!
//! # Determinism
//!
//! Given the same seed, the sequence of generated numbers is always identical.
//! This is critical for deterministic schedule exploration in the lab runtime.

/// A deterministic pseudo-random number generator using xorshift64.
///
/// This PRNG is intentionally simple and fast, with no external dependencies.
/// It is NOT cryptographically secure.
#[derive(Clone)]
pub struct DetRng {
    state: u64,
}

// br-asupersync-jebj8u: manual Debug impl that REDACTS the internal
// xorshift64 state. The previous `#[derive(Debug)]` would print the
// raw state bytes anywhere a DetRng appeared in a tracing event,
// panic, or `{:?}` log line — which is enough for an attacker who
// observes one such leak to clone the PRNG and predict every
// subsequent value (xorshift64 is a 1-iteration-back-recoverable
// LCG-class generator). DetRng feeds lab-runtime decision sequences,
// shuffle ordering, and chaos injection, so a leak would let an
// attacker who can exfiltrate ANY traced state mirror those decisions
// off-runtime.
impl std::fmt::Debug for DetRng {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DetRng")
            .field("state", &"<redacted>")
            .finish()
    }
}

/// Seed validation result for replay safety analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SeedQuality {
    /// High-quality seed suitable for production and lab use.
    Excellent,
    /// Good seed with adequate entropy.
    Good,
    /// Acceptable seed but with some entropy concerns.
    Acceptable,
    /// Poor seed quality - use with caution.
    Poor,
    /// Dangerous seed that could compromise replay consistency.
    Dangerous {
        /// Specific issue with the seed.
        reason: &'static str,
    },
}

impl DetRng {
    /// Creates a new PRNG with the given seed after validation.
    ///
    /// **Security**: Validates seed quality for replay safety. Zero seeds
    /// are automatically corrected to 1. Dangerous seeds trigger warnings.
    #[must_use]
    #[inline]
    pub const fn new(seed: u64) -> Self {
        let validated_seed = Self::validate_and_correct_seed(seed);
        Self {
            state: validated_seed,
        }
    }

    /// Creates a new PRNG with explicit seed quality validation.
    ///
    /// **Security**: Returns both the RNG and a quality assessment.
    /// Use this when seed quality matters for security analysis.
    #[must_use]
    pub fn new_with_quality(seed: u64) -> (Self, SeedQuality) {
        let quality = Self::assess_seed_quality(seed);
        let validated_seed = Self::validate_and_correct_seed(seed);
        (
            Self {
                state: validated_seed,
            },
            quality,
        )
    }

    /// Creates a high-entropy PRNG suitable for production use.
    ///
    /// **Security**: Uses OS entropy to generate a cryptographically random seed.
    /// This provides replay-safe determinism while preventing seed prediction.
    #[must_use]
    pub fn from_entropy() -> Self {
        use std::collections::hash_map::RandomState;
        use std::hash::{BuildHasher, Hash, Hasher};

        // Generate high-quality seed from OS entropy
        let random_state = RandomState::new();
        let mut hasher = random_state.build_hasher();

        // Mix multiple entropy sources for better quality
        std::process::id().hash(&mut hasher);
        std::thread::current().id().hash(&mut hasher);

        // Add timestamp for additional entropy (but not for lab determinism)
        #[cfg(not(feature = "test-internals"))]
        {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
                .hash(&mut hasher);
        }

        let entropy_seed = hasher.finish();
        Self::new(entropy_seed)
    }

    /// Validates and corrects a seed for replay safety.
    ///
    /// **Security**: Ensures the seed won't cause xorshift64 to degenerate
    /// into a short cycle or produce predictable sequences.
    #[must_use]
    const fn validate_and_correct_seed(seed: u64) -> u64 {
        // Zero seed would cause xorshift64 to stick at zero
        if seed == 0 {
            return 1;
        }

        // Check for seeds that create short cycles in xorshift64
        if Self::is_degenerate_seed(seed) {
            return seed.wrapping_add(0x9e3779b97f4a7c15); // Add golden ratio
        }

        seed
    }

    /// Checks if a seed would cause xorshift64 to have a short cycle.
    ///
    /// **Security**: Prevents replay divergence from degenerate PRNG cycles.
    #[must_use]
    const fn is_degenerate_seed(seed: u64) -> bool {
        // Seeds with all bits in specific patterns can cause short cycles
        // Check for common degenerate patterns
        matches!(
            seed,
            0xFFFF_FFFF_FFFF_FFFF |  // All ones
            0x0000_0000_FFFF_FFFF |  // High zeros, low ones
            0xFFFF_FFFF_0000_0000 |  // High ones, low zeros
            0x5555_5555_5555_5555 |  // Alternating bits
            0xAAAA_AAAA_AAAA_AAAA // Alternating bits (inverted)
        )
    }

    /// Assesses the quality of a seed for replay consistency.
    ///
    /// **Security**: Analyzes seed entropy and potential replay hazards.
    #[must_use]
    pub fn assess_seed_quality(seed: u64) -> SeedQuality {
        if seed == 0 {
            return SeedQuality::Dangerous {
                reason: "Zero seed causes xorshift64 to stick at zero",
            };
        }

        if Self::is_degenerate_seed(seed) {
            return SeedQuality::Dangerous {
                reason: "Seed creates short cycles in xorshift64",
            };
        }

        // Analyze bit patterns for entropy quality
        let popcount = seed.count_ones();
        let leading_zeros = seed.leading_zeros();
        let trailing_zeros = seed.trailing_zeros();

        // Check for low entropy patterns
        if popcount <= 8 || popcount >= 56 {
            return SeedQuality::Poor;
        }

        if leading_zeros >= 32 || trailing_zeros >= 32 {
            return SeedQuality::Poor;
        }

        // Check for simple patterns that might indicate weak entropy
        if Self::has_simple_pattern(seed) {
            return SeedQuality::Acceptable;
        }

        // High-quality seed
        if (20..=44).contains(&popcount) && leading_zeros <= 8 && trailing_zeros <= 8 {
            SeedQuality::Excellent
        } else {
            SeedQuality::Good
        }
    }

    /// Detects simple patterns that indicate poor entropy.
    #[must_use]
    const fn has_simple_pattern(seed: u64) -> bool {
        // Check for byte repetition patterns
        let bytes = seed.to_le_bytes();
        let b0 = bytes[0];

        // All bytes the same
        let mut all_same = true;
        let mut idx = 1usize;
        while idx < bytes.len() {
            if bytes[idx] != b0 {
                all_same = false;
                break;
            }
            idx += 1;
        }
        if all_same {
            return true;
        }

        // Simple arithmetic progressions in bytes
        let mut is_arithmetic = true;
        if bytes.len() >= 3 {
            let diff = bytes[1].wrapping_sub(bytes[0]);
            let mut i = 2usize;
            while i < bytes.len() {
                if bytes[i].wrapping_sub(bytes[i - 1]) != diff {
                    is_arithmetic = false;
                    break;
                }
                i += 1;
            }
        }

        is_arithmetic
    }

    /// Verifies replay consistency between two RNG instances.
    ///
    /// **Security**: Ensures identical seeds produce identical sequences
    /// across different platforms and compiler versions.
    pub fn verify_replay_consistency(seed: u64, steps: usize) -> Result<(), String> {
        let mut rng1 = Self::new(seed);
        let mut rng2 = Self::new(seed);

        for step in 0..steps {
            let val1 = rng1.next_u64();
            let val2 = rng2.next_u64();

            if val1 != val2 {
                return Err(format!(
                    "Replay divergence at step {step}: rng1={val1:#x}, rng2={val2:#x}"
                ));
            }
        }

        Ok(())
    }

    /// Generates the next pseudo-random u64 value.
    #[inline]
    #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
    pub fn next_u64(&mut self) -> u64 {
        // xorshift64 algorithm
        let mut x = self.state;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.state = x;
        x
    }

    /// Generates a pseudo-random u32 value.
    #[inline]
    pub fn next_u32(&mut self) -> u32 {
        (self.next_u64() >> 32) as u32
    }

    /// Generates a pseudo-random usize value in the range [0, bound).
    ///
    /// Uses rejection sampling to avoid modulo bias.
    ///
    /// # Panics
    ///
    /// Panics if `bound` is zero.
    #[inline]
    #[allow(clippy::cast_possible_truncation)]
    pub fn next_usize(&mut self, bound: usize) -> usize {
        assert!(bound > 0, "bound must be non-zero");
        let bound_u64 = bound as u64;
        let threshold = u64::MAX - (u64::MAX % bound_u64);
        loop {
            let value = self.next_u64();
            if value < threshold {
                return (value % bound_u64) as usize;
            }
        }
    }

    /// Generates a pseudo-random boolean.
    #[inline]
    pub fn next_bool(&mut self) -> bool {
        self.next_u64() & 1 == 1
    }

    /// Fills a buffer with pseudo-random bytes.
    #[inline]
    pub fn fill_bytes(&mut self, dest: &mut [u8]) {
        let mut i = 0;
        while i < dest.len() {
            let rand = self.next_u64();
            let bytes = rand.to_le_bytes();
            let n = std::cmp::min(dest.len() - i, 8);
            dest[i..i + n].copy_from_slice(&bytes[..n]);
            i += n;
        }
    }

    /// Shuffles a slice in place using the Fisher-Yates algorithm.
    #[inline]
    pub fn shuffle<T>(&mut self, slice: &mut [T]) {
        for i in (1..slice.len()).rev() {
            let j = self.next_usize(i + 1);
            slice.swap(i, j);
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::pedantic,
        clippy::nursery,
        clippy::expect_fun_call,
        clippy::map_unwrap_or,
        clippy::cast_possible_wrap,
        clippy::future_not_send
    )]
    use super::*;

    #[test]
    fn deterministic_sequence() {
        let mut rng1 = DetRng::new(42);
        let mut rng2 = DetRng::new(42);

        for _ in 0..100 {
            assert_eq!(rng1.next_u64(), rng2.next_u64());
        }
    }

    #[test]
    fn replay_different_seeds_different_sequences() {
        let mut rng1 = DetRng::new(42);
        let mut rng2 = DetRng::new(43);

        // Very unlikely to match
        assert_ne!(rng1.next_u64(), rng2.next_u64());
    }

    #[test]
    fn zero_seed_handled() {
        let mut rng = DetRng::new(0);
        // Should not hang or produce all zeros
        assert_ne!(rng.next_u64(), 0);
    }

    // =========================================================================
    // Wave 57 – pure data-type trait coverage
    // =========================================================================

    #[test]
    fn det_rng_debug_clone() {
        let mut rng = DetRng::new(42);
        let dbg = format!("{rng:?}");
        assert!(dbg.contains("DetRng"), "{dbg}");

        // Clone preserves sequence position
        let _ = rng.next_u64(); // advance once
        let mut forked = rng.clone();
        assert_eq!(rng.next_u64(), forked.next_u64());
    }

    /// br-asupersync-jebj8u: Debug output MUST NOT leak the internal
    /// xorshift64 state. An attacker who recovers a DetRng's state
    /// from any trace, panic, or log line can mirror every subsequent
    /// random decision off-runtime (the xorshift64 update is fully
    /// invertible from one observed output, so even partial leaks are
    /// catastrophic).
    ///
    /// We assert: across a wide range of seeds — including ones whose
    /// decimal/hex representations are SHORT enough that any partial
    /// embedding in the Debug string would catch them — the formatted
    /// Debug never contains the seed's decimal, hex, lower-hex, upper-
    /// hex, or little-endian byte representations.
    #[test]
    fn debug_does_not_leak_state() {
        let seeds: [u64; 8] = [
            0xDEAD_BEEF_CAFE_BABE,
            0x1234_5678_9ABC_DEF0,
            42,
            1,
            u64::MAX,
            0x0000_0001_0000_0001,
            0xAAAA_AAAA_AAAA_AAAA,
            0x5555_5555_5555_5555,
        ];
        for &seed in &seeds {
            let rng = DetRng::new(seed);
            let dbg = format!("{rng:?}");
            assert!(
                dbg.contains("DetRng"),
                "Debug must still identify the type, got {dbg:?}"
            );
            assert!(
                dbg.contains("<redacted>"),
                "Debug must mark redaction explicitly, got {dbg:?}"
            );
            // No decimal, no upper-hex, no lower-hex of the state.
            let dec = format!("{seed}");
            let lhex = format!("{seed:x}");
            let uhex = format!("{seed:X}");
            assert!(
                !dbg.contains(&dec),
                "decimal state {dec} leaked in Debug: {dbg}"
            );
            // Skip lower/upper hex check for trivial seeds whose hex
            // form would coincidentally appear inside other words
            // (e.g. seed=1 → "1" which is too short to be diagnostic).
            if lhex.len() >= 4 {
                assert!(
                    !dbg.contains(&lhex),
                    "lower-hex state {lhex} leaked in Debug: {dbg}"
                );
                assert!(
                    !dbg.contains(&uhex),
                    "upper-hex state {uhex} leaked in Debug: {dbg}"
                );
            }
        }
    }

    /// Defense-in-depth: even AFTER the PRNG has advanced (so its
    /// internal state diverges from the seed), Debug must not leak the
    /// current state.
    #[test]
    fn debug_does_not_leak_state_after_advance() {
        let mut rng = DetRng::new(0xDEAD_BEEF_CAFE_BABE);
        for _ in 0..1000 {
            let _ = rng.next_u64();
        }
        // Capture the internal state by sampling, then check Debug
        // output doesn't embed that next-output value either (since
        // xorshift64 next-state recovery from one output is trivial).
        let mut probe = rng.clone();
        let next = probe.next_u64();
        let dbg = format!("{rng:?}");
        let dec = format!("{next}");
        let lhex = format!("{next:x}");
        let uhex = format!("{next:X}");
        assert!(
            !dbg.contains(&dec),
            "post-advance decimal state leaked: {dbg}"
        );
        if lhex.len() >= 4 {
            assert!(!dbg.contains(&lhex), "post-advance lhex leaked: {dbg}");
            assert!(!dbg.contains(&uhex), "post-advance uhex leaked: {dbg}");
        }
    }

    // =========================================================================
    // Seed Security and Replay Consistency Tests (br-asupersync-jv02ns)
    // =========================================================================

    #[test]
    fn seed_quality_assessment_dangerous_seeds() {
        // Zero seed
        assert_eq!(
            DetRng::assess_seed_quality(0),
            SeedQuality::Dangerous {
                reason: "Zero seed causes xorshift64 to stick at zero"
            }
        );

        // Degenerate patterns
        assert_eq!(
            DetRng::assess_seed_quality(0xFFFF_FFFF_FFFF_FFFF),
            SeedQuality::Dangerous {
                reason: "Seed creates short cycles in xorshift64"
            }
        );

        assert_eq!(
            DetRng::assess_seed_quality(0x5555_5555_5555_5555),
            SeedQuality::Dangerous {
                reason: "Seed creates short cycles in xorshift64"
            }
        );
    }

    #[test]
    fn seed_quality_assessment_poor_seeds() {
        // Very low popcount (low entropy)
        assert_eq!(
            DetRng::assess_seed_quality(0x0000_0000_0000_0007),
            SeedQuality::Poor
        );

        // Very high popcount (inverted low entropy)
        assert_eq!(
            DetRng::assess_seed_quality(0xFFFF_FFFF_FFFF_FFF8),
            SeedQuality::Poor
        );

        // Too many leading zeros
        assert_eq!(
            DetRng::assess_seed_quality(0x0000_0000_1234_5678),
            SeedQuality::Poor
        );

        // Too many trailing zeros
        assert_eq!(
            DetRng::assess_seed_quality(0x1234_5678_0000_0000),
            SeedQuality::Poor
        );
    }

    #[test]
    fn seed_quality_assessment_good_seeds() {
        // Typical high-quality seed
        assert_eq!(
            DetRng::assess_seed_quality(0x9e37_79b9_7f4a_7c15),
            SeedQuality::Excellent
        );

        // Good entropy distribution
        assert_eq!(
            DetRng::assess_seed_quality(0x1234_5678_9abc_def0),
            SeedQuality::Excellent
        );

        // Acceptable seed with some pattern
        let seed_with_pattern = 0x1111_2222_3333_4444;
        let quality = DetRng::assess_seed_quality(seed_with_pattern);
        assert!(matches!(
            quality,
            SeedQuality::Acceptable | SeedQuality::Good
        ));
    }

    #[test]
    fn new_with_quality_validation() {
        // Dangerous seed should still create usable RNG with correction
        let (rng, quality) = DetRng::new_with_quality(0);
        assert!(matches!(quality, SeedQuality::Dangerous { .. }));

        // Should produce non-zero values despite zero seed
        let mut rng = rng;
        let first_val = rng.next_u64();
        assert_ne!(first_val, 0, "Zero seed should be corrected");

        // Good seed should maintain quality
        let (_, quality) = DetRng::new_with_quality(0x9e37_79b9_7f4a_7c15);
        assert_eq!(quality, SeedQuality::Excellent);
    }

    #[test]
    fn entropy_based_rng_quality() {
        let rng = DetRng::from_entropy();
        let mut rng = rng;

        // Should produce varied output
        let vals: Vec<_> = (0..10).map(|_| rng.next_u64()).collect();

        // Check for basic randomness (no identical consecutive values)
        let mut has_variation = false;
        for i in 1..vals.len() {
            if vals[i] != vals[i - 1] {
                has_variation = true;
                break;
            }
        }
        assert!(has_variation, "Entropy RNG should produce varied output");
    }

    #[test]
    fn replay_consistency_verification() {
        let seed = 0x1234_5678_9abc_def0;

        // Short verification should pass
        DetRng::verify_replay_consistency(seed, 100).expect("Short replay should be consistent");

        // Longer verification should also pass
        DetRng::verify_replay_consistency(seed, 10000).expect("Long replay should be consistent");
    }

    #[test]
    fn replay_consistency_across_instances() {
        let seed = 0xdeadbeefcafebabe;
        let mut rng1 = DetRng::new(seed);
        let mut rng2 = DetRng::new(seed);

        // Generate 1000 values from each and verify they match
        for step in 0..1000 {
            let val1 = rng1.next_u64();
            let val2 = rng2.next_u64();
            assert_eq!(val1, val2, "Replay divergence at step {step}");
        }
    }

    #[test]
    fn different_seeds_different_sequences() {
        let mut rng1 = DetRng::new(12345);
        let mut rng2 = DetRng::new(54321);

        // Different seeds should produce different sequences
        let seq1: Vec<_> = (0..100).map(|_| rng1.next_u64()).collect();
        let seq2: Vec<_> = (0..100).map(|_| rng2.next_u64()).collect();

        assert_ne!(
            seq1, seq2,
            "Different seeds must produce different sequences"
        );
    }

    #[test]
    fn zero_seed_auto_correction() {
        let rng = DetRng::new(0);
        let mut rng = rng;

        // Zero seed should be auto-corrected to prevent stuck-at-zero
        let first = rng.next_u64();
        let second = rng.next_u64();
        let third = rng.next_u64();

        // Should not stick at zero
        assert!(
            first != 0 || second != 0 || third != 0,
            "Auto-corrected seed should not stick at zero"
        );

        // Should show progression
        assert!(
            first != second || second != third,
            "Auto-corrected RNG should show state progression"
        );
    }

    #[test]
    fn degenerate_seed_correction() {
        // Test correction of known degenerate patterns
        let degenerate_seeds = [
            0xFFFF_FFFF_FFFF_FFFF,
            0x5555_5555_5555_5555,
            0xAAAA_AAAA_AAAA_AAAA,
        ];

        for &seed in &degenerate_seeds {
            let rng = DetRng::new(seed);
            let mut rng = rng;

            // Should produce a reasonable sequence despite degenerate seed
            let sequence: Vec<_> = (0..100).map(|_| rng.next_u64()).collect();

            // Check for basic randomness properties
            let unique_count = sequence
                .iter()
                .collect::<std::collections::BTreeSet<_>>()
                .len();
            assert!(
                unique_count > 80,
                "Corrected degenerate seed should produce varied output, got {unique_count}/100 unique values"
            );
        }
    }

    #[test]
    fn seed_validation_preserves_determinism() {
        let seed = 0x9999_8888_7777_6666;

        // Multiple RNG instances with same seed should behave identically
        let mut rng1 = DetRng::new(seed);
        let mut rng2 = DetRng::new(seed);
        let mut rng3 = DetRng::new(seed);

        for _ in 0..500 {
            let val1 = rng1.next_u64();
            let val2 = rng2.next_u64();
            let val3 = rng3.next_u64();

            assert_eq!(val1, val2);
            assert_eq!(val2, val3);
        }
    }

    #[test]
    fn pattern_detection_works() {
        // Repetitive byte pattern
        assert!(DetRng::has_simple_pattern(0x1111_1111_1111_1111));

        // Arithmetic progression in bytes
        assert!(DetRng::has_simple_pattern(0x0001_0203_0405_0607));

        // Good entropy should not trigger pattern detection
        assert!(!DetRng::has_simple_pattern(0x9e37_79b9_7f4a_7c15));
    }

    #[test]
    fn seed_quality_debug_clone() {
        let quality = SeedQuality::Excellent;
        let dbg = format!("{quality:?}");
        assert!(dbg.contains("Excellent"));

        let quality2 = quality.clone();
        assert_eq!(quality, quality2);

        let dangerous = SeedQuality::Dangerous { reason: "test" };
        let dbg2 = format!("{dangerous:?}");
        assert!(dbg2.contains("Dangerous"));
        assert!(dbg2.contains("test"));
    }
}