use std::fs::File; use std::io::Write; use cbz2pdf::formats::cbz::CbzReader; use cbz2pdf::formats::FormatReader; #[test] fn cbz_reader_reads_jpgs_and_sorts_by_name() { // Build a temporary CBZ with 3 jpgs (including in a subdir) and one non-jpg let temp_dir = tempfile::tempdir().expect("create temp dir"); let cbz_path = temp_dir.path().join("book.cbz"); { let file = File::create(&cbz_path).expect("create cbz"); let mut zip = zip::ZipWriter::new(file); let options = zip::write::SimpleFileOptions::default(); // out of order names zip.start_file("002.jpg", options).unwrap(); // Create a tiny JPEG using the JPEG encoder let img = image::DynamicImage::new_rgb8(1, 1).to_rgb8(); let mut buf = Vec::new(); { let mut cursor = std::io::Cursor::new(&mut buf); let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut cursor, 80); enc.encode(&img, 1, 1, image::ColorType::Rgb8.into()) .unwrap(); } zip.write_all(&buf).unwrap(); zip.start_file("001.jpg", options).unwrap(); zip.write_all(&buf).unwrap(); // nested path should be accepted zip.start_file("subdir/003.jpg", options).unwrap(); zip.write_all(&buf).unwrap(); // non-jpg should be ignored zip.start_file("notes.txt", options).unwrap(); zip.write_all(b"hello").unwrap(); zip.finish().unwrap(); } let reader = CbzReader; let doc = reader.read(&cbz_path).expect("read cbz"); assert_eq!(doc.pages.len(), 3, "should include only jpg files"); let names: Vec = doc.pages.iter().map(|p| p.name.clone()).collect(); assert_eq!( names, vec!["001.jpg", "002.jpg", "003.jpg"], "pages sorted by name" ); // temp_dir goes out of scope and cleans up automatically }