use std::time::Duration; use anyhow::Result; use app_test_support::TestAppServer; use codex_app_server_protocol::PluginUninstallParams; use codex_app_server_protocol::PluginUninstallResponse; use codex_app_server_protocol::RequestId; use pretty_assertions::assert_eq; use tempfile::TempDir; use tokio::time::timeout; const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); #[tokio::test] async fn plugin_uninstall_removes_plugin_cache_and_config_entry() -> Result<()> { let codex_home = TempDir::new()?; write_installed_plugin(&codex_home, "debug", "sample-plugin")?; std::fs::write( codex_home.path().join("config.toml"), r#"[features] plugins = true [plugins."sample-plugin@debug"] enabled = true "#, )?; let mut mcp = TestAppServer::builder() .with_codex_home(codex_home.path()) .build_initialized_with_timeout(DEFAULT_TIMEOUT) .await?; let response = uninstall_plugin(&mut mcp, "sample-plugin@debug").await?; assert_eq!(response, PluginUninstallResponse {}); assert!( !codex_home .path() .join("plugins/cache/debug/sample-plugin") .exists() ); let config = std::fs::read_to_string(codex_home.path().join("config.toml"))?; assert!(!config.contains(r#"[plugins."sample-plugin@debug"]"#)); let response = uninstall_plugin(&mut mcp, "sample-plugin@debug").await?; assert_eq!(response, PluginUninstallResponse {}); Ok(()) } #[tokio::test] async fn plugin_uninstall_rejects_plugin_id_with_spaces() -> Result<()> { let codex_home = TempDir::new()?; write_plugins_enabled_config(codex_home.path())?; let mut mcp = TestAppServer::builder() .with_codex_home(codex_home.path()) .build_initialized_with_timeout(DEFAULT_TIMEOUT) .await?; let request_id = mcp .send_plugin_uninstall_request(PluginUninstallParams { plugin_id: "sample plugin".to_string(), }) .await?; let err = timeout( DEFAULT_TIMEOUT, mcp.read_stream_until_error_message(RequestId::Integer(request_id)), ) .await??; assert_eq!(err.error.code, -32600); assert!(err.error.message.contains("invalid plugin key")); Ok(()) } #[tokio::test] async fn plugin_uninstall_rejects_path_traversal_plugin_id() -> Result<()> { let codex_home = TempDir::new()?; write_plugins_enabled_config(codex_home.path())?; let mut mcp = TestAppServer::builder() .with_codex_home(codex_home.path()) .build_initialized_with_timeout(DEFAULT_TIMEOUT) .await?; let request_id = mcp .send_plugin_uninstall_request(PluginUninstallParams { plugin_id: "linear/../../oops".to_string(), }) .await?; let err = timeout( DEFAULT_TIMEOUT, mcp.read_stream_until_error_message(RequestId::Integer(request_id)), ) .await??; assert_eq!(err.error.code, -32600); assert!(err.error.message.contains("invalid plugin key")); Ok(()) } #[tokio::test] async fn plugin_uninstall_rejects_empty_plugin_id() -> Result<()> { let codex_home = TempDir::new()?; write_plugins_enabled_config(codex_home.path())?; let mut mcp = TestAppServer::builder() .with_codex_home(codex_home.path()) .build_initialized_with_timeout(DEFAULT_TIMEOUT) .await?; let request_id = mcp .send_plugin_uninstall_request(PluginUninstallParams { plugin_id: String::new(), }) .await?; let err = timeout( DEFAULT_TIMEOUT, mcp.read_stream_until_error_message(RequestId::Integer(request_id)), ) .await??; assert_eq!(err.error.code, -32600); assert!(err.error.message.contains("invalid plugin key")); Ok(()) } async fn uninstall_plugin( mcp: &mut TestAppServer, plugin_id: &str, ) -> Result { let request_id = mcp .send_plugin_uninstall_request(PluginUninstallParams { plugin_id: plugin_id.to_string(), }) .await?; timeout(DEFAULT_TIMEOUT, mcp.read_response(request_id)).await? } fn write_installed_plugin( codex_home: &TempDir, marketplace_name: &str, plugin_name: &str, ) -> Result<()> { let plugin_root = codex_home .path() .join("plugins/cache") .join(marketplace_name) .join(plugin_name) .join("local/.codex-plugin"); std::fs::create_dir_all(&plugin_root)?; std::fs::write( plugin_root.join("plugin.json"), format!(r#"{{"name":"{plugin_name}"}}"#), )?; Ok(()) } fn write_plugins_enabled_config(codex_home: &std::path::Path) -> std::io::Result<()> { std::fs::write( codex_home.join("config.toml"), r#"[features] plugins = true "#, ) }