|
| 1 | +"""In-memory fake backend for CI / headless tool tests. |
| 2 | +
|
| 3 | +Drop-in replacement for the wrapper layer's mouse / keyboard / screen |
| 4 | +calls that records every invocation rather than touching the real OS. |
| 5 | +Activate via :func:`install_fake_backend` (or set |
| 6 | +``JE_AUTOCONTROL_FAKE_BACKEND=1`` before starting the MCP server) so |
| 7 | +test agents can drive the full tool registry on a CI runner without a |
| 8 | +display server. |
| 9 | +""" |
| 10 | +import os |
| 11 | +import threading |
| 12 | +from dataclasses import dataclass, field |
| 13 | +from typing import Any, Dict, List, Tuple |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class FakeState: |
| 18 | + """Records what the model would have done if the OS were real.""" |
| 19 | + |
| 20 | + cursor: Tuple[int, int] = (0, 0) |
| 21 | + screen_size: Tuple[int, int] = (1920, 1080) |
| 22 | + clipboard_text: str = "" |
| 23 | + typed_text: List[str] = field(default_factory=list) |
| 24 | + keys_pressed: List[Any] = field(default_factory=list) |
| 25 | + mouse_actions: List[Tuple[str, Any, ...]] = field(default_factory=list) |
| 26 | + |
| 27 | + |
| 28 | +def fake_state() -> FakeState: |
| 29 | + """Return the process-wide fake state.""" |
| 30 | + return _STATE |
| 31 | + |
| 32 | + |
| 33 | +_STATE = FakeState() |
| 34 | +_STATE_LOCK = threading.Lock() |
| 35 | + |
| 36 | + |
| 37 | +def reset_fake_state() -> None: |
| 38 | + """Reset every recorded interaction. Useful between tests.""" |
| 39 | + global _STATE |
| 40 | + with _STATE_LOCK: |
| 41 | + _STATE = FakeState() |
| 42 | + |
| 43 | + |
| 44 | +# === Patched callables ====================================================== |
| 45 | + |
| 46 | +def _fake_get_mouse_position() -> Tuple[int, int]: |
| 47 | + return _STATE.cursor |
| 48 | + |
| 49 | + |
| 50 | +def _fake_set_mouse_position(x: int, y: int) -> Tuple[int, int]: |
| 51 | + with _STATE_LOCK: |
| 52 | + _STATE.cursor = (int(x), int(y)) |
| 53 | + _STATE.mouse_actions.append(("set_position", int(x), int(y))) |
| 54 | + return _STATE.cursor |
| 55 | + |
| 56 | + |
| 57 | +def _fake_click_mouse(mouse_keycode: Any, x: Any = None, |
| 58 | + y: Any = None) -> Tuple[Any, int, int]: |
| 59 | + cx, cy = _STATE.cursor if x is None or y is None else (int(x), int(y)) |
| 60 | + with _STATE_LOCK: |
| 61 | + _STATE.cursor = (cx, cy) |
| 62 | + _STATE.mouse_actions.append(("click", mouse_keycode, cx, cy)) |
| 63 | + return mouse_keycode, cx, cy |
| 64 | + |
| 65 | + |
| 66 | +def _fake_press_mouse(mouse_keycode: Any, x: Any = None, |
| 67 | + y: Any = None) -> Tuple[Any, int, int]: |
| 68 | + cx, cy = _STATE.cursor if x is None or y is None else (int(x), int(y)) |
| 69 | + with _STATE_LOCK: |
| 70 | + _STATE.mouse_actions.append(("press", mouse_keycode, cx, cy)) |
| 71 | + return mouse_keycode, cx, cy |
| 72 | + |
| 73 | + |
| 74 | +def _fake_release_mouse(mouse_keycode: Any, x: Any = None, |
| 75 | + y: Any = None) -> Tuple[Any, int, int]: |
| 76 | + cx, cy = _STATE.cursor if x is None or y is None else (int(x), int(y)) |
| 77 | + with _STATE_LOCK: |
| 78 | + _STATE.mouse_actions.append(("release", mouse_keycode, cx, cy)) |
| 79 | + return mouse_keycode, cx, cy |
| 80 | + |
| 81 | + |
| 82 | +def _fake_mouse_scroll(scroll_value: int, x: Any = None, y: Any = None, |
| 83 | + scroll_direction: str = "scroll_down" |
| 84 | + ) -> Tuple[int, str]: |
| 85 | + with _STATE_LOCK: |
| 86 | + _STATE.mouse_actions.append( |
| 87 | + ("scroll", int(scroll_value), scroll_direction), |
| 88 | + ) |
| 89 | + return int(scroll_value), scroll_direction |
| 90 | + |
| 91 | + |
| 92 | +def _fake_screen_size() -> Tuple[int, int]: |
| 93 | + return _STATE.screen_size |
| 94 | + |
| 95 | + |
| 96 | +def _fake_write(text: str, *_args, **_kwargs) -> str: |
| 97 | + with _STATE_LOCK: |
| 98 | + _STATE.typed_text.append(text) |
| 99 | + return text |
| 100 | + |
| 101 | + |
| 102 | +def _fake_type_keyboard(keycode: Any, *_args, **_kwargs) -> str: |
| 103 | + with _STATE_LOCK: |
| 104 | + _STATE.keys_pressed.append(keycode) |
| 105 | + return str(keycode) |
| 106 | + |
| 107 | + |
| 108 | +def _fake_hotkey(keys: List[Any], *_args, **_kwargs) -> Tuple[str, str]: |
| 109 | + joined = ",".join(str(k) for k in keys) |
| 110 | + with _STATE_LOCK: |
| 111 | + _STATE.keys_pressed.append(("hotkey", joined)) |
| 112 | + return joined, joined |
| 113 | + |
| 114 | + |
| 115 | +def _fake_get_clipboard() -> str: |
| 116 | + return _STATE.clipboard_text |
| 117 | + |
| 118 | + |
| 119 | +def _fake_set_clipboard(text: str) -> None: |
| 120 | + with _STATE_LOCK: |
| 121 | + _STATE.clipboard_text = str(text) |
| 122 | + |
| 123 | + |
| 124 | +# === Install / uninstall ==================================================== |
| 125 | + |
| 126 | +_INSTALLED: Dict[str, Any] = {} |
| 127 | + |
| 128 | + |
| 129 | +def install_fake_backend() -> None: |
| 130 | + """Replace the headless API entry points with the fake recorders.""" |
| 131 | + if _INSTALLED: |
| 132 | + return |
| 133 | + from je_auto_control.utils.clipboard import clipboard as clipboard_module |
| 134 | + from je_auto_control.wrapper import auto_control_keyboard as kbd_module |
| 135 | + from je_auto_control.wrapper import auto_control_mouse as mouse_module |
| 136 | + from je_auto_control.wrapper import auto_control_screen as screen_module |
| 137 | + targets: Dict[Any, Dict[str, Any]] = { |
| 138 | + mouse_module: { |
| 139 | + "get_mouse_position": _fake_get_mouse_position, |
| 140 | + "set_mouse_position": _fake_set_mouse_position, |
| 141 | + "click_mouse": _fake_click_mouse, |
| 142 | + "press_mouse": _fake_press_mouse, |
| 143 | + "release_mouse": _fake_release_mouse, |
| 144 | + "mouse_scroll": _fake_mouse_scroll, |
| 145 | + }, |
| 146 | + screen_module: {"screen_size": _fake_screen_size}, |
| 147 | + kbd_module: { |
| 148 | + "write": _fake_write, |
| 149 | + "type_keyboard": _fake_type_keyboard, |
| 150 | + "hotkey": _fake_hotkey, |
| 151 | + }, |
| 152 | + clipboard_module: { |
| 153 | + "get_clipboard": _fake_get_clipboard, |
| 154 | + "set_clipboard": _fake_set_clipboard, |
| 155 | + }, |
| 156 | + } |
| 157 | + for module, replacements in targets.items(): |
| 158 | + for name, replacement in replacements.items(): |
| 159 | + key = f"{module.__name__}.{name}" |
| 160 | + _INSTALLED[key] = (module, name, getattr(module, name)) |
| 161 | + setattr(module, name, replacement) |
| 162 | + |
| 163 | + |
| 164 | +def uninstall_fake_backend() -> None: |
| 165 | + """Restore the real backend functions previously replaced.""" |
| 166 | + while _INSTALLED: |
| 167 | + _key, value = _INSTALLED.popitem() |
| 168 | + module, name, original = value |
| 169 | + setattr(module, name, original) |
| 170 | + |
| 171 | + |
| 172 | +def maybe_install_from_env() -> bool: |
| 173 | + """Install the fake backend when ``JE_AUTOCONTROL_FAKE_BACKEND`` is truthy.""" |
| 174 | + raw = os.environ.get("JE_AUTOCONTROL_FAKE_BACKEND", "").strip().lower() |
| 175 | + if raw in {"1", "true", "yes", "on"}: |
| 176 | + install_fake_backend() |
| 177 | + return True |
| 178 | + return False |
| 179 | + |
| 180 | + |
| 181 | +__all__ = [ |
| 182 | + "FakeState", "fake_state", "install_fake_backend", |
| 183 | + "maybe_install_from_env", "reset_fake_state", "uninstall_fake_backend", |
| 184 | +] |
0 commit comments