Skip to main content

execution_engine_core/framework/
mod.rs

1// Copyright 2024 Vincents AI
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// Framework module for setting up test environments
5
6use anyhow::Result;
7use std::path::{Path, PathBuf};
8use std::sync::Arc;
9use tempfile::TempDir;
10
11pub mod assertions;
12pub mod database;
13pub mod service;
14
15pub struct TestEnvironment {
16    name: String,
17    temp_dir: TempDir,
18    isolated: bool,
19    cleanup_on_drop: bool,
20}
21
22impl TestEnvironment {
23    pub fn new(name: &str) -> Result<Self> {
24        let temp_dir = TempDir::new()?;
25        Ok(Self {
26            name: name.to_string(),
27            temp_dir,
28            isolated: true,
29            cleanup_on_drop: true,
30        })
31    }
32
33    pub fn path(&self) -> &Path {
34        self.temp_dir.path()
35    }
36
37    pub fn is_isolated(&self) -> bool {
38        self.isolated
39    }
40
41    pub fn set_cleanup_on_drop(&mut self, cleanup: bool) {
42        self.cleanup_on_drop = cleanup;
43    }
44
45    pub fn max_test_duration_secs(&self) -> u64 {
46        300 // 5 minutes default
47    }
48}
49
50pub struct TestConfiguration {
51    pub temp_dir: Option<PathBuf>,
52    pub cleanup_on_drop: bool,
53    pub max_test_duration_secs: u64,
54}
55
56impl Default for TestConfiguration {
57    fn default() -> Self {
58        Self {
59            temp_dir: None,
60            cleanup_on_drop: true,
61            max_test_duration_secs: 300,
62        }
63    }
64}
65
66pub struct TestFramework {
67    environments: Vec<TestEnvironment>,
68    current_env: Option<String>,
69}
70
71impl TestFramework {
72    /// Provide a shared ServiceRegistry for tests/plugins
73    pub fn service_registry(&self) -> Arc<crate::service_registry::ServiceRegistry> {
74        // In tests we create a fresh registry when needed. For simplicity return a new one.
75        Arc::new(crate::service_registry::ServiceRegistry::new())
76    }
77}
78
79impl TestFramework {
80    pub fn new() -> Self {
81        Self {
82            environments: Vec::new(),
83            current_env: None,
84        }
85    }
86
87    pub fn create_test_environment(&mut self, name: &str) -> Result<String> {
88        let env = TestEnvironment::new(name)?;
89        let env_name = env.name.clone();
90        self.environments.push(env);
91        Ok(env_name)
92    }
93
94    pub fn get_environment(&self, name: &str) -> Option<&TestEnvironment> {
95        self.environments.iter().find(|env| env.name == name)
96    }
97
98    pub fn set_current_environment(&mut self, name: &str) {
99        self.current_env = Some(name.to_string());
100    }
101
102    pub fn cleanup_all(&mut self) -> Result<()> {
103        for _env in self.environments.drain(..) {
104            // Cleanup happens when TempDir is dropped
105        }
106        self.current_env = None;
107        Ok(())
108    }
109}
110
111impl Default for TestFramework {
112    fn default() -> Self {
113        Self::new()
114    }
115}