test_opencode_go_validation_fallback.py
1 """Tests for the static-catalog fallback in validate_requested_model. 2 3 OpenCode Go and OpenCode Zen publish an OpenAI-compatible API at paths that do 4 NOT expose ``/models`` (the path returns the marketing site's HTML 404). This 5 caused ``validate_requested_model`` to return ``accepted=False`` for every 6 model on those providers, which in turn made ``switch_model()`` fail and the 7 gateway's ``/model <name> --provider opencode-go`` command never write to 8 ``_session_model_overrides``. 9 10 These tests cover the catalog-fallback path: when ``fetch_api_models`` returns 11 ``None``, the validator must consult ``provider_model_ids()`` for the provider 12 (populated from ``_PROVIDER_MODELS``) rather than rejecting outright. 13 """ 14 15 from unittest.mock import patch 16 17 from hermes_cli.models import validate_requested_model 18 19 20 _UNREACHABLE_PROBE = { 21 "models": None, 22 "probed_url": "https://opencode.ai/zen/go/v1/models", 23 "resolved_base_url": "https://opencode.ai/zen/go/v1", 24 "suggested_base_url": None, 25 "used_fallback": False, 26 } 27 28 29 def _patched(func): 30 """Decorator: force fetch_api_models / probe_api_models to simulate an 31 unreachable /models endpoint, proving the catalog path is used.""" 32 def wrapper(*args, **kwargs): 33 with patch("hermes_cli.models.fetch_api_models", return_value=None), \ 34 patch("hermes_cli.models.probe_api_models", return_value=_UNREACHABLE_PROBE): 35 return func(*args, **kwargs) 36 wrapper.__name__ = func.__name__ 37 return wrapper 38 39 40 # --------------------------------------------------------------------------- 41 # opencode-go: curated catalog in _PROVIDER_MODELS 42 # --------------------------------------------------------------------------- 43 44 45 @_patched 46 def test_opencode_go_known_model_accepted(): 47 """A model present in the opencode-go curated catalog must be accepted 48 even when /models is unreachable.""" 49 result = validate_requested_model("kimi-k2.6", "opencode-go") 50 assert result["accepted"] is True 51 assert result["persist"] is True 52 assert result["recognized"] is True 53 assert result["message"] is None 54 55 56 @_patched 57 def test_opencode_go_known_model_case_insensitive(): 58 """Catalog lookup is case-insensitive.""" 59 result = validate_requested_model("KIMI-K2.6", "opencode-go") 60 assert result["accepted"] is True 61 assert result["recognized"] is True 62 63 64 @_patched 65 def test_opencode_go_typo_auto_corrected(): 66 """A close typo (>= 0.9 similarity) is auto-corrected to the catalog 67 entry.""" 68 # 'kimi-k2.55' vs 'kimi-k2.5' ratio ≈ 0.95 — within the 0.9 cutoff. 69 result = validate_requested_model("kimi-k2.55", "opencode-go") 70 assert result["accepted"] is True 71 assert result["recognized"] is True 72 assert result.get("corrected_model") == "kimi-k2.5" 73 74 75 @_patched 76 def test_opencode_go_unknown_model_accepted_with_suggestion(): 77 """An unknown model that has a medium-similarity match (>= 0.5 but < 0.9) 78 is accepted with recognized=False and a 'similar models' hint. The key 79 invariant: the gateway MUST be able to persist this override, so 80 accepted/persist must both be True.""" 81 # 'kimi-k3-preview' vs 'kimi-k2.6' — similar enough to suggest, not to auto-correct. 82 result = validate_requested_model("kimi-k3-preview", "opencode-go") 83 assert result["accepted"] is True 84 assert result["persist"] is True 85 assert result["recognized"] is False 86 assert "kimi-k3-preview" in result["message"] 87 assert "curated catalog" in result["message"] 88 89 90 @_patched 91 def test_opencode_go_totally_unknown_model_still_accepted(): 92 """A model with zero similarity to the catalog is still accepted (no 93 suggestion line) so the user can try a model that hasn't made it into the 94 curated list yet.""" 95 result = validate_requested_model("some-brand-new-model", "opencode-go") 96 assert result["accepted"] is True 97 assert result["persist"] is True 98 assert result["recognized"] is False 99 # No suggestion text (no close matches) 100 assert "Similar models" not in result["message"] 101 assert "opencode" in result["message"].lower() or "opencode go" in result["message"].lower() 102 103 104 # --------------------------------------------------------------------------- 105 # opencode-zen: same pattern as opencode-go 106 # --------------------------------------------------------------------------- 107 108 109 @_patched 110 def test_opencode_zen_known_model_accepted(): 111 """opencode-zen also uses _PROVIDER_MODELS; kimi-k2 is in its catalog.""" 112 result = validate_requested_model("kimi-k2", "opencode-zen") 113 assert result["accepted"] is True 114 assert result["recognized"] is True 115 116 117 # --------------------------------------------------------------------------- 118 # Unknown provider with no catalog: soft-accept (honors the comment's intent) 119 # --------------------------------------------------------------------------- 120 121 122 @_patched 123 def test_provider_without_catalog_accepts_with_warning(): 124 """When a provider has no entry in _PROVIDER_MODELS and /models is 125 unreachable, accept the model with a 'Note:' warning rather than reject. 126 This matches the in-code comment: 'Accept and persist, but warn so typos 127 don't silently break things.'""" 128 # Use a made-up provider name that won't resolve to any catalog. 129 result = validate_requested_model("some-model", "provider-that-does-not-exist") 130 assert result["accepted"] is True 131 assert result["persist"] is True 132 assert result["recognized"] is False 133 assert "Note:" in result["message"]