use assert_cmd::Command as CargoCommand;
use predicates::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Command as ShellCommand;
use tempfile::tempdir;
const COMPAT_FIXTURE_DIR: &str = "../../tests/fixtures/compat";
fn fixture_path(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join(COMPAT_FIXTURE_DIR)
.join(name)
}
fn qpdf_available() -> bool {
ShellCommand::new("qpdf")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[must_use]
fn skip_if_qpdf_missing() -> bool {
if qpdf_available() {
return false;
}
let on_ci = std::env::var_os("CI").is_some();
if on_ci {
panic!(
"qpdf is required for cli_static_id tests on CI; \
install qpdf in the workflow before running this test suite"
);
}
eprintln!(
"skipping: qpdf not available (target_os={}, CI={})",
std::env::consts::OS,
on_ci
);
true
}
fn run_flpdf_static_id(input: &Path, output: &Path) {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("rewrite")
.arg("--static-id")
.arg(input)
.arg(output)
.assert()
.success();
}
fn run_qpdf_static_id(input: &Path, output: &Path) {
let status = ShellCommand::new("qpdf")
.arg("--static-id")
.arg(input)
.arg(output)
.status()
.expect("invoking qpdf");
assert!(status.success(), "qpdf --static-id failed");
}
fn extract_id_array(pdf_bytes: &[u8]) -> (Vec<u8>, Vec<u8>) {
let idx = pdf_bytes
.windows(3)
.enumerate()
.filter(|(_, w)| *w == b"/ID")
.map(|(i, _)| i)
.next_back()
.expect("/ID not found");
let after = &pdf_bytes[idx + 3..];
let open = after.iter().position(|&b| b == b'[').expect("expected [");
let body = &after[open + 1..];
let lt0 = body.iter().position(|&b| b == b'<').expect("expected <");
let gt0 = body[lt0 + 1..]
.iter()
.position(|&b| b == b'>')
.expect("expected >");
let first = body[lt0 + 1..lt0 + 1 + gt0].to_vec();
let rest = &body[lt0 + gt0 + 2..];
let lt1 = rest.iter().position(|&b| b == b'<').expect("expected <");
let gt1 = rest[lt1 + 1..]
.iter()
.position(|&b| b == b'>')
.expect("expected >");
let second = rest[lt1 + 1..lt1 + 1 + gt1].to_vec();
(first, second)
}
#[test]
fn static_id_is_deterministic_across_runs() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let a = tmp.path().join("a.pdf");
let b = tmp.path().join("b.pdf");
run_flpdf_static_id(&input, &a);
run_flpdf_static_id(&input, &b);
let bytes_a = std::fs::read(&a).expect("read a");
let bytes_b = std::fs::read(&b).expect("read b");
assert_eq!(
bytes_a, bytes_b,
"two --static-id runs produced different bytes"
);
}
#[test]
fn static_id_is_reapplied_to_every_split_chunk() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("three-page.pdf");
let output = tmp.path().join("chunks.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args([
"rewrite",
"--static-id",
"--split-pages=1",
input.to_str().unwrap(),
output.to_str().unwrap(),
])
.assert()
.success();
for page in 1..=3 {
let chunk = tmp.path().join(format!("chunks-{page}.pdf"));
let (_id0, id1) = extract_id_array(&std::fs::read(chunk).expect("read split chunk"));
assert_eq!(
id1, b"31415926535897932384626433832795",
"static /ID[1] must be reapplied to chunk {page}"
);
}
}
#[test]
fn static_id_trailer_id_matches_qpdf_oracle() {
if skip_if_qpdf_missing() {
return;
}
let tmp = tempdir().expect("tempdir");
for name in ["one-page.pdf", "two-page.pdf", "three-page.pdf"] {
let input = fixture_path(name);
let flpdf_out = tmp.path().join(format!("flpdf_{name}"));
let qpdf_out = tmp.path().join(format!("qpdf_{name}"));
run_flpdf_static_id(&input, &flpdf_out);
run_qpdf_static_id(&input, &qpdf_out);
let flpdf_bytes = std::fs::read(&flpdf_out).expect("read flpdf");
let qpdf_bytes = std::fs::read(&qpdf_out).expect("read qpdf");
let (flpdf_id0, flpdf_id1) = extract_id_array(&flpdf_bytes);
let (qpdf_id0, qpdf_id1) = extract_id_array(&qpdf_bytes);
assert_eq!(
flpdf_id0, qpdf_id0,
"{name}: /ID[0] (permanent identifier) diverged from qpdf"
);
assert_eq!(
flpdf_id1, qpdf_id1,
"{name}: /ID[1] (changing identifier) diverged from qpdf"
);
assert_eq!(
flpdf_id1, b"31415926535897932384626433832795",
"/ID[1] is not the qpdf static-id constant"
);
}
}
#[test]
fn static_id_emits_test_only_warning_on_stderr() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("rewrite")
.arg("--static-id")
.arg(&input)
.arg(&output)
.assert()
.success()
.stderr(predicate::str::contains("--static-id"))
.stderr(predicate::str::contains("testing only"));
}
#[test]
fn static_id_top_level_alias_is_silent() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--static-id")
.arg(&input)
.arg(&output)
.assert()
.success()
.stderr(predicate::str::contains("testing only").not());
}
#[test]
fn static_id_warning_suppressed_by_env_var() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.env("FLPDF_STATIC_ID_QUIET", "1")
.arg("rewrite")
.arg("--static-id")
.arg(&input)
.arg(&output)
.assert()
.success()
.stderr(predicate::str::is_empty());
}
#[test]
fn no_static_id_emits_no_static_id_warning() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("rewrite")
.arg(&input)
.arg(&output)
.assert()
.success()
.stderr(predicate::str::contains("testing only").not());
}
#[test]
fn static_id_with_linearize_succeeds() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("rewrite")
.arg("--linearize")
.arg("--static-id")
.arg(&input)
.arg(&output)
.assert()
.success();
assert!(
output.exists(),
"linearized --static-id output must be created"
);
}