feat(tools): add long_press tool with humanize hook
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from device.manager import DeviceManager
|
||||||
|
from tests.fakes import FakeDriver
|
||||||
|
|
||||||
|
|
||||||
|
def _connected(driver: FakeDriver) -> DeviceManager:
|
||||||
|
manager = DeviceManager()
|
||||||
|
manager.register_device("phone", lambda: driver)
|
||||||
|
manager.connect("phone", max_retries=1)
|
||||||
|
return manager
|
||||||
|
|
||||||
|
|
||||||
|
def test_long_press_disabled_passes_exact_coords(monkeypatch):
|
||||||
|
monkeypatch.setenv("APEX_HUMANIZE_ENABLED", "false")
|
||||||
|
from tools.long_press import long_press
|
||||||
|
|
||||||
|
driver = FakeDriver()
|
||||||
|
res = long_press(100, 200, duration_ms=1500, manager=_connected(driver))
|
||||||
|
assert driver.calls[-1] == ("long_press", (100, 200, 1500))
|
||||||
|
assert res["action"] == "long_press"
|
||||||
|
assert res["duration_ms"] == 1500
|
||||||
|
|
||||||
|
|
||||||
|
def test_long_press_enabled_jitters_within_radius(monkeypatch):
|
||||||
|
import random
|
||||||
|
|
||||||
|
monkeypatch.setenv("APEX_HUMANIZE_ENABLED", "true")
|
||||||
|
from tools.humanize import set_rng
|
||||||
|
from tools.long_press import long_press
|
||||||
|
|
||||||
|
set_rng(random.Random(5))
|
||||||
|
try:
|
||||||
|
driver = FakeDriver()
|
||||||
|
long_press(100, 200, duration_ms=1500, manager=_connected(driver))
|
||||||
|
finally:
|
||||||
|
set_rng(None)
|
||||||
|
name, args = driver.calls[-1]
|
||||||
|
assert name == "long_press"
|
||||||
|
px, py, _ = args
|
||||||
|
assert abs(px - 100) <= 5.0 and abs(py - 200) <= 5.0
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from device.manager import DeviceManager
|
||||||
|
from tools._device import get_driver
|
||||||
|
from tools.humanize import (
|
||||||
|
get_rng,
|
||||||
|
jitter_duration,
|
||||||
|
jitter_point,
|
||||||
|
load_humanize_config,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def long_press(
|
||||||
|
x: float,
|
||||||
|
y: float,
|
||||||
|
*,
|
||||||
|
duration_ms: int = 1200,
|
||||||
|
device_id: str | None = None,
|
||||||
|
manager: DeviceManager | None = None,
|
||||||
|
) -> dict[str, object]:
|
||||||
|
cfg = load_humanize_config()
|
||||||
|
px, py = x, y
|
||||||
|
dms = duration_ms
|
||||||
|
if cfg.enabled:
|
||||||
|
rng = get_rng()
|
||||||
|
px, py = jitter_point(x, y, radius=cfg.tap_radius_px, rng=rng)
|
||||||
|
dms = jitter_duration(duration_ms, spread=cfg.duration_spread, rng=rng)
|
||||||
|
get_driver(device_id, manager=manager).long_press(px, py, dms)
|
||||||
|
return {"ok": True, "action": "long_press", "x": px, "y": py, "duration_ms": dms}
|
||||||
Reference in New Issue
Block a user