use checkpointkit::{load, save, CheckpointError, ProgressTracker};
use proptest::prelude::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tempfile::tempdir;
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
struct ArbitraryStruct {
a: i32,
b: String,
c: Vec<u8>,
}
prop_compose! {
fn arbitrary_struct()(
a in any::<i32>(),
b in ".*",
c in prop::collection::vec(any::<u8>(), 0..100)
) -> ArbitraryStruct {
ArbitraryStruct { a, b, c }
}
}
proptest! {
#[test]
fn test_depth_save_and_load_arbitrary_struct(s in arbitrary_struct()) {
let dir = tempdir().unwrap();
let path = dir.path().join("prop_struct.json");
save(&path, &s).unwrap();
let loaded: Option<ArbitraryStruct> = load(&path).unwrap();
prop_assert_eq!(loaded, Some(s));
}
#[test]
fn test_depth_progress_tracker_increment_never_exceeds_total(
total in 1..1000u64,
increments in prop::collection::vec(0..500u64, 1..50)
) {
let dir = tempdir().unwrap();
let path = dir.path().join("prop_progress.json");
let mut tracker = ProgressTracker::new(total, &path, Duration::from_secs(1)).unwrap();
let mut expected = 0u64;
for inc in increments {
expected = expected.saturating_add(inc).min(total);
let current = tracker.increment(inc).unwrap();
prop_assert_eq!(current, expected);
prop_assert_eq!(tracker.current(), expected);
prop_assert!(tracker.current() <= total);
}
}
#[test]
fn test_depth_load_on_garbage_bytes_never_panics(
garbage in prop::collection::vec(any::<u8>(), 0..1000)
) {
let dir = tempdir().unwrap();
let path = dir.path().join("garbage_prop.json");
std::fs::write(&path, &garbage).unwrap();
let result: Result<Option<ArbitraryStruct>, CheckpointError> = load(&path);
match result {
Ok(_)
| Err(CheckpointError::Serialize(_))
| Err(CheckpointError::CorruptCheckpoint(_)) => {}
Err(e) => prop_assert!(false, "Expected Ok, Serialize, or CorruptCheckpoint error, got {:?}", e),
}
}
}