| """Tests for microagent loading in runtime."""
|
|
|
| from pathlib import Path
|
|
|
| from conftest import (
|
| _close_test_runtime,
|
| _load_runtime,
|
| )
|
|
|
| from openhands.microagent import KnowledgeMicroAgent, RepoMicroAgent, TaskMicroAgent
|
|
|
|
|
| def _create_test_microagents(test_dir: str):
|
| """Create test microagent files in the given directory."""
|
| microagents_dir = Path(test_dir) / '.openhands' / 'microagents'
|
| microagents_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
| knowledge_dir = microagents_dir / 'knowledge'
|
| knowledge_dir.mkdir(exist_ok=True)
|
| knowledge_agent = """---
|
| name: test_knowledge_agent
|
| type: knowledge
|
| version: 1.0.0
|
| agent: CodeActAgent
|
| triggers:
|
| - test
|
| - pytest
|
| ---
|
|
|
| # Test Guidelines
|
|
|
| Testing best practices and guidelines.
|
| """
|
| (knowledge_dir / 'knowledge.md').write_text(knowledge_agent)
|
|
|
|
|
| repo_agent = """---
|
| name: test_repo_agent
|
| type: repo
|
| version: 1.0.0
|
| agent: CodeActAgent
|
| ---
|
|
|
| # Test Repository Agent
|
|
|
| Repository-specific test instructions.
|
| """
|
| (microagents_dir / 'repo.md').write_text(repo_agent)
|
|
|
|
|
| task_dir = microagents_dir / 'tasks' / 'nested'
|
| task_dir.mkdir(parents=True, exist_ok=True)
|
| task_agent = """---
|
| name: test_task
|
| type: task
|
| version: 1.0.0
|
| agent: CodeActAgent
|
| ---
|
|
|
| # Test Task
|
|
|
| Test task content
|
| """
|
| (task_dir / 'task.md').write_text(task_agent)
|
|
|
|
|
| legacy_instructions = """# Legacy Instructions
|
|
|
| These are legacy repository instructions.
|
| """
|
| (Path(test_dir) / '.openhands_instructions').write_text(legacy_instructions)
|
|
|
|
|
| def test_load_microagents_with_trailing_slashes(
|
| temp_dir, runtime_cls, run_as_openhands
|
| ):
|
| """Test loading microagents when directory paths have trailing slashes."""
|
|
|
| _create_test_microagents(temp_dir)
|
| runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
|
| try:
|
|
|
| loaded_agents = runtime.get_microagents_from_selected_repo(None)
|
|
|
|
|
| knowledge_agents = [
|
| a for a in loaded_agents if isinstance(a, KnowledgeMicroAgent)
|
| ]
|
| repo_agents = [a for a in loaded_agents if isinstance(a, RepoMicroAgent)]
|
| task_agents = [a for a in loaded_agents if isinstance(a, TaskMicroAgent)]
|
|
|
|
|
| assert len(knowledge_agents) == 1
|
| agent = knowledge_agents[0]
|
| assert agent.name == 'test_knowledge_agent'
|
| assert 'test' in agent.triggers
|
| assert 'pytest' in agent.triggers
|
|
|
|
|
| assert len(repo_agents) == 2
|
| repo_names = {a.name for a in repo_agents}
|
| assert 'test_repo_agent' in repo_names
|
| assert 'repo_legacy' in repo_names
|
|
|
|
|
| assert len(task_agents) == 1
|
| agent = task_agents[0]
|
| assert agent.name == 'test_task'
|
|
|
| finally:
|
| _close_test_runtime(runtime)
|
|
|
|
|
| def test_load_microagents_with_selected_repo(temp_dir, runtime_cls, run_as_openhands):
|
| """Test loading microagents from a selected repository."""
|
|
|
| repo_dir = Path(temp_dir) / 'OpenHands'
|
| repo_dir.mkdir(parents=True)
|
| _create_test_microagents(str(repo_dir))
|
|
|
| runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
|
| try:
|
|
|
| loaded_agents = runtime.get_microagents_from_selected_repo(
|
| 'All-Hands-AI/OpenHands'
|
| )
|
|
|
|
|
| knowledge_agents = [
|
| a for a in loaded_agents if isinstance(a, KnowledgeMicroAgent)
|
| ]
|
| repo_agents = [a for a in loaded_agents if isinstance(a, RepoMicroAgent)]
|
| task_agents = [a for a in loaded_agents if isinstance(a, TaskMicroAgent)]
|
|
|
|
|
| assert len(knowledge_agents) == 1
|
| agent = knowledge_agents[0]
|
| assert agent.name == 'test_knowledge_agent'
|
| assert 'test' in agent.triggers
|
| assert 'pytest' in agent.triggers
|
|
|
|
|
| assert len(repo_agents) == 2
|
| repo_names = {a.name for a in repo_agents}
|
| assert 'test_repo_agent' in repo_names
|
| assert 'repo_legacy' in repo_names
|
|
|
|
|
| assert len(task_agents) == 1
|
| agent = task_agents[0]
|
| assert agent.name == 'test_task'
|
|
|
| finally:
|
| _close_test_runtime(runtime)
|
|
|
|
|
| def test_load_microagents_with_missing_files(temp_dir, runtime_cls, run_as_openhands):
|
| """Test loading microagents when some files are missing."""
|
|
|
| microagents_dir = Path(temp_dir) / '.openhands' / 'microagents'
|
| microagents_dir.mkdir(parents=True, exist_ok=True)
|
|
|
| repo_agent = """---
|
| name: test_repo_agent
|
| type: repo
|
| version: 1.0.0
|
| agent: CodeActAgent
|
| ---
|
|
|
| # Test Repository Agent
|
|
|
| Repository-specific test instructions.
|
| """
|
| (microagents_dir / 'repo.md').write_text(repo_agent)
|
|
|
| runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
|
| try:
|
|
|
| loaded_agents = runtime.get_microagents_from_selected_repo(None)
|
|
|
|
|
| knowledge_agents = [
|
| a for a in loaded_agents if isinstance(a, KnowledgeMicroAgent)
|
| ]
|
| repo_agents = [a for a in loaded_agents if isinstance(a, RepoMicroAgent)]
|
| task_agents = [a for a in loaded_agents if isinstance(a, TaskMicroAgent)]
|
|
|
| assert len(knowledge_agents) == 0
|
| assert len(repo_agents) == 1
|
| assert len(task_agents) == 0
|
|
|
| agent = repo_agents[0]
|
| assert agent.name == 'test_repo_agent'
|
|
|
| finally:
|
| _close_test_runtime(runtime)
|
|
|