use mcp_runner::config::ServerConfig;
use mcp_runner::error::Result;
use mcp_runner::server::lifecycle::{ServerLifecycleEvent, ServerLifecycleManager};
use mcp_runner::server::monitor::{ServerHealth, ServerMonitor, ServerMonitorConfig};
use mcp_runner::server::{ServerProcess, ServerStatus};
use std::collections::HashMap;
use std::sync::Arc;
#[tokio::test]
async fn test_server_process() -> Result<()> {
let config = ServerConfig {
command: "echo".to_string(),
args: vec!["hello".to_string()],
env: HashMap::new(),
};
let mut server = ServerProcess::new("echo".to_string(), config);
assert_eq!(server.status(), ServerStatus::Stopped);
server.start().await?;
assert_eq!(server.status(), ServerStatus::Running);
server.stop().await?;
assert_eq!(server.status(), ServerStatus::Stopped);
Ok(())
}
#[tokio::test]
async fn test_server_io() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn test_server_lifecycle() -> Result<()> {
let lifecycle_manager = ServerLifecycleManager::new();
let config = ServerConfig {
command: "echo".to_string(),
args: vec!["hello".to_string()],
env: HashMap::new(),
};
let server = ServerProcess::new("test-server".to_string(), config);
let id = server.id();
lifecycle_manager.record_event(
id,
"test-server".to_string(),
ServerLifecycleEvent::Started,
Some("Test start".to_string()),
)?;
lifecycle_manager.record_event(
id,
"test-server".to_string(),
ServerLifecycleEvent::Stopped,
Some("Test stop".to_string()),
)?;
let events = lifecycle_manager.get_server_events(id, None)?;
assert_eq!(events.len(), 2);
assert_eq!(events[0].event, ServerLifecycleEvent::Stopped);
assert_eq!(events[1].event, ServerLifecycleEvent::Started);
let status = lifecycle_manager.get_status(id)?;
assert_eq!(status, ServerStatus::Stopped);
Ok(())
}
#[tokio::test]
async fn test_server_monitor() -> Result<()> {
let lifecycle_manager = Arc::new(ServerLifecycleManager::new());
let config = ServerMonitorConfig::default();
let mut monitor = ServerMonitor::new(Arc::clone(&lifecycle_manager), config);
monitor.start()?;
let server_config = ServerConfig {
command: "echo".to_string(),
args: vec!["hello".to_string()],
env: HashMap::new(),
};
let server = ServerProcess::new("test-server".to_string(), server_config);
let id = server.id();
lifecycle_manager.record_event(
id,
"test-server".to_string(),
ServerLifecycleEvent::Started,
None,
)?;
let health = monitor.check_health(id, "test-server").await?;
assert_eq!(health, ServerHealth::Healthy);
lifecycle_manager.record_event(
id,
"test-server".to_string(),
ServerLifecycleEvent::Failed,
None,
)?;
let health = monitor.check_health(id, "test-server").await?;
assert_eq!(health, ServerHealth::Unhealthy);
monitor.stop()?;
Ok(())
}