Files
hermes-agent/tests/hermes_cli/plugin_host_support.py
BearHuddleston 0886999b76 test(plugins): extract shared plugin-host test setup into plugin_host_support
Move PROBE_PLUGIN, _home_with_plugins, MODEL_PROVIDER_PLUGIN, and
_hosted_model_provider out of tests/hermes_cli/test_plugin_host.py into
tests/hermes_cli/plugin_host_support.py so sibling plugin-host suites
and other tests no longer import helpers from a test module.

Update tests/hermes_cli/test_plugin_host.py and all five existing
importers (test_cron_plugin_api, test_plugin_home_moved,
test_plugin_host_prompt_sections, test_plugin_session_client,
test_plugin_session_turn_origin) to import from plugin_host_support.
No re-export shim is left in test_plugin_host.py.
2026-11-27 20:53:18 -06:00

72 lines
3.1 KiB
Python

"""Shared plugin fixtures and profile-home setup for plugin host tests."""
import hermes_yaml as yaml
from hermes_cli import plugins as plugins_mod
PROBE_PLUGIN = '''
import json, os
from agent.image_gen_provider import ImageGenProvider
class Painter(ImageGenProvider):
@property
def name(self):
return "hostpainter"
def generate(self, prompt, aspect_ratio="landscape", **kw):
return {"success": True, "image": f"{prompt}@{os.getpid()}"}
SEEN = []
def register(ctx):
ctx.register_tool(name="hostprobe_pid", toolset="hostprobe", schema={"name": "hostprobe_pid",
"description": "d", "parameters": {"type": "object", "properties": {}}},
handler=lambda args, **kw: json.dumps({"pid": os.getpid(), "seen": SEEN, "x": args.get("x")}))
ctx.register_tool(name="hostprobe_nested", toolset="hostprobe", schema={"name": "hostprobe_nested",
"description": "d", "parameters": {"type": "object", "properties": {}}},
handler=lambda args, **kw: ctx.dispatch_tool("hostprobe_pid", {"x": "nested"}))
ctx.register_tool(name="hostprobe_crash", toolset="hostprobe", schema={"name": "hostprobe_crash",
"description": "d", "parameters": {"type": "object", "properties": {}}},
handler=lambda args, **kw: os._exit(3))
ctx.register_hook("post_tool_call", lambda tool_name=None, **kw: SEEN.append(tool_name))
ctx.register_image_gen_provider(Painter())
'''
def _home_with_plugins(tmp_path, monkeypatch, plugins: dict, isolation="host"):
home = tmp_path / ".hermes"
home.mkdir(parents=True, exist_ok=True)
for name, body in plugins.items():
plugin_dir = home / "plugins" / name
plugin_dir.mkdir(parents=True)
(plugin_dir / "plugin.yaml").write_text(yaml.safe_dump({"name": name, "version": "1.0"}), encoding="utf-8")
(plugin_dir / "__init__.py").write_text(body, encoding="utf-8")
(home / "config.yaml").write_text(yaml.safe_dump(
{"plugins": {"enabled": list(plugins), "isolation": isolation}}), encoding="utf-8")
empty_bundled = tmp_path / "bundled"
empty_bundled.mkdir()
monkeypatch.setenv("HOME", str(tmp_path / "os-home"))
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setattr(plugins_mod, "get_bundled_plugins_dir", lambda: empty_bundled)
return home
MODEL_PROVIDER_PLUGIN = '''
import os
from providers import register_provider
from providers.base import ProviderProfile
class HostModel(ProviderProfile):
def build_extra_body(self, *, session_id=None, **context):
return {"pid": os.getpid(), "session_id": session_id}
register_provider(HostModel(name="hostmodel", base_url="https://hostmodel.example/v1", env_vars=("HOSTMODEL_KEY",)))
'''
def _hosted_model_provider(home) -> None:
"""Install ``MODEL_PROVIDER_PLUGIN`` as the model-provider plugin ``hostmodel`` under *home*."""
plugin_dir = home / "plugins" / "model-providers" / "hostmodel"
plugin_dir.mkdir(parents=True)
(plugin_dir / "plugin.yaml").write_text("name: hostmodel\nkind: model-provider\n", encoding="utf-8")
(plugin_dir / "__init__.py").write_text(MODEL_PROVIDER_PLUGIN, encoding="utf-8")