Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
chore: add tests for background shutdown
Vanalitef22b8349fc07
diff --git a/src-tauri/src/core/mcp/helpers.rs b/src-tauri/src/core/mcp/helpers.rsindex a3875af85..f2b970583 100644--- a/src-tauri/src/core/mcp/helpers.rs+++ b/src-tauri/src/core/mcp/helpers.rs@@ -32,7 +32,7 @@ pub enum ShutdownContext {}impl ShutdownContext {- fn per_server_timeout(&self) -> Duration {+ pub fn per_server_timeout(&self) -> Duration {match self {Self::AppExit => Duration::from_millis(500),Self::ManualRestart => Duration::from_secs(2),@@ -40,7 +40,7 @@ impl ShutdownContext {}}- fn overall_timeout(&self) -> Duration {+ pub fn overall_timeout(&self) -> Duration {match self {Self::AppExit => Duration::from_millis(1500),Self::ManualRestart => Duration::from_secs(5),diff --git a/src-tauri/src/core/mcp/tests.rs b/src-tauri/src/core/mcp/tests.rsindex 8352eb9a4..0e30ab005 100644--- a/src-tauri/src/core/mcp/tests.rs+++ b/src-tauri/src/core/mcp/tests.rs@@ -228,3 +228,125 @@ fn test_bin_path_construction_windows() {let bun_path_str = bun_path.display().to_string();assert_eq!(bun_path_str, r"C:\Program Files\bin\bun.exe");}++// ============================================================================+// Shutdown Context Tests+// ============================================================================++use super::helpers::ShutdownContext;+use std::time::Duration;++#[test]+fn test_shutdown_context_app_exit_timeouts() {+ let context = ShutdownContext::AppExit;+ assert_eq!(context.per_server_timeout(), Duration::from_millis(500));+ assert_eq!(context.overall_timeout(), Duration::from_millis(1500));+}++#[test]+fn test_shutdown_context_manual_restart_timeouts() {+ let context = ShutdownContext::ManualRestart;+ assert_eq!(context.per_server_timeout(), Duration::from_secs(2));+ assert_eq!(context.overall_timeout(), Duration::from_secs(5));+}++#[test]+fn test_shutdown_context_factory_reset_timeouts() {+ let context = ShutdownContext::FactoryReset;+ assert_eq!(context.per_server_timeout(), Duration::from_secs(5));+ assert_eq!(context.overall_timeout(), Duration::from_secs(10));+}++#[test]+fn test_shutdown_context_overall_greater_than_per_server() {+ for context in [+ ShutdownContext::AppExit,+ ShutdownContext::ManualRestart,+ ShutdownContext::FactoryReset,+ ] {+ assert!(+ context.overall_timeout() > context.per_server_timeout(),+ "Overall timeout should be greater than per-server timeout for {:?}",+ context+ );+ }+}++#[test]+fn test_shutdown_context_is_copy() {+ let context = ShutdownContext::AppExit;+ let copied = context;+ assert!(matches!(context, ShutdownContext::AppExit));+ assert!(matches!(copied, ShutdownContext::AppExit));+}++#[tokio::test]+async fn test_background_cleanup_with_empty_state() {+ use super::helpers::background_cleanup_mcp_servers;++ let app = mock_app();+ let servers_state: SharedMcpServers = Arc::new(Mutex::new(HashMap::new()));+ app.manage(AppState {+ mcp_servers: servers_state.clone(),+ ..Default::default()+ });++ let state = app.state::<AppState>();+ background_cleanup_mcp_servers(app.handle(), &state).await;++ let servers = state.mcp_servers.lock().await;+ assert!(servers.is_empty());++ let active = state.mcp_active_servers.lock().await;+ assert!(active.is_empty());++ let counts = state.mcp_restart_counts.lock().await;+ assert!(counts.is_empty());+}++#[tokio::test]+async fn test_stop_mcp_servers_with_context_empty_servers() {+ use super::helpers::{stop_mcp_servers_with_context, ShutdownContext};++ let app = mock_app();+ let servers_state: SharedMcpServers = Arc::new(Mutex::new(HashMap::new()));+ app.manage(AppState {+ mcp_servers: servers_state.clone(),+ ..Default::default()+ });++ let state = app.state::<AppState>();+ let result =+ stop_mcp_servers_with_context(app.handle(), &state, ShutdownContext::AppExit).await;++ assert!(result.is_ok());+}++#[tokio::test]+async fn test_stop_mcp_servers_prevents_concurrent_shutdown() {+ use super::helpers::{stop_mcp_servers_with_context, ShutdownContext};++ let app = mock_app();+ let servers_state: SharedMcpServers = Arc::new(Mutex::new(HashMap::new()));+ app.manage(AppState {+ mcp_servers: servers_state.clone(),+ ..Default::default()+ });++ let state = app.state::<AppState>();++ {+ let mut shutdown_flag = state.mcp_shutdown_in_progress.lock().await;+ *shutdown_flag = true;+ }++ let result =+ stop_mcp_servers_with_context(app.handle(), &state, ShutdownContext::AppExit).await;++ assert!(result.is_ok());++ {+ let shutdown_flag = state.mcp_shutdown_in_progress.lock().await;+ assert!(*shutdown_flag);+ }+}