//! Tests for the Inkling image bridge: path encoding, source extraction across //! OpenAI/Responses/Anthropic shapes, and materialization (PNG passthrough, //! WebP->PNG normalization, local path). Host-independent: images are built in //! memory with the image crate. use base64::Engine; use serde_json::json; use strata_ctl::inkling::images::{encode_path, extract_sources, materialize}; fn png_bytes() -> Vec { let img = image::DynamicImage::ImageRgba8(image::RgbaImage::new(2, 2)); let mut buf = Vec::new(); img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png) .unwrap(); buf } fn webp_bytes() -> Vec { let img = image::DynamicImage::ImageRgba8(image::RgbaImage::new(2, 2)); let mut buf = Vec::new(); img.write_to( &mut std::io::Cursor::new(&mut buf), image::ImageFormat::WebP, ) .unwrap(); buf } fn data_url(mime: &str, bytes: &[u8]) -> String { format!( "data:{mime};base64,{}", base64::engine::general_purpose::STANDARD.encode(bytes) ) } #[test] fn encode_path_only_percent_and_space() { assert_eq!(encode_path("/mnt/my pic.png"), "/mnt/my%20pic.png"); assert_eq!(encode_path("/a%b.png"), "/a%25b.png"); assert_eq!(encode_path("/a %b.png"), "/a%20%25b.png"); // slashes stay literal (the engine only decodes %20 and %25) assert_eq!(encode_path("/mnt/x/y.png"), "/mnt/x/y.png"); } #[test] fn extract_sources_shapes_and_order() { let msgs = json!([ {"role": "user", "content": [ {"type": "text", "text": "guarda"}, {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAA"}}, {"type": "image_url", "image_url": "/local/a.png"} ]}, {"role": "user", "content": [ {"type": "input_image", "image_url": "https://x/b.png"}, {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": "BBB"}} ]} ]); let srcs = extract_sources(&msgs); assert_eq!( srcs, vec![ "data:image/png;base64,AAA", "/local/a.png", "https://x/b.png", "data:image/jpeg;base64,BBB", ] ); } #[test] fn extract_ignores_text_only() { let msgs = json!([{"role": "user", "content": "ciao"}]); assert!(extract_sources(&msgs).is_empty()); } #[test] fn materialize_png_passthrough() { let png = png_bytes(); let paths = materialize(&[data_url("image/png", &png)]).unwrap(); assert_eq!(paths.len(), 1); assert_eq!(paths[0].extension().unwrap(), "png"); assert_eq!(std::fs::read(&paths[0]).unwrap(), png); let _ = std::fs::remove_file(&paths[0]); } #[test] fn materialize_webp_normalizes_to_png() { let webp = webp_bytes(); let paths = materialize(&[data_url("image/webp", &webp)]).unwrap(); assert_eq!(paths.len(), 1); assert_eq!(paths[0].extension().unwrap(), "png"); let out = std::fs::read(&paths[0]).unwrap(); assert!( out.starts_with(&[0x89, b'P', b'N', b'G']), "not converted to PNG" ); let _ = std::fs::remove_file(&paths[0]); } #[test] fn materialize_local_path() { let png = png_bytes(); let src = std::env::temp_dir().join(format!("strata-ctl-img-src-{}.png", std::process::id())); std::fs::write(&src, &png).unwrap(); let paths = materialize(&[src.to_string_lossy().to_string()]).unwrap(); assert_eq!(std::fs::read(&paths[0]).unwrap(), png); let _ = std::fs::remove_file(&paths[0]); let _ = std::fs::remove_file(&src); }