use checkpointkit::{load, save, CheckpointError, ProgressTracker};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tempfile::tempdir;
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
struct DepthState {
hero: String,
level: u32,
inventory: Vec<String>,
}
#[test]
fn test_depth_save_and_load_success() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_state.json");
let state = DepthState {
hero: "Arthas".to_string(),
level: 80,
inventory: vec!["Frostmourne".to_string(), "Invincible".to_string()],
};
assert!(save(&path, &state).is_ok(), "save should succeed");
let loaded: Option<DepthState> = load(&path).unwrap();
assert_eq!(loaded, Some(state), "loaded state should match saved state");
}
#[test]
fn test_depth_load_missing_file_returns_none() {
let dir = tempdir().unwrap();
let path = dir.path().join("nowhere.json");
let loaded: Option<DepthState> = load(&path).unwrap();
assert_eq!(loaded, None, "loading a missing file should return None");
}
#[test]
fn test_depth_checkpoint_error_is_disk_full() {
let path = std::path::PathBuf::from("/tmp/full");
let err = std::io::Error::new(std::io::ErrorKind::StorageFull, "disk full");
let checkpoint_err = CheckpointError::from_io_error(path.clone(), err);
assert!(checkpoint_err.is_disk_full(), "should identify as disk full");
let other_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
let not_full_err = CheckpointError::from_io_error(path, other_err);
assert!(!not_full_err.is_disk_full(), "should not identify as disk full");
}
#[test]
fn test_depth_progress_tracker_initialization() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let tracker = ProgressTracker::new(1000, &path, Duration::from_secs(1)).unwrap();
assert_eq!(tracker.current(), 0, "initial current should be 0");
assert_eq!(tracker.total(), 1000, "total should match configured");
assert_eq!(tracker.resume().unwrap(), 0, "resume should return 0 if no checkpoint");
}
#[test]
fn test_depth_progress_tracker_increment() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let mut tracker = ProgressTracker::new(1000, &path, Duration::from_secs(1)).unwrap();
let current = tracker.increment(250).unwrap();
assert_eq!(current, 250, "increment returns updated current");
assert_eq!(tracker.current(), 250, "current matches returned value");
let current = tracker.increment(250).unwrap();
assert_eq!(current, 500, "increment returns updated current");
assert_eq!(tracker.current(), 500, "current matches returned value");
}
#[test]
fn test_depth_progress_tracker_caps_at_total() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let mut tracker = ProgressTracker::new(500, &path, Duration::from_secs(1)).unwrap();
tracker.increment(300).unwrap();
let current = tracker.increment(300).unwrap();
assert_eq!(current, 500, "increment should cap at total");
assert_eq!(tracker.current(), 500, "current should be capped at total");
}
#[test]
fn test_depth_progress_tracker_auto_saves_on_zero_interval() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
tracker.increment(42).unwrap();
assert_eq!(tracker.resume().unwrap(), 42, "resume should reflect auto-saved value when interval is ZERO");
}
#[test]
fn test_depth_progress_tracker_auto_saves_on_completion() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let mut tracker = ProgressTracker::new(100, &path, Duration::from_secs(100)).unwrap();
tracker.increment(100).unwrap();
assert_eq!(tracker.resume().unwrap(), 100, "resume should reflect auto-saved value on completion");
}
#[test]
fn test_depth_progress_tracker_resume_saved_state() {
let dir = tempdir().unwrap();
let path = dir.path().join("depth_progress.json");
let mut tracker1 = ProgressTracker::new(100, &path, Duration::ZERO).unwrap();
tracker1.increment(50).unwrap();
drop(tracker1);
let tracker2 = ProgressTracker::new(100, &path, Duration::from_secs(1)).unwrap();
assert_eq!(tracker2.current(), 50, "should resume with current=50");
assert_eq!(tracker2.total(), 100, "should retain total=100");
}