test_microagent_utils.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import pytest
  3. from pytest import MonkeyPatch
  4. import openhands.agenthub # noqa: F401
  5. from openhands.core.exceptions import (
  6. AgentNotRegisteredError,
  7. MicroAgentValidationError,
  8. )
  9. from openhands.utils.microagent import MicroAgent
  10. CONTENT = (
  11. '# dummy header\n' 'dummy content\n' '## dummy subheader\n' 'dummy subcontent\n'
  12. )
  13. def test_micro_agent_load(tmp_path, monkeypatch: MonkeyPatch):
  14. with open(os.path.join(tmp_path, 'dummy.md'), 'w') as f:
  15. f.write(
  16. (
  17. '---\n'
  18. 'name: dummy\n'
  19. 'agent: CodeActAgent\n'
  20. 'require_env_var:\n'
  21. ' SANDBOX_OPENHANDS_TEST_ENV_VAR: "Set this environment variable for testing purposes"\n'
  22. '---\n' + CONTENT
  23. )
  24. )
  25. # Patch the required environment variable
  26. monkeypatch.setenv('SANDBOX_OPENHANDS_TEST_ENV_VAR', 'dummy_value')
  27. micro_agent = MicroAgent(os.path.join(tmp_path, 'dummy.md'))
  28. assert micro_agent is not None
  29. assert micro_agent.content == CONTENT.strip()
  30. def test_not_existing_agent(tmp_path, monkeypatch: MonkeyPatch):
  31. with open(os.path.join(tmp_path, 'dummy.md'), 'w') as f:
  32. f.write(
  33. (
  34. '---\n'
  35. 'name: dummy\n'
  36. 'agent: NotExistingAgent\n'
  37. 'require_env_var:\n'
  38. ' SANDBOX_OPENHANDS_TEST_ENV_VAR: "Set this environment variable for testing purposes"\n'
  39. '---\n' + CONTENT
  40. )
  41. )
  42. monkeypatch.setenv('SANDBOX_OPENHANDS_TEST_ENV_VAR', 'dummy_value')
  43. with pytest.raises(AgentNotRegisteredError):
  44. MicroAgent(os.path.join(tmp_path, 'dummy.md'))
  45. def test_not_existing_env_var(tmp_path):
  46. with open(os.path.join(tmp_path, 'dummy.md'), 'w') as f:
  47. f.write(
  48. (
  49. '---\n'
  50. 'name: dummy\n'
  51. 'agent: CodeActAgent\n'
  52. 'require_env_var:\n'
  53. ' SANDBOX_OPENHANDS_TEST_ENV_VAR: "Set this environment variable for testing purposes"\n'
  54. '---\n' + CONTENT
  55. )
  56. )
  57. with pytest.raises(MicroAgentValidationError) as excinfo:
  58. MicroAgent(os.path.join(tmp_path, 'dummy.md'))
  59. assert 'Set this environment variable for testing purposes' in str(excinfo.value)