Centralize Hydrabase enablement

Move Hydrabase availability checks into metadata_service so source resolution owns the policy. Keep web_server delegating to the centralized helper and add tests for the enabled/disabled cases.
This commit is contained in:
Antti Kettunen 2026-04-18 18:47:54 +03:00
parent 2b575a59ae
commit cca396e7bd
3 changed files with 58 additions and 12 deletions

View file

@ -451,18 +451,32 @@ def get_discogs_client(token: Optional[str] = None):
return client return client
def get_hydrabase_client(allow_fallback: bool = True): def is_hydrabase_enabled() -> bool:
"""Return current Hydrabase client if connected. """Return True when Hydrabase is connected and allowed for metadata use."""
try:
import importlib
ws = importlib.import_module('web_server')
client = getattr(ws, 'hydrabase_client', None)
if not client or not client.is_connected():
return False
return bool(getattr(ws, 'dev_mode_enabled', False))
except Exception:
return False
def get_hydrabase_client(allow_fallback: bool = True, require_enabled: bool = True):
"""Return current Hydrabase client if connected and enabled.
If allow_fallback is True, return iTunes fallback when Hydrabase is not If allow_fallback is True, return iTunes fallback when Hydrabase is not
connected. If False, return None instead. connected or not enabled. If False, return None instead.
""" """
try: try:
import importlib import importlib
ws = importlib.import_module('web_server') ws = importlib.import_module('web_server')
client = getattr(ws, 'hydrabase_client', None) client = getattr(ws, 'hydrabase_client', None)
if client and client.is_connected(): if client and client.is_connected():
return client if not require_enabled or bool(getattr(ws, 'dev_mode_enabled', False)):
return client
except Exception: except Exception:
pass pass
if allow_fallback: if allow_fallback:

View file

@ -105,3 +105,39 @@ def test_deezer_client_cache_tracks_token(monkeypatch):
assert first is not second assert first is not second
assert calls["deezer"] == 2 assert calls["deezer"] == 2
class _FakeHydrabaseClient:
def __init__(self, connected=True):
self._connected = connected
def is_connected(self):
return self._connected
def test_hydrabase_enabled_requires_connection_and_dev_mode(monkeypatch):
fake_ws = types.ModuleType("web_server")
fake_ws.hydrabase_client = _FakeHydrabaseClient(connected=True)
fake_ws.dev_mode_enabled = True
monkeypatch.setitem(sys.modules, "web_server", fake_ws)
assert metadata_service.is_hydrabase_enabled() is True
fake_ws.dev_mode_enabled = False
assert metadata_service.is_hydrabase_enabled() is False
fake_ws.dev_mode_enabled = True
fake_ws.hydrabase_client = _FakeHydrabaseClient(connected=False)
assert metadata_service.is_hydrabase_enabled() is False
def test_get_client_for_source_hydrabase_requires_enablement(monkeypatch):
fake_ws = types.ModuleType("web_server")
fake_ws.hydrabase_client = _FakeHydrabaseClient(connected=True)
fake_ws.dev_mode_enabled = False
monkeypatch.setitem(sys.modules, "web_server", fake_ws)
assert metadata_service.get_client_for_source("hydrabase") is None
fake_ws.dev_mode_enabled = True
assert metadata_service.get_client_for_source("hydrabase") is fake_ws.hydrabase_client

View file

@ -5867,15 +5867,11 @@ _COMPARISON_MAX_ENTRIES = 50
_comparison_lock = threading.Lock() _comparison_lock = threading.Lock()
def _is_hydrabase_active(): def _is_hydrabase_active():
"""Check if Hydrabase should be used as the PRIMARY metadata source (replaces Spotify). """Check if Hydrabase is connected and enabled for metadata use."""
Only active in dev mode the legacy 'Hydrabase replaces everything' behavior.
When selected as a fallback source, Hydrabase works through the normal fallback
path (_get_metadata_fallback_client) just like iTunes/Deezer not as primary."""
try: try:
if hydrabase_client is None or not hydrabase_client.is_connected(): from core.metadata_service import is_hydrabase_enabled
return False return is_hydrabase_enabled()
return dev_mode_enabled except Exception:
except (NameError, Exception):
return False return False
def _run_background_comparison(query, hydrabase_counts=None): def _run_background_comparison(query, hydrabase_counts=None):