x11-desktop / scripts /verify_agent.py
3v324v23's picture
Refactor agent to use Computer interface and add deployment script
4408617
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()