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