| 1 | """Follow-up to #1319: paid YouTube transcript spend stops at the target (#1314, #1315).""" |
| 2 | |
| 3 | import threading |
| 4 | from unittest import mock |
| 5 | |
| 6 | import pytest |
| 7 | from lib import youtube_yt |
| 8 | |
| 9 | |
| 10 | def _ytdlp_rate_limited(video_id, temp_dir, status=None, fast_fail=False): |
| 11 | if status is not None: |
| 12 | status["ytdlp_error"] = "HTTP Error 429: Too Many Requests" |
| 13 | return None |
| 14 | |
| 15 | |
| 16 | @pytest.fixture |
| 17 | def _all_ytdlp_fetches_fail(): |
| 18 | with mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), \ |
| 19 | mock.patch.object(youtube_yt, "_ytdlp_ssh_host", return_value=None), \ |
| 20 | mock.patch.object(youtube_yt, "_fetch_transcript_ytdlp", side_effect=_ytdlp_rate_limited): |
| 21 | yield |
| 22 | |
| 23 | |
| 24 | @pytest.mark.usefixtures("_all_ytdlp_fetches_fail") |
| 25 | def test_parallel_sc_rescues_stop_at_the_cap(): |
| 26 | """24 candidates (3x a deep target of 8) all failing in yt-dlp pay for 8 rescues, not 24.""" |
| 27 | with mock.patch.object( |
| 28 | youtube_yt, "_sc_fetch_transcript", side_effect=lambda vid, token: f"text {vid}", |
| 29 | ) as sc_mock: |
| 30 | results = youtube_yt.fetch_transcripts_parallel( |
| 31 | [f"v{i}" for i in range(24)], token="sc-key", max_sc_rescues=8, |
| 32 | ) |
| 33 | |
| 34 | assert sc_mock.call_count == 8 |
| 35 | assert sum(1 for value in results.values() if value) == 8 |
| 36 | |
| 37 | |
| 38 | @pytest.mark.usefixtures("_all_ytdlp_fetches_fail") |
| 39 | def test_parallel_without_a_cap_keeps_rescuing_every_failure(): |
| 40 | with mock.patch.object( |
| 41 | youtube_yt, "_sc_fetch_transcript", side_effect=lambda vid, token: f"text {vid}", |
| 42 | ) as sc_mock: |
| 43 | youtube_yt.fetch_transcripts_parallel([f"v{i}" for i in range(5)], token="sc-key") |
| 44 | |
| 45 | assert sc_mock.call_count == 5 |
| 46 | |
| 47 | |
| 48 | def test_search_and_transcribe_caps_rescues_at_the_depth_target(): |
| 49 | items = [ |
| 50 | {"video_id": f"v{i}", "title": f"t{i}", "date": "2026-06-15", |
| 51 | "engagement": {"views": 100 - i}} |
| 52 | for i in range(30) |
| 53 | ] |
| 54 | with mock.patch.object(youtube_yt, "expand_youtube_queries", return_value=["q"]), \ |
| 55 | mock.patch.object(youtube_yt, "search_youtube", return_value={"items": items}), \ |
| 56 | mock.patch.object(youtube_yt, "fetch_transcripts_parallel", return_value={}) as parallel: |
| 57 | youtube_yt.search_and_transcribe( |
| 58 | "topic", "2026-06-01", "2026-07-01", depth="deep", token="sc-key", |
| 59 | ) |
| 60 | |
| 61 | assert parallel.call_args.kwargs["max_sc_rescues"] == youtube_yt.TRANSCRIPT_LIMITS["deep"] |
| 62 | |
| 63 | |
| 64 | @pytest.mark.parametrize( |
| 65 | ("configured", "expected"), |
| 66 | [ |
| 67 | ("en,es,pt", "en-orig,es-orig,pt-orig,en,es,pt"), |
| 68 | ("fr,en-orig,en", "fr-orig,en-orig,fr,en"), |
| 69 | ("en", "en-orig,en"), |
| 70 | ("en-orig", "en-orig"), |
| 71 | ("en-orig,es", "en-orig,es-orig,es"), |
| 72 | ], |
| 73 | ) |
| 74 | def test_every_orig_track_is_requested_before_any_plain_code( |
| 75 | configured, expected, monkeypatch: pytest.MonkeyPatch, |
| 76 | ): |
| 77 | """A Spanish video dubbed into English reaches `es-orig` before the translated `en` can 429.""" |
| 78 | monkeypatch.setenv("LAST30DAYS_YT_SUB_LANGS", configured) |
| 79 | assert youtube_yt._ytdlp_request_langs() == expected |
| 80 | |
| 81 | |
| 82 | def _sc_search_items(count): |
| 83 | return [ |
| 84 | {"id": f"v{i}", "title": f"t{i}", "date": "2026-06-15", "views": 100 - i} |
| 85 | for i in range(count) |
| 86 | ] |
| 87 | |
| 88 | |
| 89 | def test_free_transcripts_count_toward_the_backfill_target(): |
| 90 | """Default depth targets 2; with 2 free transcripts in hand the backfill pays for none.""" |
| 91 | with mock.patch.object(youtube_yt, "_sc_youtube_search", return_value=_sc_search_items(6)), \ |
| 92 | mock.patch.object(youtube_yt, "_sc_fetch_transcript") as transcripts: |
| 93 | youtube_yt.search_youtube_sc( |
| 94 | "topic", "2026-06-01", "2026-07-01", depth="default", token="sc-key", |
| 95 | skip_transcript_ids={"free1", "free2"}, |
| 96 | ) |
| 97 | |
| 98 | transcripts.assert_not_called() |
| 99 | |
| 100 | |
| 101 | def test_empty_paid_transcripts_do_not_count_toward_the_target(): |
| 102 | target = youtube_yt.TRANSCRIPT_LIMITS["deep"] |
| 103 | calls = [] |
| 104 | |
| 105 | def _every_third_empty(vid, token): |
| 106 | calls.append(vid) |
| 107 | return "" if len(calls) % 3 == 0 else f"text {vid}" |
| 108 | |
| 109 | with mock.patch.object(youtube_yt, "_sc_youtube_search", return_value=_sc_search_items(30)), \ |
| 110 | mock.patch.object(youtube_yt, "_sc_fetch_transcript", side_effect=_every_third_empty): |
| 111 | result = youtube_yt.search_youtube_sc( |
| 112 | "topic", "2026-06-01", "2026-07-01", depth="deep", token="sc-key", |
| 113 | ) |
| 114 | |
| 115 | assert sum(1 for item in result["items"] if item.get("transcript_snippet")) == target |
| 116 | assert len(calls) <= target * 3 |
| 117 | |
| 118 | |
| 119 | @pytest.mark.usefixtures("_all_ytdlp_fetches_fail") |
| 120 | def test_an_empty_rescue_gives_its_slot_back(): |
| 121 | """A paid miss does not use up the cap, so a later video still gets rescued.""" |
| 122 | with mock.patch.object( |
| 123 | youtube_yt, "_sc_fetch_transcript", |
| 124 | side_effect=lambda vid, token: "" if vid == "v0" else f"text {vid}", |
| 125 | ): |
| 126 | results = youtube_yt.fetch_transcripts_parallel( |
| 127 | [f"v{i}" for i in range(4)], token="sc-key", max_sc_rescues=2, max_workers=1, |
| 128 | ) |
| 129 | |
| 130 | assert sum(1 for value in results.values() if value) == 2 |
| 131 | |
| 132 | |
| 133 | @pytest.mark.usefixtures("_all_ytdlp_fetches_fail") |
| 134 | def test_slots_held_by_slow_empty_rescues_go_to_waiting_videos(): |
| 135 | """Two slow paid misses hold both default-depth slots; later videos wait for them, then fill the target.""" |
| 136 | budget = youtube_yt._RescueBudget(2) |
| 137 | waiting_threads = set() |
| 138 | all_waiting = threading.Event() |
| 139 | condition_wait = budget._cond.wait |
| 140 | calls = [] |
| 141 | calls_lock = threading.Lock() |
| 142 | |
| 143 | def _wait(timeout=None): |
| 144 | waiting_threads.add(threading.get_ident()) |
| 145 | if len(waiting_threads) == 3: |
| 146 | all_waiting.set() |
| 147 | return condition_wait(timeout) |
| 148 | |
| 149 | def _sc(vid, token): |
| 150 | with calls_lock: |
| 151 | calls.append(vid) |
| 152 | empty = len(calls) <= 2 |
| 153 | if empty: |
| 154 | assert all_waiting.wait(5), "other workers never waited for the two held slots" |
| 155 | return "" |
| 156 | return f"text {vid}" |
| 157 | |
| 158 | with mock.patch.object(youtube_yt, "_RescueBudget", return_value=budget), \ |
| 159 | mock.patch.object(budget._cond, "wait", side_effect=_wait), \ |
| 160 | mock.patch.object(youtube_yt, "_sc_fetch_transcript", side_effect=_sc) as sc_mock: |
| 161 | results = youtube_yt.fetch_transcripts_parallel( |
| 162 | [f"v{i}" for i in range(6)], token="sc-key", max_sc_rescues=2, max_workers=5, |
| 163 | ) |
| 164 | |
| 165 | assert sum(1 for value in results.values() if value) == 2 |
| 166 | assert sc_mock.call_count == 4 |
| 167 |