webclaw/crates/webclaw-llm/src/testing.rs
Valerio c99ec684fa Initial release: webclaw v0.1.0 — web content extraction for LLMs
CLI + MCP server for extracting clean, structured content from any URL.
6 Rust crates, 10 MCP tools, TLS fingerprinting, 5 output formats.

MIT Licensed | https://webclaw.io
2026-03-23 18:31:11 +01:00

48 lines
1.4 KiB
Rust

/// Shared test utilities for webclaw-llm.
///
/// Provides a configurable mock LLM provider for unit tests across
/// extract, chain, and other modules that need a fake LLM backend.
#[cfg(test)]
pub(crate) mod mock {
use async_trait::async_trait;
use crate::error::LlmError;
use crate::provider::{CompletionRequest, LlmProvider};
/// A mock LLM provider that returns a canned response or error.
/// Covers the common test cases: success, failure, and availability.
pub struct MockProvider {
pub name: &'static str,
pub response: Result<String, String>,
pub available: bool,
}
impl MockProvider {
/// Shorthand for a mock that always succeeds with the given response.
pub fn ok(response: &str) -> Self {
Self {
name: "mock",
response: Ok(response.to_string()),
available: true,
}
}
}
#[async_trait]
impl LlmProvider for MockProvider {
async fn complete(&self, _request: &CompletionRequest) -> Result<String, LlmError> {
match &self.response {
Ok(text) => Ok(text.clone()),
Err(msg) => Err(LlmError::ProviderError(msg.clone())),
}
}
async fn is_available(&self) -> bool {
self.available
}
fn name(&self) -> &str {
self.name
}
}
}