aprender-verify 0.29.0

A scientific experiment into realistic provability with Rust - asymptotic test effectiveness framework
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
//! # Chaos Engineering Module
//!
//! Chaos engineering infrastructure adapted from **renacer v0.4.1**
//! (<https://github.com/paiml/renacer>).
//!
//! This module provides chaos engineering capabilities for testing system resilience
//! under adverse conditions. It integrates with the certeza testing framework to
//! validate behavior under resource constraints and failure scenarios.
//!
//! ## Chaos Testing Philosophy
//!
//! Chaos engineering validates that systems behave correctly under stress:
//! - Memory constraints (allocation pressure)
//! - CPU limits (throttling)
//! - Timeout conditions (deadlines)
//! - Signal injection (interrupts)
//!
//! ## Tier Integration
//!
//! - **Tier 2 (ON-COMMIT)**: Basic chaos tests with gentle constraints
//! - **Tier 3 (ON-MERGE/NIGHTLY)**: Aggressive chaos tests with extreme limits
//!
//! ## Example
//!
//! ```rust
//! use certeza::chaos::{ChaosConfig, ChaosResult};
//! use std::time::Duration;
//!
//! // Gentle chaos for development (renacer preset)
//! let config = ChaosConfig::gentle();
//!
//! // Aggressive chaos for CI (renacer preset)
//! let aggressive = ChaosConfig::aggressive();
//!
//! // Custom configuration (renacer builder pattern)
//! let custom = ChaosConfig::new()
//!     .with_memory_limit(128 * 1024 * 1024)  // 128MB
//!     .with_cpu_limit(0.5)                   // 50% CPU
//!     .with_timeout(Duration::from_secs(30))
//!     .with_signal_injection(true)
//!     .build();
//! ```
//!
//! ## Source
//!
//! This implementation is based on the renacer project's Sprint 29 chaos engineering
//! framework, adapted for certeza's tiered TDD-X approach.

use std::time::Duration;

/// Result type for chaos engineering operations.
///
/// Based on renacer's `ChaosResult<T>` pattern.
///
/// # Examples
///
/// ```rust
/// use certeza::chaos::{ChaosResult, ChaosError};
/// use std::time::Duration;
///
/// fn simulated_operation(limit: Duration) -> ChaosResult<String> {
///     let elapsed = Duration::from_secs(5);
///     if elapsed > limit {
///         Err(ChaosError::Timeout { elapsed, limit })
///     } else {
///         Ok("success".to_string())
///     }
/// }
/// ```
pub type ChaosResult<T> = Result<T, ChaosError>;

/// Error types for chaos engineering scenarios.
///
/// Based on renacer's chaos error taxonomy. These errors represent
/// intentional failure modes injected during chaos testing.
///
/// # Examples
///
/// ```rust
/// use certeza::chaos::ChaosError;
/// use std::time::Duration;
///
/// let error = ChaosError::MemoryLimitExceeded {
///     limit: 1024,
///     used: 2048,
/// };
///
/// assert_eq!(
///     format!("{}", error),
///     "Memory limit exceeded: 2048 > 1024 bytes"
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChaosError {
    /// Memory allocation exceeded configured limit.
    ///
    /// Represents out-of-memory conditions under resource constraints.
    MemoryLimitExceeded {
        /// Configured memory limit in bytes
        limit: usize,
        /// Actual memory usage in bytes
        used: usize,
    },

    /// Operation exceeded configured timeout.
    ///
    /// Represents deadline violations in time-constrained scenarios.
    Timeout {
        /// Actual elapsed time
        elapsed: Duration,
        /// Configured timeout limit
        limit: Duration,
    },

    /// Signal injection failed.
    ///
    /// Represents failures in interrupt simulation (SIGINT, SIGTERM, etc.).
    SignalInjectionFailed {
        /// Signal number (e.g., 2 for SIGINT, 15 for SIGTERM)
        signal: i32,
        /// Human-readable failure reason
        reason: String,
    },
}

impl std::fmt::Display for ChaosError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MemoryLimitExceeded { limit, used } => {
                write!(f, "Memory limit exceeded: {used} > {limit} bytes")
            }
            Self::Timeout { elapsed, limit } => {
                write!(f, "Timeout: {elapsed:?} > {limit:?}")
            }
            Self::SignalInjectionFailed { signal, reason } => {
                write!(f, "Signal injection failed ({signal}): {reason}")
            }
        }
    }
}

impl std::error::Error for ChaosError {}

/// Configuration for chaos engineering experiments.
///
/// Defines resource constraints and failure scenarios to inject during testing.
/// All constraints are optional - zero/default values mean no limit enforced.
///
/// # Examples
///
/// ```rust
/// use certeza::chaos::ChaosConfig;
/// use std::time::Duration;
///
/// // Default: no limits
/// let default = ChaosConfig::default();
/// assert_eq!(default.memory_limit, 0);
/// assert_eq!(default.cpu_limit, 0.0);
///
/// // Gentle constraints for local development
/// let gentle = ChaosConfig::gentle();
/// assert_eq!(gentle.memory_limit, 512 * 1024 * 1024);  // 512MB
///
/// // Aggressive constraints for CI/CD
/// let aggressive = ChaosConfig::aggressive();
/// assert_eq!(aggressive.memory_limit, 64 * 1024 * 1024);  // 64MB
/// assert!(aggressive.signal_injection);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ChaosConfig {
    /// Maximum memory allocation in bytes (0 = unlimited).
    ///
    /// When non-zero, operations exceeding this limit should fail gracefully.
    pub memory_limit: usize,

    /// CPU utilization limit as fraction (0.0-1.0, 0.0 = unlimited).
    ///
    /// Represents the maximum CPU fraction available to the process.
    /// Values outside [0.0, 1.0] are automatically clamped.
    pub cpu_limit: f64,

    /// Operation timeout duration.
    ///
    /// Long-running operations should respect this deadline and fail
    /// gracefully if exceeded.
    pub timeout: Duration,

    /// Whether to inject simulated signal interrupts.
    ///
    /// When enabled, tests may simulate SIGINT, SIGTERM, or other signals
    /// to validate interrupt handling.
    pub signal_injection: bool,
}

impl Default for ChaosConfig {
    /// Creates a configuration with no limits (passthrough mode).
    ///
    /// Useful for baseline testing without chaos constraints.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    ///
    /// let config = ChaosConfig::default();
    /// assert_eq!(config.memory_limit, 0);
    /// assert_eq!(config.cpu_limit, 0.0);
    /// assert!(!config.signal_injection);
    /// ```
    fn default() -> Self {
        Self {
            memory_limit: 0,
            cpu_limit: 0.0,
            timeout: Duration::from_secs(60),
            signal_injection: false,
        }
    }
}

impl ChaosConfig {
    /// Creates a new chaos configuration with default (unlimited) settings.
    ///
    /// Use the builder pattern to customize constraints.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    /// use std::time::Duration;
    ///
    /// let config = ChaosConfig::new()
    ///     .with_memory_limit(256 * 1024 * 1024)
    ///     .with_timeout(Duration::from_secs(120))
    ///     .build();
    ///
    /// assert_eq!(config.memory_limit, 256 * 1024 * 1024);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the memory limit in bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    ///
    /// let config = ChaosConfig::new()
    ///     .with_memory_limit(128 * 1024 * 1024);  // 128MB
    ///
    /// assert_eq!(config.memory_limit, 128 * 1024 * 1024);
    /// ```
    #[must_use]
    pub const fn with_memory_limit(mut self, bytes: usize) -> Self {
        self.memory_limit = bytes;
        self
    }

    /// Sets the CPU utilization limit as a fraction (0.0-1.0).
    ///
    /// Values outside the valid range are automatically clamped.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    ///
    /// // Valid fraction
    /// let config = ChaosConfig::new().with_cpu_limit(0.5);
    /// assert_eq!(config.cpu_limit, 0.5);
    ///
    /// // Clamped to maximum
    /// let clamped = ChaosConfig::new().with_cpu_limit(2.0);
    /// assert_eq!(clamped.cpu_limit, 1.0);
    ///
    /// // Clamped to minimum
    /// let negative = ChaosConfig::new().with_cpu_limit(-0.5);
    /// assert_eq!(negative.cpu_limit, 0.0);
    /// ```
    #[must_use]
    pub const fn with_cpu_limit(mut self, fraction: f64) -> Self {
        self.cpu_limit = fraction.clamp(0.0, 1.0);
        self
    }

    /// Sets the operation timeout.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    /// use std::time::Duration;
    ///
    /// let config = ChaosConfig::new()
    ///     .with_timeout(Duration::from_secs(30));
    ///
    /// assert_eq!(config.timeout, Duration::from_secs(30));
    /// ```
    #[must_use]
    pub const fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Enables or disables signal injection.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    ///
    /// let config = ChaosConfig::new().with_signal_injection(true);
    /// assert!(config.signal_injection);
    /// ```
    #[must_use]
    pub const fn with_signal_injection(mut self, enabled: bool) -> Self {
        self.signal_injection = enabled;
        self
    }

    /// Finalizes the configuration (terminal builder method).
    ///
    /// This is a no-op that exists for ergonomic builder pattern completion.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    ///
    /// let config = ChaosConfig::new()
    ///     .with_memory_limit(256 * 1024 * 1024)
    ///     .build();
    /// ```
    #[must_use]
    pub const fn build(self) -> Self {
        self
    }

    /// Creates a gentle chaos configuration for local development.
    ///
    /// Applies moderate constraints that catch obvious issues without
    /// excessive developer friction.
    ///
    /// **Constraints:**
    /// - Memory: 512MB
    /// - CPU: 80% (0.8)
    /// - Timeout: 120 seconds
    /// - Signal injection: disabled
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    /// use std::time::Duration;
    ///
    /// let config = ChaosConfig::gentle();
    /// assert_eq!(config.memory_limit, 512 * 1024 * 1024);
    /// assert_eq!(config.cpu_limit, 0.8);
    /// assert_eq!(config.timeout, Duration::from_secs(120));
    /// assert!(!config.signal_injection);
    /// ```
    #[must_use]
    pub fn gentle() -> Self {
        Self::new()
            .with_memory_limit(512 * 1024 * 1024) // 512MB
            .with_cpu_limit(0.8) // 80% CPU
            .with_timeout(Duration::from_secs(120))
    }

    /// Creates an aggressive chaos configuration for CI/CD and stress testing.
    ///
    /// Applies severe constraints to expose edge cases and failure modes
    /// that might occur in production under load.
    ///
    /// **Constraints:**
    /// - Memory: 64MB
    /// - CPU: 25% (0.25)
    /// - Timeout: 10 seconds
    /// - Signal injection: enabled
    ///
    /// # Examples
    ///
    /// ```rust
    /// use certeza::chaos::ChaosConfig;
    /// use std::time::Duration;
    ///
    /// let config = ChaosConfig::aggressive();
    /// assert_eq!(config.memory_limit, 64 * 1024 * 1024);
    /// assert_eq!(config.cpu_limit, 0.25);
    /// assert_eq!(config.timeout, Duration::from_secs(10));
    /// assert!(config.signal_injection);
    /// ```
    #[must_use]
    pub fn aggressive() -> Self {
        Self::new()
            .with_memory_limit(64 * 1024 * 1024) // 64MB
            .with_cpu_limit(0.25) // 25% CPU
            .with_timeout(Duration::from_secs(10))
            .with_signal_injection(true)
    }
}

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

    // ChaosConfig tests
    #[test]
    #[allow(clippy::float_cmp)]
    fn test_default_config() {
        let config = ChaosConfig::default();
        assert_eq!(config.memory_limit, 0);
        assert_eq!(config.cpu_limit, 0.0);
        assert_eq!(config.timeout, Duration::from_secs(60));
        assert!(!config.signal_injection);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_builder_pattern() {
        let config = ChaosConfig::new()
            .with_memory_limit(1024)
            .with_cpu_limit(0.5)
            .with_timeout(Duration::from_secs(30))
            .with_signal_injection(true)
            .build();

        assert_eq!(config.memory_limit, 1024);
        assert_eq!(config.cpu_limit, 0.5);
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert!(config.signal_injection);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_cpu_limit_clamping() {
        let too_high = ChaosConfig::new().with_cpu_limit(2.0);
        assert_eq!(too_high.cpu_limit, 1.0);

        let too_low = ChaosConfig::new().with_cpu_limit(-1.0);
        assert_eq!(too_low.cpu_limit, 0.0);

        let valid = ChaosConfig::new().with_cpu_limit(0.75);
        assert_eq!(valid.cpu_limit, 0.75);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_gentle_preset() {
        let config = ChaosConfig::gentle();
        assert_eq!(config.memory_limit, 512 * 1024 * 1024);
        assert_eq!(config.cpu_limit, 0.8);
        assert_eq!(config.timeout, Duration::from_secs(120));
        assert!(!config.signal_injection);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_aggressive_preset() {
        let config = ChaosConfig::aggressive();
        assert_eq!(config.memory_limit, 64 * 1024 * 1024);
        assert_eq!(config.cpu_limit, 0.25);
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert!(config.signal_injection);
    }

    #[test]
    #[allow(clippy::redundant_clone)]
    fn test_clone() {
        let original = ChaosConfig::gentle();
        let cloned = original.clone();
        assert_eq!(&original, &cloned);
    }

    #[test]
    fn test_debug_format() {
        let config = ChaosConfig::new();
        let debug_str = format!("{config:?}");
        assert!(debug_str.contains("ChaosConfig"));
        assert!(debug_str.contains("memory_limit"));
    }

    // ChaosError tests
    #[test]
    fn test_memory_limit_exceeded_display() {
        let error = ChaosError::MemoryLimitExceeded { limit: 1024, used: 2048 };
        assert_eq!(format!("{error}"), "Memory limit exceeded: 2048 > 1024 bytes");
    }

    #[test]
    fn test_timeout_display() {
        let error =
            ChaosError::Timeout { elapsed: Duration::from_secs(5), limit: Duration::from_secs(3) };
        let display = format!("{error}");
        assert!(display.contains("Timeout"));
        assert!(display.contains("5s"));
        assert!(display.contains("3s"));
    }

    #[test]
    fn test_signal_injection_failed_display() {
        let error = ChaosError::SignalInjectionFailed {
            signal: 2,
            reason: "Process not found".to_string(),
        };
        assert_eq!(format!("{error}"), "Signal injection failed (2): Process not found");
    }

    #[test]
    #[allow(clippy::redundant_clone)]
    fn test_chaos_error_clone() {
        let error = ChaosError::MemoryLimitExceeded { limit: 100, used: 200 };
        let cloned = error.clone();
        assert_eq!(&error, &cloned);
    }

    #[test]
    fn test_chaos_error_debug() {
        let error = ChaosError::Timeout {
            elapsed: Duration::from_secs(1),
            limit: Duration::from_millis(500),
        };
        let debug_str = format!("{error:?}");
        assert!(debug_str.contains("Timeout"));
    }

    // ChaosResult tests
    #[test]
    fn test_chaos_result_ok() {
        let result: ChaosResult<i32> = Ok(42);
        if let Ok(value) = result {
            assert_eq!(value, 42);
        } else {
            panic!("Expected Ok result");
        }
    }

    #[test]
    fn test_chaos_result_err() {
        let result: ChaosResult<i32> =
            Err(ChaosError::MemoryLimitExceeded { limit: 100, used: 200 });
        assert!(result.is_err());
    }
}