test_agent.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import asyncio
  2. import os
  3. import shutil
  4. import subprocess
  5. import pytest
  6. from opendevin.controller.state.state import State
  7. from opendevin.core.main import main
  8. from opendevin.core.schema import AgentState
  9. workspace_base = os.getenv('WORKSPACE_BASE')
  10. @pytest.mark.skipif(
  11. os.getenv('AGENT') == 'CodeActAgent'
  12. and os.getenv('SANDBOX_TYPE').lower() == 'exec',
  13. reason='CodeActAgent does not support exec sandbox since exec sandbox is NOT stateful',
  14. )
  15. def test_write_simple_script():
  16. task = "Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point."
  17. final_state: State = asyncio.run(main(task, exit_on_message=True))
  18. assert final_state.agent_state == AgentState.STOPPED
  19. # Verify the script file exists
  20. script_path = os.path.join(workspace_base, 'hello.sh')
  21. assert os.path.exists(script_path), 'The file "hello.sh" does not exist'
  22. # Run the script and capture the output
  23. result = subprocess.run(['bash', script_path], capture_output=True, text=True)
  24. # Verify the output from the script
  25. assert (
  26. result.stdout.strip() == 'hello'
  27. ), f'Expected output "hello", but got "{result.stdout.strip()}"'
  28. @pytest.mark.skipif(
  29. os.getenv('AGENT') == 'CodeActAgent'
  30. and os.getenv('SANDBOX_TYPE').lower() == 'exec',
  31. reason='CodeActAgent does not support exec sandbox since exec sandbox is NOT stateful',
  32. )
  33. @pytest.mark.skipif(
  34. os.getenv('AGENT') == 'SWEAgent',
  35. reason='SWEAgent is not capable of this task right now',
  36. )
  37. @pytest.mark.skipif(
  38. os.getenv('SANDBOX_TYPE') == 'local',
  39. reason='local sandbox shows environment-dependent absolute path for pwd command',
  40. )
  41. def test_edits():
  42. # Move workspace artifacts to workspace_base location
  43. source_dir = os.path.join(os.path.dirname(__file__), 'workspace/test_edits/')
  44. files = os.listdir(source_dir)
  45. for file in files:
  46. dest_file = os.path.join(workspace_base, file)
  47. if os.path.exists(dest_file):
  48. os.remove(dest_file)
  49. shutil.copy(os.path.join(source_dir, file), dest_file)
  50. # Execute the task
  51. task = 'Fix typos in bad.txt. Do not ask me for confirmation at any point.'
  52. final_state: State = asyncio.run(main(task, exit_on_message=True))
  53. assert final_state.agent_state == AgentState.STOPPED
  54. # Verify bad.txt has been fixed
  55. text = """This is a stupid typo.
  56. Really?
  57. No more typos!
  58. Enjoy!
  59. """
  60. with open(os.path.join(workspace_base, 'bad.txt'), 'r') as f:
  61. content = f.read()
  62. assert content.strip() == text.strip()
  63. @pytest.mark.skipif(
  64. os.getenv('AGENT') != 'CodeActAgent',
  65. reason='currently only CodeActAgent defaults to have IPython (Jupyter) execution',
  66. )
  67. @pytest.mark.skipif(
  68. os.getenv('SANDBOX_TYPE') != 'ssh',
  69. reason='Currently, only ssh sandbox supports stateful tasks',
  70. )
  71. def test_ipython():
  72. # Execute the task
  73. task = "Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point."
  74. final_state: State = asyncio.run(main(task, exit_on_message=True))
  75. assert final_state.agent_state == AgentState.STOPPED
  76. # Verify the file exists
  77. file_path = os.path.join(workspace_base, 'test.txt')
  78. assert os.path.exists(file_path), 'The file "test.txt" does not exist'
  79. # Verify the file contains the expected content
  80. with open(file_path, 'r') as f:
  81. content = f.read()
  82. assert (
  83. content.strip() == 'hello world'
  84. ), f'Expected content "hello world", but got "{content.strip()}"'
  85. @pytest.mark.skipif(
  86. os.getenv('AGENT') != 'CodeActAgent',
  87. reason='currently only CodeActAgent defaults to have IPython (Jupyter) execution',
  88. )
  89. @pytest.mark.skipif(
  90. os.getenv('SANDBOX_TYPE') != 'ssh',
  91. reason='Currently, only ssh sandbox supports stateful tasks',
  92. )
  93. def test_ipython_module():
  94. # Execute the task
  95. task = "Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point."
  96. final_state: State = asyncio.run(main(task, exit_on_message=True))
  97. assert final_state.agent_state == AgentState.STOPPED
  98. # Verify the file exists
  99. file_path = os.path.join(workspace_base, 'test.txt')
  100. assert os.path.exists(file_path), 'The file "test.txt" does not exist'
  101. # Verify the file contains the expected content
  102. with open(file_path, 'r') as f:
  103. content = f.read()
  104. assert (
  105. content.strip() == '1.0.9'
  106. ), f'Expected content "1.0.9", but got "{content.strip()}"'