test_browsing_agent_parser.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import pytest
  2. from openhands.agenthub.browsing_agent.response_parser import (
  3. BrowseInteractiveAction,
  4. BrowsingResponseParser,
  5. )
  6. @pytest.mark.parametrize(
  7. 'action_str, expected',
  8. [
  9. ("click('81'", "click('81')```"),
  10. (
  11. '"We need to search the internet\n```goto("google.com")',
  12. '"We need to search the internet\n```goto("google.com"))```',
  13. ),
  14. ("```click('81'", "```click('81')```"),
  15. ("click('81')", "click('81'))```"),
  16. ],
  17. )
  18. def test_parse_response(action_str: str, expected: str) -> None:
  19. # BrowsingResponseParser.parse_response
  20. parser = BrowsingResponseParser()
  21. response = {'choices': [{'message': {'content': action_str}}]}
  22. result = parser.parse_response(response)
  23. assert result == expected
  24. @pytest.mark.parametrize(
  25. 'action_str, expected_browser_actions, expected_thought, expected_msg_content',
  26. [
  27. ("click('81')```", "click('81')", '', ''),
  28. ("```click('81')```", "click('81')", '', ''),
  29. (
  30. "We need to perform a click\n```click('81')",
  31. "click('81')",
  32. 'We need to perform a click',
  33. '',
  34. ),
  35. ],
  36. )
  37. def test_parse_action(
  38. action_str: str,
  39. expected_browser_actions: str,
  40. expected_thought: str,
  41. expected_msg_content: str,
  42. ) -> None:
  43. # BrowsingResponseParser.parse_action
  44. parser = BrowsingResponseParser()
  45. action = parser.parse_action(action_str)
  46. assert isinstance(action, BrowseInteractiveAction)
  47. assert action.browser_actions == expected_browser_actions
  48. assert action.thought == expected_thought
  49. assert action.browsergym_send_msg_to_user == expected_msg_content