Skip to main content

MockProvider

Struct MockProvider 

Source
pub struct MockProvider {
    pub info: ModelInfo,
    pub responses: Vec<CompletionResponse>,
    /* private fields */
}
Expand description

Deterministic mock provider that returns responses in sequence.

Each call to complete() returns the next response from the internal sequence. When all responses have been returned, subsequent calls return the last response (clamp behavior, not wrap-around).

§Thread Safety

MockProvider is Send + Sync by design — it uses AtomicUsize for lock-free call indexing.

§Example

use traitclaw_test_utils::provider::MockProvider;

let p = MockProvider::text("hello");
// p.complete(req).await returns "hello" every time

Fields§

§info: ModelInfo

Model information returned by Provider::model_info.

§responses: Vec<CompletionResponse>

Ordered list of responses to return.

Implementations§

Source§

impl MockProvider

Source

pub fn text(text: &str) -> Self

Create a provider that always returns a single text response.

§Example
use traitclaw_test_utils::provider::MockProvider;

let p = MockProvider::text("I am a mock LLM");
Source

pub fn sequence(responses: Vec<CompletionResponse>) -> Self

Create a provider with an explicit sequence of responses.

Responses are returned in order. Once exhausted, the last response is repeated.

§Panics

Panics if responses is empty.

§Example
use traitclaw_test_utils::provider::MockProvider;
use traitclaw_core::types::completion::{CompletionResponse, ResponseContent, Usage};

let p = MockProvider::sequence(vec![
    CompletionResponse {
        content: ResponseContent::Text("first".into()),
        usage: Usage { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
    },
    CompletionResponse {
        content: ResponseContent::Text("second".into()),
        usage: Usage { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
    },
]);
Source

pub fn tool_then_text(tool_calls: Vec<ToolCall>, final_text: &str) -> Self

Create a provider that returns tool calls first, then a final text response.

This is the standard pattern for testing ReAct-style agent loops.

§Example
use traitclaw_test_utils::provider::MockProvider;
use traitclaw_core::types::tool_call::ToolCall;

let p = MockProvider::tool_then_text(
    vec![ToolCall {
        id: "call_1".into(),
        name: "search".into(),
        arguments: r#"{"query":"rust"}"#.into(),
    }],
    "Here are the results.",
);
Source

pub fn always_tool_calls(tool_calls: Vec<ToolCall>) -> Self

Create a provider that always returns tool calls (never text).

Useful for testing tool-budget guards and loop detection.

Source

pub fn error(msg: &str) -> Self

Create a provider that always returns an error.

Useful for testing error handling paths in strategies and agents.

§Example
use traitclaw_test_utils::provider::MockProvider;

let p = MockProvider::error("API rate limit exceeded");
// p.complete(req).await will return Err(Error::Runtime(...))
Source

pub fn call_count(&self) -> usize

Returns how many times complete() has been called.

Trait Implementations§

Source§

impl Provider for MockProvider

Source§

fn complete<'life0, 'async_trait>( &'life0 self, _req: CompletionRequest, ) -> Pin<Box<dyn Future<Output = Result<CompletionResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a completion request and get a full response.
Source§

fn stream<'life0, 'async_trait>( &'life0 self, _req: CompletionRequest, ) -> Pin<Box<dyn Future<Output = Result<CompletionStream>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a completion request and get a streaming response.
Source§

fn model_info(&self) -> &ModelInfo

Get information about the model.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more