Commit b80fa6b7
b80fa6b715b2Merged
Scott Nixon <[email protected]> ยท 8 months ago ยท 2 files ยท +1 added ยท ~1 modified
Description
refactor(agent): extract test mocks to test_utils.rs [attractor-klf]
Move MockEnv, SequenceMockProvider, make_client, and EchoTool out of the inline mod tests block into a dedicated #[cfg(test)] test_utils module. Reduces lib.rs by ~170 lines.
Details
- Commit
- b80fa6b715b2
- Parent
- e08157b321cd
crates/attractor-agent/src/lib.rsmodifiedView file
@@ -7,6 +7,8 @@
77
pub mod loop_detection;88
pub mod prompt_builder;99
pub mod subagent;10+
#[cfg(test)]11+
mod test_utils;1012
pub use fidelity::{apply_fidelity, FidelityMode};1113
pub use loop_detection::{LoopDetector, SteeringInjector};1214
pub use prompt_builder::{discover_project_docs, ProjectDoc, SystemPromptBuilder};@@ -425,189 +427,9 @@
425427
mod tests {426428
use super::*;427429
use async_trait::async_trait;428-
use attractor_llm::{FinishReason, ProviderAdapter, Response, StreamEvent, Usage};429-
use attractor_tools::{DirEntry, ExecResult, GrepOptions, Tool, ToolDefinition as ToolsToolDef};430-
use futures_core::Stream;431-
use std::collections::HashMap;432-
use std::path::{Path, PathBuf};433-
use std::pin::Pin;434-
use std::sync::Mutex;435-
436-
// -----------------------------------------------------------------------437-
// Mock ExecutionEnvironment438-
// -----------------------------------------------------------------------439-
440-
struct MockEnv;441-
442-
#[async_trait]443-
impl ExecutionEnvironment for MockEnv {444-
async fn read_file(&self, _path: &Path) -> attractor_types::Result<String> {445-
Ok("mock file content".to_string())446-
}447-
async fn write_file(&self, _path: &Path, _content: &str) -> attractor_types::Result<()> {448-
Ok(())449-
}450-
async fn file_exists(&self, _path: &Path) -> attractor_types::Result<bool> {451-
Ok(false)452-
}453-
async fn list_directory(454-
&self,455-
_path: &Path,456-
_depth: usize,457-
) -> attractor_types::Result<Vec<DirEntry>> {458-
Ok(vec![])459-
}460-
async fn exec_command(461-
&self,462-
_command: &str,463-
_timeout_ms: u64,464-
_cwd: Option<&Path>,465-
_env_vars: Option<&HashMap<String, String>>,466-
) -> attractor_types::Result<ExecResult> {467-
Ok(ExecResult {468-
stdout: String::new(),469-
stderr: String::new(),470-
exit_code: 0,471-
timed_out: false,472-
duration_ms: 0,473-
})474-
}475-
async fn grep(476-
&self,477-
_pattern: &str,478-
_path: &Path,479-
_options: &GrepOptions,480-
) -> attractor_types::Result<String> {481-
Ok(String::new())482-
}483-
async fn glob_files(484-
&self,485-
_pattern: &str,486-
_base: &Path,487-
) -> attractor_types::Result<Vec<PathBuf>> {488-
Ok(vec![])489-
}490-
fn working_directory(&self) -> &Path {491-
Path::new("/tmp")492-
}493-
fn platform(&self) -> &str {494-
"test"495-
}496-
}497-
498-
// -----------------------------------------------------------------------499-
// Mock LLM Provider500-
// -----------------------------------------------------------------------501-
502-
/// A mock provider that returns responses from a pre-configured sequence.503-
/// Each call to `complete` pops the next response from the queue.504-
struct SequenceMockProvider {505-
responses: Mutex<VecDeque<Response>>,506-
}507-
508-
impl SequenceMockProvider {509-
fn new(responses: Vec<Response>) -> Self {510-
Self {511-
responses: Mutex::new(VecDeque::from(responses)),512-
}513-
}514-
515-
fn single_text(text: &str) -> Self {516-
Self::new(vec![Response {517-
id: "resp-1".into(),518-
text: text.to_string(),519-
tool_calls: vec![],520-
reasoning: None,521-
usage: Usage::default(),522-
model: "mock-model".into(),523-
finish_reason: FinishReason::EndTurn,524-
}])525-
}526-
}527-
528-
#[async_trait]529-
impl ProviderAdapter for SequenceMockProvider {530-
async fn complete(&self, _request: &Request) -> Result<Response, AttractorError> {531-
let mut queue = self.responses.lock().unwrap();532-
match queue.pop_front() {533-
Some(resp) => Ok(resp),534-
None => Ok(Response {535-
id: "resp-fallback".into(),536-
text: "No more responses".into(),537-
tool_calls: vec![],538-
reasoning: None,539-
usage: Usage::default(),540-
model: "mock-model".into(),541-
finish_reason: FinishReason::EndTurn,542-
}),543-
}544-
}545-
fn stream(546-
&self,547-
_request: &Request,548-
) -> Pin<Box<dyn Stream<Item = StreamEvent> + Send + '_>> {549-
Box::pin(tokio_stream::empty::<StreamEvent>())550-
}551-
fn name(&self) -> &str {552-
"mock"553-
}554-
fn default_model(&self) -> &str {555-
"mock-model"556-
}557-
fn supports_tools(&self) -> bool {558-
true559-
}560-
fn supports_streaming(&self) -> bool {561-
false562-
}563-
fn supports_reasoning(&self) -> bool {564-
false565-
}566-
fn context_window_size(&self) -> usize {567-
128_000568-
}569-
}570-
571-
/// Helper: build an LlmClient with the given mock provider.572-
fn make_client(provider: SequenceMockProvider) -> attractor_llm::LlmClient {573-
let mut client = attractor_llm::LlmClient::new();574-
client.register_provider(provider);575-
client576-
}577-
578-
// -----------------------------------------------------------------------579-
// Mock Tool580-
// -----------------------------------------------------------------------581-
582-
struct EchoTool;583-
584-
#[async_trait]585-
impl Tool for EchoTool {586-
fn definition(&self) -> ToolsToolDef {587-
ToolsToolDef {588-
name: "echo".to_string(),589-
description: "Echoes the input text".to_string(),590-
parameters: serde_json::json!({591-
"type": "object",592-
"properties": {593-
"text": { "type": "string" }594-
}595-
}),596-
}597-
}598-
599-
async fn execute(600-
&self,601-
arguments: serde_json::Value,602-
_env: &dyn ExecutionEnvironment,603-
) -> attractor_types::Result<String> {604-
let text = arguments605-
.get("text")606-
.and_then(|v| v.as_str())607-
.unwrap_or("(empty)");608-
Ok(text.to_string())609-
}610-
}430+
use attractor_llm::{FinishReason, Response, Usage};431+
use attractor_tools::{Tool, ToolDefinition as ToolsToolDef};432+
use crate::test_utils::{make_client, EchoTool, MockEnv, SequenceMockProvider};611433
612434
// -----------------------------------------------------------------------613435
// Test 1: Session creation with configcrates/attractor-agent/src/test_utils.rsaddedView file
Loading diffโฆ