Files
leleka/desktop-core/tests/api_over_runtime/streaming.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

123 lines
4.5 KiB
Rust

//! Responses over real gRPC: streaming, usage, length, tool calls and stop markers.
use crate::harness::{Runtime, Stack, assert_one_terminal, body, sse_events, text_of, usage};
use leleka_backend_api::FinishReason;
use leleka_backend_api::scripted::Script;
use serde_json::{Value, json};
use strata_control::worker::WorkerState;
#[tokio::test(flavor = "multi_thread")]
async fn responses_over_real_grpc_stream_and_complete_with_the_engines_usage() {
let runtime = Runtime::start(None).await;
runtime.operator_loads_the_model().await;
let stack = Stack::start("chain-basic", runtime.address).await;
stack.wait_state(WorkerState::Ready).await;
runtime
.engine
.push(Script::stop_with(&["hel", "lo ", "жук"], usage(11, 3)));
let (status, text) = stack
.post("/v1/responses", &body(json!({"stream": true})))
.await;
assert_eq!(status, 200, "{text}");
let events = sse_events(&text);
assert_one_terminal(&events);
assert_eq!(events.last().unwrap()["type"], "response.completed");
let deltas: String = events
.iter()
.filter(|e| e["type"] == "response.output_text.delta")
.map(|e| e["delta"].as_str().unwrap())
.collect();
assert_eq!(deltas, "hello жук");
runtime
.engine
.push(Script::stop_with(&["done"], usage(11, 1)));
let (status, text) = stack.post("/v1/responses", &body(json!({}))).await;
assert_eq!(status, 200, "{text}");
let response: Value = serde_json::from_str(&text).unwrap();
assert_eq!(text_of(&response), "done");
assert_eq!(response["usage"]["input_tokens"], 11);
// The prompt the engine got through gRPC is the rendered template, and the sampling defaults
// stayed unset.
let seen = runtime.engine.requests();
assert_eq!(
seen[0].prompt,
"<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
);
assert_eq!(seen[0].max_output_tokens, 4096);
stack.finish().await;
runtime.stop().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn length_tool_call_and_stop_marker_over_real_grpc() {
let runtime = Runtime::start(None).await;
runtime.operator_loads_the_model().await;
let stack = Stack::start("chain-finish", runtime.address).await;
stack.wait_state(WorkerState::Ready).await;
runtime.engine.push(Script::finishing(
&["cut"],
FinishReason::Length,
usage(5, 1),
));
let (_, text) = stack
.post(
"/v1/responses",
&body(json!({"stream": true, "max_output_tokens": 1})),
)
.await;
let events = sse_events(&text);
assert_one_terminal(&events);
assert_eq!(events.last().unwrap()["type"], "response.incomplete");
let markup = "<tool_call>\n<function=lookup>\n<parameter=key>\nvalue\n</parameter>\n</function>\n</tool_call>";
let pieces: Vec<String> = markup
.chars()
.collect::<Vec<_>>()
.chunks(9)
.map(|c| c.iter().collect())
.collect();
let refs: Vec<&str> = pieces.iter().map(String::as_str).collect();
runtime.engine.push(Script::stop_with(&refs, usage(30, 20)));
let (status, text) = stack
.post(
"/v1/responses",
&body(json!({"tools": [{"type": "function", "name": "lookup",
"parameters": {"type": "object", "properties": {"key": {"type": "string"}},
"required": ["key"], "additionalProperties": false}}]})),
)
.await;
assert_eq!(status, 200, "{text}");
let response: Value = serde_json::from_str(&text).unwrap();
let call = response["output"]
.as_array()
.unwrap()
.iter()
.find(|item| item["type"] == "function_call")
.unwrap();
assert_eq!(call["name"], "lookup");
assert_eq!(
serde_json::from_str::<Value>(call["arguments"].as_str().unwrap()).unwrap(),
json!({"key": "value"})
);
// The model ends its turn with a marker; the engine would keep going, the marker stops it.
runtime
.engine
.push(Script::hanging_after(&["ok", "<|im_end|>", "junk"]));
let (status, text) = stack.post("/v1/responses", &body(json!({}))).await;
assert_eq!(status, 200, "{text}");
assert_eq!(
text_of(&serde_json::from_str(&text).unwrap()),
"ok",
"{text}"
);
let ids = runtime.engine.cancels();
assert_eq!(ids.len(), 1, "the marker cancelled the runtime generation");
stack.finish().await;
runtime.stop().await;
}