checkpointkit 0.1.2

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

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

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
struct GapState {
    data: String,
}

#[test]
#[cfg(unix)]
fn test_depth_gap_symlink_handling() {
    let dir = tempdir().unwrap();
    let target_path = dir.path().join("target.json");
    let link_path = dir.path().join("link.json");
    
    save(&target_path, &GapState { data: "target".to_string() }).unwrap();
    std::os::unix::fs::symlink(&target_path, &link_path).unwrap();
    
    let state = GapState { data: "new_data".to_string() };
    save(&link_path, &state).unwrap();
    
    let meta = std::fs::symlink_metadata(&link_path).unwrap();
    
    assert!(
        meta.is_file(), 
        "Gap: atomic rename should have replaced the symlink with a regular file"
    );
}

#[test]
fn test_depth_gap_zero_byte_file_is_none() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("empty_gap.json");
    
    std::fs::write(&path, []).unwrap();
    
    let result: Result<Option<GapState>, _> = load(&path);

    match &result {
        // An existing 0-byte checkpoint is corruption, not absence: `load`
        // fails closed with CorruptCheckpoint instead of silently restarting.
        Err(CheckpointError::CorruptCheckpoint(p)) => {
            assert_eq!(p, &path, "error must name the corrupt checkpoint path");
        }
        _ => panic!("Gap finding: empty file returned {result:?}"),
    }
}

#[test]
fn test_depth_gap_concurrent_progress_trackers() {
    use std::sync::{Arc, Barrier};
    use std::thread;
    
    let dir = tempdir().unwrap();
    let path = Arc::new(dir.path().join("concurrent_progress.json"));
    let barrier = Arc::new(Barrier::new(5));
    
    let mut handles = vec![];
    for _ in 0..5 {
        let path = Arc::clone(&path);
        let barrier = Arc::clone(&barrier);
        handles.push(thread::spawn(move || {
            let mut tracker = ProgressTracker::new(100, &*path, Duration::ZERO).unwrap();
            barrier.wait();
            tracker.increment(10).unwrap();
        }));
    }
    
    for h in handles {
        h.join().unwrap();
    }
    
    let final_tracker = ProgressTracker::new(100, &*path, Duration::ZERO).unwrap();
    assert!(final_tracker.current() >= 10);
    assert!(final_tracker.current() <= 50);
}

#[test]
fn test_depth_gap_deeply_nested_structure_overflow() {
    #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
    enum RecursiveState {
        End,
        Next(Box<RecursiveState>),
    }
    
    let mut state = RecursiveState::End;
    for _ in 0..1000 {
        state = RecursiveState::Next(Box::new(state));
    }
    
    let dir = tempdir().unwrap();
    let path = dir.path().join("deep.json");
    
    let res = save(&path, &state);
    assert!(res.is_ok(), "Deeply nested save failed: {res:?}");
    
    let loaded: Result<Option<RecursiveState>, _> = load(&path);
    match loaded {
        Ok(_) | Err(CheckpointError::Serialize(_)) => {}
        Err(e) => panic!("Deeply nested load failed with unexpected error: {e:?}"),
    }
}