| 1 | """A stored comment says when its author is the item's own author. |
| 2 | |
| 3 | A YouTube channel's display name ("Engineering Explained") and the handle its |
| 4 | comments carry ("@EngExplained") can differ, so comparing names cannot always |
| 5 | tell a creator's pinned note from a community reaction. Where a source says so |
| 6 | itself, the fetcher stores ``is_uploader`` on the comment and the quote pool |
| 7 | reads it before falling back to names. A comment without the key is unknown. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | from unittest import mock |
| 12 | |
| 13 | from lib import instagram, normalize, reddit_enrich, render, schema, tiktok, youtube_yt |
| 14 | from lib.subproc import SubprocResult |
| 15 | |
| 16 | FROM_DATE, TO_DATE = "2026-10-11", "2026-11-10" |
| 17 | |
| 18 | |
| 19 | def _ytdlp(comments, **video): |
| 20 | payload = {"id": "abc123", "title": "vid", "comments": comments, **video} |
| 21 | result = SubprocResult(returncode=0, stdout=json.dumps(payload), stderr="") |
| 22 | with mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), \ |
| 23 | mock.patch.object(youtube_yt.subproc, "run_with_timeout", return_value=result): |
| 24 | return youtube_yt._fetch_video_comments_ytdlp("abc123", max_comments=5) |
| 25 | |
| 26 | |
| 27 | def test_ytdlp_comment_keeps_the_uploader_flag_it_carries(): |
| 28 | got = _ytdlp([ |
| 29 | {"author": "@EngExplained", "author_id": "UC1", "author_is_uploader": True, |
| 30 | "text": "Addressing technical questions!", "like_count": 683}, |
| 31 | {"author": "@viewer", "author_id": "UC2", "author_is_uploader": False, |
| 32 | "text": "Great breakdown of the gearbox", "like_count": 120}, |
| 33 | ]) |
| 34 | assert [c["is_uploader"] for c in got] == [True, False] |
| 35 | |
| 36 | |
| 37 | def test_ytdlp_comment_falls_back_to_the_channel_id_when_it_has_no_flag(): |
| 38 | got = _ytdlp( |
| 39 | [ |
| 40 | {"author": "@EngExplained", "author_id": "UC1", "text": "A pinned note", "like_count": 9}, |
| 41 | {"author": "@viewer", "author_id": "UC2", "text": "A viewer reaction", "like_count": 8}, |
| 42 | {"author": "@nobody", "text": "No id on this one", "like_count": 7}, |
| 43 | ], |
| 44 | channel_id="UC1", |
| 45 | ) |
| 46 | assert [c.get("is_uploader") for c in got] == [True, False, None] |
| 47 | assert "is_uploader" not in got[2] |
| 48 | |
| 49 | |
| 50 | def test_ytdlp_comment_without_any_identity_has_no_flag(): |
| 51 | (got,) = _ytdlp([{"author": "@BestFlorin", "text": "Trump said the strait is open", "like_count": 11, |
| 52 | "_time_text": "2 days ago"}]) |
| 53 | assert got == {"author": "@BestFlorin", "text": "Trump said the strait is open", "likes": 11, |
| 54 | "date": "2 days ago"} |
| 55 | |
| 56 | |
| 57 | def test_scrapecreators_youtube_comment_keeps_a_creator_flag_when_one_is_sent(): |
| 58 | sc_payload = {"comments": [ |
| 59 | {"text": "from the channel", "author": {"name": "@x", "isCreator": True}, "likes": 3}, |
| 60 | {"text": "from a viewer", "author": {"name": "@y", "isCreator": False}, "likes": 2}, |
| 61 | {"text": "unknown", "author": {"name": "@z"}, "likes": 1}, |
| 62 | ]} |
| 63 | with mock.patch.object(youtube_yt, "_ytdlp_comments_result", return_value=([], False)), \ |
| 64 | mock.patch.object(youtube_yt.http, "get", return_value=sc_payload): |
| 65 | got = youtube_yt._fetch_video_comments("abc123", token="sk-live", max_comments=5) |
| 66 | assert [c.get("is_uploader") for c in got] == [True, False, None] |
| 67 | |
| 68 | |
| 69 | def test_tiktok_comments_are_flagged_against_the_videos_creator_handle(): |
| 70 | fake = {"comments": [ |
| 71 | {"text": "thanks for watching everyone", "digg_count": 900, "user": {"unique_id": "YeniusWest"}}, |
| 72 | {"text": "actually great quote", "digg_count": 500, "user": {"unique_id": "markosoo3"}}, |
| 73 | {"text": "no handle on this comment", "digg_count": 100, "user": {"nickname": "Display Name"}}, |
| 74 | ]} |
| 75 | item = {"url": "https://www.tiktok.com/@yeniuswest/video/1", "author_name": "yeniuswest", |
| 76 | "engagement": {"views": 10}} |
| 77 | with mock.patch.object(tiktok.http, "get", return_value=fake): |
| 78 | tiktok.enrich_with_comments([item], token="k") |
| 79 | assert [c.get("is_uploader") for c in item["top_comments"]] == [True, False, None] |
| 80 | |
| 81 | |
| 82 | def test_instagram_comments_are_flagged_against_the_posts_owner(): |
| 83 | fake = {"comments": [ |
| 84 | {"text": "appreciate you all", "comment_like_count": 500, "user": {"username": "IamKevinWong"}}, |
| 85 | {"text": "he really walked out", "comment_like_count": 40, "user": {"username": "glovejones"}}, |
| 86 | ]} |
| 87 | item = {"url": "https://www.instagram.com/reel/ABC/", "author_name": "iamkevinwong", |
| 88 | "engagement": {"views": 10}} |
| 89 | with mock.patch.object(instagram.http, "get", return_value=fake): |
| 90 | instagram.enrich_with_comments([item], token="k") |
| 91 | assert [c.get("is_uploader") for c in item["top_comments"]] == [True, False] |
| 92 | |
| 93 | |
| 94 | def _thread(comments): |
| 95 | return [ |
| 96 | {"data": {"children": [{"kind": "t3", "data": { |
| 97 | "score": 10, "num_comments": 2, "upvote_ratio": 0.9, "created_utc": 1790000000, |
| 98 | "permalink": "/r/sub/comments/abc/t/", "title": "t", "selftext": ""}}]}}, |
| 99 | {"data": {"children": [{"kind": "t1", "data": c} for c in comments]}}, |
| 100 | ] |
| 101 | |
| 102 | |
| 103 | def test_reddit_thread_comment_keeps_is_submitter(): |
| 104 | parsed = reddit_enrich.parse_thread_data(_thread([ |
| 105 | {"body": "OP here, thanks all", "author": "thread_op", "score": 50, "is_submitter": True, |
| 106 | "permalink": "/r/sub/comments/abc/t/c1/"}, |
| 107 | {"body": "A reply from the crowd", "author": "replier", "score": 40, "is_submitter": False, |
| 108 | "permalink": "/r/sub/comments/abc/t/c2/"}, |
| 109 | {"body": "An older capture with no field", "author": "old", "score": 30, |
| 110 | "permalink": "/r/sub/comments/abc/t/c3/"}, |
| 111 | ])) |
| 112 | assert [c.get("is_submitter") for c in parsed["comments"]] == [True, False, None] |
| 113 | stored = reddit_enrich.stored_top_comments(parsed["comments"]) |
| 114 | assert [c.get("is_uploader") for c in stored] == [True, False, None] |
| 115 | assert "is_uploader" not in stored[2] |
| 116 | |
| 117 | |
| 118 | def test_normalize_carries_the_flag_and_leaves_unknown_out(): |
| 119 | item = normalize._normalize_youtube("youtube", { |
| 120 | "id": "v1", "title": "A video", "url": "https://www.youtube.com/watch?v=v1", |
| 121 | "channel_name": "Engineering Explained", "date": "2026-11-01", |
| 122 | "top_comments": [ |
| 123 | {"author": "@EngExplained", "text": "A pinned note", "likes": 9, "is_uploader": True}, |
| 124 | {"author": "@viewer", "text": "A viewer reaction", "likes": 8, "is_uploader": False}, |
| 125 | {"author": "@old", "text": "Stored before the flag", "likes": 7}, |
| 126 | ], |
| 127 | }, 0, FROM_DATE, TO_DATE) |
| 128 | stored = item.metadata["top_comments"] |
| 129 | assert [c.get("is_uploader") for c in stored] == [True, False, None] |
| 130 | assert "is_uploader" not in stored[2] |
| 131 | |
| 132 | |
| 133 | def _pool_item(comments, author="Engineering Explained"): |
| 134 | item = schema.SourceItem( |
| 135 | item_id="v1", source="youtube", title="A video", body="", author=author, |
| 136 | url="https://www.youtube.com/watch?v=v1", metadata={"top_comments": comments}, |
| 137 | ) |
| 138 | return item |
| 139 | |
| 140 | |
| 141 | def test_pool_trusts_the_flag_before_the_names(): |
| 142 | item = _pool_item([]) |
| 143 | flagged = {"author": "@EngExplained", "score": 683, "excerpt": "x", "is_uploader": True} |
| 144 | cleared = {"author": "@EngineeringExplained", "score": 5, "excerpt": "x", "is_uploader": False} |
| 145 | unknown_same = {"author": "@EngineeringExplained", "score": 5, "excerpt": "x"} |
| 146 | unknown_other = {"author": "@viewer", "score": 5, "excerpt": "x"} |
| 147 | assert render._is_community_comment(item, flagged, "A pinned note") is False |
| 148 | # A fan account that shares the channel's name is still a viewer. |
| 149 | assert render._is_community_comment(item, cleared, "A viewer reaction") is True |
| 150 | assert render._is_community_comment(item, unknown_same, "A pinned note") is False |
| 151 | assert render._is_community_comment(item, unknown_other, "A viewer reaction") is True |
| 152 |