use checkpointkit::{load, save, CheckpointError, ProgressTracker};
use serde::{Deserialize, Serialize};
use std::fs;
use std::time::Duration;
use tempfile::tempdir;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct CorruptionState {
value: String,
metadata: u64,
}
#[test]
fn corruption_garbled_json_returns_error() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt1.json");
fs::write(&path, b"{ \x00\x01\x02 garbled \xff }").unwrap();
let result = load::<CorruptionState>(&path);
assert!(
matches!(result, Err(CheckpointError::Serialize(_))),
"Expected Serialize error for garbled JSON, got {:?}",
result
);
}
#[test]
fn corruption_wrong_schema_returns_error() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt2.json");
fs::write(&path, b"[\"some\", \"array\"]").unwrap();
let result = load::<CorruptionState>(&path);
assert!(
matches!(result, Err(CheckpointError::Serialize(_))),
"Expected Serialize error for wrong schema, got {:?}",
result
);
}
#[test]
fn corruption_wrong_types_in_schema_returns_error() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt3.json");
fs::write(&path, b"{\"value\": \"test\", \"metadata\": \"not_a_number\"}").unwrap();
let result = load::<CorruptionState>(&path);
assert!(
matches!(result, Err(CheckpointError::Serialize(_))),
"Expected Serialize error for wrong field types, got {:?}",
result
);
}
#[test]
fn corruption_empty_file_returns_none() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt4.json");
fs::File::create(&path).unwrap();
let result = load::<CorruptionState>(&path);
assert!(
matches!(&result, Err(CheckpointError::CorruptCheckpoint(p)) if p == &path),
"Expected CorruptCheckpoint for empty file, got {:?}",
result
);
}
#[test]
fn corruption_progress_tracker_returns_error() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt_tracker.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
tracker.increment(10).unwrap();
drop(tracker);
fs::write(&path, b"{\"total\": 100, \"progress\": ").unwrap();
let result = ProgressTracker::new(100, &path, Duration::ZERO);
assert!(
matches!(result, Err(CheckpointError::Serialize(_))),
"Expected Serialize error for corrupted progress tracker, got {:?}",
result
);
}
#[test]
fn corruption_overwrite_corrupted_file_succeeds() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt5.json");
fs::write(&path, b"[[[garbage]]]").unwrap();
assert!(load::<CorruptionState>(&path).is_err());
let valid_state = CorruptionState {
value: "recovered".to_string(),
metadata: 42,
};
save(&path, &valid_state).unwrap();
let loaded: CorruptionState = load(&path).unwrap().unwrap();
assert_eq!(loaded, valid_state);
}
#[test]
fn corruption_truncated_json_returns_error() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt6.json");
fs::write(&path, b"{\"value\": \"test\", \"meta").unwrap();
let result = load::<CorruptionState>(&path);
assert!(
matches!(result, Err(CheckpointError::Serialize(_))),
"Expected Serialize error for truncated JSON, got {:?}",
result
);
}
#[test]
fn corruption_single_bit_flip_in_large_payload() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupt7.json");
let state = CorruptionState {
value: "a".repeat(1024 * 1024),
metadata: 0,
};
save(&path, &state).unwrap();
let mut bytes = fs::read(&path).unwrap();
let mid = bytes.len() / 2;
bytes[mid] ^= 0x01;
fs::write(&path, &bytes).unwrap();
let result = load::<CorruptionState>(&path);
match result {
Ok(Some(loaded)) => {
assert_ne!(loaded, state, "Bit flip did not change the loaded data!");
}
Ok(None) => panic!("Should not be None"),
Err(_) => {
}
}
}