"""download_backends — the single predicate that routes a platform to the native ingester vs. gallery-dl. Pure, no DB.""" from pathlib import Path import pytest from backend.app.services.download_backends import ( NATIVE_INGESTER_PLATFORMS, _campaign_resolution_error, _native_ingester_cls, _unsupported_platform_message, run_download, uses_native_ingester, verify_source_credential, ) from backend.app.services.gallery_dl import ErrorType from backend.app.services.pixiv_ingester import PixivIngester def test_native_platforms(): for platform in ("patreon", "subscribestar"): assert uses_native_ingester(platform) is True assert platform in NATIVE_INGESTER_PLATFORMS def test_pixiv_is_no_longer_native(): """Retired at milestone #406. The refusal below is what stops it falling through to gallery-dl now that it is not native.""" assert uses_native_ingester("pixiv") is False assert "pixiv" not in NATIVE_INGESTER_PLATFORMS # --- the retired-platform guard (#406 phase 1) ----------------------------- class _RecordingGalleryDL: """Stands in for GalleryDLService: records whether a download was attempted.""" def __init__(self): self.calls = [] async def download(self, **kwargs): self.calls.append(kwargs["platform"]) return "reached gallery-dl" def _ctx(platform): return { "platform": platform, "url": f"https://example.invalid/{platform}", "artist_slug": "someone", "cookies_path": None, "auth_token": None, } @pytest.mark.asyncio async def test_a_retired_platform_never_reaches_a_downloader(): """An enabled source on a retired platform is data that survives a deploy. Unguarded, pixiv — no longer native — would fall straight through to the gallery-dl branch, which still has a pixiv extractor.""" gdl = _RecordingGalleryDL() result, campaign_id = await run_download( ctx=_ctx("pixiv"), source_config=None, skip_value=False, mode=None, gdl=gdl, sync_session_factory=None, ) assert gdl.calls == [] assert result.success is False assert result.error_type == ErrorType.UNSUPPORTED_URL assert "pixiv" in result.error_message assert campaign_id is None @pytest.mark.asyncio async def test_a_supported_gallery_dl_platform_still_reaches_gallery_dl(): """The positive control. Without it, a guard that refused EVERY platform would pass the test above just as well (rule #167).""" gdl = _RecordingGalleryDL() result, _ = await run_download( ctx=_ctx("hentaifoundry"), source_config=None, skip_value=False, mode=None, gdl=gdl, sync_session_factory=None, ) assert gdl.calls == ["hentaifoundry"] assert result == "reached gallery-dl" def test_the_guard_discriminates_by_registration(): assert _unsupported_platform_message("hentaifoundry") is None assert _unsupported_platform_message("patreon") is None assert _unsupported_platform_message("pixiv") is not None assert _unsupported_platform_message("deviantart") is not None @pytest.mark.asyncio async def test_verifying_a_retired_platform_is_inconclusive_not_rejected(): """Nothing is probed, so nothing is rejected — returning False would tell the operator their credential is bad when the platform is simply gone.""" ok, message = await verify_source_credential( platform="pixiv", url="https://www.pixiv.net/users/1", artist_slug="someone", config_overrides=None, cookies_path=None, auth_token=None, images_root=Path("/nonexistent"), ) assert ok is None assert "pixiv" in message def test_gallery_dl_platforms_are_not_native(): # The platforms still served by gallery-dl must NOT route to the native # ingester — guards an accidental over-broad migration. for platform in ("hentaifoundry", "discord"): assert uses_native_ingester(platform) is False def test_unknown_platform_is_not_native(): assert uses_native_ingester("nonsense") is False def test_pixiv_dispatches_to_its_ingester(): assert _native_ingester_cls("pixiv") is PixivIngester def test_pixiv_resolution_error_names_the_expected_url_shape(): msg = _campaign_resolution_error("pixiv", "https://www.pixiv.net/artworks/1") assert "pixiv user id" in msg assert "users/" in msg