Skip to main content

Module mock

Module mock 

Source
Expand description

Mock provider for testing.

MockProvider is a queue-based fake that lets tests control exactly what responses and errors a provider returns, without touching the network. It implements Provider, so it works anywhere a real provider does — including through DynProvider via the blanket impl.

§Usage

use llm_stack::mock::{MockProvider, MockError};
use llm_stack::{Provider, ChatParams, ChatResponse, ContentBlock};
use llm_stack::chat::StopReason;
use llm_stack::usage::Usage;
use std::collections::{HashMap, HashSet};

let mock = MockProvider::new(llm_stack::provider::ProviderMetadata {
    name: "test".into(),
    model: "test-model".into(),
    context_window: 4096,
    capabilities: HashSet::new(),
});

mock.queue_response(ChatResponse {
    content: vec![ContentBlock::Text("Hello!".into())],
    usage: Usage::default(),
    stop_reason: StopReason::EndTurn,
    model: "test-model".into(),
    metadata: HashMap::new(),
});

let resp = mock.generate(&ChatParams::default()).await.unwrap();
assert_eq!(mock.recorded_calls().len(), 1);

§Why MockError instead of LlmError?

LlmError contains Box<dyn Error> and is not Clone, so it can’t be stored in a queue. MockError mirrors the common error variants in a cloneable form and converts to LlmError at dequeue time.

Structs§

MockProvider
A queue-based mock provider for unit and integration tests.

Enums§

MockError
Cloneable error subset for mock queuing.