use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;
fn one_page_pdf_with_stream() -> Vec<u8> {
let content_data = b"BT /F1 12 Tf (Hello) Tj ET";
let stream_obj = format!(
"4 0 obj\n<< /Length {} >>\nstream\n{}\nendstream\nendobj\n",
content_data.len(),
String::from_utf8_lossy(content_data),
);
let mut pdf = b"%PDF-1.4\n".to_vec();
let off1 = pdf.len();
pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
let off2 = pdf.len();
pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
let off3 = pdf.len();
pdf.extend_from_slice(
b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >>\nendobj\n",
);
let off4 = pdf.len();
pdf.extend_from_slice(stream_obj.as_bytes());
let xref_start = pdf.len();
let xref = format!(
"xref\n0 5\n\
0000000000 65535 f \n\
{off1:010} 00000 n \n\
{off2:010} 00000 n \n\
{off3:010} 00000 n \n\
{off4:010} 00000 n \n"
);
pdf.extend_from_slice(xref.as_bytes());
pdf.extend_from_slice(
format!("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n").as_bytes(),
);
pdf
}
fn write_temp_pdf(bytes: &[u8]) -> tempfile::NamedTempFile {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.as_file_mut().write_all(bytes).unwrap();
f
}
#[test]
fn json_flag_outputs_json_to_stdout() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args(["--json", input.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("\"version\""))
.stdout(predicate::str::contains("\"pages\""))
.stderr(predicate::str::is_empty());
}
#[test]
fn json_output_flag_writes_to_file_and_stdout_empty() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out_path = temp.path().join("out.json");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-output",
out_path.to_str().unwrap(),
input.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::is_empty());
let content = std::fs::read_to_string(&out_path).unwrap();
assert!(
content.contains("\"version\""),
"expected JSON in output file"
);
}
#[test]
fn json_key_pages_limits_output() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-key",
"pages",
input.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("\"pages\""))
.stdout(predicate::str::contains("\"qpdf\"").not())
.stdout(predicate::str::contains("\"encrypt\"").not());
}
#[test]
fn json_object_selector_limits_qpdf_section() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-object",
"3",
input.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("\"obj:3 0 R\""))
.stdout(predicate::str::contains("\"obj:1 0 R\"").not());
}
#[test]
fn json_key_invalid_exits_nonzero_with_error() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-key",
"invalidkey",
input.path().to_str().unwrap(),
])
.assert()
.code(2)
.stderr(predicate::str::contains("--json-key"));
}
#[test]
fn json_object_invalid_exits_nonzero_with_error() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-object",
"xyz",
input.path().to_str().unwrap(),
])
.assert()
.code(2)
.stderr(predicate::str::contains("--json-object"))
.stderr(predicate::str::contains("xyz"));
}
#[test]
fn json_stream_data_inline_includes_data_field() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-stream-data",
"inline",
input.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("\"data\""));
}
#[test]
fn json_stream_data_file_creates_side_files() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out_path = temp.path().join("out.json");
let prefix = temp.path().join("sf").to_str().unwrap().to_string();
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-output",
out_path.to_str().unwrap(),
"--json-stream-data",
"file",
"--json-stream-prefix",
&prefix,
input.path().to_str().unwrap(),
])
.assert()
.success();
let content = std::fs::read_to_string(&out_path).unwrap();
assert!(
content.contains("\"datafile\""),
"expected datafile entries in JSON output"
);
let side_file = format!("{prefix}-004");
assert!(
std::path::Path::new(&side_file).exists(),
"expected side file {side_file} to exist"
);
}
#[test]
fn json_output_without_stream_data_flag_does_not_emit_stream_payload() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out_path = temp.path().join("out.json");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-output",
out_path.to_str().unwrap(),
input.path().to_str().unwrap(),
])
.assert()
.success();
let content = std::fs::read_to_string(&out_path).unwrap();
assert!(
!content.contains("\"data\""),
"default stream-data is 'none'; --json-output alone must not inline stream bytes (got data field)"
);
assert!(
!content.contains("\"datafile\""),
"default stream-data is 'none'; --json-output alone must not produce datafile entries"
);
}
#[test]
fn json_key_pages_does_not_write_side_files_for_filtered_streams() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let prefix = temp.path().join("sf").to_str().unwrap().to_string();
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--json-key",
"pages",
"--json-stream-data",
"file",
"--json-stream-prefix",
&prefix,
input.path().to_str().unwrap(),
])
.assert()
.success();
let side_file = format!("{prefix}-004");
assert!(
!std::path::Path::new(&side_file).exists(),
"no side file should be written when qpdf section is filtered out (got {side_file})"
);
}
#[test]
fn json_key_without_json_flag_is_usage_error() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args(["--json-key", "pages", input.path().to_str().unwrap()])
.assert()
.code(2)
.stderr(predicate::str::contains("--json"));
}
#[test]
fn json_output_without_json_flag_is_usage_error() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out_path = temp.path().join("out.json");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json-output",
out_path.to_str().unwrap(),
input.path().to_str().unwrap(),
])
.assert()
.code(2)
.stderr(predicate::str::contains("--json"));
}
#[test]
fn json_stream_data_without_json_flag_is_usage_error() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json-stream-data",
"inline",
input.path().to_str().unwrap(),
])
.assert()
.code(2)
.stderr(predicate::str::contains("--json"));
}
#[test]
fn json_flag_conflicts_with_subcommand() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out_path = temp.path().join("out.pdf");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"rewrite",
input.path().to_str().unwrap(),
out_path.to_str().unwrap(),
])
.assert()
.code(2);
}
#[test]
fn json_flag_conflicts_with_check_mode() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args(["--json", "--check", input.path().to_str().unwrap()])
.assert()
.code(2);
}
#[test]
fn json_flag_conflicts_with_output_positional() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let out = temp.path().join("out.pdf");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
input.path().to_str().unwrap(),
out.to_str().unwrap(),
])
.assert()
.code(2);
}
#[test]
fn json_flag_conflicts_with_show_info() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args(["--json", "--show-info", input.path().to_str().unwrap()])
.assert()
.code(2);
}
#[test]
fn json_flag_conflicts_with_linearize_pass1() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let temp = tempfile::tempdir().unwrap();
let p1 = temp.path().join("pass1.bin");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--linearize-pass1",
p1.to_str().unwrap(),
input.path().to_str().unwrap(),
])
.assert()
.code(2);
}
#[test]
fn json_flag_conflicts_with_compress_streams() {
let input = write_temp_pdf(&one_page_pdf_with_stream());
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.args([
"--json",
"--compress-streams=n",
input.path().to_str().unwrap(),
])
.assert()
.code(2);
}