checkpointkit 0.1.4

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

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

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
struct ComplexState {
    id: String,
    items_processed: u64,
    errors: Vec<String>,
    is_complete: bool,
}

#[test]
fn test_long_running_process_simulation() {
    let dir = tempdir().unwrap();
    let state_path = dir.path().join("state.json");
    let progress_path = dir.path().join("progress.json");
    
    let total_items = 100;
    
    // Simulate process start
    let mut tracker = ProgressTracker::new(total_items, &progress_path, Duration::from_millis(10)).unwrap();
    let mut state = ComplexState {
        id: "job_123".to_string(),
        items_processed: tracker.current(),
        errors: vec![],
        is_complete: false,
    };
    
    // Process a few items
    tracker.increment(20).unwrap();
    state.items_processed = tracker.current();
    save(&state_path, &state).unwrap();
    
    // Process a few more items and hit an error
    tracker.increment(15).unwrap();
    state.items_processed = tracker.current();
    state.errors.push("timeout on item 30".to_string());
    save(&state_path, &state).unwrap();
    
    // Ensure the intervals pass so that it actually saves
    std::thread::sleep(Duration::from_millis(15));
    // Force a save by incrementing 0 after the interval has passed
    tracker.increment(0).unwrap();

    // Simulate a crash and restart
    drop(tracker);
    
    let mut tracker2 = ProgressTracker::new(total_items, &progress_path, Duration::from_millis(10)).unwrap();
    let mut state2: ComplexState = load(&state_path).unwrap().unwrap();
    
    assert!(tracker2.current() == 35, "progress should have saved 35");
    
    // Finish the job
    let remaining = total_items - tracker2.current();
    tracker2.increment(remaining).unwrap();
    state2.items_processed = tracker2.current();
    state2.is_complete = true;
    save(&state_path, &state2).unwrap();
    
    // Verify final state
    let final_state: ComplexState = load(&state_path).unwrap().unwrap();
    assert_eq!(final_state.items_processed, 100);
    assert!(final_state.is_complete);
    assert_eq!(final_state.errors.len(), 1);
    
    let final_tracker = ProgressTracker::new(total_items, &progress_path, Duration::from_millis(10)).unwrap();
    assert_eq!(final_tracker.current(), 100);
}
mod test_depth_gap;