checkpointkit 0.1.4

Crash-resume checkpoint persistence with atomic writes and progress tracking
Documentation
//! crash/partial_write tests for checkpointkit.

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

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct PartialState {
    id: u64,
    payload: String,
}

/// A partially written file where the JSON structure is open and suddenly ends.
#[test]
fn partial_write_abrupt_eof() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("partial1.json");

    fs::write(&path, b"{\"id\": 42, \"payload\": \"some dat").unwrap();

    let result = load::<PartialState>(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error for abrupt EOF, got {:?}",
        result
    );
}

/// A partially written file where the JSON structure ends inside an array/struct without closing brackets.
#[test]
fn partial_write_missing_closing_brackets() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("partial2.json");

    fs::write(&path, b"{\"id\": 42, \"payload\": \"done\"").unwrap();

    let result = load::<PartialState>(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error for missing closing bracket, got {:?}",
        result
    );
}

/// A partially written file that happens to end exactly at a comma.
#[test]
fn partial_write_trailing_comma() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("partial3.json");

    fs::write(&path, b"{\"id\": 42,").unwrap();

    let result = load::<PartialState>(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error for trailing comma EOF, got {:?}",
        result
    );
}

/// A partially written file containing only a field name.
#[test]
fn partial_write_only_field_name() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("partial4.json");

    fs::write(&path, b"{\"id\":").unwrap();

    let result = load::<PartialState>(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error for trailing colon EOF, got {:?}",
        result
    );
}

/// A partial write where a numeric value is cut off.
/// This might parse successfully if the numeric value is valid (e.g. `4` instead of `42`),
/// but it will fail later due to missing closing bracket.
#[test]
fn partial_write_cut_off_number() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("partial5.json");

    fs::write(&path, b"{\"id\": 4").unwrap(); // missing closing bracket

    let result = load::<PartialState>(&path);
    assert!(
        matches!(result, Err(CheckpointError::Serialize(_))),
        "Expected Serialize error for cut off number missing bracket, got {:?}",
        result
    );
}