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, true)
.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, true)
.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, true)
.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"
);
}
fn user_with_picture() -> ChatMessage {
ChatMessage::user_with_images("what is in this screenshot?", vec![picture(200, 200)])
}
#[test]
fn non_vision_models_get_words_about_the_picture_never_pixels() {
let messages = vec![user_with_picture()];
let request = build_chat_request(&messages, &[], None, None, false, None, false)
.expect("small picture fits the budget");
assert_eq!(request.messages.len(), 1);
match &request.messages[0] {
ChatMessage::User {
content, images, ..
} => {
assert!(
images.is_empty(),
"no picture may ride a text-only model's wire"
);
assert!(
content.contains("cannot see images"),
"the residue must name the withholding"
);
assert!(
content.contains("/model"),
"the residue must say how to recover"
);
}
other => panic!("unexpected message: {other:?}"),
}
let rendered = serde_json::to_string(&request.messages).expect("messages serialize");
let payload = image_ops::to_base64(&png_bytes(200, 200));
assert!(
!rendered.contains(&payload),
"no image payload may reach the wire"
);
}
#[test]
fn vision_models_still_receive_the_picture() {
let messages = vec![user_with_picture()];
let request = build_chat_request(&messages, &[], None, None, false, None, true)
.expect("small picture fits the budget");
match &request.messages[0] {
ChatMessage::User { images, .. } => assert_eq!(images.len(), 1),
other => panic!("unexpected message: {other:?}"),
}
let rendered = serde_json::to_string(&request.messages).expect("messages serialize");
let payload = image_ops::to_base64(&png_bytes(200, 200));
assert!(rendered.contains(&payload), "the vision model gets pixels");
}
#[test]
fn normalization_leaves_the_session_history_untouched() {
let messages = vec![user_with_picture()];
let _ = build_chat_request(&messages, &[], None, None, false, None, false)
.expect("small picture fits the budget");
match &messages[0] {
ChatMessage::User { images, .. } => {
assert_eq!(images.len(), 1, "history must keep the picture");
}
other => panic!("unexpected message: {other:?}"),
}
}
#[test]
fn normalization_strips_tool_result_pictures_too() {
let messages = vec![ChatMessage::tool_with_images(
"t1",
"shot loaded",
vec![picture(200, 200)],
)];
let request = build_chat_request(&messages, &[], None, None, false, None, false)
.expect("small picture fits the budget");
match &request.messages[0] {
ChatMessage::Tool {
content, images, ..
} => {
assert!(images.is_empty(), "tool picture must be withheld");
assert!(content.contains("cannot see images"));
}
other => panic!("unexpected message: {other:?}"),
}
}