Files
hermes-agent/tests/tools/conftest.py
kshitijk4poor 99028fa831 refactor(tts): signal each lease owner, then drop the local models once
The unload released each acquiring profile with release_tts_provider(),
which both signals the profile's user provider and clears every local
model cache. It then swept the caches again per engine name to catch what
a failed owner left behind. That is N+M cache passes for one context-free
operation, and the per-owner call hid that the sweep is what makes a
failed owner safe.

Split the two halves. _signal_release() runs in each owner's acquisition
context, because it reads that profile's config, hook and secrets.
_drop_local_models() reads no config and runs once, outside any owner.
release_tts_provider() is composed from the same two pieces, so its
public contract does not change: default/blank -> signal + clear all;
named -> clear that cache only, no signal; {"released": n}.

Tests (new guard file only):
- public-API parity for release_tts_provider (none/blank/whitespace,
  named with normalization, unknown name), plus one unload that signals
  each owner once in its own profile and counts models once. These also
  pass on the previous lifecycle file, which shows the refactor keeps
  its behaviour.
- the bare-home fixtures share one `homes` fixture. It is HERMES_HOME
  override only, deliberately not bind_profile_scope (no secret hydration).
- the barrier test releases the real teardown lock if a failing run
  acquired it, instead of leaving the module lock held.
- the web release comment now names the scope's real remaining side
  effects (hosting activation, secret-source hydration).
2026-11-19 19:00:02 +05:30

240 lines
8.6 KiB
Python

"""Shared fixtures for tests/tools/ web-provider tests.
Per-file subprocess isolation means each test file gets a fresh interpreter,
so module-level state (like the web-search-provider registry) is empty when
a file starts. The ``web_registry_populated`` fixture registers all bundled
providers before each test and resets the registry afterwards — tests that
depend on the registry being populated should use it explicitly or via
``@pytest.mark.usefixtures("web_registry_populated")``.
"""
from unittest.mock import patch
import pytest
@pytest.fixture(autouse=True)
def _no_host_browser_use_cli():
"""Keep the host's browser-harness install out of tests.
browser_exec is advertised whenever browser-harness is importable, so a developer machine
would expose (and could spawn) the real harness in every tool-surface test. Pin discovery to
"not installed"; tests that exercise the CLI path monkeypatch ``bu_cli._find_cli`` themselves.
"""
try:
import tools.browser_use_cli as bu_cli
except Exception:
yield
return
# Keep a handle to the real discovery function so TestFindCli (and any
# test that wants genuine PM discovery) can restore it explicitly.
if not hasattr(bu_cli, "_find_cli_unpatched"):
bu_cli._find_cli_unpatched = bu_cli._find_cli
with patch.object(bu_cli, "_find_cli", lambda: None):
yield
@pytest.fixture(autouse=True)
def _no_chromium_download():
"""A test that reaches the launcher without a fake binary must never start PM's ~170 MB Chromium
download; the one-shot auto-install is pinned to "declined"."""
try:
import tools.browser_launcher as launcher
except Exception:
yield
return
with patch.object(launcher, "_maybe_autoinstall_chromium", lambda: False):
yield
@pytest.fixture(autouse=True)
def _no_shared_temp_socket_dirs():
"""A served profile's over-budget harness runtime dir is moved to a private ``hbh-<hash>`` dir
in the shared temp root (sun_path budget). Unit tests never claim one there: fake CLIs would
leave latch files behind. The dir keeps its long path; tests of the claim restore the real one."""
try:
import tools.browser_harness_dirs as harness_dirs
except Exception:
yield
return
with patch.object(harness_dirs, "claim_private_dir", lambda path: False):
yield
@pytest.fixture(autouse=True)
def _no_host_bot_desktop_autostart():
"""Keep the host's TigerVNC/Xfce install out of tests.
``computer_use`` auto-starts the profile's Bot Desktop on a headless Linux
host with the packages installed, so a developer box that has them would
launch a real Xvnc + Xfce session per test. Pin the binaries to "missing";
tests that exercise the desktop path monkeypatch ``runtime`` themselves.
"""
try:
from tools.bot_desktop import runtime as bd_runtime
except Exception:
yield
return
with patch.object(bd_runtime, "missing_binaries", lambda: ["Xvnc"]):
yield
@pytest.fixture(autouse=True)
def _materialize_mcp_sdk_symbols():
"""Materialize the lazily-imported MCP SDK before each tools test.
``tools/mcp_tool.py`` defers the ~260ms ``mcp`` SDK import until first
real use (CLI startup perf). Tests in this directory patch SDK symbols
(``ClientSession``, ``stdio_client``, ``_MCP_HTTP_AVAILABLE``, ...) on
the module and expect the pre-lazy eager-import world: symbols bound,
availability flags reflecting the installed SDK. Ensure that state up
front so ``mock.patch`` sees real originals and ``_ensure_mcp_sdk()``
can never clobber a patched flag mid-test (it no-ops once attempted).
"""
try:
from tools import mcp_tool
mcp_tool._ensure_mcp_sdk()
except Exception:
pass
yield
@pytest.fixture(autouse=True)
def _clear_web_result_cache():
"""Reset the web_search TTL memo between tests.
The memo is module-global state in tools/web_result_cache.py; without
this, a test that exercised web_search_tool leaves a cached response
that a later test with the same query would receive instead of its own
mocked provider result.
"""
from tools.web_result_cache import search_memo
search_memo.clear()
yield
search_memo.clear()
def register_all_web_providers():
"""Register all bundled web-search providers into the global registry.
This is the single source of truth for the provider list used by
test classes that need the registry populated for dispatch checks.
"""
from agent.web_search_registry import register_provider, _reset_for_tests
from plugins.web.brave_free.provider import BraveFreeWebSearchProvider
from plugins.web.ddgs.provider import DDGSWebSearchProvider
from plugins.web.exa.provider import ExaWebSearchProvider
from plugins.web.firecrawl.provider import FirecrawlWebSearchProvider
from plugins.web.parallel.provider import ParallelWebSearchProvider
from plugins.web.keenable.provider import KeenableWebSearchProvider
from plugins.web.tavily.provider import TavilyWebSearchProvider
from plugins.web.perplexity.provider import PerplexityWebSearchProvider
from plugins.web.searxng.provider import SearXNGWebSearchProvider
from plugins.web.xai.provider import XAIWebSearchProvider
_reset_for_tests()
for cls in (
BraveFreeWebSearchProvider,
DDGSWebSearchProvider,
ExaWebSearchProvider,
FirecrawlWebSearchProvider,
ParallelWebSearchProvider,
KeenableWebSearchProvider,
TavilyWebSearchProvider,
PerplexityWebSearchProvider,
SearXNGWebSearchProvider,
XAIWebSearchProvider,
):
register_provider(cls())
@pytest.fixture
def grant_computer_use_approvals(monkeypatch):
"""Answer every computer_use approval prompt with "once" through the shared gate.
computer_use fails CLOSED when nobody can answer (no interactive user, no
gateway), so dispatch tests that only care about routing must present an
interactive CLI with a granting callback. "once" persists nothing, so no
grant leaks into ``tools.approval``'s session/permanent stores.
"""
from tools.computer_use import tool as cu_tool
monkeypatch.setenv("HERMES_INTERACTIVE", "1")
cu_tool.set_approval_callback(lambda command, description, **kw: "once")
yield
cu_tool.set_approval_callback(None)
@pytest.fixture
def web_registry_populated():
"""Populate the web-search-provider registry for one test, then reset."""
register_all_web_providers()
yield
from agent.web_search_registry import _reset_for_tests
_reset_for_tests()
class FakeKeepWarmTimer:
"""Inert TTS keep-warm ``threading.Timer``: records its interval; ``fire()`` runs the unload."""
def __init__(self, interval, function, args=None, kwargs=None):
self.interval, self.function, self.args = interval, function, tuple(args or ())
self.started = self.cancelled = self.daemon = False
def start(self):
self.started = True
def cancel(self):
self.cancelled = True
@property
def pending(self):
return self.started and not self.cancelled
def fire(self):
self.function(*self.args)
@pytest.fixture
def tts_keep_warm_timers(monkeypatch):
"""Every keep-warm timer the TTS lease lifecycle creates, as ``FakeKeepWarmTimer``s."""
from tools import tts_tool_lifecycle
created: list = []
def make(*args, **kwargs):
created.append(timer := FakeKeepWarmTimer(*args, **kwargs))
return timer
monkeypatch.setattr(tts_tool_lifecycle, "_make_keep_warm_timer", make)
return created
@pytest.fixture
def tts_lifecycle_reset():
"""Empty TTS leases, owners and resident local models around a test; opt in per module."""
from tools import tts_tool_lifecycle, tts_tool_local
def reset():
tts_tool_lifecycle._reset_tts_leases_for_tests()
for cache in tts_tool_local._LOCAL_TTS_MODEL_CACHES.values():
cache.clear()
reset()
yield
reset()
@pytest.fixture
def disable_lazy_stt_install():
"""Disarm the runtime lazy-install probe so static ``_HAS_FASTER_WHISPER``
patches accurately simulate 'faster-whisper not installed'.
Without this, ``_try_lazy_install_stt()`` calls
``importlib.util.find_spec("faster_whisper")``, which returns truthy
whenever the package is installed in the dev / CI environment —
defeating the test's ``_HAS_FASTER_WHISPER=False`` patch.
Opt in at module scope with
``pytestmark = pytest.mark.usefixtures("disable_lazy_stt_install")``.
"""
with patch("tools.transcription_tools._try_lazy_install_stt", return_value=False):
yield