|
12 | 12 | from hud.tools.computer.openai import OpenAIComputerTool |
13 | 13 | from hud.tools.computer.qwen import QwenComputerTool |
14 | 14 | from hud.tools.executors.base import BaseExecutor |
15 | | -from hud.tools.types import Coordinate |
| 15 | +from hud.tools.executors.xdo import XDOExecutor |
| 16 | +from hud.tools.types import ContentResult, Coordinate |
| 17 | + |
| 18 | + |
| 19 | +class RecordingXDOExecutor(XDOExecutor): |
| 20 | + def __init__(self): |
| 21 | + super().__init__() |
| 22 | + self.commands: list[str] = [] |
| 23 | + |
| 24 | + async def execute(self, command: str, take_screenshot: bool = True): |
| 25 | + self.commands.append(command) |
| 26 | + return ContentResult(output=command) |
| 27 | + |
| 28 | + |
| 29 | +class RecordingExecutor(BaseExecutor): |
| 30 | + def __init__(self): |
| 31 | + super().__init__() |
| 32 | + self.drag_paths: list[list[tuple[int, int]]] = [] |
| 33 | + |
| 34 | + async def drag(self, path, pattern=None, hold_keys=None, take_screenshot=True): |
| 35 | + self.drag_paths.append(path) |
| 36 | + return await super().drag(path, pattern, hold_keys, take_screenshot=False) |
16 | 37 |
|
17 | 38 |
|
18 | 39 | @pytest.mark.asyncio |
@@ -151,6 +172,51 @@ def test_normalized_coordinate_max_stays_in_display_bounds(): |
151 | 172 | assert int(y) <= comp.environment_height - 1 |
152 | 173 |
|
153 | 174 |
|
| 175 | +def test_drag_path_interpolation_adds_intermediate_points(): |
| 176 | + executor = BaseExecutor() |
| 177 | + |
| 178 | + path = executor._interpolate_drag_path([(0, 0), (120, 0)]) |
| 179 | + |
| 180 | + assert path[0] == (0, 0) |
| 181 | + assert path[-1] == (120, 0) |
| 182 | + assert len(path) == 11 |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.asyncio |
| 186 | +async def test_gemini_drag_clamps_edges_and_interpolates_executor_path(): |
| 187 | + executor = RecordingExecutor() |
| 188 | + comp = GeminiComputerTool(executor=executor, width=1400, height=850) |
| 189 | + |
| 190 | + blocks = await comp( |
| 191 | + action="drag_and_drop", |
| 192 | + x=0, |
| 193 | + y=500, |
| 194 | + destination_x=1000, |
| 195 | + destination_y=500, |
| 196 | + ) |
| 197 | + |
| 198 | + assert blocks |
| 199 | + path = executor.drag_paths[0] |
| 200 | + assert path[0][0] >= 20 |
| 201 | + assert path[-1][0] <= comp.environment_width - 1 - 20 |
| 202 | + |
| 203 | + interpolated = executor._interpolate_drag_path(path) |
| 204 | + assert len(interpolated) > 2 |
| 205 | + |
| 206 | + |
| 207 | +@pytest.mark.asyncio |
| 208 | +async def test_xdo_drag_executes_interpolated_mouse_moves(): |
| 209 | + executor = RecordingXDOExecutor() |
| 210 | + |
| 211 | + result = await executor.drag([(0, 0), (120, 0)], take_screenshot=False) |
| 212 | + |
| 213 | + mouse_moves = [command for command in executor.commands if command.startswith("mousemove ")] |
| 214 | + assert result.output == "Dragged along 11 points" |
| 215 | + assert len(mouse_moves) == 11 |
| 216 | + assert mouse_moves[0] == "mousemove 0 0" |
| 217 | + assert mouse_moves[-1] == "mousemove 120 0" |
| 218 | + |
| 219 | + |
154 | 220 | class TestHudComputerToolExtended: |
155 | 221 | """Extended tests for HudComputerTool covering edge cases and platform logic.""" |
156 | 222 |
|
|
0 commit comments