Skip to content

Commit 4670ab9

Browse files
committed
tests: offline test
Signed-off-by: Alexandre Milesi <[email protected]>
1 parent 6c41800 commit 4670ab9

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

lib/llm/src/preprocessor/media/decoders/image.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ impl Decoder for ImageDecoder {
2525
let n_channels = img.color().channel_count();
2626

2727
let max_pixels = self.max_pixels.unwrap_or(usize::MAX);
28+
let pixel_count = (width as usize)
29+
.checked_mul(height as usize)
30+
.ok_or_else(|| anyhow::anyhow!("Image dimensions {width}x{height} overflow usize"))?;
2831
anyhow::ensure!(
29-
(width as usize) * (height as usize) <= max_pixels,
32+
pixel_count <= max_pixels,
3033
"Image dimensions {width}x{height} exceed max pixels {max_pixels}"
3134
);
3235
let data = match n_channels {

lib/llm/src/preprocessor/media/loader.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,30 @@ mod tests {
120120
use super::*;
121121
use dynamo_async_openai::types::{ChatCompletionRequestMessageContentPartImage, ImageUrl};
122122

123-
// warning: non-airgap test
124123
#[tokio::test]
125124
async fn test_fetch_and_decode() {
125+
let test_image_bytes =
126+
include_bytes!("../../../tests/data/media/llm-optimize-deploy-graphic.png");
127+
128+
let mut server = mockito::Server::new_async().await;
129+
let mock = server
130+
.mock("GET", "/llm-optimize-deploy-graphic.png")
131+
.with_status(200)
132+
.with_header("content-type", "image/png")
133+
.with_body(&test_image_bytes[..])
134+
.create_async()
135+
.await;
136+
126137
let media_decoder = MediaDecoder::default();
127-
let loader = MediaLoader::new(media_decoder, MediaFetcher::default()).unwrap();
138+
let fetcher = MediaFetcher {
139+
allow_direct_ip: true,
140+
allow_direct_port: true,
141+
..Default::default()
142+
};
128143

129-
let image_url = ImageUrl::from(
130-
"https://developer-blogs.nvidia.com/wp-content/uploads/2023/11/llm-optimize-deploy-graphic.png",
131-
);
144+
let loader = MediaLoader::new(media_decoder, fetcher).unwrap();
145+
146+
let image_url = ImageUrl::from(format!("{}/llm-optimize-deploy-graphic.png", server.url()));
132147
let content_part = ChatCompletionRequestUserMessageContentPart::ImageUrl(
133148
ChatCompletionRequestMessageContentPartImage { image_url },
134149
);
@@ -145,10 +160,12 @@ mod tests {
145160

146161
// Verify image dimensions: 1,999px × 1,125px (width × height)
147162
// Shape format is [height, width, channels]
148-
assert!(!data.shape.is_empty(), "Shape should not be empty");
163+
assert_eq!(data.shape.len(), 3);
149164
assert_eq!(data.shape[0], 1125, "Height should be 1125");
150165
assert_eq!(data.shape[1], 1999, "Width should be 1999");
151166
assert_eq!(data.shape[2], 4, "RGBA channels should be 4");
167+
168+
mock.assert_async().await;
152169
}
153170

154171
#[test]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
llm-optimize-deploy-graphic.png filter=lfs diff=lfs merge=lfs -text
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)