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");
fs::write(&path, br#"{"name":"test""#).unwrap();
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");
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");
fs::write(&path, b"\0\0\0\0").unwrap();
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");
checkpointkit::save(&path, &42).unwrap();
let tmp_path = dir.path().join("valid.json.tmp.12345");
fs::write(&tmp_path, b"garbage").unwrap();
let loaded: Option<i32> = load(&path).unwrap();
assert_eq!(loaded, Some(42));
}