返回 last30days-skill
test_cluster_v3.py
根目录 / tests / test_cluster_v3.py
1 import unittest
2
3 from lib import cluster, schema
4
5
6 def make_candidate(candidate_id: str, source: str, title: str, snippet: str, score: float) -> schema.Candidate:
7 return schema.Candidate(
8 candidate_id=candidate_id,
9 item_id=candidate_id,
10 source=source,
11 title=title,
12 url=f"https://example.com/{candidate_id}",
13 snippet=snippet,
14 subquery_labels=["primary"],
15 native_ranks={"primary:reddit": 1},
16 local_relevance=0.8,
17 freshness=80,
18 engagement=10,
19 source_quality=0.7,
20 rrf_score=0.02,
21 rerank_score=score,
22 final_score=score,
23 )
24
25
26 class ClusterV3Tests(unittest.TestCase):
27 def test_singleton_clusters_for_non_clustered_plan(self):
28 plan = schema.QueryPlan(
29 intent="how_to",
30 freshness_mode="balanced_recent",
31 cluster_mode="none",
32 raw_topic="docker setup",
33 subqueries=[schema.SubQuery(label="primary", search_query="docker setup", ranking_query="How do I set up Docker?", sources=["reddit"])],
34 source_weights={"reddit": 1.0},
35 )
36 candidates = [
37 make_candidate("c1", "reddit", "Docker setup guide", "Step by step setup", 80),
38 make_candidate("c2", "youtube", "Docker install video", "Video walkthrough", 75),
39 ]
40 clusters = cluster.cluster_candidates(candidates, plan)
41 self.assertEqual(2, len(clusters))
42 self.assertEqual(["c1"], clusters[0].representative_ids)
43 self.assertEqual(["c2"], clusters[1].representative_ids)
44
45 def test_breaking_news_clusters_related_items(self):
46 plan = schema.QueryPlan(
47 intent="breaking_news",
48 freshness_mode="strict_recent",
49 cluster_mode="story",
50 raw_topic="model launch",
51 subqueries=[schema.SubQuery(label="primary", search_query="model launch", ranking_query="What happened in the model launch?", sources=["reddit", "x"])],
52 source_weights={"reddit": 0.5, "x": 0.5},
53 )
54 candidates = [
55 make_candidate("c1", "reddit", "Open model launch reactions", "People are reacting to the open model launch today.", 88),
56 make_candidate("c2", "x", "Open model launch update", "People are reacting to the open model launch today on X.", 84),
57 make_candidate("c3", "youtube", "Different topic", "A separate discussion about hardware benchmarks.", 70),
58 ]
59 clusters = cluster.cluster_candidates(candidates, plan)
60 self.assertEqual(2, len(clusters))
61 self.assertEqual(2, len(clusters[0].candidate_ids))
62 self.assertIn("c1", clusters[0].candidate_ids)
63 self.assertIn("c2", clusters[0].candidate_ids)
64
65
66 class TestCrossSourceMerging(unittest.TestCase):
67 """Test the entity-based second pass that merges same-story clusters across sources."""
68
69 def _plan(self, intent="breaking_news"):
70 return schema.QueryPlan(
71 intent=intent,
72 freshness_mode="strict_recent",
73 cluster_mode="story",
74 raw_topic="test",
75 subqueries=[schema.SubQuery(label="primary", search_query="test", ranking_query="test", sources=["reddit", "x", "tiktok"])],
76 source_weights={"reddit": 0.5, "x": 0.5, "tiktok": 0.5},
77 )
78
79 def test_same_story_different_phrasing_merges(self):
80 """Wireless Festival example: same event, different wording, different sources."""
81 candidates = [
82 make_candidate("c1", "reddit", "Kanye West to headline all three nights of Wireless Festival 2026", "Big announcement for Wireless.", 80),
83 make_candidate("c2", "x", "BREAKING: Kanye West is making his massive UK comeback at Wireless Festival this July", "Ye returns to UK.", 75),
84 make_candidate("c3", "youtube", "Kanye West BULLY Album Review - Knox Hill Reacts", "Full album reaction and breakdown.", 70),
85 ]
86 clusters = cluster.cluster_candidates(candidates, self._plan())
87 # c1 and c2 should merge (Kanye + Wireless + Festival overlap), c3 should stay separate
88 self.assertEqual(2, len(clusters))
89 wireless_cluster = next(cl for cl in clusters if len(cl.candidate_ids) == 2)
90 self.assertIn("c1", wireless_cluster.candidate_ids)
91 self.assertIn("c2", wireless_cluster.candidate_ids)
92 self.assertEqual(sorted(["reddit", "x"]), wireless_cluster.sources)
93 # Multi-source cluster should not have "single-source" uncertainty
94 self.assertNotEqual("single-source", wireless_cluster.uncertainty)
95
96 def test_different_stories_dont_merge(self):
97 """Different topics should stay separate even with some entity overlap (e.g., 'Kanye')."""
98 candidates = [
99 make_candidate("c1", "reddit", "Kanye West BULLY Album First Impressions Thread", "What do you think of BULLY?", 80),
100 make_candidate("c2", "x", "Kanye West apology for antisemitism in Wall Street Journal ad", "Full page WSJ ad.", 75),
101 make_candidate("c3", "tiktok", "Kanye West Wireless Festival ticket prices breakdown", "How much for Wireless tickets?", 70),
102 ]
103 clusters = cluster.cluster_candidates(candidates, self._plan())
104 # These are 3 different stories, should remain as 3 clusters
105 self.assertEqual(3, len(clusters))
106
107 def test_same_source_clusters_dont_merge(self):
108 """Two single-source clusters from the same source should not merge via entity pass."""
109 candidates = [
110 make_candidate("c1", "reddit", "Kanye West Wireless Festival headline announcement", "Three nights!", 80),
111 make_candidate("c2", "reddit", "Kanye West returning to Wireless Festival confirmed", "UK comeback.", 70),
112 ]
113 initial = [
114 schema.Cluster(
115 cluster_id=f"cluster-{candidate.candidate_id}",
116 title=candidate.title,
117 candidate_ids=[candidate.candidate_id],
118 representative_ids=[candidate.candidate_id],
119 sources=[candidate.source],
120 score=candidate.final_score,
121 )
122 for candidate in candidates
123 ]
124 clusters = cluster._merge_entity_clusters(initial, candidates)
125 self.assertEqual([("c1",), ("c2",)], [tuple(cl.candidate_ids) for cl in clusters])
126 self.assertEqual([["reddit"], ["reddit"]], [cl.sources for cl in clusters])
127
128
129 class TestPolymarketIsolation(unittest.TestCase):
130 """Polymarket clusters must not merge with non-Polymarket clusters via entity overlap."""
131
132 def _plan(self):
133 return schema.QueryPlan(
134 intent="breaking_news",
135 freshness_mode="strict_recent",
136 cluster_mode="story",
137 raw_topic="test",
138 subqueries=[schema.SubQuery(label="primary", search_query="test", ranking_query="test", sources=["reddit", "x", "polymarket"])],
139 source_weights={"reddit": 0.5, "x": 0.5, "polymarket": 0.5},
140 )
141
142 def test_polymarket_does_not_merge_into_news_cluster(self):
143 """A Polymarket prediction about Sam Altman should not merge into a news cluster about Sam Altman."""
144 candidates = [
145 make_candidate("c1", "reddit", "Sam Altman personal rivalry with Elon Musk escalates", "The feud between Sam Altman and Elon Musk continues.", 80),
146 make_candidate("c2", "polymarket", "Sam Altman equity stake in OpenAI valued at $500M", "Will Sam Altman receive equity in OpenAI restructuring?", 75),
147 ]
148 clusters = cluster.cluster_candidates(candidates, self._plan())
149 self.assertEqual(2, len(clusters), "Polymarket and news clusters should remain separate")
150 # Each cluster should have exactly one candidate
151 for cl in clusters:
152 self.assertEqual(1, len(cl.candidate_ids))
153
154 def test_two_polymarket_clusters_not_blocked_by_poly_guard(self):
155 """Two Polymarket items about the same topic are not blocked by the Polymarket guard.
156
157 Note: same-source clusters are still blocked by the existing same-source
158 guard, so we verify the poly guard specifically by checking that two
159 polymarket items with high text similarity merge via the greedy pass.
160 """
161 candidates = [
162 make_candidate("c1", "polymarket", "Sam Altman equity stake in OpenAI restructuring", "Will Sam Altman get equity in the OpenAI restructuring deal?", 80),
163 make_candidate("c2", "polymarket", "Sam Altman equity stake in OpenAI restructuring odds", "Will Sam Altman get equity in the OpenAI restructuring deal? Current odds.", 75),
164 ]
165 clusters = cluster.cluster_candidates(candidates, self._plan())
166 # High text similarity means greedy pass merges them
167 self.assertEqual(1, len(clusters))
168 self.assertEqual(2, len(clusters[0].candidate_ids))
169
170 def test_neither_polymarket_still_merges(self):
171 """Non-Polymarket clusters with entity overlap should still merge (existing behavior)."""
172 candidates = [
173 make_candidate("c1", "reddit", "Sam Altman OpenAI restructuring announcement details", "Sam Altman announces major OpenAI restructuring.", 80),
174 make_candidate("c2", "x", "Sam Altman reveals OpenAI restructuring plan for 2026", "Major OpenAI restructuring coming says Sam Altman.", 75),
175 ]
176 clusters = cluster.cluster_candidates(candidates, self._plan())
177 self.assertEqual(1, len(clusters))
178 self.assertEqual(2, len(clusters[0].candidate_ids))
179
180
181 class TestStaleClusterDemotion(unittest.TestCase):
182 """Stale candidates must never become cluster representatives or titles."""
183
184 def _plan(self):
185 return schema.QueryPlan(
186 intent="breaking_news",
187 freshness_mode="strict_recent",
188 cluster_mode="story",
189 raw_topic="test",
190 subqueries=[schema.SubQuery(label="primary", search_query="test", ranking_query="test", sources=["reddit", "x"])],
191 source_weights={"reddit": 0.5, "x": 0.5},
192 )
193
194 def _candidate_with_date(
195 self, candidate_id: str, source: str, title: str, score: float,
196 published_at: str, date_confidence: str, range_from: str, range_to: str,
197 ) -> schema.Candidate:
198 item = schema.SourceItem(
199 item_id=candidate_id,
200 source=source,
201 title=title,
202 body=title,
203 url=f"https://example.com/{candidate_id}",
204 published_at=published_at,
205 date_confidence=date_confidence,
206 )
207 return schema.Candidate(
208 candidate_id=candidate_id,
209 item_id=candidate_id,
210 source=source,
211 title=title,
212 url=f"https://example.com/{candidate_id}",
213 snippet=title,
214 subquery_labels=["primary"],
215 native_ranks={"primary:reddit": 1},
216 local_relevance=0.8,
217 freshness=80,
218 engagement=10,
219 source_quality=0.7,
220 rrf_score=0.02,
221 rerank_score=score,
222 final_score=score,
223 source_items=[item],
224 metadata={"range_from": range_from, "range_to": range_to},
225 )
226
227 def test_stale_candidate_not_cluster_representative(self):
228 """A stale item with higher final_score must not lead a cluster over a fresh item.
229
230 This guards against the issue where a 2025-10 video ranked #1 in a
231 2026-07 brief because clustering re-sorted by final_score alone.
232 """
233 range_from = "2026-06-15"
234 range_to = "2026-07-15"
235 stale = self._candidate_with_date(
236 "stale", "reddit", "Model launch reactions discussion",
237 score=95.0,
238 published_at="2025-10-15",
239 date_confidence="low",
240 range_from=range_from,
241 range_to=range_to,
242 )
243 fresh = self._candidate_with_date(
244 "fresh", "x", "Model launch reactions update",
245 score=50.0,
246 published_at="2026-07-10",
247 date_confidence="high",
248 range_from=range_from,
249 range_to=range_to,
250 )
251 candidates = [stale, fresh]
252 clusters = cluster.cluster_candidates(candidates, self._plan())
253
254 self.assertEqual(1, len(clusters))
255 self.assertEqual("fresh", clusters[0].representative_ids[0])
256 self.assertEqual(fresh.title, clusters[0].title)
257
258
259 class TestClusterUncertainty(unittest.TestCase):
260 def test_single_source_returns_single_source(self):
261 candidates = [make_candidate("c1", "reddit", "Title", "Body", 80)]
262 result = cluster._cluster_uncertainty(candidates)
263 self.assertEqual("single-source", result)
264
265 def test_multi_source_high_score_returns_none(self):
266 candidates = [
267 make_candidate("c1", "reddit", "Title", "Body", 80),
268 make_candidate("c2", "x", "Title2", "Body2", 70),
269 ]
270 result = cluster._cluster_uncertainty(candidates)
271 self.assertIsNone(result)
272
273 def test_multi_source_low_score_returns_thin_evidence(self):
274 candidates = [
275 make_candidate("c1", "reddit", "Title", "Body", 30),
276 make_candidate("c2", "x", "Title2", "Body2", 40),
277 ]
278 result = cluster._cluster_uncertainty(candidates)
279 self.assertEqual("thin-evidence", result)
280
281 if __name__ == "__main__":
282 unittest.main()
283
283 lines PYTHON