1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Tests for `utils::pdf_vision::render_pdf_pages`.
//!
//! Covers the regression where `pdftoppm` would loop past the document's
//! actual page count, fail on a later batch, and cause the whole render
//! to be discarded (the "truncated after page 5" symptom). We can't
//! unit-test the pdftoppm shell behavior directly without a real PDF
//! and the binary on PATH, so these focus on what we can deterministically
//! check: the function entry-point contract.
use crate::utils::pdf_vision::render_pdf_pages;
use std::fs;
#[test]
fn missing_pdf_returns_error_with_path() {
let tmp = tempfile::tempdir().expect("tempdir");
let out_dir = tmp.path().join("pages");
let result = render_pdf_pages("/no/such/file.pdf", 10, out_dir.to_str().unwrap());
let err = result.expect_err("missing pdf must error");
assert!(err.contains("/no/such/file.pdf"));
}
#[test]
fn output_directory_is_created_if_missing() {
let tmp = tempfile::tempdir().expect("tempdir");
// Write a 1-byte fake "pdf" so existence check passes — render
// itself will fail (not a real PDF) but we only care that the
// output directory was created during setup.
let pdf = tmp.path().join("fake.pdf");
fs::write(&pdf, b"%PDF-1.4\n").expect("write fake pdf");
let out = tmp.path().join("nested").join("renders");
assert!(!out.exists(), "precondition: output dir must not exist yet");
let _ = render_pdf_pages(pdf.to_str().unwrap(), 5, out.to_str().unwrap());
assert!(out.exists(), "output dir must be created on entry");
}
/// When no PDF renderer succeeds (pdfium feature off + no valid input
/// for pdftoppm), the helper must surface an actionable error rather
/// than panic or silently produce an empty Ok.
#[test]
fn unrenderable_pdf_returns_err_not_empty_ok() {
let tmp = tempfile::tempdir().expect("tempdir");
let pdf = tmp.path().join("not-really-a-pdf.pdf");
fs::write(&pdf, b"this is not a pdf").expect("write fake");
let out = tmp.path().join("out");
let result = render_pdf_pages(pdf.to_str().unwrap(), 100, out.to_str().unwrap());
// Either an Err (pdftoppm rejected the input) or an Ok with zero
// pages — both are acceptable contracts; an Ok with no pages would
// be the bug we explicitly want to avoid.
if let Ok(paths) = &result {
assert!(
!paths.is_empty(),
"Ok(vec![]) is forbidden — must be Err if no pages rendered"
);
}
}