//! The picture encoder: requirement, token counting and unload. use crate::harness::{Harness, base64, data_url, png}; use serde_json::json; #[tokio::test] async fn pictures_need_the_encoder_and_a_readable_image() { // without the encoder: the Python server's words, in each dialect let h = Harness::start("noenc", None).await; let message = "this server was started without the vision encoder (run setup again and choose 'vision'), so it cannot read images"; for (path, body) in [ ( "/v1/chat/completions", json!({"model": "strata-test", "max_tokens": 5, "messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": data_url(1)}}]}]}), ), ( "/v1/messages", json!({"model": "strata-test", "max_tokens": 5, "messages": [{"role": "user", "content": [{"type": "image", "source": {"type": "url", "url": "http://x/y.png"}}]}]}), ), ( "/v1/responses", json!({"model": "strata-test", "store": false, "input": [{"role": "user", "content": [{"type": "input_image", "image_url": data_url(1)}]}]}), ), ] { let (status, body) = h.post(path, body).await; assert_eq!(status, 400, "{path} {body}"); let text = body.to_string(); assert!(text.contains(message), "{path} {text}"); } assert!(h.engine_lines().is_empty()); assert_eq!(h.get("/props").await["modalities"]["vision"], false); assert_eq!(h.get("/v1/status").await["vision"]["enabled"], false); assert_eq!(h.get("/api/health").await["images"], false); h.finish().await; // an encoder that cannot read the picture let h = Harness::start("bad-image", Some("err")).await; let (status, body) = h .post("/v1/chat/completions", json!({"model": "strata-test", "max_tokens": 5, "messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": data_url(1)}}]}]})) .await; assert_eq!(status, 400, "{body}"); assert!( body.to_string() .contains("the image could not be read: cannot read the image"), "{body}" ); // unsupported formats and unreadable sources for (url, want) in [ ( "data:image/webp;base64,UklGRg==", "this image format is not supported", ), ( "no/such/file.png", "an image must be a data: URL, an http(s) URL or a local file path", ), ( "", "an image must be a data: URL, an http(s) URL or a local file path", ), ] { let (status, body) = h .post("/v1/chat/completions", json!({"model": "strata-test", "max_tokens": 5, "messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": url}}]}]})) .await; assert_eq!(status, 400, "{body}"); assert!(body.to_string().contains(want), "{body}"); } assert!(h.engine_lines().is_empty()); h.finish().await; } #[tokio::test] async fn token_counters_do_not_start_the_encoder_and_count_one_pad_per_picture() { let h = Harness::start("count", Some("ok")).await; let count = |n: usize| { h.post( "/v1/messages/count_tokens", json!({"model": "strata-test", "messages": [{"role": "user", "content": (0..n) .map(|i| json!({"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": base64(&png(i as u8))}})) .collect::>()}]}), ) }; let (status, one) = count(1).await; assert_eq!(status, 200, "{one}"); let (_, three) = count(3).await; // three pictures add two more `<|vision_start|><|image_pad|><|vision_end|>` triples: 6 tokens assert_eq!( three["input_tokens"].as_u64().unwrap() - one["input_tokens"].as_u64().unwrap(), 6 ); assert!(h.lines("enc.log").is_empty(), "counting never encodes"); let (status, body) = h .post( "/v1/responses/input_tokens", json!({"model": "strata-test", "input": [{"role": "user", "content": [{"type": "input_image", "image_url": data_url(1)}]}]}), ) .await; assert_eq!(status, 200, "{body}"); assert!(h.lines("enc.log").is_empty()); h.finish().await; } #[tokio::test] async fn unload_stops_the_encoder_and_the_next_request_starts_it_before_the_engine() { let h = Harness::start("lifecycle", Some("ok")).await; assert_eq!(h.get("/props").await["modalities"]["vision"], true); assert_eq!(h.get("/v1/status").await["vision"]["available"], true); assert_eq!(h.get("/api/health").await["images"], true); assert_eq!( h.get("/v1/models").await["data"][0]["architecture"]["input_modalities"], json!(["text", "image"]) ); let vision = h.vision.clone().unwrap(); let body = json!({"model": "strata-test", "max_tokens": 5, "messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": data_url(1)}}]}]}); assert_eq!(h.post("/v1/chat/completions", body.clone()).await.0, 200); assert!(vision.alive().await); let (status, answer) = h.post("/unload", json!({})).await; assert_eq!(status, 200, "{answer}"); assert!( !vision.alive().await, "unloading the model unloads the encoder" ); // the cached picture needs no encoder, but the request restarts both assert_eq!(h.post("/v1/chat/completions", body).await.0, 200); assert!(vision.alive().await); assert_eq!(h.lines("starts.log").len(), 2); assert_eq!(h.lines("boots").len(), 2); h.finish().await; }