import pytest from shortstats.youtube import QuotaExceeded, YouTubeClient, YouTubeError, parse_duration from .fake_youtube import FakeYouTube @pytest.mark.parametrize("value,expected", [ ("PT45S", 45), ("PT1M5S", 65), ("PT3M", 180), ("PT1H2M3S", 3723), ("P1DT1S", 86401), ("P0D", 0), (None, None), ("", None), ("bogus", None), ]) def test_parse_duration(value, expected): assert parse_duration(value) == expected def client_for(fake): return YouTubeClient("test-key", "https://yt.test/youtube/v3", transport=fake.transport()) def test_comment_threads_newest_first_with_pagination(): fake = FakeYouTube() fake.page_size = 2 for i in range(3): fake.add_comment("vid00000001", f"c{i}", f"commentaire {i}", f"2026-10-0{i + 1}T10:00:00Z") client = client_for(fake) first = client.comment_threads("vid00000001") assert [t.top.comment_id for t in first.threads] == ["c2", "c1"] and first.next_page_token assert first.threads[0].top.text == "commentaire 2" and first.threads[0].top.parent_id is None assert first.threads[0].top.published_at.day == 3 second = client.comment_threads("vid00000001", first.next_page_token) assert [t.top.comment_id for t in second.threads] == ["c0"] and second.next_page_token is None assert client.quota_used == 2 assert fake.calls[0][1]["part"] == "snippet" and fake.calls[0][1]["maxResults"] == "100" def test_comment_threads_with_replies_and_reply_pages(): fake = FakeYouTube() replies = [(f"r{i}", f"réponse {i}", "2026-10-02T10:00:00Z") for i in range(7)] fake.add_comment("vid00000001", "top", "fil", "2026-10-01T10:00:00Z", replies=replies) client = client_for(fake) page = client.comment_threads("vid00000001", with_replies=True) thread = page.threads[0] assert thread.reply_count == 7 and len(thread.replies) == 5 assert thread.replies[0].parent_id == "top" and thread.replies[0].comment_id == "top.r0" assert fake.calls[-1][1]["part"] == "snippet,replies" fake.page_size = 4 got, token = client.comment_replies("top") assert len(got) == 4 and token more, token = client.comment_replies("top", token) assert len(more) == 3 and token is None and more[-1].text == "réponse 6" assert client.quota_used == 3 def test_comments_disabled(): fake = FakeYouTube() fake.disabled.add("vid00000001") with pytest.raises(YouTubeError) as err: client_for(fake).comment_threads("vid00000001") assert err.value.reason == "commentsDisabled" def test_statistics_with_title(): fake = FakeYouTube() fake.add_short("vid00000001", "2026-10-01T10:00:00Z", title="Nouveau titre", views=5, likes=None, comments=2, listed=False) stats = client_for(fake).statistics(["vid00000001", "absent00001"]) assert list(stats) == ["vid00000001"] s = stats["vid00000001"] assert (s.title, s.view_count, s.like_count, s.comment_count) == ("Nouveau titre", 5, None, 2) assert fake.calls[0][1]["part"] == "snippet,statistics" def test_quota_error_is_specific(): fake = FakeYouTube() fake.quota_exceeded = True with pytest.raises(QuotaExceeded): client_for(fake).videos(["vid00000001"]) def test_videos_hidden_likes_and_batching(): fake = FakeYouTube() ids = [f"v{i:010d}" for i in range(120)] for vid in ids: fake.add_short(vid, "2026-10-01T10:00:00Z", likes=None, listed=False) client = client_for(fake) details = client.videos(ids) assert len(details) == 120 assert details[ids[0]].like_count is None assert details[ids[0]].duration_s == 45 assert client.quota_used == 3 # 3 lots de 50 maximum def test_missing_playlist_returns_empty(): fake = FakeYouTube() assert client_for(fake).recent_playlist_videos("UUSH" + "z" * 22) == []