| 1 | """Comparison entities split one single-topic yt-dlp budget instead of each taking a full one (#1335).""" |
| 2 | |
| 3 | from unittest import mock |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from lib import pipeline, schema, youtube_yt |
| 8 | from tests.competitor_cli_audit_helpers import run_competitor_cli |
| 9 | |
| 10 | |
| 11 | def _count_ytdlp_work(config): |
| 12 | """Run one entity's YouTube lane and count the yt-dlp calls it queues.""" |
| 13 | calls = {"searches": 0, "transcripts": 0, "comments": 0} |
| 14 | |
| 15 | def search(q, *_args, **_kwargs): |
| 16 | calls["searches"] += 1 |
| 17 | return {"items": [ |
| 18 | {"video_id": f"{q}-{i}", "title": q, "date": "2026-06-20", |
| 19 | "engagement": {"views": 100 - i, "likes": 1, "comments": 1}} |
| 20 | for i in range(8) |
| 21 | ]} |
| 22 | |
| 23 | def transcript(*_args, **_kwargs): |
| 24 | calls["transcripts"] += 1 |
| 25 | return "transcript text" |
| 26 | |
| 27 | def comments(*_args, **_kwargs): |
| 28 | calls["comments"] += 1 |
| 29 | return [{"author": "a", "text": "c", "likes": 1, "date": ""}] |
| 30 | |
| 31 | with ( |
| 32 | mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), |
| 33 | mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), |
| 34 | mock.patch.object(youtube_yt, "search_youtube", side_effect=search), |
| 35 | mock.patch.object(youtube_yt, "fetch_transcript", side_effect=transcript), |
| 36 | mock.patch.object(youtube_yt, "_fetch_video_comments", side_effect=comments), |
| 37 | mock.patch.object(pipeline.env, "is_youtube_sc_available", return_value=False), |
| 38 | mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=True), |
| 39 | ): |
| 40 | items, _artifact = pipeline._retrieve_stream_impl( |
| 41 | topic="Widget launch news", |
| 42 | subquery=schema.SubQuery( |
| 43 | label="q", search_query="Widget", ranking_query="Widget", sources=["youtube"], |
| 44 | ), |
| 45 | source="youtube", |
| 46 | config=config, |
| 47 | depth="default", |
| 48 | date_range=("2026-06-01", "2026-07-01"), |
| 49 | runtime=schema.ProviderRuntime( |
| 50 | reasoning_provider="mock", planner_model="mock", rerank_model="mock", |
| 51 | ), |
| 52 | mock=False, |
| 53 | ) |
| 54 | assert items |
| 55 | return calls |
| 56 | |
| 57 | |
| 58 | def test_single_topic_keeps_the_full_youtube_budget(): |
| 59 | assert _count_ytdlp_work({}) == {"searches": 2, "transcripts": 6, "comments": 3} |
| 60 | |
| 61 | |
| 62 | def test_three_entity_comparison_splits_one_single_topic_budget(): |
| 63 | """Three entities x full budget queued 3x the work on the shared 2-slot gate and timed out.""" |
| 64 | assert _count_ytdlp_work({"_comparison_entity_count": 3}) == { |
| 65 | "searches": 1, "transcripts": 3, "comments": 1, |
| 66 | } |
| 67 | |
| 68 | |
| 69 | @pytest.mark.parametrize( |
| 70 | ("total", "entities", "share"), |
| 71 | [(2, 1, 2), (2, 2, 1), (2, 3, 1), (3, 2, 2), (8, 3, 3), (0, 3, 0), (3, 6, 1)], |
| 72 | ) |
| 73 | def test_every_entity_keeps_at_least_one_of_each_budgeted_call(total, entities, share): |
| 74 | assert youtube_yt.comparison_share(total, entities) == share |
| 75 | |
| 76 | |
| 77 | def test_backfill_targets_the_entity_share(): |
| 78 | items = [ |
| 79 | schema.SourceItem( |
| 80 | item_id=f"v{i}", source="youtube", title="t", body="", url="u", |
| 81 | ) |
| 82 | for i in range(6) |
| 83 | ] |
| 84 | with ( |
| 85 | mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), |
| 86 | mock.patch.object(youtube_yt, "fetch_transcripts_parallel", return_value={}) as fetch, |
| 87 | ): |
| 88 | youtube_yt.backfill_transcripts(items, depth="default", entity_count=3) |
| 89 | |
| 90 | assert fetch.call_args.kwargs["max_sc_rescues"] == 1 |
| 91 | assert len(fetch.call_args.args[0]) == 3 |
| 92 | |
| 93 | |
| 94 | def test_thin_result_paid_fallback_targets_the_entity_share(): |
| 95 | raw = [{"id": f"v{i}", "title": "Widget", "date": "2026-06-20", "views": 100 - i} for i in range(8)] |
| 96 | with ( |
| 97 | mock.patch.object(youtube_yt, "_sc_youtube_search", return_value=raw), |
| 98 | mock.patch.object(youtube_yt, "_sc_fetch_transcript", return_value=None) as paid, |
| 99 | ): |
| 100 | youtube_yt.search_youtube_sc( |
| 101 | "Widget", "2026-06-01", "2026-07-01", depth="default", token="sc-key", |
| 102 | entity_count=3, |
| 103 | ) |
| 104 | |
| 105 | assert paid.call_count == 3 |
| 106 | |
| 107 | |
| 108 | def test_every_comparison_subrun_learns_the_entity_count(monkeypatch, tmp_path): |
| 109 | observed = run_competitor_cli(monkeypatch, tmp_path) |
| 110 | |
| 111 | for entity in ("MainBrand", "PeerOne", "PeerTwo"): |
| 112 | assert observed.calls[entity]["config"]["_comparison_entity_count"] == 3 |
| 113 |