Files
kaizen-agentic/tests/test_registry.py
tegwick 38965c1d4a Implement hybrid agent distribution system
Complete implementation of the agent distribution framework including:

CORE INFRASTRUCTURE:
- AgentRegistry: Agent discovery, categorization, and dependency management
- AgentInstaller: Agent installation, updates, and removal with safety measures
- ProjectInitializer: Template-based project initialization with agent integration
- CLI Tool: Comprehensive kaizen-agentic command-line interface

DISTRIBUTION FEATURES:
- Python package distribution with console script entry point
- Agent categorization (project-management, development-process, code-quality, etc.)
- Project templates (python-basic, python-web, python-cli, python-data, comprehensive)
- Dependency resolution and validation
- Idempotent operations with backup and rollback support

CLI COMMANDS:
- kaizen-agentic init: Initialize new projects with agents
- kaizen-agentic install/update/remove: Manage agents in existing projects
- kaizen-agentic list/status/validate: Discovery and maintenance
- kaizen-agentic templates: Project template management

INTEGRATION & DOCUMENTATION:
- Makefile targets for agent management (list-agents, update-agents, etc.)
- Automatic Claude Code configuration updates (CLAUDE.md)
- Comprehensive documentation (GETTING_STARTED, AGENT_DISTRIBUTION, CLI_CHEAT_SHEET)
- Multi-language build system integration examples
- Complete test coverage for all components

PACKAGE STRUCTURE:
- Console script: kaizen-agentic command available globally
- Package data: All agents included for distribution
- Dependencies: click, pyyaml for CLI and parsing
- Testing: Comprehensive test suite for registry and installer

This enables sharing specialized AI agents across projects with easy installation,
updates, and management through both CLI and integrated Makefile targets.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-19 02:31:15 +02:00

230 lines
6.3 KiB
Python

"""Tests for agent registry functionality."""
import pytest
from pathlib import Path
from kaizen_agentic.registry import AgentRegistry, AgentDefinition, AgentCategory
def test_agent_definition_from_file(tmp_path):
"""Test creating AgentDefinition from a markdown file."""
agent_content = """---
name: test-agent
description: A test agent for demonstration
model: inherit
---
# Test Agent
This is a test agent that demonstrates the functionality.
It uses todo-keeper and changelog-keeper agents for dependencies.
"""
agent_file = tmp_path / "agent-test-agent.md"
agent_file.write_text(agent_content)
agent_def = AgentDefinition.from_file(agent_file)
assert agent_def.name == "test-agent"
assert agent_def.description == "A test agent for demonstration"
assert agent_def.model == "inherit"
assert agent_def.file_path == agent_file
def test_agent_registry_load_agents(tmp_path):
"""Test loading agents from directory."""
# Create test agents
agent1_content = """---
name: agent-one
description: First test agent
---
# Agent One
"""
agent2_content = """---
name: agent-two
description: Second test agent
---
# Agent Two
"""
(tmp_path / "agent-agent-one.md").write_text(agent1_content)
(tmp_path / "agent-agent-two.md").write_text(agent2_content)
registry = AgentRegistry(tmp_path)
assert len(registry._agents) == 2
assert "agent-one" in registry._agents
assert "agent-two" in registry._agents
def test_agent_registry_get_agent(tmp_path):
"""Test getting specific agent."""
agent_content = """---
name: test-agent
description: A test agent
---
# Test Agent
"""
(tmp_path / "agent-test-agent.md").write_text(agent_content)
registry = AgentRegistry(tmp_path)
agent = registry.get_agent("test-agent")
assert agent is not None
assert agent.name == "test-agent"
missing_agent = registry.get_agent("missing-agent")
assert missing_agent is None
def test_agent_registry_list_agents(tmp_path):
"""Test listing agents."""
# Create test agents with different categories
todo_agent = """---
name: todo-keeper
description: Manages TODO files
---
# TODO Keeper
"""
test_agent = """---
name: test-runner
description: Runs tests
---
# Test Runner
"""
(tmp_path / "agent-todo-keeper.md").write_text(todo_agent)
(tmp_path / "agent-test-runner.md").write_text(test_agent)
registry = AgentRegistry(tmp_path)
all_agents = registry.list_agents()
assert len(all_agents) == 2
# Test filtering by category
project_agents = registry.list_agents(AgentCategory.PROJECT_MANAGEMENT)
assert len(project_agents) == 1
assert project_agents[0].name == "todo-keeper"
def test_agent_registry_resolve_dependencies(tmp_path):
"""Test dependency resolution."""
# Create agents with dependencies
base_agent = """---
name: base-agent
description: Base agent with no dependencies
---
# Base Agent
"""
dependent_agent = """---
name: dependent-agent
description: Agent that depends on base-agent
---
# Dependent Agent
This agent uses base-agent for functionality.
"""
complex_agent = """---
name: complex-agent
description: Agent with multiple dependencies
---
# Complex Agent
This agent uses both base-agent and dependent-agent.
"""
(tmp_path / "agent-base-agent.md").write_text(base_agent)
(tmp_path / "agent-dependent-agent.md").write_text(dependent_agent)
(tmp_path / "agent-complex-agent.md").write_text(complex_agent)
registry = AgentRegistry(tmp_path)
# Test simple dependency resolution
resolved = registry.resolve_dependencies(["dependent-agent"])
assert "base-agent" in resolved
assert "dependent-agent" in resolved
# Test complex dependency resolution
resolved = registry.resolve_dependencies(["complex-agent"])
assert all(agent in resolved for agent in ["base-agent", "dependent-agent", "complex-agent"])
def test_agent_registry_get_templates(tmp_path):
"""Test getting predefined templates."""
registry = AgentRegistry(tmp_path)
templates = registry.get_agent_templates()
assert "python-basic" in templates
assert "python-web" in templates
assert "python-cli" in templates
assert "python-data" in templates
assert "comprehensive" in templates
# Check that templates contain expected agents
assert "setup-repository" in templates["python-basic"]
assert "todo-keeper" in templates["python-basic"]
def test_agent_registry_validate_agents(tmp_path):
"""Test agent validation."""
# Create valid agent
valid_agent = """---
name: valid-agent
description: A valid agent
---
# Valid Agent
"""
# Create invalid agent (missing required fields)
invalid_agent = """---
description: Missing name field
---
# Invalid Agent
"""
(tmp_path / "agent-valid-agent.md").write_text(valid_agent)
(tmp_path / "agent-invalid-agent.md").write_text(invalid_agent)
registry = AgentRegistry(tmp_path)
errors = registry.validate_agents()
# The invalid agent should not be loaded, so no validation errors
# (it fails during loading)
assert len(errors) == 0 # Because invalid agents aren't loaded
def test_agent_category_determination():
"""Test automatic category determination."""
# Test project management category
assert AgentDefinition._determine_category("todo-keeper", "") == AgentCategory.PROJECT_MANAGEMENT
assert AgentDefinition._determine_category("changelog-helper", "") == AgentCategory.PROJECT_MANAGEMENT
# Test testing category
assert AgentDefinition._determine_category("test-runner", "") == AgentCategory.TESTING
assert AgentDefinition._determine_category("tdd-workflow", "") == AgentCategory.TESTING
# Test code quality category
assert AgentDefinition._determine_category("code-refactoring", "") == AgentCategory.CODE_QUALITY
assert AgentDefinition._determine_category("optimization-helper", "") == AgentCategory.CODE_QUALITY
# Test documentation category
assert AgentDefinition._determine_category("documentation-generator", "") == AgentCategory.DOCUMENTATION
assert AgentDefinition._determine_category("claude-helper", "") == AgentCategory.DOCUMENTATION
# Test infrastructure category
assert AgentDefinition._determine_category("setup-repository", "") == AgentCategory.INFRASTRUCTURE
assert AgentDefinition._determine_category("tooling-manager", "") == AgentCategory.INFRASTRUCTURE