pub struct MockChatAdapter { /* private fields */ }Expand description
A deterministic ChatAdapter: script the model’s turns, then
assert on what it was asked.
let adapter = MockChatAdapter::new();
adapter.call_tool("read_order", serde_json::json!({ "id": 7 }));
adapter.reply("Your order shipped.");One queued turn is consumed per round-trip. A tool-call turn therefore needs another turn queued behind it, because the chat loop calls the adapter again after running the tool — the mistake is common enough that the exhaustion error says so.
Scripting takes &self so a test can queue turns after handing the
adapter to a harness.
Implementations§
Source§impl MockChatAdapter
impl MockChatAdapter
pub fn new() -> Self
pub fn arc() -> Arc<Self>
Sourcepub fn with_fallback_reply(self, text: impl Into<String>) -> Self
pub fn with_fallback_reply(self, text: impl Into<String>) -> Self
Answer anything unscripted with text instead of failing —
for tests that care about one turn and not about the rest.
Off by default: a silent stand-in reply turns “the test didn’t script enough” into a confusing assertion failure further down.
Sourcepub fn reply(&self, text: impl Into<String>) -> &Self
pub fn reply(&self, text: impl Into<String>) -> &Self
Queue a turn where the model answers with plain text.
Sourcepub fn call_tool(
&self,
name: impl Into<String>,
arguments: impl ToArguments,
) -> &Self
pub fn call_tool( &self, name: impl Into<String>, arguments: impl ToArguments, ) -> &Self
Queue a turn where the model calls a tool. The tool itself runs for real — this only scripts the model’s decision to call it.
Sourcepub fn enqueue(&self, turn: ScriptedTurn) -> &Self
pub fn enqueue(&self, turn: ScriptedTurn) -> &Self
Queue a raw turn.
Sourcepub fn respond_with(
&self,
responder: impl Fn(&ChatAdapterRequest) -> Vec<ChatItem> + Send + Sync + 'static,
) -> &Self
pub fn respond_with( &self, responder: impl Fn(&ChatAdapterRequest) -> Vec<ChatItem> + Send + Sync + 'static, ) -> &Self
Queue a turn computed from the request — for asserting on what the model was given and branching on it in one place.
Sourcepub fn requests(&self) -> Vec<RecordedRequest>
pub fn requests(&self) -> Vec<RecordedRequest>
Every request the adapter received, oldest first.
pub fn last_request(&self) -> Option<RecordedRequest>
Sourcepub fn call_count(&self) -> usize
pub fn call_count(&self) -> usize
How many round-trips happened — the cheapest check that a tool loop ran as many times as expected, and no more.