Skip to main content

mockforge_test/
config.rs

1//! Configuration for MockForge test servers
2
3use std::path::PathBuf;
4use std::time::Duration;
5
6/// Configuration for a MockForge test server
7#[derive(Debug, Clone)]
8pub struct ServerConfig {
9    /// HTTP server port (default: auto-assigned)
10    pub http_port: u16,
11
12    /// WebSocket server port (optional)
13    pub ws_port: Option<u16>,
14
15    /// gRPC server port (optional)
16    pub grpc_port: Option<u16>,
17
18    /// Admin UI port (optional)
19    pub admin_port: Option<u16>,
20
21    /// Metrics/Prometheus port (optional)
22    pub metrics_port: Option<u16>,
23
24    /// Path to OpenAPI specification file (optional)
25    pub spec_file: Option<PathBuf>,
26
27    /// Path to workspace directory (optional)
28    pub workspace_dir: Option<PathBuf>,
29
30    /// Profile name for configuration (optional)
31    pub profile: Option<String>,
32
33    /// Enable admin UI (default: false)
34    pub enable_admin: bool,
35
36    /// Enable metrics endpoint (default: false)
37    pub enable_metrics: bool,
38
39    /// Additional CLI arguments
40    pub extra_args: Vec<String>,
41
42    /// Health check timeout (default: 30 seconds)
43    pub health_timeout: Duration,
44
45    /// Health check interval (default: 100ms)
46    pub health_interval: Duration,
47
48    /// Working directory for the server process (optional)
49    pub working_dir: Option<PathBuf>,
50
51    /// Environment variables for the server process
52    pub env_vars: Vec<(String, String)>,
53
54    /// Path to mockforge binary (optional, will search PATH if not provided)
55    pub binary_path: Option<PathBuf>,
56
57    /// Management token for the HTTP data port's control plane (optional).
58    ///
59    /// When unset, [`crate::MockForgeServer::start`] uses `MOCKFORGE_MANAGEMENT_TOKEN`
60    /// from `env_vars` or the parent environment, and otherwise generates a random
61    /// token. The token is passed to the child as `MOCKFORGE_MANAGEMENT_TOKEN`.
62    pub management_token: Option<String>,
63}
64
65impl Default for ServerConfig {
66    fn default() -> Self {
67        Self {
68            http_port: 0, // 0 means auto-assign
69            ws_port: None,
70            grpc_port: None,
71            admin_port: None,
72            metrics_port: None,
73            spec_file: None,
74            workspace_dir: None,
75            profile: None,
76            enable_admin: false,
77            enable_metrics: false,
78            extra_args: Vec::new(),
79            health_timeout: Duration::from_secs(30),
80            health_interval: Duration::from_millis(100),
81            working_dir: None,
82            env_vars: Vec::new(),
83            binary_path: None,
84            management_token: None,
85        }
86    }
87}
88
89impl ServerConfig {
90    /// Create a new builder for ServerConfig
91    pub fn builder() -> ServerConfigBuilder {
92        ServerConfigBuilder::default()
93    }
94}
95
96/// Builder for ServerConfig
97#[derive(Debug, Default)]
98pub struct ServerConfigBuilder {
99    config: ServerConfig,
100}
101
102impl ServerConfigBuilder {
103    /// Set HTTP port (0 for auto-assign)
104    pub fn http_port(mut self, port: u16) -> Self {
105        self.config.http_port = port;
106        self
107    }
108
109    /// Set WebSocket port
110    pub fn ws_port(mut self, port: u16) -> Self {
111        self.config.ws_port = Some(port);
112        self
113    }
114
115    /// Set gRPC port
116    pub fn grpc_port(mut self, port: u16) -> Self {
117        self.config.grpc_port = Some(port);
118        self
119    }
120
121    /// Set admin UI port
122    pub fn admin_port(mut self, port: u16) -> Self {
123        self.config.admin_port = Some(port);
124        self
125    }
126
127    /// Set metrics port
128    pub fn metrics_port(mut self, port: u16) -> Self {
129        self.config.metrics_port = Some(port);
130        self
131    }
132
133    /// Set OpenAPI specification file
134    pub fn spec_file<P: Into<PathBuf>>(mut self, path: P) -> Self {
135        self.config.spec_file = Some(path.into());
136        self
137    }
138
139    /// Set workspace directory
140    pub fn workspace_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
141        self.config.workspace_dir = Some(path.into());
142        self
143    }
144
145    /// Set profile name
146    pub fn profile<S: Into<String>>(mut self, profile: S) -> Self {
147        self.config.profile = Some(profile.into());
148        self
149    }
150
151    /// Enable admin UI
152    pub fn enable_admin(mut self, enable: bool) -> Self {
153        self.config.enable_admin = enable;
154        self
155    }
156
157    /// Enable metrics endpoint
158    pub fn enable_metrics(mut self, enable: bool) -> Self {
159        self.config.enable_metrics = enable;
160        self
161    }
162
163    /// Add extra CLI argument
164    pub fn extra_arg<S: Into<String>>(mut self, arg: S) -> Self {
165        self.config.extra_args.push(arg.into());
166        self
167    }
168
169    /// Add multiple extra CLI arguments
170    pub fn extra_args<I, S>(mut self, args: I) -> Self
171    where
172        I: IntoIterator<Item = S>,
173        S: Into<String>,
174    {
175        self.config.extra_args.extend(args.into_iter().map(Into::into));
176        self
177    }
178
179    /// Set health check timeout
180    pub fn health_timeout(mut self, timeout: Duration) -> Self {
181        self.config.health_timeout = timeout;
182        self
183    }
184
185    /// Set health check interval
186    pub fn health_interval(mut self, interval: Duration) -> Self {
187        self.config.health_interval = interval;
188        self
189    }
190
191    /// Set working directory
192    pub fn working_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
193        self.config.working_dir = Some(path.into());
194        self
195    }
196
197    /// Add environment variable
198    pub fn env_var<K, V>(mut self, key: K, value: V) -> Self
199    where
200        K: Into<String>,
201        V: Into<String>,
202    {
203        self.config.env_vars.push((key.into(), value.into()));
204        self
205    }
206
207    /// Set path to mockforge binary
208    pub fn binary_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
209        self.config.binary_path = Some(path.into());
210        self
211    }
212
213    /// Set the management token for the server's control plane
214    pub fn management_token<S: Into<String>>(mut self, token: S) -> Self {
215        self.config.management_token = Some(token.into());
216        self
217    }
218
219    /// Build the ServerConfig
220    pub fn build(self) -> ServerConfig {
221        self.config
222    }
223}
224
225#[cfg(test)]
226mod tests {
227    use super::*;
228
229    #[test]
230    fn test_default_config() {
231        let config = ServerConfig::default();
232        assert_eq!(config.http_port, 0);
233        assert!(config.ws_port.is_none());
234        assert!(config.grpc_port.is_none());
235        assert!(config.admin_port.is_none());
236        assert!(config.metrics_port.is_none());
237        assert!(config.spec_file.is_none());
238        assert!(config.workspace_dir.is_none());
239        assert!(config.profile.is_none());
240        assert!(!config.enable_admin);
241        assert!(!config.enable_metrics);
242        assert!(config.extra_args.is_empty());
243        assert_eq!(config.health_timeout, Duration::from_secs(30));
244        assert_eq!(config.health_interval, Duration::from_millis(100));
245        assert!(config.working_dir.is_none());
246        assert!(config.env_vars.is_empty());
247        assert!(config.binary_path.is_none());
248    }
249
250    #[test]
251    fn test_builder() {
252        let config = ServerConfig::builder()
253            .http_port(3000)
254            .ws_port(3001)
255            .grpc_port(3002)
256            .admin_port(3003)
257            .enable_admin(true)
258            .enable_metrics(true)
259            .profile("test")
260            .extra_arg("--verbose")
261            .build();
262
263        assert_eq!(config.http_port, 3000);
264        assert_eq!(config.ws_port, Some(3001));
265        assert_eq!(config.grpc_port, Some(3002));
266        assert_eq!(config.admin_port, Some(3003));
267        assert!(config.enable_admin);
268        assert!(config.enable_metrics);
269        assert_eq!(config.profile, Some("test".to_string()));
270        assert_eq!(config.extra_args, vec!["--verbose"]);
271    }
272
273    #[test]
274    fn test_builder_metrics_port() {
275        let config = ServerConfig::builder().metrics_port(9090).build();
276        assert_eq!(config.metrics_port, Some(9090));
277    }
278
279    #[test]
280    fn test_builder_spec_file() {
281        let config = ServerConfig::builder().spec_file("/path/to/spec.yaml").build();
282        assert_eq!(config.spec_file, Some(PathBuf::from("/path/to/spec.yaml")));
283    }
284
285    #[test]
286    fn test_builder_workspace_dir() {
287        let config = ServerConfig::builder().workspace_dir("/path/to/workspace").build();
288        assert_eq!(config.workspace_dir, Some(PathBuf::from("/path/to/workspace")));
289    }
290
291    #[test]
292    fn test_builder_extra_args() {
293        let config = ServerConfig::builder().extra_args(vec!["--verbose", "--debug"]).build();
294        assert_eq!(config.extra_args, vec!["--verbose", "--debug"]);
295    }
296
297    #[test]
298    fn test_builder_extra_args_combined() {
299        let config = ServerConfig::builder()
300            .extra_arg("--first")
301            .extra_args(vec!["--second", "--third"])
302            .extra_arg("--fourth")
303            .build();
304        assert_eq!(config.extra_args, vec!["--first", "--second", "--third", "--fourth"]);
305    }
306
307    #[test]
308    fn test_builder_health_timeout() {
309        let config = ServerConfig::builder().health_timeout(Duration::from_secs(60)).build();
310        assert_eq!(config.health_timeout, Duration::from_secs(60));
311    }
312
313    #[test]
314    fn test_builder_health_interval() {
315        let config = ServerConfig::builder().health_interval(Duration::from_millis(500)).build();
316        assert_eq!(config.health_interval, Duration::from_millis(500));
317    }
318
319    #[test]
320    fn test_builder_working_dir() {
321        let config = ServerConfig::builder().working_dir("/tmp/test").build();
322        assert_eq!(config.working_dir, Some(PathBuf::from("/tmp/test")));
323    }
324
325    #[test]
326    fn test_builder_env_var() {
327        let config = ServerConfig::builder()
328            .env_var("RUST_LOG", "debug")
329            .env_var("MY_VAR", "value")
330            .build();
331        assert_eq!(config.env_vars.len(), 2);
332        assert!(config.env_vars.contains(&("RUST_LOG".to_string(), "debug".to_string())));
333        assert!(config.env_vars.contains(&("MY_VAR".to_string(), "value".to_string())));
334    }
335
336    #[test]
337    fn test_builder_binary_path() {
338        let config = ServerConfig::builder().binary_path("/usr/local/bin/mockforge").build();
339        assert_eq!(config.binary_path, Some(PathBuf::from("/usr/local/bin/mockforge")));
340    }
341
342    #[test]
343    fn test_config_clone() {
344        let config = ServerConfig::builder().http_port(3000).profile("test").build();
345
346        let cloned = config.clone();
347        assert_eq!(config.http_port, cloned.http_port);
348        assert_eq!(config.profile, cloned.profile);
349    }
350
351    #[test]
352    fn test_config_debug() {
353        let config = ServerConfig::default();
354        let debug = format!("{:?}", config);
355        assert!(debug.contains("ServerConfig"));
356    }
357
358    #[test]
359    fn test_builder_debug() {
360        let builder = ServerConfigBuilder::default();
361        let debug = format!("{:?}", builder);
362        assert!(debug.contains("ServerConfigBuilder"));
363    }
364
365    #[test]
366    fn test_builder_full_chain() {
367        let config = ServerConfig::builder()
368            .http_port(3000)
369            .ws_port(3001)
370            .grpc_port(3002)
371            .admin_port(3003)
372            .metrics_port(9090)
373            .spec_file("/spec.yaml")
374            .workspace_dir("/workspace")
375            .profile("production")
376            .enable_admin(true)
377            .enable_metrics(true)
378            .extra_arg("--verbose")
379            .health_timeout(Duration::from_secs(60))
380            .health_interval(Duration::from_millis(200))
381            .working_dir("/working")
382            .env_var("KEY", "VALUE")
383            .binary_path("/bin/mockforge")
384            .build();
385
386        assert_eq!(config.http_port, 3000);
387        assert_eq!(config.ws_port, Some(3001));
388        assert_eq!(config.grpc_port, Some(3002));
389        assert_eq!(config.admin_port, Some(3003));
390        assert_eq!(config.metrics_port, Some(9090));
391        assert_eq!(config.spec_file, Some(PathBuf::from("/spec.yaml")));
392        assert_eq!(config.workspace_dir, Some(PathBuf::from("/workspace")));
393        assert_eq!(config.profile, Some("production".to_string()));
394        assert!(config.enable_admin);
395        assert!(config.enable_metrics);
396        assert_eq!(config.health_timeout, Duration::from_secs(60));
397        assert_eq!(config.health_interval, Duration::from_millis(200));
398        assert_eq!(config.working_dir, Some(PathBuf::from("/working")));
399        assert_eq!(config.binary_path, Some(PathBuf::from("/bin/mockforge")));
400    }
401
402    #[test]
403    fn test_server_config_builder_method() {
404        let builder = ServerConfig::builder();
405        let config = builder.http_port(8080).build();
406        assert_eq!(config.http_port, 8080);
407    }
408}