mockforge-test 0.3.249

Test utilities for MockForge - easy integration with Playwright and Vitest
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Configuration for MockForge test servers

use std::path::PathBuf;
use std::time::Duration;

/// Configuration for a MockForge test server
#[derive(Debug, Clone)]
pub struct ServerConfig {
    /// HTTP server port (default: auto-assigned)
    pub http_port: u16,

    /// WebSocket server port (optional)
    pub ws_port: Option<u16>,

    /// gRPC server port (optional)
    pub grpc_port: Option<u16>,

    /// Admin UI port (optional)
    pub admin_port: Option<u16>,

    /// Metrics/Prometheus port (optional)
    pub metrics_port: Option<u16>,

    /// Path to OpenAPI specification file (optional)
    pub spec_file: Option<PathBuf>,

    /// Path to workspace directory (optional)
    pub workspace_dir: Option<PathBuf>,

    /// Profile name for configuration (optional)
    pub profile: Option<String>,

    /// Enable admin UI (default: false)
    pub enable_admin: bool,

    /// Enable metrics endpoint (default: false)
    pub enable_metrics: bool,

    /// Additional CLI arguments
    pub extra_args: Vec<String>,

    /// Health check timeout (default: 30 seconds)
    pub health_timeout: Duration,

    /// Health check interval (default: 100ms)
    pub health_interval: Duration,

    /// Working directory for the server process (optional)
    pub working_dir: Option<PathBuf>,

    /// Environment variables for the server process
    pub env_vars: Vec<(String, String)>,

    /// Path to mockforge binary (optional, will search PATH if not provided)
    pub binary_path: Option<PathBuf>,

    /// Management token for the HTTP data port's control plane (optional).
    ///
    /// When unset, [`crate::MockForgeServer::start`] uses `MOCKFORGE_MANAGEMENT_TOKEN`
    /// from `env_vars` or the parent environment, and otherwise generates a random
    /// token. The token is passed to the child as `MOCKFORGE_MANAGEMENT_TOKEN`.
    pub management_token: Option<String>,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            http_port: 0, // 0 means auto-assign
            ws_port: None,
            grpc_port: None,
            admin_port: None,
            metrics_port: None,
            spec_file: None,
            workspace_dir: None,
            profile: None,
            enable_admin: false,
            enable_metrics: false,
            extra_args: Vec::new(),
            health_timeout: Duration::from_secs(30),
            health_interval: Duration::from_millis(100),
            working_dir: None,
            env_vars: Vec::new(),
            binary_path: None,
            management_token: None,
        }
    }
}

impl ServerConfig {
    /// Create a new builder for ServerConfig
    pub fn builder() -> ServerConfigBuilder {
        ServerConfigBuilder::default()
    }
}

/// Builder for ServerConfig
#[derive(Debug, Default)]
pub struct ServerConfigBuilder {
    config: ServerConfig,
}

impl ServerConfigBuilder {
    /// Set HTTP port (0 for auto-assign)
    pub fn http_port(mut self, port: u16) -> Self {
        self.config.http_port = port;
        self
    }

    /// Set WebSocket port
    pub fn ws_port(mut self, port: u16) -> Self {
        self.config.ws_port = Some(port);
        self
    }

    /// Set gRPC port
    pub fn grpc_port(mut self, port: u16) -> Self {
        self.config.grpc_port = Some(port);
        self
    }

    /// Set admin UI port
    pub fn admin_port(mut self, port: u16) -> Self {
        self.config.admin_port = Some(port);
        self
    }

    /// Set metrics port
    pub fn metrics_port(mut self, port: u16) -> Self {
        self.config.metrics_port = Some(port);
        self
    }

    /// Set OpenAPI specification file
    pub fn spec_file<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.config.spec_file = Some(path.into());
        self
    }

    /// Set workspace directory
    pub fn workspace_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.config.workspace_dir = Some(path.into());
        self
    }

    /// Set profile name
    pub fn profile<S: Into<String>>(mut self, profile: S) -> Self {
        self.config.profile = Some(profile.into());
        self
    }

    /// Enable admin UI
    pub fn enable_admin(mut self, enable: bool) -> Self {
        self.config.enable_admin = enable;
        self
    }

    /// Enable metrics endpoint
    pub fn enable_metrics(mut self, enable: bool) -> Self {
        self.config.enable_metrics = enable;
        self
    }

    /// Add extra CLI argument
    pub fn extra_arg<S: Into<String>>(mut self, arg: S) -> Self {
        self.config.extra_args.push(arg.into());
        self
    }

    /// Add multiple extra CLI arguments
    pub fn extra_args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.config.extra_args.extend(args.into_iter().map(Into::into));
        self
    }

    /// Set health check timeout
    pub fn health_timeout(mut self, timeout: Duration) -> Self {
        self.config.health_timeout = timeout;
        self
    }

    /// Set health check interval
    pub fn health_interval(mut self, interval: Duration) -> Self {
        self.config.health_interval = interval;
        self
    }

    /// Set working directory
    pub fn working_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.config.working_dir = Some(path.into());
        self
    }

    /// Add environment variable
    pub fn env_var<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.config.env_vars.push((key.into(), value.into()));
        self
    }

    /// Set path to mockforge binary
    pub fn binary_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.config.binary_path = Some(path.into());
        self
    }

    /// Set the management token for the server's control plane
    pub fn management_token<S: Into<String>>(mut self, token: S) -> Self {
        self.config.management_token = Some(token.into());
        self
    }

    /// Build the ServerConfig
    pub fn build(self) -> ServerConfig {
        self.config
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = ServerConfig::default();
        assert_eq!(config.http_port, 0);
        assert!(config.ws_port.is_none());
        assert!(config.grpc_port.is_none());
        assert!(config.admin_port.is_none());
        assert!(config.metrics_port.is_none());
        assert!(config.spec_file.is_none());
        assert!(config.workspace_dir.is_none());
        assert!(config.profile.is_none());
        assert!(!config.enable_admin);
        assert!(!config.enable_metrics);
        assert!(config.extra_args.is_empty());
        assert_eq!(config.health_timeout, Duration::from_secs(30));
        assert_eq!(config.health_interval, Duration::from_millis(100));
        assert!(config.working_dir.is_none());
        assert!(config.env_vars.is_empty());
        assert!(config.binary_path.is_none());
    }

    #[test]
    fn test_builder() {
        let config = ServerConfig::builder()
            .http_port(3000)
            .ws_port(3001)
            .grpc_port(3002)
            .admin_port(3003)
            .enable_admin(true)
            .enable_metrics(true)
            .profile("test")
            .extra_arg("--verbose")
            .build();

        assert_eq!(config.http_port, 3000);
        assert_eq!(config.ws_port, Some(3001));
        assert_eq!(config.grpc_port, Some(3002));
        assert_eq!(config.admin_port, Some(3003));
        assert!(config.enable_admin);
        assert!(config.enable_metrics);
        assert_eq!(config.profile, Some("test".to_string()));
        assert_eq!(config.extra_args, vec!["--verbose"]);
    }

    #[test]
    fn test_builder_metrics_port() {
        let config = ServerConfig::builder().metrics_port(9090).build();
        assert_eq!(config.metrics_port, Some(9090));
    }

    #[test]
    fn test_builder_spec_file() {
        let config = ServerConfig::builder().spec_file("/path/to/spec.yaml").build();
        assert_eq!(config.spec_file, Some(PathBuf::from("/path/to/spec.yaml")));
    }

    #[test]
    fn test_builder_workspace_dir() {
        let config = ServerConfig::builder().workspace_dir("/path/to/workspace").build();
        assert_eq!(config.workspace_dir, Some(PathBuf::from("/path/to/workspace")));
    }

    #[test]
    fn test_builder_extra_args() {
        let config = ServerConfig::builder().extra_args(vec!["--verbose", "--debug"]).build();
        assert_eq!(config.extra_args, vec!["--verbose", "--debug"]);
    }

    #[test]
    fn test_builder_extra_args_combined() {
        let config = ServerConfig::builder()
            .extra_arg("--first")
            .extra_args(vec!["--second", "--third"])
            .extra_arg("--fourth")
            .build();
        assert_eq!(config.extra_args, vec!["--first", "--second", "--third", "--fourth"]);
    }

    #[test]
    fn test_builder_health_timeout() {
        let config = ServerConfig::builder().health_timeout(Duration::from_secs(60)).build();
        assert_eq!(config.health_timeout, Duration::from_secs(60));
    }

    #[test]
    fn test_builder_health_interval() {
        let config = ServerConfig::builder().health_interval(Duration::from_millis(500)).build();
        assert_eq!(config.health_interval, Duration::from_millis(500));
    }

    #[test]
    fn test_builder_working_dir() {
        let config = ServerConfig::builder().working_dir("/tmp/test").build();
        assert_eq!(config.working_dir, Some(PathBuf::from("/tmp/test")));
    }

    #[test]
    fn test_builder_env_var() {
        let config = ServerConfig::builder()
            .env_var("RUST_LOG", "debug")
            .env_var("MY_VAR", "value")
            .build();
        assert_eq!(config.env_vars.len(), 2);
        assert!(config.env_vars.contains(&("RUST_LOG".to_string(), "debug".to_string())));
        assert!(config.env_vars.contains(&("MY_VAR".to_string(), "value".to_string())));
    }

    #[test]
    fn test_builder_binary_path() {
        let config = ServerConfig::builder().binary_path("/usr/local/bin/mockforge").build();
        assert_eq!(config.binary_path, Some(PathBuf::from("/usr/local/bin/mockforge")));
    }

    #[test]
    fn test_config_clone() {
        let config = ServerConfig::builder().http_port(3000).profile("test").build();

        let cloned = config.clone();
        assert_eq!(config.http_port, cloned.http_port);
        assert_eq!(config.profile, cloned.profile);
    }

    #[test]
    fn test_config_debug() {
        let config = ServerConfig::default();
        let debug = format!("{:?}", config);
        assert!(debug.contains("ServerConfig"));
    }

    #[test]
    fn test_builder_debug() {
        let builder = ServerConfigBuilder::default();
        let debug = format!("{:?}", builder);
        assert!(debug.contains("ServerConfigBuilder"));
    }

    #[test]
    fn test_builder_full_chain() {
        let config = ServerConfig::builder()
            .http_port(3000)
            .ws_port(3001)
            .grpc_port(3002)
            .admin_port(3003)
            .metrics_port(9090)
            .spec_file("/spec.yaml")
            .workspace_dir("/workspace")
            .profile("production")
            .enable_admin(true)
            .enable_metrics(true)
            .extra_arg("--verbose")
            .health_timeout(Duration::from_secs(60))
            .health_interval(Duration::from_millis(200))
            .working_dir("/working")
            .env_var("KEY", "VALUE")
            .binary_path("/bin/mockforge")
            .build();

        assert_eq!(config.http_port, 3000);
        assert_eq!(config.ws_port, Some(3001));
        assert_eq!(config.grpc_port, Some(3002));
        assert_eq!(config.admin_port, Some(3003));
        assert_eq!(config.metrics_port, Some(9090));
        assert_eq!(config.spec_file, Some(PathBuf::from("/spec.yaml")));
        assert_eq!(config.workspace_dir, Some(PathBuf::from("/workspace")));
        assert_eq!(config.profile, Some("production".to_string()));
        assert!(config.enable_admin);
        assert!(config.enable_metrics);
        assert_eq!(config.health_timeout, Duration::from_secs(60));
        assert_eq!(config.health_interval, Duration::from_millis(200));
        assert_eq!(config.working_dir, Some(PathBuf::from("/working")));
        assert_eq!(config.binary_path, Some(PathBuf::from("/bin/mockforge")));
    }

    #[test]
    fn test_server_config_builder_method() {
        let builder = ServerConfig::builder();
        let config = builder.http_port(8080).build();
        assert_eq!(config.http_port, 8080);
    }
}