"""Tests for agent installer functionality.""" import json import pytest from pathlib import Path from kaizen_agentic.installer import AgentInstaller, ProjectInitializer, InstallationConfig from kaizen_agentic.registry import AgentRegistry @pytest.fixture def test_registry(tmp_path): """Create a test registry with sample agents.""" # Create test agents agents_dir = tmp_path / "agents" agents_dir.mkdir() base_agent = """--- name: base-agent description: Base test agent category: testing --- # Base Agent This is a base agent for testing. """ setup_agent = """--- name: setupRepository description: Repository setup agent category: infrastructure --- # Setup Repository Sets up repository structure. """ todo_agent = """--- name: keepaTodofile description: TODO management agent category: project-management --- # TODO Keeper Manages TODO files. """ changelog_agent = """--- name: keepaChangelog description: Changelog management agent category: project-management --- # Changelog Keeper Manages CHANGELOG files. """ (agents_dir / "agent-base-agent.md").write_text(base_agent) (agents_dir / "agent-setupRepository.md").write_text(setup_agent) (agents_dir / "agent-keepaTodofile.md").write_text(todo_agent) (agents_dir / "agent-keepaChangelog.md").write_text(changelog_agent) return AgentRegistry(agents_dir) def test_install_agents(test_registry, tmp_path): """Test installing agents into a project.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" config = InstallationConfig( target_dir=project_dir, create_backup=False, update_docs=False ) results = installer.install_agents(["base-agent"], config) assert results["base-agent"] == "INSTALLED" assert (project_dir / "agents" / "agent-base-agent.md").exists() def test_install_agents_with_dependencies(test_registry, tmp_path): """Test installing agents with dependency resolution.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" config = InstallationConfig( target_dir=project_dir, create_backup=False, update_docs=False ) # Install an agent that depends on others results = installer.install_agents(["setupRepository"], config) assert "setupRepository" in results assert results["setupRepository"] == "INSTALLED" def test_list_installed_agents(test_registry, tmp_path): """Test listing installed agents.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" # Initially no agents installed = installer.list_installed_agents(project_dir) assert installed == [] # Install some agents config = InstallationConfig(target_dir=project_dir, create_backup=False, update_docs=False) installer.install_agents(["base-agent", "keepaTodofile"], config) # Check installed agents installed = installer.list_installed_agents(project_dir) assert "base-agent" in installed assert "keepaTodofile" in installed assert len(installed) == 2 def test_update_agents(test_registry, tmp_path): """Test updating installed agents.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" # Install initial agents config = InstallationConfig(target_dir=project_dir, create_backup=False, update_docs=False) installer.install_agents(["base-agent"], config) # Update all agents results = installer.update_agents(project_dir) assert "base-agent" in results assert results["base-agent"] == "INSTALLED" # Update specific agents results = installer.update_agents(project_dir, ["base-agent"]) assert results["base-agent"] == "INSTALLED" def test_remove_agents(test_registry, tmp_path): """Test removing agents from a project.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" # Install agents first config = InstallationConfig(target_dir=project_dir, create_backup=False, update_docs=False) installer.install_agents(["base-agent", "keepaTodofile"], config) # Remove an agent results = installer.remove_agents(["base-agent"], project_dir) assert results["base-agent"] == "REMOVED" assert not (project_dir / "agents" / "agent-base-agent.md").exists() # Try to remove non-existent agent results = installer.remove_agents(["non-existent"], project_dir) assert results["non-existent"] == "NOT_FOUND" def test_validate_installation(test_registry, tmp_path): """Test validating agent installation.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" # Validate empty project errors = installer.validate_installation(project_dir) assert "project" in errors assert "No agents directory found" in errors["project"] # Install agents and validate config = InstallationConfig(target_dir=project_dir, create_backup=False, update_docs=False) installer.install_agents(["base-agent"], config) errors = installer.validate_installation(project_dir) assert len(errors) == 0 # Should be valid def test_update_claude_config(test_registry, tmp_path): """Test updating Claude configuration.""" installer = AgentInstaller(test_registry) project_dir = tmp_path / "test_project" claude_config = project_dir / "claude_config.json" config = InstallationConfig( target_dir=project_dir, claude_config_path=claude_config, create_backup=False, update_docs=False ) installer.install_agents(["base-agent"], config) # Check that config was created and updated assert claude_config.exists() with open(claude_config) as f: config_data = json.load(f) assert "agents" in config_data assert "base-agent" in config_data["agents"] assert config_data["agents"]["base-agent"]["enabled"] is True def test_project_initializer(test_registry, tmp_path): """Test project initialization.""" initializer = ProjectInitializer(test_registry) project_dir = tmp_path / "new_project" initializer.init_project( project_dir, template="python-basic", project_name="new_project" ) # Check that project structure was created assert project_dir.exists() assert (project_dir / "src").exists() assert (project_dir / "tests").exists() assert (project_dir / "README.md").exists() assert (project_dir / ".gitignore").exists() assert (project_dir / "pyproject.toml").exists() # Check that agents were installed assert (project_dir / "agents").exists() agents_installed = len([f for f in (project_dir / "agents").glob("agent-*.md")]) assert agents_installed > 0 def test_project_initializer_custom_agents(test_registry, tmp_path): """Test project initialization with custom agents.""" initializer = ProjectInitializer(test_registry) project_dir = tmp_path / "custom_project" initializer.init_project( project_dir, template="python-basic", agent_names=["base-agent", "keepaTodofile"], project_name="custom_project" ) # Check that specific agents were installed assert (project_dir / "agents" / "agent-base-agent.md").exists() assert (project_dir / "agents" / "agent-keepaTodofile.md").exists() def test_installation_config(): """Test InstallationConfig creation.""" config = InstallationConfig( target_dir=Path("/tmp/test"), claude_config_path=Path("/tmp/test/claude.json"), makefile_path=Path("/tmp/test/Makefile"), update_docs=True, create_backup=False ) assert config.target_dir == Path("/tmp/test") assert config.claude_config_path == Path("/tmp/test/claude.json") assert config.makefile_path == Path("/tmp/test/Makefile") assert config.update_docs is True assert config.create_backup is False