test: add unit tests for 8 untested modules (batch 3) (#191)
* test: add unit tests for 8 untested modules (batch 3) New test files (143 tests total): - tools/debug_helpers.py: DebugSession enable/disable, log, save, session info - tools/skills_guard.py: scan_file, scan_skill, trust levels, install policy, structural checks - tools/skills_sync.py: manifest read/write, skill discovery, sync logic - gateway/sticker_cache.py: cache CRUD, sticker injection text builders - gateway/channel_directory.py: channel resolution, display formatting, session building - gateway/hooks.py: hook discovery, sync/async emit, wildcard matching - gateway/mirror.py: session lookup, JSONL append, mirror_to_session - honcho_integration/client.py: config from env/file, session name resolution, linked workspaces Also documents a gap in skills_guard: multi-word prompt injection variants like "ignore all prior instructions" bypass the regex scanner. * test: strengthen sticker injection tests with exact format assertions Replace loose "contains" checks with exact output matching for build_sticker_injection and build_animated_sticker_injection. Add edge cases: set_name without emoji, empty description, empty emoji. * test: remove skills_guard gap-documenting test to avoid conflict with fix PR
This commit is contained in:
parent
4d6f380bd1
commit
3b745633e4
9 changed files with 1554 additions and 0 deletions
213
tests/gateway/test_hooks.py
Normal file
213
tests/gateway/test_hooks.py
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
"""Tests for gateway/hooks.py — event hook system."""
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from gateway.hooks import HookRegistry
|
||||
|
||||
|
||||
def _create_hook(hooks_dir, hook_name, events, handler_code):
|
||||
"""Helper to create a hook directory with HOOK.yaml and handler.py."""
|
||||
hook_dir = hooks_dir / hook_name
|
||||
hook_dir.mkdir(parents=True)
|
||||
(hook_dir / "HOOK.yaml").write_text(
|
||||
f"name: {hook_name}\n"
|
||||
f"description: Test hook\n"
|
||||
f"events: {events}\n"
|
||||
)
|
||||
(hook_dir / "handler.py").write_text(handler_code)
|
||||
return hook_dir
|
||||
|
||||
|
||||
class TestHookRegistryInit:
|
||||
def test_empty_registry(self):
|
||||
reg = HookRegistry()
|
||||
assert reg.loaded_hooks == []
|
||||
assert reg._handlers == {}
|
||||
|
||||
|
||||
class TestDiscoverAndLoad:
|
||||
def test_loads_valid_hook(self, tmp_path):
|
||||
_create_hook(tmp_path, "my-hook", '["agent:start"]',
|
||||
"def handle(event_type, context):\n pass\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 1
|
||||
assert reg.loaded_hooks[0]["name"] == "my-hook"
|
||||
assert "agent:start" in reg.loaded_hooks[0]["events"]
|
||||
|
||||
def test_skips_missing_hook_yaml(self, tmp_path):
|
||||
hook_dir = tmp_path / "bad-hook"
|
||||
hook_dir.mkdir()
|
||||
(hook_dir / "handler.py").write_text("def handle(e, c): pass\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 0
|
||||
|
||||
def test_skips_missing_handler_py(self, tmp_path):
|
||||
hook_dir = tmp_path / "bad-hook"
|
||||
hook_dir.mkdir()
|
||||
(hook_dir / "HOOK.yaml").write_text("name: bad\nevents: ['agent:start']\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 0
|
||||
|
||||
def test_skips_no_events(self, tmp_path):
|
||||
hook_dir = tmp_path / "empty-hook"
|
||||
hook_dir.mkdir()
|
||||
(hook_dir / "HOOK.yaml").write_text("name: empty\nevents: []\n")
|
||||
(hook_dir / "handler.py").write_text("def handle(e, c): pass\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 0
|
||||
|
||||
def test_skips_no_handle_function(self, tmp_path):
|
||||
hook_dir = tmp_path / "no-handle"
|
||||
hook_dir.mkdir()
|
||||
(hook_dir / "HOOK.yaml").write_text("name: no-handle\nevents: ['agent:start']\n")
|
||||
(hook_dir / "handler.py").write_text("def something_else(): pass\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 0
|
||||
|
||||
def test_nonexistent_hooks_dir(self, tmp_path):
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path / "nonexistent"):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 0
|
||||
|
||||
def test_multiple_hooks(self, tmp_path):
|
||||
_create_hook(tmp_path, "hook-a", '["agent:start"]',
|
||||
"def handle(e, c): pass\n")
|
||||
_create_hook(tmp_path, "hook-b", '["session:start", "session:reset"]',
|
||||
"def handle(e, c): pass\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg.loaded_hooks) == 2
|
||||
|
||||
|
||||
class TestEmit:
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_emit_calls_sync_handler(self, tmp_path):
|
||||
results = []
|
||||
|
||||
_create_hook(tmp_path, "sync-hook", '["agent:start"]',
|
||||
"results = []\n"
|
||||
"def handle(event_type, context):\n"
|
||||
" results.append(event_type)\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
# Inject our results list into the handler's module globals
|
||||
handler_fn = reg._handlers["agent:start"][0]
|
||||
handler_fn.__globals__["results"] = results
|
||||
|
||||
await reg.emit("agent:start", {"test": True})
|
||||
assert "agent:start" in results
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_emit_calls_async_handler(self, tmp_path):
|
||||
results = []
|
||||
|
||||
hook_dir = tmp_path / "async-hook"
|
||||
hook_dir.mkdir()
|
||||
(hook_dir / "HOOK.yaml").write_text(
|
||||
"name: async-hook\nevents: ['agent:end']\n"
|
||||
)
|
||||
(hook_dir / "handler.py").write_text(
|
||||
"import asyncio\n"
|
||||
"results = []\n"
|
||||
"async def handle(event_type, context):\n"
|
||||
" results.append(event_type)\n"
|
||||
)
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
handler_fn = reg._handlers["agent:end"][0]
|
||||
handler_fn.__globals__["results"] = results
|
||||
|
||||
await reg.emit("agent:end", {})
|
||||
assert "agent:end" in results
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_wildcard_matching(self, tmp_path):
|
||||
results = []
|
||||
|
||||
_create_hook(tmp_path, "wildcard-hook", '["command:*"]',
|
||||
"results = []\n"
|
||||
"def handle(event_type, context):\n"
|
||||
" results.append(event_type)\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
handler_fn = reg._handlers["command:*"][0]
|
||||
handler_fn.__globals__["results"] = results
|
||||
|
||||
await reg.emit("command:reset", {})
|
||||
assert "command:reset" in results
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_no_handlers_for_event(self, tmp_path):
|
||||
reg = HookRegistry()
|
||||
# Should not raise
|
||||
await reg.emit("unknown:event", {})
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_handler_error_does_not_propagate(self, tmp_path):
|
||||
_create_hook(tmp_path, "bad-hook", '["agent:start"]',
|
||||
"def handle(event_type, context):\n"
|
||||
" raise ValueError('boom')\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
# Should not raise even though handler throws
|
||||
await reg.emit("agent:start", {})
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_emit_default_context(self, tmp_path):
|
||||
captured = []
|
||||
|
||||
_create_hook(tmp_path, "ctx-hook", '["agent:start"]',
|
||||
"captured = []\n"
|
||||
"def handle(event_type, context):\n"
|
||||
" captured.append(context)\n")
|
||||
|
||||
reg = HookRegistry()
|
||||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
handler_fn = reg._handlers["agent:start"][0]
|
||||
handler_fn.__globals__["captured"] = captured
|
||||
|
||||
await reg.emit("agent:start") # no context arg
|
||||
assert captured[0] == {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue