Skip to main content

execution_engine_core/framework/
service.rs

1// Copyright 2024 Vincents AI
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Service testing utilities for the test framework
5//!
6//! This module provides mock implementations of plugin services for testing.
7//!
8//! NOTE: This is a stub module. The implementation is tracked by task:
9//! c7c4f29f-95b0-4263-94f0-f1b95e478d33
10
11/// Mock plugin for testing plugin interactions
12///
13/// # Example (when implemented)
14/// ```ignore
15/// let plugin = MockPlugin::builder("test-plugin")
16///     .with_version("1.0.0")
17///     .with_capability("test.action")
18///     .build();
19/// ```
20pub struct MockPlugin {
21    name: String,
22    version: String,
23    capabilities: Vec<String>,
24}
25
26impl MockPlugin {
27    /// Creates a new builder for constructing a MockPlugin
28    pub fn builder(name: &str) -> MockPluginBuilder {
29        MockPluginBuilder {
30            name: name.to_string(),
31            version: "0.0.0".to_string(),
32            capabilities: Vec::new(),
33        }
34    }
35
36    pub fn name(&self) -> &str {
37        &self.name
38    }
39
40    pub fn version(&self) -> &str {
41        &self.version
42    }
43
44    pub fn has_capability(&self, capability: &str) -> bool {
45        self.capabilities.iter().any(|c| c == capability)
46    }
47}
48
49pub struct MockPluginBuilder {
50    name: String,
51    version: String,
52    capabilities: Vec<String>,
53}
54
55impl MockPluginBuilder {
56    pub fn with_version(mut self, version: &str) -> Self {
57        self.version = version.to_string();
58        self
59    }
60
61    pub fn with_capability(mut self, capability: &str) -> Self {
62        self.capabilities.push(capability.to_string());
63        self
64    }
65
66    pub fn build(self) -> MockPlugin {
67        MockPlugin {
68            name: self.name,
69            version: self.version,
70            capabilities: self.capabilities,
71        }
72    }
73}
74
75/// Mock service registry for testing service discovery
76pub struct MockServiceRegistry {
77    services: Vec<(String, String, *mut std::ffi::c_void)>,
78}
79
80impl MockServiceRegistry {
81    pub fn new() -> Self {
82        Self {
83            services: Vec::new(),
84        }
85    }
86
87    pub fn register_service(&mut self, name: &str, type_name: &str, ptr: *mut std::ffi::c_void) {
88        self.services
89            .push((name.to_string(), type_name.to_string(), ptr));
90    }
91
92    pub fn has_service(&self, name: &str) -> bool {
93        self.services.iter().any(|(n, _, _)| n == name)
94    }
95
96    pub fn service_count(&self) -> usize {
97        self.services.len()
98    }
99}
100
101impl Default for MockServiceRegistry {
102    fn default() -> Self {
103        Self::new()
104    }
105}
106
107/// Lifecycle event for simulation
108#[derive(Debug, Clone)]
109pub enum LifecycleEvent {
110    PluginLoaded { name: String },
111    PluginInitialized { name: String },
112    PluginStarted { name: String },
113    PluginStopped { name: String },
114    PluginUnloaded { name: String },
115}
116
117/// Lifecycle simulator for testing plugin lifecycle handling
118pub struct LifecycleSimulator {
119    events: Vec<LifecycleEvent>,
120}
121
122impl LifecycleSimulator {
123    pub fn new() -> Self {
124        Self { events: Vec::new() }
125    }
126
127    pub fn emit_event(&mut self, event: LifecycleEvent) {
128        self.events.push(event);
129    }
130
131    pub fn event_history(&self) -> &[LifecycleEvent] {
132        &self.events
133    }
134}
135
136impl Default for LifecycleSimulator {
137    fn default() -> Self {
138        Self::new()
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    #[test]
147    fn test_mock_plugin_has_capability() {
148        let plugin = MockPlugin::builder("test")
149            .with_capability("test.action")
150            .build();
151        assert!(plugin.has_capability("test.action"));
152        assert!(!plugin.has_capability("other.action"));
153    }
154
155    #[test]
156    fn test_mock_plugin_version() {
157        let plugin = MockPlugin::builder("test").with_version("1.0.0").build();
158        assert_eq!(plugin.version(), "1.0.0");
159    }
160
161    #[test]
162    fn test_mock_service_registry() {
163        let mut registry = MockServiceRegistry::new();
164        assert_eq!(registry.service_count(), 0);
165
166        registry.register_service("test.service", "test.v1.Service", std::ptr::null_mut());
167        assert!(registry.has_service("test.service"));
168        assert!(!registry.has_service("other.service"));
169        assert_eq!(registry.service_count(), 1);
170    }
171
172    #[test]
173    fn test_lifecycle_simulator() {
174        let mut simulator = LifecycleSimulator::new();
175
176        simulator.emit_event(LifecycleEvent::PluginLoaded {
177            name: "test".to_string(),
178        });
179        simulator.emit_event(LifecycleEvent::PluginInitialized {
180            name: "test".to_string(),
181        });
182
183        let events = simulator.event_history();
184        assert_eq!(events.len(), 2);
185    }
186}