use opi_ai::message::{ImageSource, InputContent, MediaType};
#[test]
fn image_variant_from_url() {
let content = InputContent::Image {
source: ImageSource::Url {
url: "https://example.com/photo.png".into(),
},
media_type: MediaType::Png,
};
assert!(matches!(content, InputContent::Image { .. }));
}
#[test]
fn image_variant_from_base64() {
let content = InputContent::Image {
source: ImageSource::Base64 {
data: "iVBORw0KGgo=".into(),
},
media_type: MediaType::Png,
};
assert!(matches!(content, InputContent::Image { .. }));
}
#[test]
fn image_variant_from_bytes() {
let content = InputContent::Image {
source: ImageSource::Bytes {
data: vec![0x89, 0x50, 0x4E, 0x47],
},
media_type: MediaType::Png,
};
assert!(matches!(content, InputContent::Image { .. }));
}
#[test]
fn image_serde_roundtrip_url() {
let content = InputContent::Image {
source: ImageSource::Url {
url: "https://example.com/img.jpg".into(),
},
media_type: MediaType::Jpeg,
};
let json = serde_json::to_string(&content).unwrap();
let deserialized: InputContent = serde_json::from_str(&json).unwrap();
assert_eq!(content, deserialized);
}
#[test]
fn image_serde_roundtrip_base64() {
let content = InputContent::Image {
source: ImageSource::Base64 {
data: "iVBORw0KGgo=".into(),
},
media_type: MediaType::Png,
};
let json = serde_json::to_string(&content).unwrap();
let deserialized: InputContent = serde_json::from_str(&json).unwrap();
assert_eq!(content, deserialized);
}
#[test]
fn image_serde_roundtrip_bytes() {
let content = InputContent::Image {
source: ImageSource::Bytes {
data: vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A],
},
media_type: MediaType::Png,
};
let json = serde_json::to_string(&content).unwrap();
let deserialized: InputContent = serde_json::from_str(&json).unwrap();
assert_eq!(content, deserialized);
}
#[test]
fn image_serde_json_shape() {
let content = InputContent::Image {
source: ImageSource::Url {
url: "https://example.com/img.png".into(),
},
media_type: MediaType::Png,
};
let json = serde_json::to_string(&content).unwrap();
let val: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(val["type"], "image");
assert_eq!(val["source"]["type"], "url");
assert_eq!(val["source"]["url"], "https://example.com/img.png");
assert_eq!(val["media_type"], "image/png");
}
#[test]
fn media_types_cover_standard_formats() {
assert_eq!(MediaType::Png.as_str(), "image/png");
assert_eq!(MediaType::Jpeg.as_str(), "image/jpeg");
assert_eq!(MediaType::Gif.as_str(), "image/gif");
assert_eq!(MediaType::WebP.as_str(), "image/webp");
}
#[test]
fn media_type_serde_roundtrip() {
let mt = MediaType::Jpeg;
let json = serde_json::to_string(&mt).unwrap();
assert_eq!(json, "\"image/jpeg\"");
let back: MediaType = serde_json::from_str(&json).unwrap();
assert_eq!(mt, back);
}
#[test]
fn bytes_source_preserves_binary_data() {
let binary: Vec<u8> = (0u8..=255).collect();
let content = InputContent::Image {
source: ImageSource::Bytes {
data: binary.clone(),
},
media_type: MediaType::Png,
};
let json = serde_json::to_string(&content).unwrap();
let back: InputContent = serde_json::from_str(&json).unwrap();
if let InputContent::Image {
source: ImageSource::Bytes { data },
..
} = back
{
assert_eq!(data, binary);
} else {
panic!("expected Image with Bytes source");
}
}
#[test]
fn text_variant_unaffected() {
let content = InputContent::Text {
text: "hello".into(),
};
let json = serde_json::to_string(&content).unwrap();
let back: InputContent = serde_json::from_str(&json).unwrap();
assert_eq!(content, back);
}