| 1 | """Tests for relevance-blended Top Community Comments ranking (issue #641). |
| 2 | |
| 3 | Off-topic high-traffic threads should not crowd out on-topic lower-voted threads. |
| 4 | """ |
| 5 | |
| 6 | import math |
| 7 | |
| 8 | from lib import render, schema, signals |
| 9 | |
| 10 | |
| 11 | def _candidate( |
| 12 | *, |
| 13 | source: str = "reddit", |
| 14 | title: str = "test post", |
| 15 | url: str = "https://example.com/1", |
| 16 | local_relevance: float = 0.8, |
| 17 | top_comments=None, |
| 18 | ) -> schema.Candidate: |
| 19 | source_items = [] |
| 20 | top_comments = ( |
| 21 | [{"depth": 0, **c} for c in top_comments] if top_comments is not None else None |
| 22 | ) |
| 23 | if top_comments is not None: |
| 24 | source_items.append( |
| 25 | schema.SourceItem( |
| 26 | item_id=f"si-{url}", |
| 27 | source=source, |
| 28 | title=title, |
| 29 | body="", |
| 30 | url=url, |
| 31 | metadata={"top_comments": top_comments}, |
| 32 | ) |
| 33 | ) |
| 34 | c = schema.Candidate( |
| 35 | candidate_id=f"c-{url}", |
| 36 | item_id=f"i-{url}", |
| 37 | source=source, |
| 38 | title=title, |
| 39 | url=url, |
| 40 | snippet="", |
| 41 | subquery_labels=["q1"], |
| 42 | native_ranks={source: 1}, |
| 43 | local_relevance=local_relevance, |
| 44 | freshness=50, |
| 45 | engagement=0, |
| 46 | source_quality=0.5, |
| 47 | rrf_score=0.01, |
| 48 | source_items=source_items, |
| 49 | ) |
| 50 | c.final_score = 50.0 |
| 51 | return c |
| 52 | |
| 53 | |
| 54 | def _make_report(*candidates) -> schema.Report: |
| 55 | return schema.Report( |
| 56 | topic="test topic", |
| 57 | range_from="2026-01-01", |
| 58 | range_to="2026-01-31", |
| 59 | generated_at="2026-01-31T00:00:00Z", |
| 60 | provider_runtime=schema.ProviderRuntime( |
| 61 | reasoning_provider="test", planner_model="test", rerank_model="test", |
| 62 | ), |
| 63 | query_plan=schema.QueryPlan( |
| 64 | intent="general", freshness_mode="balanced_recent", |
| 65 | cluster_mode="debate", raw_topic="test", subqueries=[], |
| 66 | source_weights={}, |
| 67 | ), |
| 68 | ranked_candidates=list(candidates), |
| 69 | clusters=[], |
| 70 | items_by_source={}, |
| 71 | errors_by_source={}, |
| 72 | ) |
| 73 | |
| 74 | |
| 75 | class TestTopCommentsRelevanceGate: |
| 76 | def test_below_floor_candidates_excluded(self): |
| 77 | """Candidates below RELEVANCE_FLOOR must not contribute comments.""" |
| 78 | off_topic = _candidate( |
| 79 | url="https://example.com/offtopic", |
| 80 | local_relevance=0.05, # below RELEVANCE_FLOOR=0.1 |
| 81 | top_comments=[{"body": "viral off-topic comment nine thousand votes", "score": 9000}], |
| 82 | ) |
| 83 | on_topic = [ |
| 84 | _candidate( |
| 85 | url=f"https://example.com/ontopic-{idx}", |
| 86 | local_relevance=0.7, |
| 87 | top_comments=[{"body": f"directly about the topic comment {idx}", "score": 50 - idx}], |
| 88 | ) |
| 89 | for idx in range(5) |
| 90 | ] |
| 91 | report = _make_report(off_topic, *on_topic) |
| 92 | lines = render._render_top_comments(report) |
| 93 | combined = "\n".join(lines) |
| 94 | assert "viral off-topic comment" not in combined |
| 95 | assert "directly about the topic" in combined |
| 96 | |
| 97 | def test_order_follows_the_ranking_not_the_vote_count(self): |
| 98 | """The pool lists top comments in the order the candidates are ranked. |
| 99 | |
| 100 | Ranking already weighs relevance, so the on-topic thread ranked first |
| 101 | leads with 50 upvotes, ahead of a barely related one with 200. |
| 102 | """ |
| 103 | ontopic_modest = _candidate( |
| 104 | url="https://example.com/ontopic", |
| 105 | local_relevance=0.90, |
| 106 | top_comments=[{"body": "On-topic comment fewer votes but on-topic", "score": 50}], |
| 107 | ) |
| 108 | viral_offtopic = _candidate( |
| 109 | url="https://example.com/viral", |
| 110 | local_relevance=0.12, # just above RELEVANCE_FLOOR |
| 111 | top_comments=[{"body": "Viral off-topic comment with moderate vote lead", "score": 200}], |
| 112 | ) |
| 113 | report = _make_report(ontopic_modest, viral_offtopic) |
| 114 | lines = render._render_top_comments(report) |
| 115 | combined = "\n".join(lines) |
| 116 | assert combined.index("On-topic comment") < combined.index("Viral off-topic") |
| 117 | |
| 118 | def test_sparse_topics_keep_comments_when_floor_would_remove_everything(self): |
| 119 | """Sparse niche topics should still surface comments below the soft relevance floor.""" |
| 120 | low_relevance_a = _candidate( |
| 121 | url="https://example.com/sparse-a", |
| 122 | local_relevance=0.08, |
| 123 | top_comments=[{"body": "Sparse topic comment still relevant enough to show", "score": 70}], |
| 124 | ) |
| 125 | low_relevance_b = _candidate( |
| 126 | url="https://example.com/sparse-b", |
| 127 | local_relevance=0.07, |
| 128 | top_comments=[{"body": "Another sparse topic comment below floor", "score": 50}], |
| 129 | ) |
| 130 | low_relevance_c = _candidate( |
| 131 | url="https://example.com/sparse-c", |
| 132 | local_relevance=0.05, |
| 133 | top_comments=[{"body": "Third sparse topic comment below floor", "score": 30}], |
| 134 | ) |
| 135 | report = _make_report(low_relevance_a, low_relevance_b, low_relevance_c) |
| 136 | |
| 137 | lines = render._render_top_comments(report) |
| 138 | combined = "\n".join(lines) |
| 139 | |
| 140 | assert "Sparse topic comment" in combined |
| 141 | assert "Another sparse topic comment" in combined |
| 142 | |
| 143 | def test_no_duplicate_comments(self): |
| 144 | """Same comment body must appear at most once even if two candidates share the text.""" |
| 145 | cand_a = _candidate( |
| 146 | url="https://example.com/a", |
| 147 | local_relevance=0.8, |
| 148 | top_comments=[{"body": "Shared comment body appears in two different threads", "score": 100}], |
| 149 | ) |
| 150 | cand_b = _candidate( |
| 151 | url="https://example.com/b", |
| 152 | local_relevance=0.8, |
| 153 | top_comments=[ |
| 154 | {"body": "Shared comment body appears in two different threads", "score": 80}, |
| 155 | {"body": "Second distinct comment that is different from the first", "score": 60}, |
| 156 | ], |
| 157 | ) |
| 158 | report = _make_report(cand_a, cand_b) |
| 159 | lines = render._render_top_comments(report) |
| 160 | occurrences = sum(1 for line in lines if "Shared comment body" in line) |
| 161 | assert occurrences == 1 |
| 162 | |
| 163 | |
| 164 | def _cluster(index: int, candidate: schema.Candidate, score: float) -> schema.Cluster: |
| 165 | return schema.Cluster( |
| 166 | cluster_id=f"cluster-{index}", |
| 167 | title=candidate.title, |
| 168 | candidate_ids=[candidate.candidate_id], |
| 169 | representative_ids=[candidate.candidate_id], |
| 170 | sources=[candidate.source], |
| 171 | score=score, |
| 172 | uncertainty=None, |
| 173 | ) |
| 174 | |
| 175 | |
| 176 | class TestTopCommentsReadAllFloorClearingClusters: |
| 177 | """The comments block is a cross-cutting evidence surface: a strong comment |
| 178 | on a thread in cluster eleven must render even though only the top eight |
| 179 | clusters are shown in ## Ranked Evidence Clusters.""" |
| 180 | |
| 181 | def _report(self): |
| 182 | candidates = [] |
| 183 | clusters = [] |
| 184 | for i in range(12): |
| 185 | comments = None |
| 186 | if i == 10: # cluster 11, below the compact cluster_limit of 8 |
| 187 | comments = [ |
| 188 | {"score": 3293, "author": "u/tableleg7", "excerpt": "Vladimir Putin doesn't care about black people"}, |
| 189 | {"score": 1151, "author": "u/stereosalvation", "excerpt": "Someone didn't get paid off yet"}, |
| 190 | {"score": 400, "author": "u/third", "excerpt": "third comment body here"}, |
| 191 | ] |
| 192 | cand = _candidate( |
| 193 | title=f"test thread {i}", |
| 194 | url=f"https://www.reddit.com/r/Kanye/comments/t{i}/", |
| 195 | local_relevance=0.6, |
| 196 | top_comments=comments if comments else [], |
| 197 | ) |
| 198 | cand.final_score = 60.0 - i |
| 199 | candidates.append(cand) |
| 200 | clusters.append(_cluster(i + 1, cand, cand.final_score)) |
| 201 | report = _make_report(*candidates) |
| 202 | report.clusters = clusters |
| 203 | return report |
| 204 | |
| 205 | def test_comment_from_cluster_eleven_renders_in_compact(self): |
| 206 | text = render.render_compact(self._report(), cluster_limit=8) |
| 207 | assert "## Top Community Comments" in text |
| 208 | assert "Vladimir Putin doesn't care about black people" in text |
| 209 | |
| 210 | def test_entity_miss_cluster_member_still_excluded(self): |
| 211 | report = self._report() |
| 212 | report.ranked_candidates[10].explanation = "fallback-local-score (entity-miss demotion)" |
| 213 | text = render.render_compact(report, cluster_limit=8) |
| 214 | assert "Vladimir Putin doesn't care" not in text |
| 215 |