Files
hermes-agent/tests/agent/test_meta_agent_init.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`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.
2026-09-13 09:18:02 -07:00

201 lines
6.0 KiB
Python

"""Tests for AIAgent provider=meta host-mandated api_mode."""
import pytest
def test_agent_init_meta_base_url_implies_codex_responses():
from run_agent import AIAgent
agent = AIAgent(
provider="meta",
base_url="https://api.meta.ai/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
api_mode=None,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.api_mode == "codex_responses"
assert agent.provider == "meta"
def test_agent_init_explicit_chat_wins_over_mandate():
from run_agent import AIAgent
agent = AIAgent(
provider="meta",
base_url="https://api.meta.ai/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
api_mode="chat_completions",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.api_mode == "chat_completions"
assert agent.provider == "meta"
def test_agent_init_meta_provider_stays_meta():
from run_agent import AIAgent
agent = AIAgent(
provider="meta",
base_url="https://api.meta.ai/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.provider == "meta"
assert agent.provider != "custom"
def test_agent_init_custom_with_meta_url_also_mandates():
from run_agent import AIAgent
agent = AIAgent(
provider="custom",
base_url="https://api.meta.ai/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.api_mode == "codex_responses"
def test_agent_init_meta_url_case_insensitive():
from run_agent import AIAgent
agent = AIAgent(
provider="meta",
base_url="https://API.META.AI/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.api_mode == "codex_responses"
def test_agent_init_anthropic_url_implies_provider_and_api_mode():
"""Regression for host-mandated api_mode shadowing provider-slug rewrite (#63425).
When provider=None and base_url is api.anthropic.com, the elif chain must
still rewrite provider to "anthropic" before the credential-pool check.
The host-mandated fallback moved to the bottom of the cascade (fix/meta-prompt-caching)
ensures this; the old top-position mandated branch set api_mode without the slug rewrite
and caused credential-pool validation to discard anthropic-scoped pools.
"""
from unittest.mock import MagicMock, patch
from run_agent import AIAgent
with patch("agent.anthropic_adapter.build_anthropic_client", return_value=MagicMock()), patch(
"agent.anthropic_credentials._is_oauth_token", return_value=False
):
agent = AIAgent(
provider=None,
base_url="https://api.anthropic.com/v1",
api_key="sk-test-anthropic",
model="claude-sonnet-4-5",
api_mode=None,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.provider == "anthropic"
assert agent.api_mode == "anthropic_messages"
def test_agent_init_anthropic_url_preserves_credential_pool():
"""Anthropic-scoped credential pool must survive provider auto-detection.
Mirrors tests/agent/test_credential_pool_auto_detect.py at the
public AIAgent surface (provider=None URL detection).
"""
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from run_agent import AIAgent
pool = SimpleNamespace(provider="anthropic")
with patch("agent.anthropic_adapter.build_anthropic_client", return_value=MagicMock()), patch(
"agent.anthropic_credentials._is_oauth_token", return_value=False
):
agent = AIAgent(
provider=None,
base_url="https://api.anthropic.com/v1",
api_key="sk-test-anthropic",
model="claude-sonnet-4-5",
api_mode=None,
credential_pool=pool,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.provider == "anthropic"
assert agent.api_mode == "anthropic_messages"
assert agent._credential_pool is pool
def test_agent_init_meta_url_without_provider_implies_codex_responses():
from run_agent import AIAgent
agent = AIAgent(
provider=None,
base_url="https://api.meta.ai/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
api_mode=None,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.api_mode == "codex_responses"
def test_agent_init_meta_provider_without_meta_url_falls_through_to_chat_completions(monkeypatch):
"""`provider="meta"` without an api.meta.ai base_url falls through to chat_completions.
The Meta wire protocol is URL-driven by design so custom OpenAI-compatible
endpoints configured under `providers.meta` are not broken by forcing Responses API.
"""
from run_agent import AIAgent
monkeypatch.setenv("META_API_KEY", "sk-test-meta")
agent_empty = AIAgent(
provider="meta",
base_url="",
api_key="sk-test-meta",
model="muse-spark-1.2",
api_mode=None,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent_empty.api_mode == "chat_completions"
assert agent_empty.provider == "meta"
agent_custom = AIAgent(
provider="meta",
base_url="https://custom.meta.endpoint.internal/v1",
api_key="sk-test-meta",
model="muse-spark-1.2",
api_mode=None,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent_custom.api_mode == "chat_completions"
assert agent_custom.provider == "meta"