Skip to main content

scud/backend/
simulated.rs

1//! Simulated backend for testing and dry-runs.
2//!
3//! Returns a canned response without making any LLM API calls.
4//! Useful for Attractor pipeline testing and `--simulated` mode.
5
6use anyhow::Result;
7use async_trait::async_trait;
8use tokio::sync::mpsc;
9use tokio_util::sync::CancellationToken;
10
11use super::{AgentBackend, AgentEvent, AgentHandle, AgentRequest, AgentResult, AgentStatus};
12
13/// A backend that returns simulated responses without calling any LLM.
14pub struct SimulatedBackend;
15
16#[async_trait]
17impl AgentBackend for SimulatedBackend {
18    async fn execute(&self, req: AgentRequest) -> Result<AgentHandle> {
19        let (tx, rx) = mpsc::channel(16);
20        let cancel = CancellationToken::new();
21
22        let prompt_preview = if req.prompt.len() > 100 {
23            format!("{}...", &req.prompt[..97])
24        } else {
25            req.prompt.clone()
26        };
27
28        let response_text = format!("[Simulated] Response for: {}", prompt_preview);
29
30        tokio::spawn(async move {
31            let _ = tx.send(AgentEvent::TextDelta(response_text.clone())).await;
32            let _ = tx
33                .send(AgentEvent::Complete(AgentResult {
34                    text: response_text,
35                    status: AgentStatus::Completed,
36                    tool_calls: vec![],
37                    usage: None,
38                }))
39                .await;
40        });
41
42        Ok(AgentHandle { events: rx, cancel })
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[tokio::test]
51    async fn test_simulated_response_contains_prompt() {
52        let backend = SimulatedBackend;
53        let req = AgentRequest {
54            prompt: "Write a test".into(),
55            ..Default::default()
56        };
57        let handle = backend.execute(req).await.unwrap();
58        let result = handle.result().await.unwrap();
59        assert!(result.text.contains("Write a test"));
60        assert!(result.text.starts_with("[Simulated]"));
61    }
62
63    #[tokio::test]
64    async fn test_simulated_truncates_long_prompt() {
65        let backend = SimulatedBackend;
66        let long_prompt = "x".repeat(200);
67        let req = AgentRequest {
68            prompt: long_prompt,
69            ..Default::default()
70        };
71        let handle = backend.execute(req).await.unwrap();
72        let result = handle.result().await.unwrap();
73        assert!(result.text.contains("..."));
74        assert!(result.text.len() < 200);
75    }
76}