Cin's review feedback: external callers reach per-source clients
via attribute access (orch.hifi.reload_instances()) — needs
generic accessors so the registry IS the single source of truth.
Adds:
- orch.client(name) — public accessor for a per-source client.
Resolves canonical names (deezer) AND legacy aliases (deezer_dl).
- orch.configured_clients() — returns {name: client} for every
initialized AND is_configured() == True source. Replaces the
6+ if/hasattr/is_configured chain Cin called out:
if hasattr(orch, 'soulseek') and orch.soulseek and \
orch.soulseek.is_configured(): ...
- orch.reload_instances(source=None) — generic dispatch for
source-specific reload calls. Replaces orch.hifi.reload_instances()
with orch.reload_instances('hifi').
- get_download_orchestrator() / set_download_orchestrator()
singleton factory matching Cin's get_metadata_engine pattern in
PR #498. web_server.py can install the orchestrator it builds
at boot so future callers grab via the factory instead of
importing the legacy `soulseek_client` global.
Phase Cin-3/Cin-4 will replace existing call sites; this commit
just provides the surface so those migrations are mechanical.
Suite still green (335 download tests + 6 new generic-accessor
tests).
207 lines
7 KiB
Python
207 lines
7 KiB
Python
from core.download_engine import DownloadEngine
|
|
from core.download_orchestrator import DownloadOrchestrator
|
|
from core.download_plugins.registry import DownloadPluginRegistry, PluginSpec
|
|
|
|
|
|
class _FakeClient:
|
|
def __init__(self, configured=True, clear_result=True):
|
|
self.configured = configured
|
|
self.clear_result = clear_result
|
|
self.clear_calls = 0
|
|
|
|
def is_configured(self):
|
|
return self.configured
|
|
|
|
async def clear_all_completed_downloads(self):
|
|
self.clear_calls += 1
|
|
return self.clear_result
|
|
|
|
|
|
def _build_orchestrator(**clients):
|
|
"""Build an orchestrator with mock clients via the registry.
|
|
|
|
The orchestrator iterates `self.registry.all_plugins()` to drive
|
|
every per-source operation, so the test must set up a real
|
|
registry with mock plugins (not just stuff attributes on the
|
|
orchestrator). Source slots not provided in `clients` are
|
|
skipped — registry only holds the ones the test cares about.
|
|
"""
|
|
registry = DownloadPluginRegistry()
|
|
name_to_display = {
|
|
'soulseek': 'Soulseek', 'youtube': 'YouTube', 'tidal': 'Tidal',
|
|
'qobuz': 'Qobuz', 'hifi': 'HiFi', 'deezer_dl': 'Deezer',
|
|
'lidarr': 'Lidarr', 'soundcloud': 'SoundCloud',
|
|
}
|
|
# 'deezer_dl' is the legacy attr name; canonical registry name is 'deezer'.
|
|
aliases_for = {'deezer_dl': ('deezer_dl',)}
|
|
canonical_for = {'deezer_dl': 'deezer'}
|
|
|
|
for slot, client in clients.items():
|
|
if client is None:
|
|
continue
|
|
canonical_name = canonical_for.get(slot, slot)
|
|
registry.register(PluginSpec(
|
|
name=canonical_name,
|
|
factory=lambda c=client: c,
|
|
display_name=name_to_display.get(slot, slot),
|
|
aliases=aliases_for.get(slot, ()),
|
|
))
|
|
registry.initialize()
|
|
|
|
orch = DownloadOrchestrator.__new__(DownloadOrchestrator)
|
|
orch.registry = registry
|
|
orch._init_failures = registry.init_failures
|
|
orch.soulseek = registry.get('soulseek')
|
|
orch.youtube = registry.get('youtube')
|
|
orch.tidal = registry.get('tidal')
|
|
orch.qobuz = registry.get('qobuz')
|
|
orch.hifi = registry.get('hifi')
|
|
orch.deezer_dl = registry.get('deezer')
|
|
orch.lidarr = registry.get('lidarr')
|
|
orch.soundcloud = registry.get('soundcloud')
|
|
# Engine — orchestrator delegates per-source query/cancel
|
|
# methods to it, so the test fixture must build one and
|
|
# register every mock plugin under its canonical name.
|
|
orch.engine = DownloadEngine()
|
|
for source_name, plugin in registry.all_plugins():
|
|
orch.engine.register_plugin(source_name, plugin)
|
|
return orch
|
|
|
|
|
|
def _run_async(coro):
|
|
import asyncio
|
|
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
return loop.run_until_complete(coro)
|
|
finally:
|
|
loop.close()
|
|
|
|
|
|
def test_clear_all_completed_downloads_ignores_unconfigured_clients():
|
|
orch = _build_orchestrator(
|
|
soulseek=_FakeClient(configured=True, clear_result=True),
|
|
youtube=_FakeClient(configured=False, clear_result=False),
|
|
)
|
|
|
|
result = _run_async(orch.clear_all_completed_downloads())
|
|
|
|
assert result is True
|
|
assert orch.soulseek.clear_calls == 1
|
|
assert orch.youtube.clear_calls == 0
|
|
|
|
|
|
def test_clear_all_completed_downloads_propagates_configured_failures():
|
|
orch = _build_orchestrator(
|
|
soulseek=_FakeClient(configured=True, clear_result=False),
|
|
)
|
|
|
|
result = _run_async(orch.clear_all_completed_downloads())
|
|
|
|
assert result is False
|
|
assert orch.soulseek.clear_calls == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Cin-2 generic accessors
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_client_returns_registered_client_by_name():
|
|
"""Cin's review feedback: orch.client('hifi') is the canonical
|
|
way to reach a per-source client, replacing orch.hifi attribute
|
|
access."""
|
|
soulseek = _FakeClient()
|
|
youtube = _FakeClient()
|
|
orch = _build_orchestrator(soulseek=soulseek, youtube=youtube)
|
|
|
|
assert orch.client('soulseek') is soulseek
|
|
assert orch.client('youtube') is youtube
|
|
assert orch.client('made_up') is None
|
|
|
|
|
|
def test_configured_clients_excludes_unconfigured_sources():
|
|
"""Replaces the legacy iteration pattern: 6+ if/hasattr/is_configured
|
|
checks per source. Single call returns dict of configured clients."""
|
|
configured = _FakeClient(configured=True)
|
|
unconfigured = _FakeClient(configured=False)
|
|
orch = _build_orchestrator(
|
|
soulseek=configured,
|
|
youtube=unconfigured,
|
|
)
|
|
result = orch.configured_clients()
|
|
assert 'soulseek' in result
|
|
assert 'youtube' not in result
|
|
assert result['soulseek'] is configured
|
|
|
|
|
|
def test_reload_instances_dispatches_to_named_source():
|
|
"""Generic dispatch — caller passes source name instead of
|
|
reaching for orch.hifi.reload_instances() directly."""
|
|
|
|
class _ReloadableClient(_FakeClient):
|
|
def __init__(self):
|
|
super().__init__(configured=True)
|
|
self.reload_called = False
|
|
|
|
def reload_instances(self):
|
|
self.reload_called = True
|
|
|
|
hifi = _ReloadableClient()
|
|
soulseek = _FakeClient() # No reload_instances method
|
|
orch = _build_orchestrator(soulseek=soulseek, hifi=hifi)
|
|
|
|
assert orch.reload_instances('hifi') is True
|
|
assert hifi.reload_called is True
|
|
|
|
|
|
def test_reload_instances_skips_clients_without_method():
|
|
"""Sources that don't expose reload_instances are skipped, not
|
|
treated as failures."""
|
|
soulseek = _FakeClient() # No reload_instances method
|
|
orch = _build_orchestrator(soulseek=soulseek)
|
|
# Calling on a source without the method = silent no-op
|
|
assert orch.reload_instances('soulseek') is True
|
|
|
|
|
|
def test_reload_instances_with_no_args_reloads_every_source():
|
|
"""When called with no source argument, hits every registered
|
|
source that exposes reload_instances."""
|
|
|
|
class _ReloadableClient(_FakeClient):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.reload_called = False
|
|
|
|
def reload_instances(self):
|
|
self.reload_called = True
|
|
|
|
a = _ReloadableClient()
|
|
b = _ReloadableClient()
|
|
orch = _build_orchestrator(soulseek=a, hifi=b)
|
|
|
|
orch.reload_instances()
|
|
assert a.reload_called is True
|
|
assert b.reload_called is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Singleton factory (matches Cin's get_metadata_engine pattern)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_download_orchestrator_returns_set_singleton():
|
|
"""When set_download_orchestrator has been called (web_server.py
|
|
does this at boot), get_download_orchestrator returns the
|
|
installed instance instead of building a fresh one."""
|
|
from core.download_orchestrator import (
|
|
get_download_orchestrator,
|
|
set_download_orchestrator,
|
|
)
|
|
|
|
orch = _build_orchestrator(soulseek=_FakeClient())
|
|
set_download_orchestrator(orch)
|
|
try:
|
|
assert get_download_orchestrator() is orch
|
|
finally:
|
|
set_download_orchestrator(None)
|