Files
leleka/control-plane/tests/vision_http/encoder.rs
T
Pavlo Ratushnyi 7610ba3568 STRA-338 size-gw: the 19 oversize files of control-plane and desktop-core split by topic (no file over 400 lines); mechanical child modules renamed
Tests become directory modules named after what they test, with a shared fixtures/harness module:
control-plane/src/{responses,ggml_bridge,gguf_f32_stream,prepare_preview}/tests/, desktop-core/src/runtime_rpc/tests/;
integration targets become tests/<name>/main.rs plus topic modules and a harness (backend_worker_http, vision_http,
memory_gate_http, tool_corpus, mcp_hub, api_over_runtime, runtime_gateway_sdet, native_tools_sdet,
native_tools_retry_sdet, managed_worker, gateway); target names are unchanged. Sources: leleka-inference.rs becomes
src/bin/leleka-inference/ (main.rs plus one module per operator option group), the cp_replay example becomes
examples/cp_replay/, and the tonic service impl of runtime_rpc delegates to session_rpc, model_rpc and generate_rpc
(bodies unchanged). impl_* modules are renamed after what they hold (base_provenance, count_tokens, manifest_fetch,
fetcher, record, capture, in_flight, tool_policy, tool_plan, server). The self-spawning tests that name themselves
(credential_child, the managed worker fixture) use the new module path or stay at the target root.

Test ids: 543 before and after in both crates (strata-control, strata-desktop-core, --all-targets), 0 missing, 0 new,
0 outcome changes; 241 ids differ only in the module path. Warnings in the two crates: 0 before and after.
Counts (strata-control / strata-desktop-core): oversize files 19 -> 0 (13 + 6). Functions over 30 lines, complexity 10,
nesting 4, wide lines, prod: 187/135/17/199 -> 187/135/17/196 and 81/59/6/54 -> 81/59/6/54; test: 214/17/30/133 ->
214/17/30/133 and 65/3/3/30 -> 65/3/3/27. The 211 functions that moved verbatim are in limits-allowlist.json (owner
STRA-338) because the diff gate treats a moved function as added; the entries fall as the functions are shortened.
2026-11-06 13:13:59 +01:00

130 lines
5.5 KiB
Rust

//! 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::<Vec<_>>()}]}),
)
};
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;
}