use super::*;
use crate::image_ops;
fn png_bytes(w: u32, h: u32) -> Vec<u8> {
let img = image::RgbaImage::from_pixel(w, h, image::Rgba([200, 100, 50, 255]));
let mut out = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
.expect("encode test png");
out
}
fn picture(w: u32, h: u32) -> llm_trait::ImageAttachment {
llm_trait::ImageAttachment::Base64 {
data: image_ops::to_base64(&png_bytes(w, h)),
media_type: Some("image/png".to_string()),
detail: None,
}
}
#[test]
fn every_in_budget_picture_reaches_the_request() {
let messages = vec![
ChatMessage::user("look at the boards"),
ChatMessage::tool_with_images("t1", "shot 1", vec![picture(200, 200)]),
ChatMessage::tool_with_images("t2", "shot 2", vec![picture(200, 200)]),
ChatMessage::tool_with_images(
"t3",
"a batch of two",
vec![picture(200, 200), picture(200, 200)],
),
];
let request = build_chat_request(&messages, &[], None, None, false, None)
.expect("small pictures fit the budget");
assert_eq!(request.messages.len(), 4, "no message is discarded");
let carried: usize = request
.messages
.iter()
.map(|m| match m {
ChatMessage::Tool { images, .. } | ChatMessage::User { images, .. } => images.len(),
_ => 0,
})
.sum();
assert_eq!(carried, 4, "all four pictures travel");
}
#[test]
fn an_over_budget_request_is_refused_with_an_inventory() {
let fat = llm_trait::ImageAttachment::Base64 {
data: "x".repeat(image_ops::MAX_IMAGE_BASE64_BYTES + 1),
media_type: Some("image/png".to_string()),
detail: None,
};
let messages = vec![ChatMessage::tool_with_images("t1", "a fat one", vec![fat])];
let err = build_chat_request(&messages, &[], None, None, false, None)
.expect_err("over the per-image line");
let text = err.to_string();
assert!(
text.contains("request NOT sent"),
"the refusal is loud: {text}"
);
assert!(
text.contains("OVER per-image budget"),
"the violator is named: {text}"
);
}
#[test]
fn the_throat_shrinks_the_wire_copy_and_keeps_the_history() {
let original = png_bytes(3200, 800);
let big = llm_trait::ImageAttachment::Base64 {
data: image_ops::to_base64(&original),
media_type: Some("image/png".to_string()),
detail: None,
};
let messages = vec![ChatMessage::tool_with_images("t1", "shot", vec![big])];
let request = build_chat_request(&messages, &[], None, None, false, None)
.expect("a big picture shrinks to fit");
let ChatMessage::Tool { images, .. } = &request.messages[0] else {
panic!("expected a tool result");
};
let llm_trait::ImageAttachment::Base64 { data, .. } = &images[0] else {
panic!("expected a base64 attachment");
};
let wire_bytes = image_ops::from_base64(data).expect("wire payload decodes");
assert_eq!(
image_ops::header_dimensions(&wire_bytes),
Some((1568, 392)),
"the wire copy is shrunk to the long-edge cap"
);
let ChatMessage::Tool {
images: history, ..
} = &messages[0]
else {
panic!("expected a tool result");
};
let llm_trait::ImageAttachment::Base64 {
data: history_data, ..
} = &history[0]
else {
panic!("expected a base64 attachment");
};
assert_eq!(
*history_data,
image_ops::to_base64(&original),
"the history keeps the original bytes"
);
}