Files
leleka/control-plane/tests/vision_http/rendering.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

107 lines
4.0 KiB
Rust

//! Picture expansion and prompt rendering match the Python handlers.
use crate::common::{PAD, SPECIALS};
use crate::harness::{Harness, geni_line};
use leleka_serving_policy::vision as policy;
use serde_json::{Value, json};
#[test]
fn the_expansion_matches_the_engine_side_position_table() {
// The rows the encoder wrote line up with the pads the server sent: one SVE1 record of 6 rows per picture.
let mut record = Vec::new();
for v in [policy::SVE1_MAGIC, 6, 3, 2, 4] {
record.extend_from_slice(&v.to_le_bytes());
}
record.extend(std::iter::repeat_n(0u8, 6 * 4 * 4));
let embeddings = policy::parse_embeddings(&record, 4).unwrap();
let ids: Vec<i64> = [1, PAD, PAD, PAD, PAD, PAD, PAD, 9]
.iter()
.map(|v| i64::from(*v))
.collect();
let positions = policy::build_positions(&ids, 16, &embeddings, 4, i64::from(PAD)).unwrap();
assert_eq!(positions.images.len(), 1);
}
/// The ids of a request as the template wrote them: bytes as bytes, specials by name, and a run of the rows one
/// picture got collapsed back to the single `<|image_pad|>` of the template.
fn prompt_of(ids: &[u32], rows: usize) -> String {
let mut bytes = Vec::new();
let mut at = 0;
while at < ids.len() {
let id = ids[at];
if id == PAD {
assert!(
ids[at..].len() >= rows && ids[at..at + rows].iter().all(|i| *i == PAD),
"a full run of {rows} rows"
);
at += rows;
} else {
at += 1;
}
match id {
0..=255 => bytes.push(id as u8),
_ => bytes.extend(SPECIALS[id as usize - 256].bytes()),
}
}
String::from_utf8(bytes).unwrap()
}
/// The pictures' tag bytes (the last byte of each fake PNG) in the order the golden lists their sources.
fn tags_of(sources: &Value) -> Vec<String> {
sources
.as_array()
.unwrap()
.iter()
.map(|source| {
let payload = source.as_str().unwrap().split_once(',').unwrap().1;
format!(
"tag={}",
policy::b64decode(payload).unwrap().last().unwrap()
)
})
.collect()
}
/// Every Chat and Messages request of `fixtures/golden/vision_dialects.json` (recorded from the Python handlers
/// `openai_to_messages` / `anthropic_to_messages` and the chat template) reaches the engine as the same prompt, with
/// the same pictures in the same order.
#[tokio::test]
async fn pictures_render_and_order_like_the_python_handlers() {
let golden: Value =
serde_json::from_str(include_str!("../fixtures/golden/vision_dialects.json")).unwrap();
let cases = golden["cases"].as_array().unwrap();
assert!(cases.len() >= 14);
let h = Harness::start("golden", Some("ok")).await;
let (mut sent, mut encoded) = (0, 0);
for (index, case) in cases.iter().enumerate() {
let mut body = case["request"].clone();
body["model"] = json!("strata-test");
body["max_tokens"] = json!(20);
let path = match case["dialect"].as_str().unwrap() {
"chat" => "/v1/chat/completions",
_ => "/v1/messages",
};
let (status, answer) = h.post(path, body).await;
assert_eq!(status, 200, "case {index}: {answer}");
let lines = h.engine_lines();
let geni: Vec<&String> = lines.iter().filter(|l| l.starts_with("GENI")).collect();
assert_eq!(geni.len(), sent + 1, "case {index}");
sent += 1;
let (_, _, ids) = geni_line(geni[sent - 1]);
assert_eq!(
prompt_of(&ids, 6),
case["prompt"].as_str().unwrap(),
"case {index}"
);
let want = tags_of(&case["images"]);
let log = h.lines("enc.log");
let got: Vec<String> = log[encoded..]
.iter()
.map(|l| l.rsplit(' ').next().unwrap().to_owned())
.collect();
encoded = log.len();
assert_eq!(got, want, "case {index}: pictures in prompt order");
}
h.finish().await;
}