test_regression_16767.py
1 import pytest 2 import sys 3 from unittest.mock import patch 4 from pathlib import Path 5 6 import hermes_cli.model_switch as ms 7 from hermes_cli.model_switch import DirectAlias 8 from hermes_cli.runtime_provider import _resolve_named_custom_runtime 9 10 def test_ensure_direct_aliases_mutates_in_place(monkeypatch): 11 """_ensure_direct_aliases mutates DIRECT_ALIASES in place (guards against rebinding regression).""" 12 # Ensure we start with an empty but existing dict to check for mutation vs rebinding 13 ms.DIRECT_ALIASES.clear() 14 initial_id = id(ms.DIRECT_ALIASES) 15 16 mock_data = { 17 "my-custom-alias": DirectAlias("custom-model:v1", "custom", "https://example.com/v1") 18 } 19 monkeypatch.setattr(ms, "_load_direct_aliases", lambda: mock_data) 20 21 ms._ensure_direct_aliases() 22 23 assert id(ms.DIRECT_ALIASES) == initial_id, f"DIRECT_ALIASES was rebound (ID changed from {initial_id} to {id(ms.DIRECT_ALIASES)})" 24 assert "my-custom-alias" in ms.DIRECT_ALIASES 25 assert ms.DIRECT_ALIASES["my-custom-alias"].model == "custom-model:v1" 26 27 def test_chat_provider_argparse_acceptance(monkeypatch): 28 """chat --provider <user-defined> is accepted by argparse (guards against restrictive choices).""" 29 recorded: dict[str, str] = {} 30 31 # Mock cmd_chat to record the provider passed to it 32 def mock_cmd_chat(args): 33 recorded["provider"] = args.provider 34 35 monkeypatch.setattr("hermes_cli.main.cmd_chat", mock_cmd_chat) 36 monkeypatch.setattr(sys, "argv", ["hermes", "chat", "--provider", "my-custom-key"]) 37 38 from hermes_cli.main import main 39 main() 40 41 assert recorded["provider"] == "my-custom-key" 42 43 def test_resolve_named_custom_runtime_honors_explicit_base_url(monkeypatch): 44 """_resolve_named_custom_runtime honors (provider='custom', explicit_base_url=...).""" 45 # Mock has_usable_secret to recognize our test key 46 monkeypatch.setattr("hermes_cli.runtime_provider.has_usable_secret", lambda x: x == "test-api-key") 47 48 result = _resolve_named_custom_runtime( 49 requested_provider="custom", 50 explicit_api_key="test-api-key", 51 explicit_base_url="http://example.test:1234/v1" 52 ) 53 54 assert result is not None 55 assert result["base_url"] == "http://example.test:1234/v1" 56 assert result["provider"] == "custom" 57 assert result["api_key"] == "test-api-key" 58 assert result["source"] == "direct-alias"