`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.
Parallel directories for one source package, folded into the mirror:
tests/acp -> tests/acp_adapter (its __init__/conftest move with it)
tests/cli -> tests/hermes_cli (prompt_toolkit fixture merged into
hermes_cli/conftest.py)
tests/run_agent -> tests/agent (backoff fixture becomes
agent/conftest.py)
tests/relay -> tests/gateway/relay
tests/state -> tests/hermes_state
246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.
Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.
Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).
Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
191 lines
7.5 KiB
Python
191 lines
7.5 KiB
Python
"""Tests for the vision-aware image preprocessing in run_agent.py.
|
|
|
|
Covers:
|
|
|
|
* ``_prepare_anthropic_messages_for_api`` — passes image parts through
|
|
unchanged when the active model reports ``supports_vision=True`` (the
|
|
adapter handles them natively), and falls back to text-description
|
|
replacement when the model lacks vision.
|
|
|
|
* ``_prepare_messages_for_non_vision_model`` — the mirror method for the
|
|
chat.completions / codex_responses paths. Same contract.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
from run_agent import AIAgent
|
|
|
|
|
|
def _make_agent() -> AIAgent:
|
|
"""Build a bare-bones AIAgent instance without running __init__.
|
|
|
|
Avoids the heavy provider/credential setup for these pure-method tests.
|
|
"""
|
|
agent = object.__new__(AIAgent)
|
|
agent.provider = "anthropic"
|
|
agent.model = "claude-sonnet-4"
|
|
agent._anthropic_image_fallback_cache = {}
|
|
return agent
|
|
|
|
|
|
IMG_PARTS_USER_MSG = {
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "What's in this image?"},
|
|
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}},
|
|
],
|
|
}
|
|
|
|
PLAIN_USER_MSG = {"role": "user", "content": "hello, no images here"}
|
|
|
|
|
|
# ─── _prepare_anthropic_messages_for_api ─────────────────────────────────────
|
|
|
|
|
|
class TestPrepareAnthropicMessages:
|
|
def test_no_images_passes_through(self):
|
|
agent = _make_agent()
|
|
msgs = [PLAIN_USER_MSG]
|
|
out = agent._prepare_anthropic_messages_for_api(msgs)
|
|
assert out is msgs # unchanged reference
|
|
|
|
def test_vision_capable_passes_images_through(self):
|
|
"""The Anthropic adapter handles image_url/input_image natively."""
|
|
agent = _make_agent()
|
|
with patch.object(agent, "_model_supports_vision", return_value=True):
|
|
out = agent._prepare_anthropic_messages_for_api([IMG_PARTS_USER_MSG])
|
|
# Passes through unchanged — image_url parts still present.
|
|
assert out[0]["content"][1]["type"] == "image_url"
|
|
|
|
def test_non_vision_replaces_images_with_text(self):
|
|
agent = _make_agent()
|
|
with patch.object(agent, "_model_supports_vision", return_value=False), \
|
|
patch.object(
|
|
agent,
|
|
"_describe_image_for_anthropic_fallback",
|
|
return_value="[Image description: a cat]",
|
|
):
|
|
out = agent._prepare_anthropic_messages_for_api([IMG_PARTS_USER_MSG])
|
|
# Content collapsed to a string containing the description + user text.
|
|
content = out[0]["content"]
|
|
assert isinstance(content, str)
|
|
assert "[Image description: a cat]" in content
|
|
assert "What's in this image?" in content
|
|
# No more image parts.
|
|
assert "image_url" not in content
|
|
|
|
|
|
# ─── _prepare_messages_for_non_vision_model ──────────────────────────────────
|
|
|
|
|
|
class TestPrepareMessagesForNonVision:
|
|
def test_no_images_passes_through(self):
|
|
agent = _make_agent()
|
|
msgs = [PLAIN_USER_MSG]
|
|
out = agent._prepare_messages_for_non_vision_model(msgs)
|
|
assert out is msgs
|
|
|
|
def test_vision_capable_passes_through(self):
|
|
"""For vision-capable models on chat.completions path, provider handles pixels."""
|
|
agent = _make_agent()
|
|
agent.provider = "openrouter"
|
|
agent.model = "anthropic/claude-sonnet-4"
|
|
with patch.object(agent, "_model_supports_vision", return_value=True):
|
|
out = agent._prepare_messages_for_non_vision_model([IMG_PARTS_USER_MSG])
|
|
assert out[0]["content"][1]["type"] == "image_url"
|
|
|
|
def test_non_vision_strips_images(self):
|
|
agent = _make_agent()
|
|
agent.provider = "openrouter"
|
|
agent.model = "qwen/qwen3-235b-a22b"
|
|
with patch.object(agent, "_model_supports_vision", return_value=False), \
|
|
patch.object(
|
|
agent,
|
|
"_describe_image_for_anthropic_fallback",
|
|
return_value="[Image description: a dog]",
|
|
):
|
|
out = agent._prepare_messages_for_non_vision_model([IMG_PARTS_USER_MSG])
|
|
content = out[0]["content"]
|
|
assert isinstance(content, str)
|
|
assert "[Image description: a dog]" in content
|
|
assert "image_url" not in content
|
|
|
|
def test_multiple_messages_with_mixed_content(self):
|
|
agent = _make_agent()
|
|
agent.model = "qwen/qwen3-235b"
|
|
msgs = [
|
|
{"role": "user", "content": "first turn"},
|
|
{"role": "assistant", "content": "ack"},
|
|
IMG_PARTS_USER_MSG,
|
|
]
|
|
with patch.object(agent, "_model_supports_vision", return_value=False), \
|
|
patch.object(
|
|
agent,
|
|
"_describe_image_for_anthropic_fallback",
|
|
return_value="[Image: thing]",
|
|
):
|
|
out = agent._prepare_messages_for_non_vision_model(msgs)
|
|
# First two messages unchanged (no images), third stripped.
|
|
assert out[0]["content"] == "first turn"
|
|
assert out[1]["content"] == "ack"
|
|
assert isinstance(out[2]["content"], str)
|
|
assert "[Image: thing]" in out[2]["content"]
|
|
|
|
|
|
# ─── _model_supports_vision ──────────────────────────────────────────────────
|
|
|
|
|
|
class TestModelSupportsVision:
|
|
def test_missing_provider_or_model_returns_false(self):
|
|
agent = _make_agent()
|
|
agent.provider = ""
|
|
agent.model = "claude-sonnet-4"
|
|
assert agent._model_supports_vision() is False
|
|
agent.provider = "anthropic"
|
|
agent.model = ""
|
|
assert agent._model_supports_vision() is False
|
|
|
|
def test_uses_get_model_capabilities(self):
|
|
agent = _make_agent()
|
|
fake_caps = MagicMock()
|
|
fake_caps.supports_vision = True
|
|
with patch("agent.models_dev.get_model_capabilities", return_value=fake_caps):
|
|
assert agent._model_supports_vision() is True
|
|
fake_caps.supports_vision = False
|
|
with patch("agent.models_dev.get_model_capabilities", return_value=fake_caps):
|
|
assert agent._model_supports_vision() is False
|
|
|
|
def test_none_caps_returns_false(self):
|
|
agent = _make_agent()
|
|
with patch("agent.models_dev.get_model_capabilities", return_value=None):
|
|
assert agent._model_supports_vision() is False
|
|
|
|
|
|
def test_top_level_model_override_wins(self):
|
|
agent = _make_agent()
|
|
agent.provider = "custom"
|
|
agent.model = "my-llava"
|
|
with patch("hermes_cli.config.load_config", return_value={"model": {"supports_vision": True}}), \
|
|
patch("agent.models_dev.get_model_capabilities", return_value=None):
|
|
assert agent._model_supports_vision() is True
|
|
|
|
|
|
def test_named_custom_provider_resolved_via_config_provider(self):
|
|
# Named custom providers get runtime self.provider rewritten to
|
|
# "custom" while the config keeps the original name under
|
|
# model.provider. The override must still resolve.
|
|
agent = _make_agent()
|
|
agent.provider = "custom"
|
|
agent.model = "my-llava"
|
|
cfg = {
|
|
"model": {"provider": "my-vllm", "default": "my-llava"},
|
|
"providers": {"my-vllm": {"models": {"my-llava": {"supports_vision": True}}}},
|
|
}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg), \
|
|
patch("agent.models_dev.get_model_capabilities", return_value=None):
|
|
assert agent._model_supports_vision() is True
|
|
|