checkpointkit 0.1.4

Crash-resume checkpoint persistence with atomic writes and progress tracking
Documentation
//! Oneshot property tests for checkpointkit.
//! See TESTING.md for the Santh testing standard.

use checkpointkit::{load, save, CheckpointError, ProgressTracker};
use proptest::prelude::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tempfile::tempdir;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
struct ArbitraryStruct {
    a: i32,
    b: String,
    c: Vec<u8>,
}

prop_compose! {
    fn arbitrary_struct()(
        a in any::<i32>(),
        b in ".*",
        c in prop::collection::vec(any::<u8>(), 0..100)
    ) -> ArbitraryStruct {
        ArbitraryStruct { a, b, c }
    }
}

proptest! {
    #[test]
    fn test_depth_save_and_load_arbitrary_struct(s in arbitrary_struct()) {
        let dir = tempdir().unwrap();
        let path = dir.path().join("prop_struct.json");
        
        save(&path, &s).unwrap();
        let loaded: Option<ArbitraryStruct> = load(&path).unwrap();
        
        prop_assert_eq!(loaded, Some(s));
    }

    #[test]
    fn test_depth_progress_tracker_increment_never_exceeds_total(
        total in 1..1000u64,
        increments in prop::collection::vec(0..500u64, 1..50)
    ) {
        let dir = tempdir().unwrap();
        let path = dir.path().join("prop_progress.json");
        
        let mut tracker = ProgressTracker::new(total, &path, Duration::from_secs(1)).unwrap();
        
        let mut expected = 0u64;
        for inc in increments {
            expected = expected.saturating_add(inc).min(total);
            let current = tracker.increment(inc).unwrap();
            
            prop_assert_eq!(current, expected);
            prop_assert_eq!(tracker.current(), expected);
            prop_assert!(tracker.current() <= total);
        }
    }
    
    #[test]
    fn test_depth_load_on_garbage_bytes_never_panics(
        garbage in prop::collection::vec(any::<u8>(), 0..1000)
    ) {
        let dir = tempdir().unwrap();
        let path = dir.path().join("garbage_prop.json");
        
        std::fs::write(&path, &garbage).unwrap();
        
        let result: Result<Option<ArbitraryStruct>, CheckpointError> = load(&path);
        
        // The documented `load` contract partitions outcomes three ways:
        //   - Ok(_)            : bytes happen to be valid JSON for the target type
        //   - Serialize(_)     : bytes are non-empty but not valid JSON for the type
        //   - CorruptCheckpoint: the file exists but is 0 bytes, i.e. truncated or
        //     externally clobbered (proptest's minimal case `garbage = []` writes
        //     exactly that file). `load` fails CLOSED on this instead of silently
        //     returning Ok(None), which would discard a corrupt checkpoint with no
        //     operator signal.
        // Any other outcome, or a panic, is a bug.
        match result {
            Ok(_)
            | Err(CheckpointError::Serialize(_))
            | Err(CheckpointError::CorruptCheckpoint(_)) => {}
            Err(e) => prop_assert!(false, "Expected Ok, Serialize, or CorruptCheckpoint error, got {:?}", e),
        }
    }
}