use std::collections::HashMap; use std::path::Path; use std::sync::Arc; use akode_config::types::PluginConfig; use akode_config::types::PluginsConfigToml; use akode_core_plugins::store::PluginStore; use akode_extension_api::ContextContributor; use akode_extension_api::ExtensionData; use akode_extension_api::PromptSlot; use akode_protocol::models::ContentItem; use pretty_assertions::assert_eq; use super::CONTENT_KIND; use super::INSTRUCTIONS_FILE; use super::MemoriaExtension; use super::MemoriaThreadState; use super::ReadyFuture; use super::ServerReadiness; use super::instructions_path_from; const INSTRUCTIONS: &str = "## Long-term memory (memoria)\n\nCall `memory_recall` first."; /// Fake MCP catalog: only the listed servers are ready. struct ReadyServers(Vec<&'static str>); impl ServerReadiness for ReadyServers { fn is_ready<'a>(&'a self, server: &'a str) -> ReadyFuture<'a> { let ready = self.0.contains(&server); Box::pin(async move { ready }) } } fn plugin_config(enabled: Option) -> PluginConfig { serde_json::from_value(serde_json::json!({ "enabled": enabled })) .expect("plugin config should deserialize") } /// Installs `memoria@imeai` in a temporary AKODE_HOME and returns its instructions path. fn install_plugin(codex_home: &Path, with_instructions: bool) -> std::path::PathBuf { let root = codex_home.join("plugins/cache/imeai/memoria/0.1.0"); std::fs::create_dir_all(root.join(".codex-plugin")).expect("create plugin root"); let path = root.join(INSTRUCTIONS_FILE); if with_instructions { std::fs::write(&path, format!("\n{INSTRUCTIONS}\n")).expect("write instructions"); } path } fn thread_store( instructions_path: Option, ready: Option>, ) -> ExtensionData { let store = ExtensionData::new("thread"); store.insert(MemoriaThreadState { instructions_path, servers: ready.map(|ready| Arc::new(ReadyServers(ready)) as Arc), }); store } async fn fragments(thread_store: &ExtensionData) -> Vec { MemoriaExtension .contribute_thread_context(&ExtensionData::new("session"), thread_store) .await } #[test] fn instructions_path_requires_an_enabled_installed_plugin() { let codex_home = tempfile::tempdir().expect("tempdir"); let store = PluginStore::new(codex_home.path().to_path_buf()); let policy = PluginsConfigToml::default(); let enabled = HashMap::from([("memoria@imeai".to_string(), plugin_config(Some(true)))]); // Configured but not installed yet. assert_eq!(instructions_path_from(&enabled, &policy, &store), None); let expected = install_plugin(codex_home.path(), /*with_instructions*/ true); assert_eq!( instructions_path_from(&enabled, &policy, &store), Some(expected.clone()) ); // Omitting `enabled` keeps an installed plugin active, like the plugin loader. let inherited = HashMap::from([("memoria@imeai".to_string(), plugin_config(None))]); assert_eq!( instructions_path_from(&inherited, &policy, &store), Some(expected) ); let disabled = HashMap::from([("memoria@imeai".to_string(), plugin_config(Some(false)))]); assert_eq!(instructions_path_from(&disabled, &policy, &store), None); let other = HashMap::from([("other@imeai".to_string(), plugin_config(Some(true)))]); assert_eq!(instructions_path_from(&other, &policy, &store), None); assert_eq!( instructions_path_from(&HashMap::new(), &policy, &store), None ); } #[tokio::test] async fn injects_instructions_when_plugin_enabled_and_server_ready() { let codex_home = tempfile::tempdir().expect("tempdir"); let path = install_plugin(codex_home.path(), /*with_instructions*/ true); let fragments = fragments(&thread_store(Some(path), Some(vec!["memoria"]))).await; assert_eq!(fragments.len(), 1); assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy); assert_eq!(fragments[0].content_kind().0, CONTENT_KIND); assert_eq!( fragments[0].content(), &ContentItem::InputText { text: INSTRUCTIONS.to_string() } ); } #[tokio::test] async fn skips_instructions_when_plugin_disabled() { assert!( fragments(&thread_store( /*instructions_path*/ None, Some(vec!["memoria"]) )) .await .is_empty() ); assert!(fragments(&ExtensionData::new("thread")).await.is_empty()); } #[tokio::test] async fn skips_instructions_when_server_not_connected() { let codex_home = tempfile::tempdir().expect("tempdir"); let path = install_plugin(codex_home.path(), /*with_instructions*/ true); assert!( fragments(&thread_store(Some(path.clone()), Some(vec!["other"]))) .await .is_empty() ); assert!( fragments(&thread_store(Some(path), /*ready*/ None)) .await .is_empty() ); } #[tokio::test] async fn skips_instructions_when_file_missing() { let codex_home = tempfile::tempdir().expect("tempdir"); let path = install_plugin(codex_home.path(), /*with_instructions*/ false); assert!( fragments(&thread_store(Some(path), Some(vec!["memoria"]))) .await .is_empty() ); }