Skip to main content

Module testing

Module testing 

Source
Expand description

Test utilities for code that depends on StreamingModelProvider.

§FakeLlmProvider

A configurable fake that returns canned responses. Use it in place of real providers in unit tests:

use llm::testing::FakeLlmProvider;
use llm::{LlmResponse, Context};

// Returns "Hello!" then Done on the first call
let provider = FakeLlmProvider::with_single_response(vec![
    LlmResponse::text("Hello!"),
    LlmResponse::done(),
]);

Multiple turns – Pass a Vec<Vec<LlmResponse>> to FakeLlmProvider::new where each inner vec is the response for one call. Calls beyond the provided responses return a bare Done.

Context capture – Call captured_contexts() to get an Arc<Mutex<Vec<Context>>> that records every context passed to stream_response.

Customization – Chain with_display_name and with_context_window to configure the provider’s metadata.

§LlmResponseBuilder

A builder for constructing response sequences with less boilerplate:

use llm::testing::llm_response;

let chunks = llm_response()
    .text(&["Hello", " world"])
    .tool_call("tc-1", "read_file", &[r#"{"path":"#, r#""foo.rs"}"#])
    .build();
// Produces: Start -> Text("Hello") -> Text(" world") -> ToolRequestStart -> ... -> Done

reasoning(&[...]) appends Reasoning frames, and usage(in, out) appends a Usage frame.

Callers that script full turns (Vec<Result<LlmResponse, LlmError>>) use the result terminators:

  • build_results() — a successful turn, wrapped in Ok.
  • build_with_error(error) — the error frame is emitted after the frames built so far, then the stream closes with Done.
  • build_interrupted(error) — the stream dies on the error; no Done is delivered.
  • failed_call(error) — a turn that fails before the provider emits any frames.

Structs§

FakeLlmProvider
LlmResponseBuilder

Functions§

failed_call
A turn whose call fails before the provider emits any frames.
llm_response
priced_model
session_usage_event