use std::path::Path;
use rustyfi_backend::ImageResource;
fn jpeg_bytes() -> Vec<u8> {
std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/dot.jpg"))
.expect("fixture must be readable")
}
#[test]
fn a_baseline_jpeg_is_recognized_with_its_true_component_count() {
let dct = ImageResource::sniff_baseline_jpeg_dct(jpeg_bytes())
.expect("dot.jpg is a baseline (SOF0) JPEG and must be recognized");
assert_eq!(dct.components, 3, "dot.jpg is a 3-component YCbCr/RGB JPEG");
}
#[test]
fn the_recognized_bytes_are_the_original_file_untouched() {
let original = jpeg_bytes();
let dct = ImageResource::sniff_baseline_jpeg_dct(original.clone())
.expect("dot.jpg is a baseline (SOF0) JPEG and must be recognized");
assert_eq!(dct.bytes, original, "sniffing must not alter the original bytes");
}
#[test]
fn a_png_is_not_mistaken_for_a_jpeg() {
let png = std::fs::read(
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("rustyfi-pdf/tests/fixtures/dot.png"),
)
.expect("the workspace's dot.png fixture must be readable");
assert!(png.starts_with(b"\x89PNG"), "sanity: this must actually be a PNG");
assert!(
ImageResource::sniff_baseline_jpeg_dct(png).is_none(),
"a PNG must never be recognized as a JPEG"
);
}
#[test]
fn empty_bytes_are_rejected_without_panicking() {
assert!(ImageResource::sniff_baseline_jpeg_dct(Vec::new()).is_none());
}
#[test]
fn a_truncated_jpeg_header_is_rejected_without_panicking() {
assert!(ImageResource::sniff_baseline_jpeg_dct(vec![0xFF, 0xD8]).is_none());
assert!(ImageResource::sniff_baseline_jpeg_dct(vec![0xFF, 0xD8, 0xFF]).is_none());
}
#[test]
fn a_progressive_jpeg_sof2_is_rejected() {
let bytes = vec![
0xFF, 0xD8, 0xFF, 0xC2, 0x00, 0x08, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, ];
assert!(
ImageResource::sniff_baseline_jpeg_dct(bytes).is_none(),
"SOF2 (progressive) must fall back to decode/re-encode, not DCTDecode passthrough"
);
}
#[test]
fn a_four_component_cmyk_jpeg_is_rejected() {
let bytes = vec![
0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x08, 0x08, 0x00, 0x01, 0x00, 0x01, 0x04, ];
assert!(
ImageResource::sniff_baseline_jpeg_dct(bytes).is_none(),
"4-component (CMYK/YCCK) JPEGs must fall back, not be embedded as DeviceCMYK"
);
}