Files
hermes-agent/tests/computer_use_fakes.py
alt-glitch c5eb493375 refactor(browser-desktop): move computer_use test seams out of tools/computer_use/tool.py
Four things in tool.py existed only for tests:

- the module-level _backend injection hook (its own comment: "backward-
  compatible empty-session injection hook (older tests)") and its mirror
  branches in _install_backend, _detach_locked, _get_backend and
  _shutdown_backend_atexit. Production installs backends only through
  _new_backend into the _backends cache, so with nothing assigning _backend
  every branch was a no-op. The two tests that assigned it now patch
  _new_backend, the seam the rest of the suite already uses; the atexit
  tests seed _backends instead.
- reset_backend_for_tests, now tests/computer_use_fakes.reset_backend (the
  same two calls: _shutdown_backend_atexit, _reset_screenshot_dedup).
- _noop_stub and _NoopBackend (pragma: no cover, docstring "Test stub"),
  moved verbatim to tests/computer_use_fakes.py as noop_stub / NoopBackend.
- the _bounds_scale and _bounds_space_note lambdas, which only indexed
  _bounds_hints; the tests index _bounds_hints directly.

None of these names is in hermes_cli/data/plugin_api.json (it publishes
tools.computer_use.backend only) or tests/compat/old_updater_surface.json.
2026-11-16 21:54:46 +05:30

36 lines
1.8 KiB
Python

"""A recording computer_use backend and a cache reset, for tests that drive ``tools.computer_use.tool``."""
from typing import Any
from tools.computer_use import tool
from tools.computer_use.backend import ActionResult, CaptureResult, ComputerUseBackend
def reset_backend() -> None:
"""Stop every cached backend and forget per-session capture state."""
tool._shutdown_backend_atexit()
tool._reset_screenshot_dedup()
def noop_stub(name: str, *params: str, result: Any = None) -> Any:
# Positional args are folded in under *params* (declared params default to None). ``result`` may be a factory
# of the recorded kwargs; None -> a trivial ok ActionResult.
def method(self, *pos, **kw):
self.calls.append((name, call := {**dict.fromkeys(params), **dict(zip(params, pos)), **kw}))
return result(call) if callable(result) else ActionResult(ok=True, action=name) if result is None else result
return method
class NoopBackend(ComputerUseBackend):
"""Records ``(name, kwargs)`` calls; returns trivial results. Tests patch ``tool._new_backend`` to return it."""
def __init__(self) -> None: self.calls: list[tuple[str, dict[str, Any]]] = []
start = stop = lambda self: None
def is_available(self) -> bool: return True
capture = noop_stub("capture", "mode", "app", "pid", "window_id", result=lambda kw: CaptureResult(
mode=kw["mode"] or "som", width=1024, height=768, png_b64=None, elements=[], app=kw["app"] or "", window_title=""))
click, drag, scroll = noop_stub("click"), noop_stub("drag"), noop_stub("scroll")
type_text, key, set_value = noop_stub("type", "text"), noop_stub("key", "keys"), noop_stub("set_value", "value", "element")
list_apps, list_windows = noop_stub("list_apps", result=[]), noop_stub("list_windows", result=[])
focus_app = noop_stub("focus_app", "app", "raise_window")