mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
230 lines
8.5 KiB
Python
230 lines
8.5 KiB
Python
"""Tests for video_analyze tool in tools/vision_tools.py."""
|
|
|
|
import asyncio
|
|
import json
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
|
|
from tools.vision_tools import (
|
|
_detect_video_mime_type,
|
|
_video_to_base64_data_url,
|
|
_handle_video_analyze,
|
|
_MAX_VIDEO_BASE64_BYTES,
|
|
video_analyze_tool,
|
|
VIDEO_ANALYZE_SCHEMA,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _detect_video_mime_type
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestDetectVideoMimeType:
|
|
"""Extension-based MIME detection for video files."""
|
|
|
|
def test_mp4(self, tmp_path):
|
|
p = tmp_path / "clip.mp4"
|
|
p.write_bytes(b"\x00" * 10)
|
|
assert _detect_video_mime_type(p) == "video/mp4"
|
|
|
|
def test_webm(self, tmp_path):
|
|
p = tmp_path / "clip.webm"
|
|
p.write_bytes(b"\x00" * 10)
|
|
assert _detect_video_mime_type(p) == "video/webm"
|
|
|
|
|
|
def test_case_insensitive(self, tmp_path):
|
|
p = tmp_path / "clip.MP4"
|
|
p.write_bytes(b"\x00" * 10)
|
|
assert _detect_video_mime_type(p) == "video/mp4"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _video_to_base64_data_url
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestVideoToBase64DataUrl:
|
|
"""Base64 encoding of video files."""
|
|
|
|
def test_produces_data_url(self, tmp_path):
|
|
p = tmp_path / "test.mp4"
|
|
p.write_bytes(b"\x00\x01\x02\x03")
|
|
result = _video_to_base64_data_url(p)
|
|
assert result.startswith("data:video/mp4;base64,")
|
|
|
|
|
|
def test_default_mime_for_unknown_ext(self, tmp_path):
|
|
p = tmp_path / "test.xyz"
|
|
p.write_bytes(b"\x00\x01\x02\x03")
|
|
result = _video_to_base64_data_url(p)
|
|
# Falls back to video/mp4
|
|
assert result.startswith("data:video/mp4;base64,")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Schema validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestVideoAnalyzeSchema:
|
|
"""Schema structure is correct."""
|
|
|
|
def test_schema_name(self):
|
|
assert VIDEO_ANALYZE_SCHEMA["name"] == "video_analyze"
|
|
|
|
|
|
def test_schema_description_mentions_video(self):
|
|
assert "video" in VIDEO_ANALYZE_SCHEMA["description"].lower()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _handle_video_analyze handler
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestHandleVideoAnalyze:
|
|
"""Tests for the registry handler wrapper."""
|
|
|
|
def test_returns_awaitable(self, tmp_path, monkeypatch):
|
|
video_file = tmp_path / "test.mp4"
|
|
video_file.write_bytes(b"\x00" * 100)
|
|
monkeypatch.setenv("AUXILIARY_VIDEO_MODEL", "")
|
|
monkeypatch.setenv("AUXILIARY_VISION_MODEL", "")
|
|
|
|
with patch("tools.vision_tools.video_analyze_tool", new_callable=AsyncMock) as mock_tool:
|
|
mock_tool.return_value = json.dumps({"success": True, "analysis": "test"})
|
|
result = _handle_video_analyze({"video_url": str(video_file), "question": "what is this?"})
|
|
# Should return an awaitable (coroutine)
|
|
assert asyncio.iscoroutine(result)
|
|
# Clean up the unawaited coroutine
|
|
result.close()
|
|
|
|
|
|
def test_falls_back_to_vision_model_env(self, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("AUXILIARY_VIDEO_MODEL", "")
|
|
monkeypatch.setenv("AUXILIARY_VISION_MODEL", "google/gemini-flash")
|
|
|
|
with patch("tools.vision_tools.video_analyze_tool", new_callable=AsyncMock) as mock_tool:
|
|
mock_tool.return_value = json.dumps({"success": True, "analysis": "ok"})
|
|
asyncio.get_event_loop().run_until_complete(
|
|
_handle_video_analyze({"video_url": "/tmp/test.mp4", "question": "test"})
|
|
)
|
|
args = mock_tool.call_args[0]
|
|
assert args[2] == "google/gemini-flash"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# video_analyze_tool — integration-style tests with mocked LLM
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestVideoAnalyzeTool:
|
|
"""Core video analysis function tests."""
|
|
|
|
def _run(self, coro):
|
|
return asyncio.get_event_loop().run_until_complete(coro)
|
|
|
|
def test_local_file_success(self, tmp_path, monkeypatch):
|
|
"""Analyze a local video file — happy path."""
|
|
video = tmp_path / "demo.mp4"
|
|
video.write_bytes(b"\x00" * 1024)
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.choices = [MagicMock()]
|
|
mock_response.choices[0].message.content = "A short video showing a demo."
|
|
|
|
with patch("tools.vision_tools.async_call_llm", new_callable=AsyncMock, return_value=mock_response):
|
|
with patch("tools.vision_tools.extract_content_or_reasoning", return_value="A short video showing a demo."):
|
|
result = self._run(video_analyze_tool(str(video), "What is this?"))
|
|
|
|
data = json.loads(result)
|
|
assert data["success"] is True
|
|
assert "demo" in data["analysis"].lower()
|
|
|
|
def test_local_file_read_guard_blocks_env_via_video_extension(self, tmp_path):
|
|
"""A .env file symlinked with a video extension must still be blocked.
|
|
|
|
_detect_video_mime_type only checks the file extension, not file
|
|
content, so without a read guard a model could point video_url at
|
|
any credential-store file (renamed/symlinked to look like a video)
|
|
and have its raw bytes base64-encoded and sent to the vision
|
|
provider. Regression for the shared agent.file_safety chokepoint
|
|
added to video_analyze_tool's local-file branch.
|
|
"""
|
|
secret = tmp_path / ".env"
|
|
secret.write_text("OPENAI_API_KEY=sk-super-secret\n", encoding="utf-8")
|
|
disguised = tmp_path / "video.mp4"
|
|
disguised.symlink_to(secret)
|
|
|
|
with patch("tools.vision_tools.async_call_llm", new_callable=AsyncMock) as mock_llm:
|
|
result = self._run(video_analyze_tool(str(disguised), "What is this?"))
|
|
|
|
data = json.loads(result)
|
|
assert data["success"] is False
|
|
assert "secret-bearing environment file" in data["error"]
|
|
mock_llm.assert_not_awaited()
|
|
|
|
|
|
def test_unsupported_format(self, tmp_path):
|
|
"""Unsupported extension raises error."""
|
|
video = tmp_path / "clip.flv"
|
|
video.write_bytes(b"\x00" * 100)
|
|
|
|
result = self._run(video_analyze_tool(str(video), "What is this?"))
|
|
data = json.loads(result)
|
|
assert data["success"] is False
|
|
assert "unsupported video format" in data["analysis"].lower()
|
|
|
|
|
|
def test_api_message_format(self, tmp_path):
|
|
"""Verify the message sent to LLM uses video_url content type."""
|
|
video = tmp_path / "test.mp4"
|
|
video.write_bytes(b"\x00" * 100)
|
|
|
|
captured_kwargs = {}
|
|
|
|
async def capture_llm(**kwargs):
|
|
captured_kwargs.update(kwargs)
|
|
mock_response = MagicMock()
|
|
mock_response.choices = [MagicMock()]
|
|
mock_response.choices[0].message.content = "OK"
|
|
return mock_response
|
|
|
|
with patch("tools.vision_tools.async_call_llm", side_effect=capture_llm):
|
|
with patch("tools.vision_tools.extract_content_or_reasoning", return_value="OK"):
|
|
self._run(video_analyze_tool(str(video), "Describe this"))
|
|
|
|
messages = captured_kwargs["messages"]
|
|
assert len(messages) == 1
|
|
content = messages[0]["content"]
|
|
assert len(content) == 2
|
|
assert content[0]["type"] == "text"
|
|
assert content[1]["type"] == "video_url"
|
|
assert "video_url" in content[1]
|
|
assert content[1]["video_url"]["url"].startswith("data:video/mp4;base64,")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Toolset registration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestVideoToolsetRegistration:
|
|
"""Verify the tool is registered correctly."""
|
|
|
|
def test_registered_in_video_toolset(self):
|
|
from tools.registry import registry
|
|
entry = registry.get_entry("video_analyze")
|
|
assert entry is not None
|
|
assert entry.toolset == "video"
|
|
assert entry.is_async is True
|
|
assert entry.emoji == "🎬"
|
|
|
|
|
|
def test_in_video_toolset_definition(self):
|
|
"""Toolset 'video' should contain video_analyze."""
|
|
from toolsets import TOOLSETS
|
|
assert "video" in TOOLSETS
|
|
assert "video_analyze" in TOOLSETS["video"]["tools"]
|