use super::*;
const SOAK_OVERSIZED_TEXT_LEN: usize = 8_000;
pub struct SoakEchoLlmClient {
cursor: AtomicUsize,
}
impl SoakEchoLlmClient {
pub fn new() -> Self {
Self {
cursor: AtomicUsize::new(0),
}
}
}
impl Default for SoakEchoLlmClient {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl InferenceAdapter for SoakEchoLlmClient {
crate::llm::mock_adapter_identity!("mock-soak-echo");
async fn chat(&self, _req: &ChatRequest) -> Result<ChatResponse, InferenceError> {
let idx = self.cursor.fetch_add(1, Ordering::SeqCst);
let fixture = match idx {
0 => soak_goal_tool_call("set_goal", idx, 1, "soak progress marker 0"),
1 => soak_clear_goal_tool_call(idx, 1),
2 => soak_goal_tool_call("set_goal", idx, 2, "soak progress marker 2"),
3 => soak_clear_goal_tool_call(idx, 2),
4 => soak_goal_tool_call("set_goal", idx, 3, &"x".repeat(SOAK_OVERSIZED_TEXT_LEN)),
5 => soak_clear_goal_tool_call(idx, 3),
6 => stop_fixture(),
_ => {
return Err(InferenceError::MissingConfig(format!(
"SoakEchoLlmClient script exhausted at call {idx}"
)));
}
};
serde_json::from_value(fixture).map_err(|e| {
InferenceError::MissingConfig(format!(
"SoakEchoLlmClient: invalid scripted fixture: {e}"
))
})
}
}
fn soak_goal_tool_call(tool_name: &str, idx: usize, slot: usize, text: &str) -> Value {
json!({
"id": "mock-soak-set",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": format!("call-soak-{idx}"),
"type": "function",
"function": {
"name": tool_name,
"arguments": json!({"slot": slot, "text": text}).to_string()
}
}]
},
"finish_reason": "tool_calls"
}],
"usage": {"prompt_tokens": 15, "completion_tokens": 5, "total_tokens": 20}
})
}
fn soak_clear_goal_tool_call(idx: usize, slot: usize) -> Value {
json!({
"id": "mock-soak-clear",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": format!("call-soak-{idx}"),
"type": "function",
"function": {
"name": "clear_goal",
"arguments": json!({"slot": slot}).to_string()
}
}]
},
"finish_reason": "tool_calls"
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
})
}
fn stop_fixture() -> Value {
json!({
"id": "mock-soak-stop",
"choices": [{
"message": {"role": "assistant", "content": "soak call complete", "tool_calls": []},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
})
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn soak_script_ends_in_a_resumable_stop_and_has_one_oversized_turn() {
let client = SoakEchoLlmClient::new();
let req = ChatRequest {
model: "mock".to_string(),
messages: vec![],
temperature: None,
max_tokens: None,
tools: None,
tool_choice: None,
usage: None,
response_schema: None,
stop: None,
};
let mut oversized_count = 0;
for i in 0..6 {
let resp = client
.chat(&req)
.await
.unwrap_or_else(|e| panic!("call {i}: {e}"));
let calls = resp.first_tool_calls();
assert_eq!(calls.len(), 1, "call {i} must be exactly one tool call");
if calls[0].function.name == "set_goal" {
let args: Value =
serde_json::from_str(&calls[0].function.arguments).expect("valid json args");
if args["text"].as_str().unwrap_or("").len() >= SOAK_OVERSIZED_TEXT_LEN {
oversized_count += 1;
}
}
}
assert_eq!(oversized_count, 1, "expected exactly one oversized turn");
let stop = client.chat(&req).await.expect("call 6 (stop)");
assert!(
stop.first_tool_calls().is_empty(),
"final turn must have no tool calls"
);
let err = client.chat(&req).await;
assert!(
err.is_err(),
"the script must not silently repeat past its end"
);
}
}