| 1 | """LAST30DAYS_YOUTUBE_BACKEND=yt-dlp keeps YouTube keyless with a key set (#1316).""" |
| 2 | |
| 3 | from pathlib import Path |
| 4 | from unittest import mock |
| 5 | |
| 6 | import pytest |
| 7 | from lib import doctor, env, pipeline, schema, youtube_yt |
| 8 | |
| 9 | KEYED = {"SCRAPECREATORS_API_KEY": "dummy-key"} |
| 10 | PINNED = {**KEYED, "LAST30DAYS_YOUTUBE_BACKEND": "yt-dlp"} |
| 11 | |
| 12 | |
| 13 | def _youtube_stream(config): |
| 14 | return pipeline._retrieve_stream_impl( |
| 15 | topic="Vuori", |
| 16 | subquery=schema.SubQuery( |
| 17 | label="q", search_query="Vuori", ranking_query="Vuori", sources=["youtube"], |
| 18 | ), |
| 19 | source="youtube", |
| 20 | config=config, |
| 21 | depth="default", |
| 22 | date_range=("2026-06-01", "2026-07-01"), |
| 23 | runtime=schema.ProviderRuntime( |
| 24 | reasoning_provider="mock", planner_model="mock", rerank_model="mock", |
| 25 | ), |
| 26 | mock=False, |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | @pytest.mark.parametrize("value", ["yt-dlp", "YT-DLP", " ytdlp "]) |
| 31 | def test_pin_turns_off_youtube_sc_with_key_set(value): |
| 32 | assert env.is_youtube_sc_available({**KEYED, "LAST30DAYS_YOUTUBE_BACKEND": value}) is False |
| 33 | |
| 34 | |
| 35 | @pytest.mark.parametrize("value", [None, "", "auto", "scrapecreators", "bogus"]) |
| 36 | def test_unset_or_unknown_pin_keeps_auto_behavior(value): |
| 37 | config = {**KEYED, "LAST30DAYS_YOUTUBE_BACKEND": value} |
| 38 | assert env.youtube_backend_pin(config) == "" |
| 39 | assert env.is_youtube_sc_available(config) is True |
| 40 | |
| 41 | |
| 42 | def test_pinned_without_ytdlp_disables_sc_comments_and_the_source(): |
| 43 | config = {**PINNED, "INCLUDE_SOURCES": "youtube_comments"} |
| 44 | with mock.patch.object(env, "is_ytdlp_available", return_value=False), \ |
| 45 | mock.patch.object(pipeline, "which", return_value=None): |
| 46 | assert env.is_youtube_comments_available(config) is False |
| 47 | assert "youtube" not in pipeline.available_sources(config) |
| 48 | |
| 49 | |
| 50 | def test_pinned_stream_makes_no_scrapecreators_calls(): |
| 51 | with ( |
| 52 | mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), |
| 53 | mock.patch.object( |
| 54 | youtube_yt, "search_and_transcribe", return_value={"items": []}, |
| 55 | ) as free_search, |
| 56 | mock.patch.object(youtube_yt, "search_youtube_sc") as sc_search, |
| 57 | mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=False), |
| 58 | ): |
| 59 | items, _artifact = _youtube_stream(dict(PINNED)) |
| 60 | |
| 61 | assert items == [] |
| 62 | sc_search.assert_not_called() |
| 63 | assert free_search.call_args.kwargs["token"] is None |
| 64 | |
| 65 | |
| 66 | def test_pinned_stream_enriches_comments_without_the_key(): |
| 67 | free = {"video_id": "a", "title": "Vuori review", "transcript_snippet": "t"} |
| 68 | with ( |
| 69 | mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), |
| 70 | mock.patch.object(youtube_yt, "search_and_transcribe", return_value={"items": [free]}), |
| 71 | mock.patch.object(youtube_yt, "search_youtube_sc") as sc_search, |
| 72 | mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=True), |
| 73 | mock.patch.object(youtube_yt, "enrich_with_comments") as comments, |
| 74 | ): |
| 75 | _youtube_stream(dict(PINNED)) |
| 76 | |
| 77 | sc_search.assert_not_called() |
| 78 | assert comments.call_args.kwargs["token"] == "" |
| 79 | |
| 80 | |
| 81 | def test_unpinned_stream_still_passes_the_key_to_comments(): |
| 82 | free = {"video_id": "a", "title": "Vuori review", "transcript_snippet": "t"} |
| 83 | with ( |
| 84 | mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), |
| 85 | mock.patch.object( |
| 86 | youtube_yt, "search_and_transcribe", return_value={"items": [free] * 3}, |
| 87 | ), |
| 88 | mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=True), |
| 89 | mock.patch.object(youtube_yt, "enrich_with_comments") as comments, |
| 90 | ): |
| 91 | _youtube_stream(dict(KEYED)) |
| 92 | |
| 93 | assert comments.call_args.kwargs["token"] == "dummy-key" |
| 94 | |
| 95 | |
| 96 | def test_fast_attempts_override_retries_keyed_fetch( |
| 97 | tmp_path: Path, monkeypatch: pytest.MonkeyPatch, |
| 98 | ): |
| 99 | monkeypatch.setenv("LAST30DAYS_YT_TRANSCRIPT_FAST_ATTEMPTS", "3") |
| 100 | monkeypatch.setenv("LAST30DAYS_YT_TRANSCRIPT_FAST_TIMEOUT", "") |
| 101 | status: dict = {} |
| 102 | with mock.patch.object( |
| 103 | youtube_yt.subproc, "run_with_timeout", side_effect=youtube_yt.subproc.SubprocTimeout, |
| 104 | ) as run_mock, mock.patch.object(youtube_yt.time, "sleep"): |
| 105 | youtube_yt._fetch_transcript_ytdlp("vid", str(tmp_path), status=status, fast_fail=True) |
| 106 | |
| 107 | assert run_mock.call_count == 3 |
| 108 | assert {c.kwargs["timeout"] for c in run_mock.call_args_list} == {12.0} |
| 109 | |
| 110 | |
| 111 | @pytest.mark.parametrize("value", ["", "0", "-1", "abc", "2.5"]) |
| 112 | def test_invalid_fast_attempts_keep_one_attempt(value, monkeypatch: pytest.MonkeyPatch): |
| 113 | monkeypatch.setenv("LAST30DAYS_YT_TRANSCRIPT_FAST_ATTEMPTS", value) |
| 114 | assert youtube_yt._transcript_fast_attempts() == 1 |
| 115 | |
| 116 | |
| 117 | def test_pin_changes_doctor_cache_fingerprint(): |
| 118 | assert doctor._config_fingerprint(dict(KEYED)) != doctor._config_fingerprint(dict(PINNED)) |
| 119 |