use std::fs;
use std::path::Path;
use tempfile::TempDir;
use driven::modules::ModuleManager;
fn create_test_module(dir: &Path, id: &str, name: &str, version: &str) {
fs::create_dir_all(dir).unwrap();
let manifest = format!(
r#"# Test Module
id|{}
nm|{}
v|{}
desc|A test module for integration testing
agent.0|test-agent
workflow.0|test-workflow
"#,
id, name, version
);
fs::write(dir.join("module.dx"), manifest).unwrap();
}
#[test]
fn test_module_manager_install_and_list() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_path = temp_dir.path().join("test-module");
create_test_module(&module_path, "test-module", "Test Module", "1.0.0");
let mut manager = ModuleManager::new(®istry_path);
manager.install(&module_path).unwrap();
let installed = manager.list_installed();
assert_eq!(installed.len(), 1);
assert_eq!(installed[0].id, "test-module");
assert_eq!(installed[0].name, "Test Module");
assert_eq!(installed[0].version, "1.0.0");
}
#[test]
fn test_module_manager_uninstall() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_path = temp_dir.path().join("test-module");
create_test_module(&module_path, "test-module", "Test Module", "1.0.0");
let mut manager = ModuleManager::new(®istry_path);
manager.install(&module_path).unwrap();
assert_eq!(manager.list_installed().len(), 1);
manager.uninstall("test-module").unwrap();
assert_eq!(manager.list_installed().len(), 0);
}
#[test]
fn test_module_manager_update() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_v1_path = temp_dir.path().join("test-module-v1");
let module_v2_path = temp_dir.path().join("test-module-v2");
create_test_module(&module_v1_path, "test-module", "Test Module", "1.0.0");
create_test_module(&module_v2_path, "test-module", "Test Module", "2.0.0");
let mut manager = ModuleManager::new(®istry_path);
manager.install(&module_v1_path).unwrap();
let module = manager.get("test-module").unwrap();
assert_eq!(module.version, "1.0.0");
manager.update("test-module", &module_v2_path).unwrap();
let module = manager.get("test-module").unwrap();
assert_eq!(module.version, "2.0.0");
}
#[test]
fn test_module_manager_dependency_check() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let base_module_path = temp_dir.path().join("base-module");
let dependent_module_path = temp_dir.path().join("dependent-module");
create_test_module(&base_module_path, "base-module", "Base Module", "1.0.0");
fs::create_dir_all(&dependent_module_path).unwrap();
let manifest = r#"# Dependent Module
id|dependent-module
nm|Dependent Module
v|1.0.0
desc|A module that depends on base-module
dep.base-module|^1.0.0
"#;
fs::write(dependent_module_path.join("module.dx"), manifest).unwrap();
let mut manager = ModuleManager::new(®istry_path);
let result = manager.install(&dependent_module_path);
assert!(result.is_err());
manager.install(&base_module_path).unwrap();
let result = manager.install(&dependent_module_path);
assert!(result.is_ok());
}
#[test]
fn test_module_namespacing() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_path = temp_dir.path().join("test-module");
create_test_module(&module_path, "my-module", "My Module", "1.0.0");
let mut manager = ModuleManager::new(®istry_path);
manager.install(&module_path).unwrap();
let agents = manager.get_all_agents();
assert!(agents.iter().any(|a| a == "my-module:test-agent"));
let workflows = manager.get_all_workflows();
assert!(workflows.iter().any(|w| w == "my-module:test-workflow"));
}
#[test]
fn test_module_isolation_disabled() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_path = temp_dir.path().join("test-module");
create_test_module(&module_path, "my-module", "My Module", "1.0.0");
let mut manager = ModuleManager::new(®istry_path);
manager.disable_isolation();
manager.install(&module_path).unwrap();
let agents = manager.get_all_agents();
assert!(agents.iter().any(|a| a == "test-agent"));
assert!(!agents.iter().any(|a| a.contains(':')));
}
#[test]
fn test_module_persistence() {
let temp_dir = TempDir::new().unwrap();
let registry_path = temp_dir.path().join("modules");
let module_path = temp_dir.path().join("test-module");
create_test_module(&module_path, "test-module", "Test Module", "1.0.0");
{
let mut manager = ModuleManager::new(®istry_path);
manager.install(&module_path).unwrap();
}
let mut manager2 = ModuleManager::new(®istry_path);
manager2.load().unwrap();
let installed = manager2.list_installed();
assert_eq!(installed.len(), 1);
assert_eq!(installed[0].id, "test-module");
}