| 1 | """Top Community Comments lines carry a quote line's attribution, ready to copy. |
| 2 | |
| 3 | Each line reads as the report's quote line does: the quoted text, the footer's |
| 4 | dot for the platform, the author, the subreddit or the platform's name, and |
| 5 | the count, then the URL. The writer copies everything before the URL without |
| 6 | looking anything up elsewhere in the evidence. |
| 7 | """ |
| 8 | |
| 9 | from lib import fusion, render, schema |
| 10 | |
| 11 | |
| 12 | def _item(source, url, comments, container=None): |
| 13 | return schema.SourceItem( |
| 14 | item_id=f"i-{url}", |
| 15 | source=source, |
| 16 | title="Kanye West post", |
| 17 | body="", |
| 18 | url=url, |
| 19 | container=container, |
| 20 | metadata=( |
| 21 | {"top_comments": [{"depth": 0, **c} for c in comments]} if comments else {} |
| 22 | ), |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | def _candidate(item): |
| 27 | candidate = schema.Candidate( |
| 28 | candidate_id=f"c-{item.url}", |
| 29 | item_id=item.item_id, |
| 30 | source=item.source, |
| 31 | title=item.title, |
| 32 | url=item.url, |
| 33 | snippet="", |
| 34 | subquery_labels=["q1"], |
| 35 | native_ranks={item.source: 1}, |
| 36 | local_relevance=0.8, |
| 37 | freshness=50, |
| 38 | engagement=0, |
| 39 | source_quality=0.5, |
| 40 | rrf_score=0.01, |
| 41 | source_items=[item], |
| 42 | ) |
| 43 | candidate.final_score = 50.0 |
| 44 | return candidate |
| 45 | |
| 46 | |
| 47 | def _report(*items): |
| 48 | return schema.Report( |
| 49 | topic="Kanye West", |
| 50 | range_from="2026-10-11", |
| 51 | range_to="2026-11-10", |
| 52 | generated_at="2026-11-10T00:00:00Z", |
| 53 | provider_runtime=schema.ProviderRuntime( |
| 54 | reasoning_provider="test", planner_model="test", rerank_model="test" |
| 55 | ), |
| 56 | query_plan=schema.QueryPlan( |
| 57 | intent="general", |
| 58 | freshness_mode="balanced_recent", |
| 59 | cluster_mode="debate", |
| 60 | raw_topic="Kanye West", |
| 61 | subqueries=[], |
| 62 | source_weights={}, |
| 63 | ), |
| 64 | ranked_candidates=[_candidate(item) for item in items], |
| 65 | clusters=[], |
| 66 | items_by_source={}, |
| 67 | errors_by_source={}, |
| 68 | ) |
| 69 | |
| 70 | |
| 71 | def _line(lines, needle): |
| 72 | (match,) = [line for line in lines if needle in line] |
| 73 | return match |
| 74 | |
| 75 | |
| 76 | TIKTOK = _item( |
| 77 | "tiktok", |
| 78 | "https://www.tiktok.com/@north/video/1", |
| 79 | [{"author": "@haktimel2", "score": 825451, "excerpt": "Ofc shes literally him😭"}], |
| 80 | ) |
| 81 | REDDIT = _item( |
| 82 | "reddit", |
| 83 | "https://www.reddit.com/r/hiphopheads/comments/abc/thread/", |
| 84 | [{"author": "CappaccinoJay", "score": 1315, "excerpt": "Internet feelings vs reality."}], |
| 85 | container="hiphopheads", |
| 86 | ) |
| 87 | def test_reddit_line_carries_dot_author_subreddit_and_upvotes(): |
| 88 | lines = render._render_top_comments(_report(REDDIT, TIKTOK)) |
| 89 | line = _line(lines, "Internet feelings") |
| 90 | assert line == ( |
| 91 | '- "Internet feelings vs reality." 🟠 u/CappaccinoJay on r/hiphopheads' |
| 92 | " · 1,315 upvotes — https://www.reddit.com/r/hiphopheads/comments/abc/thread/ — top comment" |
| 93 | ) |
| 94 | |
| 95 | |
| 96 | def test_tiktok_line_carries_dot_handle_platform_name_and_likes(): |
| 97 | lines = render._render_top_comments(_report(REDDIT, TIKTOK)) |
| 98 | line = _line(lines, "literally him") |
| 99 | assert line == ( |
| 100 | '- "Ofc shes literally him😭" 🎵 @haktimel2 on TikTok · 825,451 likes' |
| 101 | " — https://www.tiktok.com/@north/video/1 — top comment" |
| 102 | ) |
| 103 | |
| 104 | |
| 105 | def test_evidence_lines_hold_every_part_of_a_pulled_quote_attribution(): |
| 106 | lines = render._render_top_comments(_report(TIKTOK, REDDIT)) |
| 107 | for needle, parts in ( |
| 108 | ("literally him", ('"Ofc shes literally him😭"', "🎵", "@haktimel2", "on TikTok", "825,451 likes")), |
| 109 | ( |
| 110 | "Internet feelings", |
| 111 | ('"Internet feelings vs reality."', "🟠", "u/CappaccinoJay", "on r/hiphopheads", "1,315 upvotes"), |
| 112 | ), |
| 113 | ): |
| 114 | line = _line(lines, needle) |
| 115 | for part in parts: |
| 116 | assert part in line, f"{part!r} missing from {line!r}" |
| 117 | |
| 118 | |
| 119 | def test_subreddit_already_prefixed_is_not_doubled(): |
| 120 | prefixed = _item("reddit", REDDIT.url, REDDIT.metadata["top_comments"], container="r/hiphopheads") |
| 121 | line = _line(render._render_top_comments(_report(prefixed, TIKTOK)), "Internet feelings") |
| 122 | assert "on r/hiphopheads · " in line |
| 123 | assert "r/r/" not in line |
| 124 | |
| 125 | |
| 126 | def test_reddit_line_without_a_subreddit_names_the_platform(): |
| 127 | bare = _item("reddit", REDDIT.url, REDDIT.metadata["top_comments"]) |
| 128 | line = _line(render._render_top_comments(_report(bare, TIKTOK)), "Internet feelings") |
| 129 | assert '" 🟠 u/CappaccinoJay on Reddit · 1,315 upvotes — ' in line |
| 130 | assert " on r/" not in line |
| 131 | |
| 132 | |
| 133 | def test_deleted_or_missing_author_keeps_the_comment_fallback(): |
| 134 | item = _item( |
| 135 | "reddit", |
| 136 | REDDIT.url, |
| 137 | [ |
| 138 | {"author": "[deleted]", "score": 900, "excerpt": "A deleted account wrote this line."}, |
| 139 | {"score": 400, "excerpt": "Nobody is recorded as writing this one."}, |
| 140 | ], |
| 141 | container="hiphopheads", |
| 142 | ) |
| 143 | lines = render._render_top_comments(_report(item)) |
| 144 | assert '" 🟠 Comment on r/hiphopheads · 900 upvotes — ' in _line(lines, "deleted account") |
| 145 | assert '" 🟠 Comment on r/hiphopheads · 400 upvotes — ' in _line(lines, "Nobody is recorded") |
| 146 | assert "u/" not in "\n".join(lines) |
| 147 | |
| 148 | |
| 149 | def test_web_page_comment_gets_no_dot_and_no_platform_name(): |
| 150 | item = _item( |
| 151 | "grounding", |
| 152 | "https://example.com/post", |
| 153 | [{"author": "ada", "score": 12, "excerpt": "A reader comment on a web page."}], |
| 154 | ) |
| 155 | line = _line(render._render_top_comments(_report(item, TIKTOK)), "reader comment") |
| 156 | assert line == '- "A reader comment on a web page." ada · 12 votes — https://example.com/post — top comment' |
| 157 | |
| 158 | |
| 159 | def test_dots_come_from_the_footer_table(): |
| 160 | footer = {key: emoji for key, emoji, *_ in render._FOOTER_SOURCES} |
| 161 | for source in ("reddit", "x", "youtube", "tiktok", "instagram", "threads", "hackernews", "bluesky"): |
| 162 | item = _item( |
| 163 | source, |
| 164 | f"https://example.com/{source}", |
| 165 | [{"author": "ada", "score": 5, "excerpt": f"A comment found on {source} today."}], |
| 166 | ) |
| 167 | line = _line(render._render_top_comments(_report(item, TIKTOK)), f"found on {source} ") |
| 168 | assert f'" {footer[source]} ' in line, line |
| 169 | assert f" on {render._source_label(source)} · 5 " in line, line |
| 170 | |
| 171 | |
| 172 | def test_author_score_and_subreddit_survive_a_duplicate_merge(): |
| 173 | """The copy that won the comment slot arrives second; nothing is lost.""" |
| 174 | first = _item("reddit", REDDIT.url, None, container="hiphopheads") |
| 175 | second = _item( |
| 176 | "reddit", REDDIT.url, [dict(c) for c in REDDIT.metadata["top_comments"]], container="hiphopheads" |
| 177 | ) |
| 178 | (kept,) = fusion.collapse_duplicate_urls([first, second]) |
| 179 | assert kept is first |
| 180 | assert kept.metadata["top_comments"] == [ |
| 181 | {"depth": 0, "author": "CappaccinoJay", "score": 1315, "excerpt": "Internet feelings vs reality."} |
| 182 | ] |
| 183 | line = _line(render._render_top_comments(_report(kept, TIKTOK)), "Internet feelings") |
| 184 | assert "🟠 u/CappaccinoJay on r/hiphopheads · 1,315 upvotes" in line |
| 185 | |
| 186 | |
| 187 | def test_distinct_comments_with_the_same_prefix_are_not_deduplicated(): |
| 188 | prefix = "A shared opening deliberately longer than sixty characters so the old key collides." |
| 189 | first = _item( |
| 190 | "reddit", |
| 191 | "https://www.reddit.com/r/test/comments/first/thread/", |
| 192 | [{"author": "ada", "score": 10, "excerpt": prefix + " The first ending."}], |
| 193 | container="test", |
| 194 | ) |
| 195 | second = _item( |
| 196 | "reddit", |
| 197 | "https://www.reddit.com/r/test/comments/second/thread/", |
| 198 | [{"author": "bob", "score": 10, "excerpt": prefix + " A different ending."}], |
| 199 | container="test", |
| 200 | ) |
| 201 | lines = render._render_top_comments(_report(first, second)) |
| 202 | quotes = [line for line in lines if line.startswith("- ")] |
| 203 | assert len(quotes) == 2 |
| 204 | assert any("first ending" in line for line in quotes) |
| 205 | assert any("different ending" in line for line in quotes) |
| 206 | |
| 207 | |
| 208 | def test_exact_duplicate_comments_still_collapse_across_items(): |
| 209 | comment = {"author": "ada", "score": 10, "excerpt": "The same comment in two threads."} |
| 210 | first = _item( |
| 211 | "reddit", |
| 212 | "https://www.reddit.com/r/test/comments/first/thread/", |
| 213 | [comment], |
| 214 | container="test", |
| 215 | ) |
| 216 | second = _item( |
| 217 | "reddit", |
| 218 | "https://www.reddit.com/r/test/comments/second/thread/", |
| 219 | [comment], |
| 220 | container="test", |
| 221 | ) |
| 222 | assert render._render_top_comments(_report(first, second)) == [] |
| 223 | |
| 224 | |
| 225 | def test_merge_keeps_the_fuller_comment_list_with_its_authors(): |
| 226 | first = _item( |
| 227 | "tiktok", TIKTOK.url, [{"author": "@early", "score": 10, "excerpt": "An early comment on the video."}] |
| 228 | ) |
| 229 | second = _item( |
| 230 | "tiktok", |
| 231 | TIKTOK.url, |
| 232 | [ |
| 233 | {"author": "@haktimel2", "score": 825451, "excerpt": "Ofc shes literally him😭"}, |
| 234 | {"author": "@early", "score": 10, "excerpt": "An early comment on the video."}, |
| 235 | ], |
| 236 | ) |
| 237 | merged = fusion.merge_source_items(first, second) |
| 238 | assert [(c["author"], c["score"]) for c in merged.metadata["top_comments"]] == [ |
| 239 | ("@haktimel2", 825451), |
| 240 | ("@early", 10), |
| 241 | ] |
| 242 |