File size: 2,121 Bytes
4408617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
import sys
import os
import subprocess
from unittest.mock import MagicMock

# Add parent directory to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Mock subprocess
subprocess.run = MagicMock(return_value=MagicMock(returncode=0, stdout="Mock Output"))
subprocess.Popen = MagicMock()

from agent.cua_agent import ComputerUsingAgent
from agent.x11_computer import X11Computer

# Mock X11Computer methods
def mock_current_state(self):
    print("Mocking current state...")
    return MagicMock(screenshot=b"fake_screenshot", url="Mock Window")

def mock_hover_at(self, x, y):
    print(f"Mock hover at {x}, {y}")
    return MagicMock()

def mock_open_web_browser(self):
    print("Mock opening web browser")
    return MagicMock()

X11Computer.current_state = mock_current_state
X11Computer.hover_at = mock_hover_at
X11Computer.open_web_browser = mock_open_web_browser

def test_agent():
    print("Initializing Agent...")
    agent = ComputerUsingAgent()
    
    print("Testing Gemini Integration with Computer Interface...")
    
    task = "Open Firefox and hover at 100, 100"
    print(f"Task: {task}")
    
    # Mock Gemini response to return actions that use our mocked methods
    # We can't easily mock the internal GeminiClient here without more patching,
    # but the agent will try to call real API. 
    # If API call fails (no key/network), it returns fail action.
    # Let's assume the key works or we patch GeminiClient too.
    
    # Patch GeminiClient to return a fixed plan for testing logic
    from agent.cua_agent import GeminiClient
    GeminiClient.generate_actions = MagicMock(return_value=[
        {"action": "launch", "app": "firefox"},
        {"action": "mousemove", "x": 100, "y": 100},
        {"action": "done", "message": "Test complete"}
    ])
    
    result = agent.execute_task(task)
    
    print("\nResult:")
    print(f"Success: {result['success']}")
    print(f"Message: {result['message']}")
    print("Steps Executed:")
    for step in result['steps_executed']:
        print(f"  - {step}")

if __name__ == "__main__":
    test_agent()