| 1 | """Footer engagement totals and Top voices count on-topic items only. |
| 2 | |
| 3 | Waymo run, 2026-11-10: the footer said "YouTube: 4 videos | 12,745,080 views" |
| 4 | and 12,519,652 of those views were one video about the Tesla Roadster that |
| 5 | never names Waymo. In person runs, posts from related handles (--x-related) |
| 6 | about other things counted toward the X totals and Top voices. |
| 7 | |
| 8 | The items below are the YouTube section of that run's saved file. The ranking |
| 9 | already marks such an item (entity-miss demotion); the source dump keeps it. |
| 10 | """ |
| 11 | |
| 12 | from lib import render, rerank, schema |
| 13 | |
| 14 | ENTITY_MISS = rerank._FALLBACK_ENTITY_MISS_EXPLANATION |
| 15 | FIRST_PARTY = rerank._FIRST_PARTY_FALLBACK_EXPLANATION |
| 16 | |
| 17 | |
| 18 | def _item(source, item_id, title, author, engagement, body="", container=None, metadata=None): |
| 19 | return schema.SourceItem( |
| 20 | item_id=item_id, source=source, title=title, body=body, |
| 21 | url=f"https://example.test/{source}/{item_id}", author=author, container=container, |
| 22 | published_at="2026-10-20", engagement=engagement, metadata=metadata or {}, |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | def _candidate(item, explanation="fallback-local-score", final_score=50.0, local_relevance=0.6): |
| 27 | return schema.Candidate( |
| 28 | candidate_id=f"c-{item.item_id}", item_id=item.item_id, source=item.source, |
| 29 | title=item.title, url=item.url, snippet=item.body, subquery_labels=["primary"], |
| 30 | native_ranks={"primary": 1}, local_relevance=local_relevance, freshness=80, |
| 31 | engagement=1, source_quality=0.7, rrf_score=0.02, sources=[item.source], |
| 32 | source_items=[item], final_score=final_score, explanation=explanation, |
| 33 | ) |
| 34 | |
| 35 | |
| 36 | def _report(topic, items, candidates): |
| 37 | by_source: dict[str, list[schema.SourceItem]] = {} |
| 38 | for item in items: |
| 39 | by_source.setdefault(item.source, []).append(item) |
| 40 | return schema.Report( |
| 41 | topic=topic, range_from="2026-10-12", range_to="2026-11-11", |
| 42 | generated_at="2026-11-11T01:00:00+00:00", |
| 43 | provider_runtime=schema.ProviderRuntime( |
| 44 | reasoning_provider="local", planner_model="mock", rerank_model="mock", |
| 45 | ), |
| 46 | query_plan=schema.QueryPlan( |
| 47 | intent="breaking_news", freshness_mode="strict_recent", cluster_mode="story", |
| 48 | raw_topic=topic, subqueries=[], source_weights={}, |
| 49 | ), |
| 50 | clusters=[], ranked_candidates=candidates, items_by_source=by_source, |
| 51 | errors_by_source={}, |
| 52 | ) |
| 53 | |
| 54 | |
| 55 | TRANSCRIPT = {"transcript_snippet": "a transcript"} |
| 56 | ROBOTAXIS = _item("youtube", "TukUnoOi6VA", "3,200 Chinese Robotaxis Just Hit the US. Tesla Is Shocked", |
| 57 | "Auto Aspect", {"views": 132385, "likes": 1280}, |
| 58 | body="Waymo has imported more than 3,200 electric vans", metadata=TRANSCRIPT) |
| 59 | CYBERCAB = _item("youtube", "A0-2H6i2teU", "Tesla Cybercab Robotaxi Mass Operation Amazing UPDATE", |
| 60 | "TESLA CAR WORLD", {"views": 59678, "likes": 786}, |
| 61 | body="Waymo's sixth-generation autonomous driving system", metadata=TRANSCRIPT) |
| 62 | ROADSTER = _item("youtube", "scwlR0QHGOk", "The NEW Tesla Roadster: My Honest Reaction", |
| 63 | "Marques Brownlee", {"views": 12519652, "likes": 192799}, |
| 64 | body="Tesla had one of the most insane product demos of all time", metadata=TRANSCRIPT) |
| 65 | FOURTH = _item("youtube", "waymo4", "Riding a Waymo on the freeway", "Ride Review", |
| 66 | {"views": 33365, "likes": 760}, body="Waymo freeway rides", metadata=TRANSCRIPT) |
| 67 | WAYMO_VIDEOS = [ROBOTAXIS, CYBERCAB, ROADSTER, FOURTH] |
| 68 | |
| 69 | |
| 70 | def _youtube_line(report): |
| 71 | return next(line for line in render._build_source_footer_lines(report) if "YouTube" in line) |
| 72 | |
| 73 | |
| 74 | def test_saved_waymo_run_counts_views_of_on_topic_videos_only(): |
| 75 | candidates = [ |
| 76 | _candidate(ROBOTAXIS), _candidate(CYBERCAB), _candidate(FOURTH), |
| 77 | _candidate(ROADSTER, explanation=ENTITY_MISS, final_score=12.0), |
| 78 | ] |
| 79 | |
| 80 | line = _youtube_line(_report("Waymo", WAYMO_VIDEOS, candidates)) |
| 81 | |
| 82 | # 132,385 + 59,678 + 33,365; the item count and transcripts stay as they were. |
| 83 | assert line == "🔴 YouTube: 4 videos │ 225,428 views │ 4/4 with transcripts" |
| 84 | |
| 85 | |
| 86 | def test_item_pruned_as_off_topic_is_not_counted(): |
| 87 | # prune_fallback_entity_misses drops the candidate; the item stays in the |
| 88 | # dump and the pipeline records that it failed the relevance check. |
| 89 | candidates = [_candidate(ROBOTAXIS), _candidate(CYBERCAB), _candidate(FOURTH)] |
| 90 | report = _report("Waymo", WAYMO_VIDEOS, candidates) |
| 91 | report.artifacts[rerank.REJECTED_ITEMS_ARTIFACT] = rerank.rejected_item_keys( |
| 92 | [*candidates, _candidate(ROADSTER, explanation=ENTITY_MISS, final_score=12.0)], candidates, |
| 93 | ) |
| 94 | |
| 95 | assert "225,428 views" in _youtube_line(report) |
| 96 | |
| 97 | |
| 98 | def test_item_with_no_ranking_verdict_still_counts(): |
| 99 | # Fusion drops candidates for reasons that are not relevance (the |
| 100 | # per-author cap, the pool size), so a missing verdict is not "off topic": |
| 101 | # four on-topic posts by one author leave three candidates. |
| 102 | posts = [ |
| 103 | _item("x", f"p{i}", f"Elon Musk on Starship flight {i}", "SawyerMerritt", |
| 104 | {"likes": 100, "reposts": 10}) |
| 105 | for i in range(4) |
| 106 | ] |
| 107 | report = _report("Elon Musk", posts, [_candidate(post) for post in posts[:3]]) |
| 108 | |
| 109 | lines = render._build_source_footer_lines(report) |
| 110 | |
| 111 | assert lines == ["🔵 X: 4 posts │ 400 likes │ 40 reposts"] |
| 112 | assert render._top_voices_footer_line(report) == "🗣️ Top voices: @SawyerMerritt" |
| 113 | |
| 114 | |
| 115 | def test_fusion_author_cap_really_leaves_on_topic_items_without_a_candidate(): |
| 116 | # The premise of the test above, from the code: weighted_rrf keeps fewer |
| 117 | # candidates than one author's on-topic posts. |
| 118 | from lib import fusion |
| 119 | |
| 120 | posts = [ |
| 121 | _item("x", f"p{i}", f"Elon Musk on Starship flight {i}", "SawyerMerritt", |
| 122 | {"likes": 100, "reposts": 10}) |
| 123 | for i in range(8) |
| 124 | ] |
| 125 | for post in posts: |
| 126 | post.local_relevance, post.freshness, post.source_quality = 0.8, 80, 0.7 |
| 127 | plan = schema.QueryPlan( |
| 128 | intent="breaking_news", freshness_mode="strict_recent", cluster_mode="story", |
| 129 | raw_topic="Elon Musk", source_weights={}, |
| 130 | subqueries=[schema.SubQuery(label="primary", search_query="Elon Musk", |
| 131 | ranking_query="Elon Musk", sources=["x"])], |
| 132 | ) |
| 133 | |
| 134 | candidates = fusion.weighted_rrf({("primary", "x"): posts}, plan, pool_limit=50) |
| 135 | |
| 136 | assert 0 < len(candidates) < len(posts) |
| 137 | |
| 138 | |
| 139 | def test_pipeline_records_what_the_relevance_prune_rejected(): |
| 140 | kept, dropped = _candidate(ROBOTAXIS), _candidate(ROADSTER, explanation=ENTITY_MISS) |
| 141 | |
| 142 | assert rerank.rejected_item_keys([kept, dropped], [kept]) == [["youtube", ROADSTER.url]] |
| 143 | |
| 144 | |
| 145 | def test_report_with_no_ranking_counts_every_item(): |
| 146 | line = _youtube_line(_report("Waymo", WAYMO_VIDEOS, [])) |
| 147 | |
| 148 | assert "12,745,080 views" in line |
| 149 | |
| 150 | |
| 151 | def test_source_with_nothing_on_topic_is_counted_whole(): |
| 152 | candidates = [_candidate(ROADSTER, explanation=ENTITY_MISS, final_score=12.0)] |
| 153 | |
| 154 | line = _youtube_line(_report("Waymo", [ROADSTER], candidates)) |
| 155 | |
| 156 | assert "12,519,652 views" in line |
| 157 | |
| 158 | |
| 159 | OWN_POST = _item("x", "x1", "Starship flight 15 went great", "elonmusk", {"likes": 9000, "reposts": 900}) |
| 160 | ABOUT_POST = _item("x", "x2", "Elon Musk says Tesla will ship Optimus in 2027", "SawyerMerritt", |
| 161 | {"likes": 400, "reposts": 40}) |
| 162 | RELATED_ON_TOPIC = _item("x", "x3", "Elon Musk on stage at the Tesla shareholder meeting", "Tesla", |
| 163 | {"likes": 300, "reposts": 30}) |
| 164 | RELATED_OFF_TOPIC = _item("x", "x4", "Model Y is now available in two new colors", "Tesla", |
| 165 | {"likes": 50000, "reposts": 7000}) |
| 166 | RELATED_OFF_TOPIC_2 = _item("x", "x5", "Supercharger network passes 80,000 stalls", "Tesla", |
| 167 | {"likes": 30000, "reposts": 4000}) |
| 168 | |
| 169 | |
| 170 | def _person_report(): |
| 171 | items = [OWN_POST, ABOUT_POST, RELATED_ON_TOPIC, RELATED_OFF_TOPIC, RELATED_OFF_TOPIC_2] |
| 172 | candidates = [ |
| 173 | _candidate(OWN_POST, explanation=FIRST_PARTY, local_relevance=0.05), |
| 174 | _candidate(ABOUT_POST), |
| 175 | _candidate(RELATED_ON_TOPIC, explanation=FIRST_PARTY, local_relevance=0.5), |
| 176 | _candidate(RELATED_OFF_TOPIC, explanation=FIRST_PARTY, local_relevance=0.05), |
| 177 | _candidate(RELATED_OFF_TOPIC_2, explanation=rerank._FIRST_PARTY_EXPLANATION, local_relevance=0.05), |
| 178 | ] |
| 179 | return _report("Elon Musk", items, candidates) |
| 180 | |
| 181 | |
| 182 | def test_related_handle_posts_about_other_things_stay_out_of_x_totals(): |
| 183 | line = next(line for line in render._build_source_footer_lines(_person_report()) if " X:" in line) |
| 184 | |
| 185 | # The person's own posts count (nobody names themselves); a related |
| 186 | # handle's posts count only when they are about the person. |
| 187 | assert line == "🔵 X: 5 posts │ 9,700 likes │ 970 reposts" |
| 188 | |
| 189 | |
| 190 | def test_top_voices_are_drawn_from_on_topic_posts(): |
| 191 | line = render._top_voices_footer_line(_person_report()) |
| 192 | |
| 193 | # Three posts by @Tesla, one about the topic: no longer the top voice. |
| 194 | assert line == "🗣️ Top voices: @elonmusk, @SawyerMerritt, @Tesla" |
| 195 | |
| 196 | |
| 197 | def test_candidate_gate_keeps_the_topics_own_account_and_grounded_related_posts(): |
| 198 | verdicts = rerank.item_topic_verdicts(_person_report().ranked_candidates, "Elon Musk") |
| 199 | |
| 200 | assert verdicts == { |
| 201 | ("x", OWN_POST.url): True, |
| 202 | ("x", ABOUT_POST.url): True, |
| 203 | ("x", RELATED_ON_TOPIC.url): True, |
| 204 | ("x", RELATED_OFF_TOPIC.url): False, |
| 205 | ("x", RELATED_OFF_TOPIC_2.url): False, |
| 206 | } |
| 207 | |
| 208 | |
| 209 | # Microsoft run, #1449: @TheCyberSecHub's ten headline posts (93 likes at |
| 210 | # best) were "most prolific" and so the top voice, above @Microsoft's own |
| 211 | # account with a 69,910-like post. Top voices now rank @handles by engagement. |
| 212 | |
| 213 | def _x_post(index, author, likes, reposts=0, replies=0): |
| 214 | return _item("x", f"{author}-{index}", f"Microsoft news {index}", author, |
| 215 | {"likes": likes, "reposts": reposts, "replies": replies}) |
| 216 | |
| 217 | |
| 218 | def _voices(report): |
| 219 | return render._top_voices_footer_line(report) |
| 220 | |
| 221 | |
| 222 | def test_ten_zero_like_posts_lose_to_three_high_engagement_posts(): |
| 223 | posts = [_x_post(i, "Aggregator", 0, replies=1 if i % 2 else 0) for i in range(10)] |
| 224 | posts += [_x_post(i, "RealVoice", 500 + i, reposts=40) for i in range(3)] |
| 225 | report = _report("Microsoft", posts, []) |
| 226 | |
| 227 | assert _voices(report) == "🗣️ Top voices: @RealVoice" |
| 228 | |
| 229 | |
| 230 | def test_grumpy_news_style_digest_loses_to_one_liked_post(): |
| 231 | posts = [_x_post(i, "GrumpyNews_", 0, replies=1) for i in range(10)] |
| 232 | posts.append(_x_post(0, "WmBrackbill", 44)) |
| 233 | report = _report("Microsoft", posts, []) |
| 234 | |
| 235 | assert _voices(report) == "🗣️ Top voices: @WmBrackbill" |
| 236 | |
| 237 | |
| 238 | def test_volume_counts_only_three_posts_per_handle(): |
| 239 | # Ten 50-like posts score as three; three 300-like posts beat them. |
| 240 | posts = [_x_post(i, "Prolific", 50) for i in range(10)] |
| 241 | posts += [_x_post(i, "Popular", 300) for i in range(3)] |
| 242 | report = _report("Microsoft", posts, []) |
| 243 | |
| 244 | assert _voices(report) == "🗣️ Top voices: @Popular, @Prolific" |
| 245 | |
| 246 | |
| 247 | def test_handle_counted_beyond_three_posts_ranks_like_its_best_three(): |
| 248 | best_three = [_x_post(i, "Capped", 100) for i in range(3)] |
| 249 | many = best_three + [_x_post(i, "Capped", 1) for i in range(3, 10)] |
| 250 | |
| 251 | assert (render._rank_voice_handles(many) == render._rank_voice_handles(best_three) |
| 252 | == ["@Capped"]) |
| 253 | |
| 254 | |
| 255 | def test_subject_handle_is_kept_below_the_engagement_floor(): |
| 256 | posts = [_x_post(0, "Microsoft", 1), _x_post(0, "TheCyberSecHub", 93)] |
| 257 | report = _report("Microsoft", posts, []) |
| 258 | assert _voices(report) == "🗣️ Top voices: @TheCyberSecHub" |
| 259 | |
| 260 | report.artifacts["resolved"] = {"x_handle": "microsoft"} |
| 261 | assert _voices(report) == "🗣️ Top voices: @TheCyberSecHub, @Microsoft" |
| 262 | |
| 263 | |
| 264 | def test_issue_run_shape_puts_the_subject_account_first(): |
| 265 | likes = [93, 20, 6, 4, 2, 1, 3, 0, 1, 0] |
| 266 | posts = [_x_post(i, "TheCyberSecHub", n) for i, n in enumerate(likes)] |
| 267 | posts += [_x_post(i, "Microsoft", n) for i, n in enumerate([69910, 4200, 2100, 900])] |
| 268 | posts.append(_x_post(0, "mustafasuleyman", 1800, reposts=200)) |
| 269 | report = _report("Microsoft", posts, []) |
| 270 | report.artifacts["resolved"] = {"x_handle": "Microsoft"} |
| 271 | |
| 272 | # Sum of log1p over up to three posts: 93, 20 and 6 likes (about 9.5) |
| 273 | # still edge out one 2,000-engagement post (about 7.6), but no longer the |
| 274 | # subject's own account (about 27). |
| 275 | assert _voices(report) == "🗣️ Top voices: @Microsoft, @TheCyberSecHub, @mustafasuleyman" |
| 276 | stats_line = next(line for line in render.render_compact(report).splitlines() if line.startswith("- X: 15 items |")) |
| 277 | assert stats_line.endswith("voices: @Microsoft, @TheCyberSecHub, @mustafasuleyman") |
| 278 | |
| 279 | |
| 280 | def test_names_without_an_at_are_not_ranked_as_handles(): |
| 281 | video = _item("youtube", "v1", "Microsoft keynote", "Microsoft Developer", |
| 282 | {"views": 900000, "likes": 30000}) |
| 283 | posts = [video, _x_post(0, "Satya", 12)] |
| 284 | report = _report("Microsoft", posts, []) |
| 285 | |
| 286 | assert _voices(report) == "🗣️ Top voices: @Satya" |
| 287 | assert render._rank_voice_handles(posts) == ["@Satya"] |
| 288 | |
| 289 | |
| 290 | def test_handles_without_engagement_metrics_stay_eligible(): |
| 291 | # A source that reports no metrics cannot fail the floor. |
| 292 | posts = [_item("x", f"n{i}", "Microsoft news", "NoMetrics", {}) for i in range(2)] |
| 293 | posts.append(_x_post(0, "Measured", 10)) |
| 294 | |
| 295 | assert render._rank_voice_handles(posts) == ["@Measured", "@NoMetrics"] |
| 296 | |
| 297 | |
| 298 | def test_engagement_ties_fall_back_to_post_count_then_handle(): |
| 299 | posts = [_x_post(0, "beta", 20), _x_post(0, "Alpha", 20), |
| 300 | _x_post(0, "gamma", 0), _x_post(1, "gamma", 0)] |
| 301 | posts += [_item("x", "g2", "Microsoft news", "gamma", {"likes": 20})] |
| 302 | |
| 303 | assert render._rank_voice_handles(posts) == ["@gamma", "@Alpha", "@beta"] |
| 304 | |
| 305 | |
| 306 | def test_tiktok_shares_and_comments_count_as_reposts_and_replies(): |
| 307 | clip = _item("tiktok", "t1", "Microsoft layoffs", "@clipper", |
| 308 | {"views": 10000, "likes": 2, "shares": 2, "comments": 2}) |
| 309 | |
| 310 | assert render._voice_post_engagement(clip) == 6 |
| 311 | assert render._rank_voice_handles([clip]) == ["@clipper"] |
| 312 | |
| 313 | |
| 314 | def test_handle_spellings_group_case_insensitively(): |
| 315 | # Two 3-like posts each miss the floor alone; as one account they clear it. |
| 316 | posts = [_x_post(0, "Example", 3), _x_post(1, "example", 3)] |
| 317 | report = _report("Microsoft", posts, []) |
| 318 | |
| 319 | assert render._rank_voice_handles(posts) == ["@Example"] |
| 320 | assert _voices(report) == "🗣️ Top voices: @Example" |
| 321 | assert render._top_voices_overall({"x": posts}) == ["@Example"] |
| 322 |