checkpointkit 0.1.2

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

use checkpointkit::{load, CheckpointError};
use std::fs;
use tempfile::tempdir;

mod corruption;
mod disk_full;
mod partial_write;
mod power_failure;
mod wal_recovery;

#[test]
fn test_load_truncated_file() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("truncated.json");
    
    // Write an incomplete JSON file
    fs::write(&path, br#"{"name":"test""#).unwrap();
    
    // Use serde_json::Value instead of String since the JSON is an object
    let result: Result<Option<serde_json::Value>, _> = load(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error, got {result:?}"
    );
}

#[test]
fn test_load_empty_file() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("empty.json");
    
    // Write an empty file
    fs::write(&path, b"").unwrap();
    
    let result: Result<Option<String>, _> = load(&path);
    assert!(
        matches!(&result, Err(CheckpointError::CorruptCheckpoint(p)) if p == &path),
        "an existing zero-byte file is corruption, not absence: expected CorruptCheckpoint, got {result:?}"
    );
}

#[test]
fn test_load_null_bytes() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("nulls.json");
    
    // Write a file with null bytes
    fs::write(&path, b"\0\0\0\0").unwrap();
    
    // Use serde_json::Value
    let result: Result<Option<serde_json::Value>, _> = load(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error, got {result:?}"
    );
}

#[test]
fn test_orphaned_tmp_files_do_not_corrupt_load() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("valid.json");
    
    // Write a valid checkpoint
    checkpointkit::save(&path, &42).unwrap();
    
    // Simulate a crashed write by creating an orphaned tmp file
    let tmp_path = dir.path().join("valid.json.tmp.12345");
    fs::write(&tmp_path, b"garbage").unwrap();
    
    // Load should read the valid file and ignore the tmp file
    let loaded: Option<i32> = load(&path).unwrap();
    assert_eq!(loaded, Some(42));
}