mockforge-ui 0.3.88

Admin UI for MockForge - web-based interface for managing mock servers
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Health check endpoints for Kubernetes and cloud deployments

use axum::{extract::State, response::Json};
use chrono;
use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::handlers::AdminState;

/// Health check response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
    pub status: HealthStatus,
    pub timestamp: u64,
    pub version: String,
    pub uptime_seconds: u64,
    pub checks: Vec<HealthCheck>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheck {
    pub name: String,
    pub status: HealthStatus,
    pub message: Option<String>,
    pub duration_ms: u64,
}

/// Liveness probe - Is the application running?
/// Returns 200 if the application is alive, even if degraded
pub async fn liveness_probe(State(state): State<AdminState>) -> Json<HealthResponse> {
    let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();

    let uptime_seconds = (chrono::Utc::now() - state.start_time).num_seconds() as u64;

    let response = HealthResponse {
        status: HealthStatus::Healthy,
        timestamp,
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds,
        checks: vec![],
    };

    Json(response)
}

/// Readiness probe - Is the application ready to serve traffic?
/// Returns 200 only if all critical services are ready
pub async fn readiness_probe(State(state): State<AdminState>) -> Json<HealthResponse> {
    let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();

    let mut checks = vec![];
    let mut overall_status = HealthStatus::Healthy;

    // Check HTTP server
    if state.http_server_addr.is_some() {
        checks.push(HealthCheck {
            name: "http_server".to_string(),
            status: HealthStatus::Healthy,
            message: Some("HTTP server is running".to_string()),
            duration_ms: 0,
        });
    } else {
        checks.push(HealthCheck {
            name: "http_server".to_string(),
            status: HealthStatus::Degraded,
            message: Some("HTTP server is not enabled".to_string()),
            duration_ms: 0,
        });
    }

    // Check WebSocket server
    if state.ws_server_addr.is_some() {
        checks.push(HealthCheck {
            name: "websocket_server".to_string(),
            status: HealthStatus::Healthy,
            message: Some("WebSocket server is running".to_string()),
            duration_ms: 0,
        });
    }

    // Check gRPC server
    if state.grpc_server_addr.is_some() {
        checks.push(HealthCheck {
            name: "grpc_server".to_string(),
            status: HealthStatus::Healthy,
            message: Some("gRPC server is running".to_string()),
            duration_ms: 0,
        });
    }

    // Check if any critical service failed
    let critical_failures = checks.iter().any(|c| {
        matches!(c.status, HealthStatus::Unhealthy)
            && (c.name == "http_server" || c.name == "grpc_server")
    });

    if critical_failures {
        overall_status = HealthStatus::Unhealthy;
    }

    let uptime_seconds = (chrono::Utc::now() - state.start_time).num_seconds() as u64;

    let response = HealthResponse {
        status: overall_status,
        timestamp,
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds,
        checks,
    };

    Json(response)
}

/// Startup probe - Has the application completed initialization?
/// Returns 200 when the application is fully started
pub async fn startup_probe(State(state): State<AdminState>) -> Json<HealthResponse> {
    let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();

    // For now, consider started if admin UI is running
    let status = if state.api_enabled {
        HealthStatus::Healthy
    } else {
        HealthStatus::Unhealthy
    };

    let uptime_seconds = (chrono::Utc::now() - state.start_time).num_seconds() as u64;

    let response = HealthResponse {
        status: status.clone(),
        timestamp,
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds,
        checks: vec![HealthCheck {
            name: "initialization".to_string(),
            status: status.clone(),
            message: Some("Application initialized".to_string()),
            duration_ms: 0,
        }],
    };

    Json(response)
}

/// Deep health check - Comprehensive system health
/// Checks all subsystems and dependencies
pub async fn deep_health_check(State(state): State<AdminState>) -> Json<HealthResponse> {
    let start = SystemTime::now();
    let timestamp = start.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();

    let mut checks = vec![];
    let mut overall_status = HealthStatus::Healthy;

    // Check Admin UI status
    checks.push(HealthCheck {
        name: "admin_ui".to_string(),
        status: if state.api_enabled {
            HealthStatus::Healthy
        } else {
            HealthStatus::Degraded
        },
        message: Some(if state.api_enabled {
            format!("Admin UI is accessible on port {}", state.admin_port)
        } else {
            "Admin UI API endpoints are disabled".to_string()
        }),
        duration_ms: 0,
    });

    // Check all servers with addresses
    let servers = vec![
        ("http_server", state.http_server_addr.as_ref().map(|a| a.to_string())),
        ("websocket_server", state.ws_server_addr.as_ref().map(|a| a.to_string())),
        ("grpc_server", state.grpc_server_addr.as_ref().map(|a| a.to_string())),
        ("graphql_server", state.graphql_server_addr.as_ref().map(|a| a.to_string())),
    ];

    for (name, addr_opt) in servers {
        if let Some(addr) = addr_opt {
            checks.push(HealthCheck {
                name: name.to_string(),
                status: HealthStatus::Healthy,
                message: Some(format!("{} is running on {}", name, addr)),
                duration_ms: 0,
            });
        } else {
            checks.push(HealthCheck {
                name: name.to_string(),
                status: HealthStatus::Degraded,
                message: Some(format!("{} is not enabled", name)),
                duration_ms: 0,
            });
        }
    }

    // Configuration is loaded and valid (no separate check needed as it's validated at startup)

    // Check metrics
    let metrics = state.metrics.read().await;
    let total_requests = metrics.total_requests;
    drop(metrics);

    checks.push(HealthCheck {
        name: "metrics".to_string(),
        status: HealthStatus::Healthy,
        message: Some(format!("Processed {} requests", total_requests)),
        duration_ms: 0,
    });

    // Calculate overall duration
    let _duration = SystemTime::now().duration_since(start).unwrap().as_millis() as u64;

    // Determine overall status - unhealthy if any critical service is unhealthy
    let critical_failures = checks.iter().any(|c| {
        matches!(c.status, HealthStatus::Unhealthy)
            && (c.name == "http_server" || c.name == "admin_ui")
    });
    if critical_failures {
        overall_status = HealthStatus::Unhealthy;
    } else if checks.iter().any(|c| matches!(c.status, HealthStatus::Degraded)) {
        overall_status = HealthStatus::Degraded;
    }

    let uptime_seconds = (chrono::Utc::now() - state.start_time).num_seconds() as u64;

    let response = HealthResponse {
        status: overall_status,
        timestamp,
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds,
        checks,
    };

    Json(response)
}

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

    // ==================== HealthStatus Tests ====================

    #[test]
    fn test_health_status_serialization_healthy() {
        let status = HealthStatus::Healthy;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""healthy""#);
    }

    #[test]
    fn test_health_status_serialization_degraded() {
        let status = HealthStatus::Degraded;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""degraded""#);
    }

    #[test]
    fn test_health_status_serialization_unhealthy() {
        let status = HealthStatus::Unhealthy;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""unhealthy""#);
    }

    #[test]
    fn test_health_status_deserialization() {
        let healthy: HealthStatus = serde_json::from_str(r#""healthy""#).unwrap();
        assert!(matches!(healthy, HealthStatus::Healthy));

        let degraded: HealthStatus = serde_json::from_str(r#""degraded""#).unwrap();
        assert!(matches!(degraded, HealthStatus::Degraded));

        let unhealthy: HealthStatus = serde_json::from_str(r#""unhealthy""#).unwrap();
        assert!(matches!(unhealthy, HealthStatus::Unhealthy));
    }

    #[test]
    fn test_health_status_clone() {
        let status = HealthStatus::Healthy;
        let cloned = status.clone();
        assert!(matches!(cloned, HealthStatus::Healthy));
    }

    #[test]
    fn test_health_status_debug() {
        let status = HealthStatus::Healthy;
        let debug = format!("{:?}", status);
        assert_eq!(debug, "Healthy");
    }

    // ==================== HealthResponse Tests ====================

    #[test]
    fn test_health_response_structure() {
        let response = HealthResponse {
            status: HealthStatus::Healthy,
            timestamp: 1234567890,
            version: "1.0.0".to_string(),
            uptime_seconds: 3600,
            checks: vec![],
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("healthy"));
        assert!(json.contains("1.0.0"));
    }

    #[test]
    fn test_health_response_with_checks() {
        let check = HealthCheck {
            name: "database".to_string(),
            status: HealthStatus::Healthy,
            message: Some("Connected".to_string()),
            duration_ms: 5,
        };

        let response = HealthResponse {
            status: HealthStatus::Healthy,
            timestamp: 1234567890,
            version: "1.0.0".to_string(),
            uptime_seconds: 3600,
            checks: vec![check],
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("database"));
        assert!(json.contains("Connected"));
    }

    #[test]
    fn test_health_response_deserialization() {
        let json = r#"{
            "status": "healthy",
            "timestamp": 1234567890,
            "version": "1.0.0",
            "uptime_seconds": 3600,
            "checks": []
        }"#;

        let response: HealthResponse = serde_json::from_str(json).unwrap();
        assert!(matches!(response.status, HealthStatus::Healthy));
        assert_eq!(response.timestamp, 1234567890);
        assert_eq!(response.version, "1.0.0");
        assert_eq!(response.uptime_seconds, 3600);
        assert!(response.checks.is_empty());
    }

    #[test]
    fn test_health_response_clone() {
        let response = HealthResponse {
            status: HealthStatus::Degraded,
            timestamp: 1234567890,
            version: "2.0.0".to_string(),
            uptime_seconds: 7200,
            checks: vec![],
        };

        let cloned = response.clone();
        assert!(matches!(cloned.status, HealthStatus::Degraded));
        assert_eq!(cloned.version, "2.0.0");
        assert_eq!(cloned.uptime_seconds, 7200);
    }

    // ==================== HealthCheck Tests ====================

    #[test]
    fn test_health_check_creation() {
        let check = HealthCheck {
            name: "redis".to_string(),
            status: HealthStatus::Healthy,
            message: Some("Connection pool active".to_string()),
            duration_ms: 10,
        };

        assert_eq!(check.name, "redis");
        assert!(matches!(check.status, HealthStatus::Healthy));
        assert_eq!(check.message, Some("Connection pool active".to_string()));
        assert_eq!(check.duration_ms, 10);
    }

    #[test]
    fn test_health_check_no_message() {
        let check = HealthCheck {
            name: "cache".to_string(),
            status: HealthStatus::Degraded,
            message: None,
            duration_ms: 0,
        };

        assert!(check.message.is_none());
    }

    #[test]
    fn test_health_check_serialization() {
        let check = HealthCheck {
            name: "test".to_string(),
            status: HealthStatus::Unhealthy,
            message: Some("Error".to_string()),
            duration_ms: 100,
        };

        let json = serde_json::to_string(&check).unwrap();
        assert!(json.contains("test"));
        assert!(json.contains("unhealthy"));
        assert!(json.contains("Error"));
        assert!(json.contains("100"));
    }

    #[test]
    fn test_health_check_deserialization() {
        let json = r#"{
            "name": "database",
            "status": "healthy",
            "message": "OK",
            "duration_ms": 5
        }"#;

        let check: HealthCheck = serde_json::from_str(json).unwrap();
        assert_eq!(check.name, "database");
        assert!(matches!(check.status, HealthStatus::Healthy));
        assert_eq!(check.message, Some("OK".to_string()));
        assert_eq!(check.duration_ms, 5);
    }

    #[test]
    fn test_health_check_clone() {
        let check = HealthCheck {
            name: "api".to_string(),
            status: HealthStatus::Healthy,
            message: Some("Active".to_string()),
            duration_ms: 20,
        };

        let cloned = check.clone();
        assert_eq!(cloned.name, check.name);
        assert_eq!(cloned.duration_ms, check.duration_ms);
    }

    // ==================== Complex Response Tests ====================

    #[test]
    fn test_health_response_multiple_checks() {
        let checks = vec![
            HealthCheck {
                name: "http_server".to_string(),
                status: HealthStatus::Healthy,
                message: Some("Running on port 8080".to_string()),
                duration_ms: 0,
            },
            HealthCheck {
                name: "grpc_server".to_string(),
                status: HealthStatus::Healthy,
                message: Some("Running on port 50051".to_string()),
                duration_ms: 1,
            },
            HealthCheck {
                name: "websocket_server".to_string(),
                status: HealthStatus::Degraded,
                message: Some("High latency".to_string()),
                duration_ms: 50,
            },
        ];

        let response = HealthResponse {
            status: HealthStatus::Degraded,
            timestamp: 1234567890,
            version: "1.0.0".to_string(),
            uptime_seconds: 3600,
            checks,
        };

        assert_eq!(response.checks.len(), 3);
        assert!(matches!(response.status, HealthStatus::Degraded));
    }

    #[test]
    fn test_health_response_roundtrip() {
        let original = HealthResponse {
            status: HealthStatus::Healthy,
            timestamp: 9999999999,
            version: "3.0.0".to_string(),
            uptime_seconds: 86400,
            checks: vec![HealthCheck {
                name: "test".to_string(),
                status: HealthStatus::Healthy,
                message: Some("Test message".to_string()),
                duration_ms: 42,
            }],
        };

        let json = serde_json::to_string(&original).unwrap();
        let deserialized: HealthResponse = serde_json::from_str(&json).unwrap();

        assert!(matches!(deserialized.status, HealthStatus::Healthy));
        assert_eq!(deserialized.timestamp, original.timestamp);
        assert_eq!(deserialized.version, original.version);
        assert_eq!(deserialized.uptime_seconds, original.uptime_seconds);
        assert_eq!(deserialized.checks.len(), 1);
    }

    #[test]
    fn test_health_check_debug() {
        let check = HealthCheck {
            name: "debug_test".to_string(),
            status: HealthStatus::Healthy,
            message: None,
            duration_ms: 0,
        };

        let debug = format!("{:?}", check);
        assert!(debug.contains("debug_test"));
        assert!(debug.contains("Healthy"));
    }

    #[test]
    fn test_health_response_with_zero_uptime() {
        let response = HealthResponse {
            status: HealthStatus::Healthy,
            timestamp: 0,
            version: "0.0.1".to_string(),
            uptime_seconds: 0,
            checks: vec![],
        };

        assert_eq!(response.uptime_seconds, 0);
        assert_eq!(response.timestamp, 0);
    }

    #[test]
    fn test_health_check_high_duration() {
        let check = HealthCheck {
            name: "slow_check".to_string(),
            status: HealthStatus::Degraded,
            message: Some("Timeout warning".to_string()),
            duration_ms: 30000, // 30 seconds
        };

        assert_eq!(check.duration_ms, 30000);
    }
}