diff --git a/backend/services/audio_search_service.py b/backend/services/audio_search_service.py index b240913f..a6c1a98c 100644 --- a/backend/services/audio_search_service.py +++ b/backend/services/audio_search_service.py @@ -368,12 +368,17 @@ class AudioSearchService: return results except FlacfetchServiceError as e: - logger.warning(f"Remote flacfetch search failed, falling back to local: {e}") - # Fallback to local search + # flacfetch is the sole downloader. The remote service was explicitly + # configured, so a failure here must NOT silently downgrade to the + # local YouTube-only fetcher — that hides the outage and ships a worse + # source than the user expected (the 2026-06-08 incident class). + # Fail loudly. (Fallback audit 2026-06-09, Theme 1.) + logger.error(f"Remote flacfetch search failed (not falling back to local): {e}") + # Clear cached state so a later download() can't consume stale results + # from a previous successful search on this (singleton) instance. self._remote_search_id = None - results = self._fetcher.search(artist, title) - self._cached_results = results - return results + self._cached_results = [] + raise AudioSearchError(f"Remote flacfetch search failed: {e}") from e def select_best(self, results: List[AudioSearchResult]) -> int: """ diff --git a/backend/tests/test_audio_search.py b/backend/tests/test_audio_search.py index d3566591..373e2455 100644 --- a/backend/tests/test_audio_search.py +++ b/backend/tests/test_audio_search.py @@ -321,6 +321,22 @@ class TestAudioSearchServiceSearch: assert "No results found" in str(exc_info.value) + def test_remote_search_failure_does_not_fall_back_to_local(self): + """A remote flacfetch error must fail loudly, not silently downgrade to local YouTube.""" + from backend.services.flacfetch_client import FlacfetchServiceError + + service = AudioSearchService(red_api_key=None, red_api_url=None, ops_api_key=None, ops_api_url=None) + mock_remote = Mock() + mock_remote.search = AsyncMock(side_effect=FlacfetchServiceError("remote service down")) + service._remote_client = mock_remote + service._fetcher = Mock() # local fetcher — must NOT be used as a silent fallback + + with pytest.raises(AudioSearchError, match="Remote flacfetch search failed"): + service.search("ABBA", "Waterloo") + + # Critical: we did NOT silently fall back to the local YouTube-only fetcher. + service._fetcher.search.assert_not_called() + def test_search_multiple_results(self): """Test search returns multiple results with correct indices.""" mock_results = [] diff --git a/karaoke_gen/audio_fetcher.py b/karaoke_gen/audio_fetcher.py index badea6db..3bc86c0f 100644 --- a/karaoke_gen/audio_fetcher.py +++ b/karaoke_gen/audio_fetcher.py @@ -1816,11 +1816,20 @@ def create_audio_fetcher( logger=logger, ) elif api_url and not api_key: - if logger: - logger.warning("FLACFETCH_API_URL is set but FLACFETCH_API_KEY is not - falling back to local mode") + # Half-configured remote flacfetch is almost always a deploy/secret typo. + # Silently using local mode masks the misconfiguration — fail loudly so + # the operator fixes it. (Fallback audit 2026-06-09, Theme 1.) + raise AudioFetcherError( + "FLACFETCH_API_URL is set but FLACFETCH_API_KEY is not. Remote flacfetch " + "is half-configured — set both (or neither, for local mode). Refusing to " + "silently fall back to local mode and mask the misconfiguration." + ) elif api_key and not api_url: - if logger: - logger.warning("FLACFETCH_API_KEY is set but FLACFETCH_API_URL is not - falling back to local mode") + raise AudioFetcherError( + "FLACFETCH_API_KEY is set but FLACFETCH_API_URL is not. Remote flacfetch " + "is half-configured — set both (or neither, for local mode). Refusing to " + "silently fall back to local mode and mask the misconfiguration." + ) # Use local flacfetch library return FlacFetchAudioFetcher( diff --git a/pyproject.toml b/pyproject.toml index db60c9d7..0f664170 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "karaoke-gen" -version = "0.196.5" +version = "0.196.6" description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens." authors = ["Andrew Beveridge "] license = "MIT" diff --git a/tests/unit/test_audio_fetcher.py b/tests/unit/test_audio_fetcher.py index ed8aac9c..cc1e2269 100644 --- a/tests/unit/test_audio_fetcher.py +++ b/tests/unit/test_audio_fetcher.py @@ -2342,20 +2342,20 @@ class TestCreateAudioFetcherRemoteSelection: assert fetcher.api_key == "custom_key" @patch.dict(os.environ, {"FLACFETCH_API_URL": "http://localhost:8080"}, clear=False) - def test_falls_back_to_local_without_api_key(self): - """Test factory falls back to local when only URL is set.""" + def test_half_configured_url_only_raises(self): + """Half-configured remote (URL only) must fail loudly, not silently use local.""" # Clear FLACFETCH_API_KEY if it exists with patch.dict(os.environ, {"FLACFETCH_API_KEY": ""}, clear=False): - fetcher = create_audio_fetcher() - assert isinstance(fetcher, FlacFetchAudioFetcher) + with pytest.raises(AudioFetcherError, match="FLACFETCH_API_KEY is not"): + create_audio_fetcher() @patch.dict(os.environ, {"FLACFETCH_API_KEY": "test_key"}, clear=False) - def test_falls_back_to_local_without_api_url(self): - """Test factory falls back to local when only key is set.""" + def test_half_configured_key_only_raises(self): + """Half-configured remote (key only) must fail loudly, not silently use local.""" # Clear FLACFETCH_API_URL if it exists with patch.dict(os.environ, {"FLACFETCH_API_URL": ""}, clear=False): - fetcher = create_audio_fetcher() - assert isinstance(fetcher, FlacFetchAudioFetcher) + with pytest.raises(AudioFetcherError, match="FLACFETCH_API_URL is not"): + create_audio_fetcher() @patch.dict(os.environ, {}, clear=True) def test_creates_local_fetcher_by_default(self):