Skip to main content

noether_engine/executor/
mock.rs

1use super::{ExecutionError, StageExecutor};
2use noether_core::stage::StageId;
3use noether_store::StageStore;
4use serde_json::Value;
5use std::collections::HashMap;
6
7/// Mock executor that returns pre-configured or example-based outputs.
8#[derive(Debug, Default)]
9pub struct MockExecutor {
10    outputs: HashMap<String, Value>,
11}
12
13impl MockExecutor {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    /// Register a fixed output for a specific stage.
19    pub fn with_output(mut self, stage_id: &StageId, output: Value) -> Self {
20        self.outputs.insert(stage_id.0.clone(), output);
21        self
22    }
23
24    /// Pre-populate from a store: use each stage's first example output.
25    pub fn from_store(store: &(impl StageStore + ?Sized)) -> Self {
26        let mut outputs = HashMap::new();
27        for stage in store.list(None) {
28            if let Some(example) = stage.examples.first() {
29                outputs.insert(stage.id.0.clone(), example.output.clone());
30            }
31        }
32        Self { outputs }
33    }
34}
35
36impl StageExecutor for MockExecutor {
37    fn execute(&self, stage_id: &StageId, _input: &Value) -> Result<Value, ExecutionError> {
38        match self.outputs.get(&stage_id.0) {
39            Some(output) => Ok(output.clone()),
40            None => Ok(Value::Null),
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use serde_json::json;
49
50    #[test]
51    fn mock_returns_configured_output() {
52        let id = StageId("abc".into());
53        let executor = MockExecutor::new().with_output(&id, json!(42));
54        let result = executor.execute(&id, &json!(null)).unwrap();
55        assert_eq!(result, json!(42));
56    }
57
58    #[test]
59    fn mock_returns_null_for_unknown() {
60        let executor = MockExecutor::new();
61        let result = executor
62            .execute(&StageId("unknown".into()), &json!(null))
63            .unwrap();
64        assert_eq!(result, json!(null));
65    }
66}