import pytest from shortstats.youtube import QuotaExceeded, YouTubeClient, parse_duration from .fake_youtube import CHANNEL, OTHER, 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_author_comment_found_with_rank_and_pagination(): fake = FakeYouTube() fake.comments["vid00000001"] = [ [fake.thread(OTHER, f"spam {i}") for i in range(100)], [fake.thread(OTHER, "x"), fake.thread(CHANNEL, "Saison 2 de Apothecary Diaries !", likes=321, cid="pin")], ] client = client_for(fake) c = client.author_comment("vid00000001", CHANNEL, max_pages=3) assert (c.status, c.comment_id, c.like_count, c.rank) == ("found", "pin", 321, 102) assert "Apothecary" in c.text assert client.quota_used == 2 def test_author_comment_respects_max_pages(): fake = FakeYouTube() fake.comments["vid00000001"] = [[fake.thread(OTHER, "a")], [fake.thread(CHANNEL, "trop loin")]] c = client_for(fake).author_comment("vid00000001", CHANNEL, max_pages=1) assert c.status == "not_found" def test_comments_disabled(): fake = FakeYouTube() fake.disabled.add("vid00000001") assert client_for(fake).author_comment("vid00000001", CHANNEL, 3).status == "comments_disabled" 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) == []