|
| 1 | +"""Tests for YouTube link extraction and Mastodon/YouTube disambiguation.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +sys.path.append(str(Path(__file__).parent.parent / "utils")) |
| 8 | + |
| 9 | +from enrich_tba import extract_links_from_url |
| 10 | + |
| 11 | + |
| 12 | +class TestYouTubeExtraction: |
| 13 | + """Test YouTube link detection in extract_links_from_url.""" |
| 14 | + |
| 15 | + @patch("enrich_tba.get_all_links") |
| 16 | + def test_youtube_channel_detected(self, mock_links): |
| 17 | + """YouTube /@channel links are detected as youtube, not mastodon.""" |
| 18 | + mock_links.return_value = [ |
| 19 | + "https://www.youtube.com/@PyConUS", |
| 20 | + ] |
| 21 | + result = extract_links_from_url("https://pycon.org") |
| 22 | + assert "youtube" in result |
| 23 | + assert result["youtube"] == "https://www.youtube.com/@PyConUS" |
| 24 | + assert "mastodon" not in result |
| 25 | + |
| 26 | + @patch("enrich_tba.get_all_links") |
| 27 | + def test_youtube_channel_url_without_at(self, mock_links): |
| 28 | + """YouTube channel links without @ are detected.""" |
| 29 | + mock_links.return_value = [ |
| 30 | + "https://www.youtube.com/channel/UCMjMBMGt0WP2usFilILnbcA", |
| 31 | + ] |
| 32 | + result = extract_links_from_url("https://pycon.org") |
| 33 | + assert "youtube" in result |
| 34 | + assert "mastodon" not in result |
| 35 | + |
| 36 | + @patch("enrich_tba.get_all_links") |
| 37 | + def test_youtube_not_mistaken_for_mastodon(self, mock_links): |
| 38 | + """YouTube /@username must not end up in mastodon field.""" |
| 39 | + mock_links.return_value = [ |
| 40 | + "https://www.youtube.com/@EuroPython", |
| 41 | + "https://fosstodon.org/@europython", |
| 42 | + ] |
| 43 | + result = extract_links_from_url("https://europython.eu") |
| 44 | + assert result.get("youtube") == "https://www.youtube.com/@EuroPython" |
| 45 | + assert result.get("mastodon") == "https://fosstodon.org/@europython" |
| 46 | + |
| 47 | + @patch("enrich_tba.get_all_links") |
| 48 | + def test_youtu_be_short_link(self, mock_links): |
| 49 | + """Short youtu.be links are detected as youtube.""" |
| 50 | + mock_links.return_value = [ |
| 51 | + "https://youtu.be/abc123", |
| 52 | + ] |
| 53 | + result = extract_links_from_url("https://pycon.org") |
| 54 | + assert "youtube" in result |
| 55 | + assert "mastodon" not in result |
| 56 | + |
| 57 | + @patch("enrich_tba.get_all_links") |
| 58 | + def test_mastodon_still_works(self, mock_links): |
| 59 | + """Mastodon links on known instances still detected correctly.""" |
| 60 | + mock_links.return_value = [ |
| 61 | + "https://fosstodon.org/@pycon", |
| 62 | + ] |
| 63 | + result = extract_links_from_url("https://pycon.org") |
| 64 | + assert "mastodon" in result |
| 65 | + assert result["mastodon"] == "https://fosstodon.org/@pycon" |
| 66 | + assert "youtube" not in result |
| 67 | + |
| 68 | + @patch("enrich_tba.get_all_links") |
| 69 | + def test_generic_mastodon_still_works(self, mock_links): |
| 70 | + """Generic /@username on unknown instances still detected as mastodon.""" |
| 71 | + mock_links.return_value = [ |
| 72 | + "https://social.example.org/@pyconf", |
| 73 | + ] |
| 74 | + result = extract_links_from_url("https://pyconf.org") |
| 75 | + assert "mastodon" in result |
| 76 | + assert "youtube" not in result |
| 77 | + |
| 78 | + @patch("enrich_tba.get_all_links") |
| 79 | + def test_youtube_first_seen_wins(self, mock_links): |
| 80 | + """Only the first YouTube link is kept.""" |
| 81 | + mock_links.return_value = [ |
| 82 | + "https://www.youtube.com/@PyConUS", |
| 83 | + "https://www.youtube.com/@AnotherChannel", |
| 84 | + ] |
| 85 | + result = extract_links_from_url("https://pycon.org") |
| 86 | + assert result["youtube"] == "https://www.youtube.com/@PyConUS" |
| 87 | + |
| 88 | + @patch("enrich_tba.get_all_links") |
| 89 | + def test_all_social_links_extracted(self, mock_links): |
| 90 | + """YouTube, Mastodon, and Bluesky can all be extracted together.""" |
| 91 | + mock_links.return_value = [ |
| 92 | + "https://bsky.app/profile/pycon.org", |
| 93 | + "https://www.youtube.com/@PyConUS", |
| 94 | + "https://fosstodon.org/@pycon", |
| 95 | + ] |
| 96 | + result = extract_links_from_url("https://pycon.org") |
| 97 | + assert "bluesky" in result |
| 98 | + assert "youtube" in result |
| 99 | + assert "mastodon" in result |
0 commit comments