use std::time::Duration; use anyhow::Result; use app_test_support::TestAppServer; use akode_app_server_protocol::PluginSearchParams; use akode_app_server_protocol::PluginSearchResponse; use akode_app_server_protocol::PluginSearchScope; use akode_config::types::AuthCredentialsStoreMode; use akode_login::AuthKeyringBackendKind; use akode_login::login_with_api_key; use akode_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; use serde_json::json; use tempfile::TempDir; use tokio::time::timeout; const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); #[tokio::test] async fn plugin_search_returns_local_matches_for_api_key_auth() -> Result<()> { { let codex_home = TempDir::new()?; let repo_root = TempDir::new()?; let curated_root = codex_home.path().join(".tmp/plugins"); let bundled_alpha_root = codex_home .path() .join(".tmp/bundled-marketplaces/openai-bundled-alpha"); write_local_marketplace( &curated_root, "openai-api-curated", "api_marketplace.json", &[LocalPluginFixture { name: "calendar-built-in", display_name: "Calendar Built In", keywords: &[], description: "Built-in calendar", }], )?; write_local_marketplace( repo_root.path(), "personal-tools", "marketplace.json", &[ LocalPluginFixture { name: "developer-tools", display_name: "Developer Tools", keywords: &["api-key"], description: "Manage credentials", }, LocalPluginFixture { name: "japanese-notes", display_name: "日本語メモ", keywords: &[], description: "Japanese notes", }, ], )?; write_local_marketplace( &bundled_alpha_root, "openai-bundled-alpha", "marketplace.json", &[LocalPluginFixture { name: "alpha-calendar", display_name: "Alpha Calendar", keywords: &[], description: "Built-in alpha calendar", }], )?; std::fs::write( codex_home.path().join("config.toml"), "[features]\nplugins = true\n", )?; login_with_api_key( codex_home.path(), "sk-test-key", AuthCredentialsStoreMode::File, AuthKeyringBackendKind::default(), )?; let mut app_server = TestAppServer::builder() .with_codex_home(codex_home.path()) .without_auto_env() .build_initialized_with_timeout(DEFAULT_TIMEOUT) .await?; for (scope, search_term, expected_names) in [ ( Some(PluginSearchScope::Global), "calendar", vec!["calendar-built-in", "alpha-calendar"], ), ( Some(PluginSearchScope::Personal), "API key", vec!["developer-tools"], ), ( Some(PluginSearchScope::Personal), "日本語", vec!["japanese-notes"], ), (Some(PluginSearchScope::Personal), "calendar", Vec::new()), (Some(PluginSearchScope::Workspace), "calendar", Vec::new()), ( None, "calendar", vec!["calendar-built-in", "alpha-calendar"], ), (None, "!!!", Vec::new()), ] { let request_id = app_server .send_plugin_search_request(PluginSearchParams { search_term: search_term.to_string(), scope, cwds: Some(vec![ AbsolutePathBuf::try_from(repo_root.path())?, AbsolutePathBuf::try_from(bundled_alpha_root.as_path())?, ]), cursor: None, limit: None, }) .await?; let response: PluginSearchResponse = timeout(DEFAULT_TIMEOUT, app_server.read_response(request_id)).await??; assert_eq!( response .data .iter() .map(|result| result.plugin.name.as_str()) .collect::>(), expected_names ); assert_eq!(response.next_cursor, None); } } Ok(()) } struct LocalPluginFixture<'a> { name: &'a str, display_name: &'a str, keywords: &'a [&'a str], description: &'a str, } fn write_local_marketplace( root: &std::path::Path, marketplace_name: &str, manifest_name: &str, plugins: &[LocalPluginFixture<'_>], ) -> Result { std::fs::create_dir_all(root.join(".git"))?; std::fs::create_dir_all(root.join(".agents/plugins"))?; let marketplace_path = root.join(".agents/plugins").join(manifest_name); let entries = plugins .iter() .map(|plugin| { json!({ "name": plugin.name, "source": { "source": "local", "path": format!("./plugins/{}", plugin.name), }, }) }) .collect::>(); std::fs::write( &marketplace_path, serde_json::to_string(&json!({ "name": marketplace_name, "plugins": entries, }))?, )?; for plugin in plugins { let plugin_manifest = root.join("plugins").join(plugin.name).join(".codex-plugin"); std::fs::create_dir_all(&plugin_manifest)?; std::fs::write( plugin_manifest.join("plugin.json"), serde_json::to_string(&json!({ "name": plugin.name, "keywords": plugin.keywords, "interface": { "displayName": plugin.display_name, "shortDescription": plugin.description, }, }))?, )?; } Ok(AbsolutePathBuf::try_from(marketplace_path)?) }