use std::io::Cursor;
use pdfluent::prelude::*;
use pdfluent::SaveOptions;
const FIXTURE_PATH: &str = "tests/fixtures/sample.pdf";
#[test]
fn determinism_to_bytes_is_idempotent() {
let doc = PdfDocument::open(FIXTURE_PATH).expect("open fixture");
let first = doc.to_bytes().expect("first to_bytes");
let second = doc.to_bytes().expect("second to_bytes");
assert_eq!(
first.len(),
second.len(),
"to_bytes must produce same byte length across calls",
);
assert_eq!(first, second, "to_bytes must produce byte-identical output");
}
#[test]
fn determinism_multi_open_roundtrip() {
let doc1 = PdfDocument::open(FIXTURE_PATH).expect("first open");
let bytes1 = doc1.to_bytes().expect("first to_bytes");
drop(doc1);
let doc2 = PdfDocument::open(FIXTURE_PATH).expect("second open");
let bytes2 = doc2.to_bytes().expect("second to_bytes");
assert_eq!(
bytes1.len(),
bytes2.len(),
"two independent open+save round trips must agree on byte length",
);
assert_eq!(
bytes1, bytes2,
"two independent open+save round trips must agree byte-for-byte",
);
}
#[test]
fn determinism_write_to_in_memory_cursor() {
let bytes = std::fs::read(FIXTURE_PATH).expect("read fixture");
let doc1 = PdfDocument::from_bytes(&bytes).expect("from_bytes 1");
let doc2 = PdfDocument::from_bytes(&bytes).expect("from_bytes 2");
let mut sink1: Vec<u8> = Vec::new();
let mut sink2: Vec<u8> = Vec::new();
doc1.write_to(Cursor::new(&mut sink1)).expect("write 1");
doc2.write_to(Cursor::new(&mut sink2)).expect("write 2");
assert_eq!(
sink1.len(),
sink2.len(),
"write_to must produce same byte length for same input",
);
assert_eq!(
sink1, sink2,
"write_to via Cursor<Vec<u8>> must be byte-deterministic",
);
assert!(
sink1.starts_with(b"%PDF-"),
"output must begin with PDF header"
);
}
#[test]
fn determinism_to_bytes_matches_save_with() {
let doc = PdfDocument::open(FIXTURE_PATH).expect("open fixture");
let bytes_in_memory = doc.to_bytes().expect("via to_bytes");
let mut path = std::env::temp_dir();
let pid = std::process::id();
path.push(format!("pdfluent-determinism-{}.pdf", pid));
let _ = std::fs::remove_file(&path);
doc.save_with(&path, SaveOptions::new().with_overwrite(true))
.expect("save_with");
let bytes_on_disk = std::fs::read(&path).expect("read back");
let _ = std::fs::remove_file(&path);
assert_eq!(
bytes_in_memory.len(),
bytes_on_disk.len(),
"to_bytes vs save_with must agree on length",
);
assert_eq!(
bytes_in_memory, bytes_on_disk,
"to_bytes and save_with must produce identical bytes",
);
}