File size: 1,984 Bytes
070daf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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())