#!/usr/bin/env python3 """Test the execute_code tool functionality.""" import asyncio import sys from pathlib import Path # Add the backend directory to the path sys.path.insert(0, str(Path(__file__).parent)) from agent.tools.execute_code_tool import execute_code_handler async def test_execute_code(): """Test the execute_code tool.""" print("=" * 60) print("Testing execute_code Tool") print("=" * 60) # Test 1: Simple echo command print("\nTest 1: Simple echo command") result, success = await execute_code_handler({"command": "echo 'Hello from execute_code!'"}) print(f"Success: {success}") print(f"Result: {result}") # Test 2: Write a file and read it print("\nTest 2: Write and read a file") result, success = await execute_code_handler({ "command": "cat > /tmp/test_file.txt << 'EOF'\nThis is a test file\nCreated by execute_code\nEOF" }) print(f"Write success: {success}") result, success = await execute_code_handler({ "command": "cat /tmp/test_file.txt" }) print(f"Read success: {success}") print(f"Content: {result}") # Test 3: Run Python code print("\nTest 3: Run Python code") result, success = await execute_code_handler({ "command": "python3 -c \"print(2+2); print('Python works!')\"" }) print(f"Success: {success}") print(f"Result: {result}") # Test 4: Install a package and use it print("\nTest 4: Install package and use it") result, success = await execute_code_handler({ "command": "pip install --quiet sympy && python3 -c \"from sympy import symbols, diff; x=symbols('x'); print(diff(x**2, x))\"" }) print(f"Success: {success}") print(f"Result: {result}") print("\n" + "=" * 60) print("execute_code tool test complete!") print("=" * 60) if __name__ == "__main__": asyncio.run(test_execute_code())