···527527528528#[cfg(test)]
529529mod tests {
530530+ use crate::errors::AssetError;
531531+530532 use super::*;
531531- use std::path::PathBuf;
533533+ use std::{error::Error, path::PathBuf};
532534533535 #[test]
534536 fn test_placeholder_with_missing_file() {
···536538537539 let result = get_placeholder(&nonexistent_path, None);
538540539539- // Should return an error, not panic
540541 assert!(result.is_err());
541541-542542- if let Err(crate::errors::AssetError::ImageLoadFailed { path, .. }) = result {
542542+ if let Err(AssetError::ImageLoadFailed { path, .. }) = result {
543543 assert_eq!(path, nonexistent_path);
544544 } else {
545545 panic!("Expected ImageLoadFailed error");
···547547 }
548548549549 #[test]
550550- fn test_placeholder_with_invalid_image_data() {
550550+ fn test_placeholder_with_valid_image() {
551551 let temp_dir = tempfile::tempdir().unwrap();
552552+ let image_path = temp_dir.path().join("test.png");
552553553553- // Create a file with invalid image data
554554- let invalid_image_path = temp_dir.path().join("invalid.png");
555555- std::fs::write(&invalid_image_path, b"This is not a valid PNG file").unwrap();
556556-557557- let result = get_placeholder(&invalid_image_path, None);
554554+ // Create a minimal valid 1x1 PNG file using the image crate to ensure correct CRCs
555555+ let img = image::ImageBuffer::<image::Rgba<u8>, _>::from_fn(1, 1, |_x, _y| {
556556+ image::Rgba([255, 0, 0, 255])
557557+ });
558558+ img.save(&image_path).unwrap();
558559559559- // Should return an error, not panic
560560- assert!(result.is_err());
560560+ let result = get_placeholder(&image_path, None);
561561562562- if let Err(crate::errors::AssetError::ImageLoadFailed { path, .. }) = result {
563563- assert_eq!(path, invalid_image_path);
564564- } else {
565565- panic!("Expected ImageLoadFailed error");
562562+ if let Err(e) = &result {
563563+ eprintln!("get_placeholder failed: {:?}", e.source());
566564 }
567565568568- // Cleanup
569569- std::fs::remove_file(&invalid_image_path).ok();
570570- }
571571-572572- #[test]
573573- fn test_placeholder_with_valid_image() {
574574- use std::path::Path;
575575-576576- // Try to find an existing image in the examples directory
577577- let project_root = Path::new(env!("CARGO_MANIFEST_DIR"))
578578- .parent()
579579- .unwrap()
580580- .parent()
581581- .unwrap();
582582- let test_image = project_root.join("examples/image-processing/images/walrus.jpg");
583583-584584- // Skip test if the image doesn't exist (e.g., in CI without examples)
585585- if !test_image.exists() {
586586- eprintln!("Skipping test: test image not found at {:?}", test_image);
587587- return;
588588- }
589589-590590- let result = get_placeholder(&test_image, None);
591591-592592- // Should succeed
593566 assert!(result.is_ok());
594594-595567 let placeholder = result.unwrap();
596596- // Verify the placeholder has a thumbhash
597568 assert!(!placeholder.thumbhash.is_empty());
598569 assert!(!placeholder.thumbhash_base64.is_empty());
599599- }
600600-601601- #[test]
602602- fn test_placeholder_with_empty_file() {
603603- let temp_dir = tempfile::tempdir().unwrap();
604604-605605- // Create an empty file
606606- let empty_file_path = temp_dir.path().join("empty.png");
607607- std::fs::write(&empty_file_path, b"").unwrap();
608608-609609- let result = get_placeholder(&empty_file_path, None);
610610-611611- // Should return an error for empty/invalid image
612612- assert!(result.is_err());
613570 }
614571}