fix(tests): add autouse cache-isolation fixture to get_available_models test files

Fixes the CI failure introduced by #817: test_model_resolver::test_custom_endpoint_uses_model_config_api_key_for_model_discovery was failing with KeyError: 'auth' due to the 60s TTL cache in get_available_models() being populated by test_byok_model_dropdown.py tests that ran earlier. Added autouse _isolate_models_cache fixture to 5 test files. Full suite 1747/1747, QA harness green.
This commit is contained in:
nesquena-hermes
2026-04-21 17:41:05 -07:00
committed by GitHub
parent 8f1f582caf
commit 2d8bccdd96
5 changed files with 84 additions and 0 deletions

View File

@@ -5,9 +5,24 @@ When a custom_providers entry carries a `name` field (e.g. "Agent37"), the
web UI model picker should show that name as the group header rather than the web UI model picker should show that name as the group header rather than the
generic "Custom" label. generic "Custom" label.
""" """
import pytest
import api.config as config import api.config as config
@pytest.fixture(autouse=True)
def _isolate_models_cache():
"""Invalidate the models TTL cache before and after every test in this file."""
try:
config.invalidate_models_cache()
except Exception:
pass
yield
try:
config.invalidate_models_cache()
except Exception:
pass
def _models_with_cfg(model_cfg=None, custom_providers=None, active_provider=None): def _models_with_cfg(model_cfg=None, custom_providers=None, active_provider=None):
"""Temporarily patch config.cfg, call get_available_models(), restore. """Temporarily patch config.cfg, call get_available_models(), restore.

View File

@@ -3,6 +3,20 @@ import pytest
import api.config as _cfg import api.config as _cfg
@pytest.fixture(autouse=True)
def _isolate_models_cache():
"""Invalidate the models TTL cache before and after every test in this file."""
try:
_cfg.invalidate_models_cache()
except Exception:
pass
yield
try:
_cfg.invalidate_models_cache()
except Exception:
pass
def _available_models_with_cfg(cfg_override): def _available_models_with_cfg(cfg_override):
"""Helper: temporarily patch config.cfg, call get_available_models(), restore.""" """Helper: temporarily patch config.cfg, call get_available_models(), restore."""
old_cfg = dict(_cfg.cfg) old_cfg = dict(_cfg.cfg)

View File

@@ -8,9 +8,24 @@ Covers:
- minimax/MiniMax-M2.7 (slash format) is routed via openrouter when active provider differs - minimax/MiniMax-M2.7 (slash format) is routed via openrouter when active provider differs
""" """
import os import os
import pytest
import api.config as config import api.config as config
@pytest.fixture(autouse=True)
def _isolate_models_cache():
"""Invalidate the models TTL cache before and after every test in this file."""
try:
config.invalidate_models_cache()
except Exception:
pass
yield
try:
config.invalidate_models_cache()
except Exception:
pass
# ── Helper ──────────────────────────────────────────────────────────────────── # ── Helper ────────────────────────────────────────────────────────────────────
def _resolve_with_config(model_id, provider=None, base_url=None): def _resolve_with_config(model_id, provider=None, base_url=None):

View File

@@ -3,6 +3,7 @@ Tests for resolve_model_provider() model routing logic.
Verifies that model IDs are correctly resolved to (model, provider, base_url) Verifies that model IDs are correctly resolved to (model, provider, base_url)
tuples for different provider configurations. tuples for different provider configurations.
""" """
import pytest
import api.config as config import api.config as config
@@ -160,6 +161,30 @@ def test_custom_provider_model_with_slash_routes_to_named_custom_provider():
# ── get_available_models() @provider: hint behaviour ────────────────────── # ── get_available_models() @provider: hint behaviour ──────────────────────
@pytest.fixture(autouse=True)
def _isolate_models_cache():
"""Invalidate the models TTL cache before and after every test in this file.
Several helpers here mutate ``config.cfg`` in-memory and call
``get_available_models()``. Without this guard, a prior test that called
``get_available_models()`` leaves a 60-second TTL cache entry; the next
test that mutates cfg and calls the function gets a cache hit instead of
running the function body, causing silently wrong results (e.g. the
``test_custom_endpoint_uses_model_config_api_key_for_model_discovery``
``KeyError: 'auth'`` on CI where ``urlopen`` is never reached).
"""
try:
config.invalidate_models_cache()
except Exception:
pass
yield
try:
config.invalidate_models_cache()
except Exception:
pass
def _available_models_with_provider(provider): def _available_models_with_provider(provider):
"""Helper: temporarily set active_provider in config.""" """Helper: temporarily set active_provider in config."""
old_cfg = dict(config.cfg) old_cfg = dict(config.cfg)

View File

@@ -6,9 +6,24 @@ env-var fallback detection.
import os import os
import sys import sys
import types import types
import pytest
import api.config as config import api.config as config
@pytest.fixture(autouse=True)
def _isolate_models_cache():
"""Invalidate the models TTL cache before and after every test in this file."""
try:
config.invalidate_models_cache()
except Exception:
pass
yield
try:
config.invalidate_models_cache()
except Exception:
pass
# ── Provider registration ───────────────────────────────────────────── # ── Provider registration ─────────────────────────────────────────────
def test_opencode_zen_in_provider_display(): def test_opencode_zen_in_provider_display():