返回 last30days-skill
test_signals_v3.py
根目录 / tests / test_signals_v3.py
1 import math
2 import unittest
3
4 from lib import schema, signals
5 from lib.hackernews import parse_hackernews_response
6
7
8 class SignalsV3Tests(unittest.TestCase):
9 def test_reddit_engagement_uses_source_specific_formula(self):
10 item = schema.SourceItem(
11 item_id="r1",
12 source="reddit",
13 title="Title",
14 body="Body",
15 url="https://example.com",
16 engagement={"score": 99, "num_comments": 20, "upvote_ratio": 0.8},
17 metadata={"top_comments": [{"score": 10}]},
18 )
19 expected = (
20 0.50 * math.log1p(99)
21 + 0.35 * math.log1p(20)
22 + 0.05 * (0.8 * 10.0)
23 + 0.10 * math.log1p(10)
24 )
25 self.assertAlmostEqual(expected, signals.engagement_raw(item))
26
27 def test_youtube_engagement_adds_top_comment_slot(self):
28 with_comment = schema.SourceItem(
29 item_id="yt1",
30 source="youtube",
31 title="Title",
32 body="Body",
33 url="https://youtube.com/watch?v=a",
34 engagement={"views": 10000, "likes": 500, "comments": 30},
35 metadata={"top_comments": [{"score": 500}]},
36 )
37 without = schema.SourceItem(
38 item_id="yt2",
39 source="youtube",
40 title="Title",
41 body="Body",
42 url="https://youtube.com/watch?v=b",
43 engagement={"views": 10000, "likes": 500, "comments": 30},
44 metadata={"top_comments": []},
45 )
46 with_score = signals.engagement_raw(with_comment)
47 without_score = signals.engagement_raw(without)
48 self.assertIsNotNone(with_score)
49 self.assertIsNotNone(without_score)
50 self.assertGreater(with_score, without_score)
51 expected = (
52 0.45 * math.log1p(10000)
53 + 0.32 * math.log1p(500)
54 + 0.13 * math.log1p(30)
55 + 0.10 * math.log1p(500)
56 )
57 self.assertAlmostEqual(expected, with_score, places=6)
58
59 def test_youtube_engagement_empty_returns_none(self):
60 item = schema.SourceItem(
61 item_id="yt-empty",
62 source="youtube",
63 title="Title",
64 body="Body",
65 url="https://youtube.com/watch?v=e",
66 engagement={},
67 metadata={"top_comments": []},
68 )
69 self.assertIsNone(signals.engagement_raw(item))
70
71 def test_tiktok_engagement_adds_top_comment_slot(self):
72 item = schema.SourceItem(
73 item_id="tt1",
74 source="tiktok",
75 title="Title",
76 body="Body",
77 url="https://tiktok.com/@u/video/1",
78 engagement={"views": 100000, "likes": 5000, "comments": 500},
79 metadata={"top_comments": [{"score": 1200}]},
80 )
81 expected = (
82 0.45 * math.log1p(100000)
83 + 0.27 * math.log1p(5000)
84 + 0.18 * math.log1p(500)
85 + 0.10 * math.log1p(1200)
86 )
87 self.assertAlmostEqual(expected, signals.engagement_raw(item), places=6)
88
89 def test_instagram_engagement_adds_top_comment_slot(self):
90 """U2: IG gets the same 0.10 top-comment carve-out as TikTok, so a
91 highly-liked IG comment lifts its post's ranking."""
92 item = schema.SourceItem(
93 item_id="ig1",
94 source="instagram",
95 title="Title",
96 body="Body",
97 url="https://www.instagram.com/reel/ABC/",
98 engagement={"views": 100000, "likes": 5000, "comments": 500},
99 metadata={"top_comments": [{"score": 1200}]},
100 )
101 expected = (
102 0.45 * math.log1p(100000)
103 + 0.27 * math.log1p(5000)
104 + 0.18 * math.log1p(500)
105 + 0.10 * math.log1p(1200)
106 )
107 self.assertAlmostEqual(expected, signals.engagement_raw(item), places=6)
108
109 def test_instagram_comment_vote_uses_instagram_reference(self):
110 """U2: normalized_comment_vote uses the instagram reference, not the default."""
111 strength = signals.normalized_comment_vote("instagram", 5000)
112 self.assertAlmostEqual(math.log1p(5000) / 9.2, strength)
113 self.assertGreater(strength, 0.0)
114 self.assertLessEqual(strength, 1.0)
115
116 def test_youtube_ranking_promotes_viral_comment_thread(self):
117 """A moderately-viewed YouTube video with a 10k-like comment should
118 outrank a slightly-higher-viewed video with no high-signal comments."""
119 viral_comment = schema.SourceItem(
120 item_id="yt-with-viral-comment",
121 source="youtube",
122 title="Deploy to Fly.io",
123 body="Deploy to Fly.io walkthrough",
124 url="https://youtube.com/watch?v=x",
125 published_at="2026-03-15",
126 engagement={"views": 5000, "likes": 200, "comments": 50},
127 metadata={"top_comments": [{"score": 10000}]},
128 )
129 higher_views = schema.SourceItem(
130 item_id="yt-higher-views-no-comment",
131 source="youtube",
132 title="Deploy to Fly.io",
133 body="Deploy to Fly.io walkthrough",
134 url="https://youtube.com/watch?v=y",
135 published_at="2026-03-15",
136 engagement={"views": 8000, "likes": 300, "comments": 60},
137 metadata={"top_comments": []},
138 )
139 ranked = signals.annotate_stream(
140 [higher_views, viral_comment],
141 ranking_query="How do I deploy on Fly.io?",
142 freshness_mode="balanced_recent",
143 )
144 self.assertEqual("yt-with-viral-comment", ranked[0].item_id)
145
146 def test_polymarket_engagement_uses_market_fields(self):
147 item = schema.SourceItem(
148 item_id="pm1",
149 source="polymarket",
150 title="Title",
151 body="Body",
152 url="https://example.com",
153 engagement={"volume": 1000, "liquidity": 250},
154 )
155 expected = (0.60 * math.log1p(1000)) + (0.40 * math.log1p(250))
156 self.assertAlmostEqual(expected, signals.engagement_raw(item))
157
158 def test_grounding_uses_generic_fallback(self):
159 item = schema.SourceItem(
160 item_id="g1",
161 source="grounding",
162 title="Title",
163 body="Body",
164 url="https://example.com",
165 engagement={"shares": 10, "reads": 100},
166 )
167 expected = math.log1p(100) + 0.1 * math.log1p(10)
168 self.assertAlmostEqual(expected, signals.engagement_raw(item))
169
170 def test_annotate_stream_sorts_by_source_specific_reddit_engagement(self):
171 higher = schema.SourceItem(
172 item_id="r-high",
173 source="reddit",
174 title="High signal",
175 body="claude code skill",
176 url="https://example.com/high",
177 published_at="2026-03-15",
178 engagement={"score": 120, "num_comments": 40, "upvote_ratio": 0.9},
179 metadata={"top_comments": [{"score": 15}]},
180 )
181 lower = schema.SourceItem(
182 item_id="r-low",
183 source="reddit",
184 title="Lower signal",
185 body="claude code skill",
186 url="https://example.com/low",
187 published_at="2026-03-15",
188 engagement={"score": 4, "num_comments": 1, "upvote_ratio": 0.5},
189 metadata={"top_comments": [{"score": 1}]},
190 )
191 ranked = signals.annotate_stream(
192 [lower, higher],
193 ranking_query="What recent evidence matters for claude code skill?",
194 freshness_mode="balanced_recent",
195 )
196 self.assertEqual(["r-high", "r-low"], [item.item_id for item in ranked])
197
198 def test_local_relevance_dominates_over_high_engagement_noise(self):
199 relevant = schema.SourceItem(
200 item_id="relevant",
201 source="reddit",
202 title="Deploy to Fly.io with MCP in 60 seconds",
203 body="Deploy to Fly.io guide with concrete steps.",
204 url="https://example.com/relevant",
205 published_at="2026-03-15",
206 engagement={"score": 2, "num_comments": 0, "upvote_ratio": 0.8},
207 metadata={"top_comments": []},
208 )
209 noisy = schema.SourceItem(
210 item_id="noisy",
211 source="reddit",
212 title="BATTLEFIELD 6 GAME UPDATE 1.2.2.0",
213 body="Patch notes and gameplay discussion.",
214 url="https://example.com/noisy",
215 published_at="2026-03-15",
216 engagement={"score": 5000, "num_comments": 1200, "upvote_ratio": 0.95},
217 metadata={"top_comments": [{"score": 400}]},
218 )
219 ranked = signals.annotate_stream(
220 [noisy, relevant],
221 ranking_query="How do I deploy on Fly.io?",
222 freshness_mode="evergreen_ok",
223 )
224 self.assertEqual("relevant", ranked[0].item_id)
225
226 def test_prune_low_relevance_keeps_stronger_matches(self):
227 strong = schema.SourceItem(
228 item_id="strong",
229 source="reddit",
230 title="Deploy to Fly.io",
231 body="Step-by-step Fly.io deploy guide.",
232 url="https://example.com/strong",
233 local_relevance=0.3,
234 )
235 weak = schema.SourceItem(
236 item_id="weak",
237 source="reddit",
238 title="Battlefield update",
239 body="Patch notes.",
240 url="https://example.com/weak",
241 local_relevance=0.0,
242 )
243 pruned = signals.prune_low_relevance([strong, weak], minimum=0.1)
244 self.assertEqual(["strong"], [item.item_id for item in pruned])
245
246 def test_prune_low_relevance_falls_back_when_all_are_weak(self):
247 weak = schema.SourceItem(
248 item_id="weak",
249 source="reddit",
250 title="Generic post",
251 body="Generic body.",
252 url="https://example.com/weak",
253 metadata={"local_relevance": 0.02},
254 )
255 pruned = signals.prune_low_relevance([weak], minimum=0.1)
256 self.assertEqual(["weak"], [item.item_id for item in pruned])
257
258 # -- Iteration 1: HN engagement bug --
259
260 def test_hackernews_parse_emits_comments_key(self):
261 """parse_hackernews_response must emit 'comments' (not 'num_comments')."""
262 response = {
263 "hits": [
264 {
265 "objectID": "123",
266 "title": "Show HN: Something Cool",
267 "url": "https://example.com",
268 "author": "pg",
269 "points": 150,
270 "num_comments": 45,
271 "created_at_i": 1710720000,
272 },
273 ],
274 }
275 items = parse_hackernews_response(response, query="something cool")
276 self.assertIn("comments", items[0]["engagement"])
277 self.assertNotIn("num_comments", items[0]["engagement"])
278 self.assertEqual(items[0]["engagement"]["comments"], 45)
279
280 def test_hackernews_engagement_raw_uses_both_fields(self):
281 """engagement_raw for HN must weight both points and comments."""
282 item = schema.SourceItem(
283 item_id="hn1",
284 source="hackernews",
285 title="Show HN: Something",
286 body="Description",
287 url="https://example.com",
288 engagement={"points": 150, "comments": 45},
289 )
290 expected = 0.55 * math.log1p(150) + 0.45 * math.log1p(45)
291 result = signals.engagement_raw(item)
292 self.assertIsNotNone(result)
293 self.assertAlmostEqual(expected, result)
294 # Verify comments actually contributed (not just points)
295 points_only = 0.55 * math.log1p(150)
296 self.assertGreater(result, points_only)
297
298 # -- Iteration 4: Missing engagement formula tests --
299
300 def test_x_engagement_dominant_weight(self):
301 """X: likes at 0.55 should dominate over quotes at 0.05."""
302 item = schema.SourceItem(
303 item_id="x1", source="x", title="T", body="B",
304 url="https://example.com",
305 engagement={"likes": 100, "reposts": 100, "replies": 100, "quotes": 100},
306 )
307 result = signals.engagement_raw(item)
308 self.assertIsNotNone(result)
309 expected = (
310 0.55 * math.log1p(100)
311 + 0.25 * math.log1p(100)
312 + 0.15 * math.log1p(100)
313 + 0.05 * math.log1p(100)
314 )
315 self.assertAlmostEqual(expected, result)
316
317 def test_x_engagement_all_zero_returns_none(self):
318 item = schema.SourceItem(
319 item_id="x2", source="x", title="T", body="B",
320 url="https://example.com",
321 engagement={"likes": 0, "reposts": 0, "replies": 0, "quotes": 0},
322 )
323 self.assertIsNone(signals.engagement_raw(item))
324
325 def test_x_engagement_missing_fields(self):
326 """Missing fields default to 0, no crash."""
327 item = schema.SourceItem(
328 item_id="x3", source="x", title="T", body="B",
329 url="https://example.com",
330 engagement={"likes": 50},
331 )
332 result = signals.engagement_raw(item)
333 self.assertIsNotNone(result)
334 expected = 0.55 * math.log1p(50)
335 self.assertAlmostEqual(expected, result)
336
337 def test_youtube_engagement_dominant_weight(self):
338 """YouTube: views at 0.45 should dominate. With no top-comment data,
339 the remaining 0.90 of weight is split views/likes/comments 0.45/0.32/0.13."""
340 item = schema.SourceItem(
341 item_id="yt1", source="youtube", title="T", body="B",
342 url="https://example.com",
343 engagement={"views": 10000, "likes": 500, "comments": 80},
344 )
345 result = signals.engagement_raw(item)
346 self.assertIsNotNone(result)
347 expected = (
348 0.45 * math.log1p(10000)
349 + 0.32 * math.log1p(500)
350 + 0.13 * math.log1p(80)
351 )
352 self.assertAlmostEqual(expected, result)
353
354 def test_youtube_engagement_all_zero_returns_none(self):
355 item = schema.SourceItem(
356 item_id="yt2", source="youtube", title="T", body="B",
357 url="https://example.com",
358 engagement={"views": 0, "likes": 0, "comments": 0},
359 )
360 self.assertIsNone(signals.engagement_raw(item))
361
362 def test_youtube_engagement_missing_fields(self):
363 item = schema.SourceItem(
364 item_id="yt3", source="youtube", title="T", body="B",
365 url="https://example.com",
366 engagement={"views": 5000},
367 )
368 result = signals.engagement_raw(item)
369 self.assertIsNotNone(result)
370 expected = 0.45 * math.log1p(5000)
371 self.assertAlmostEqual(expected, result)
372
373 def test_tiktok_engagement_dominant_weight(self):
374 item = schema.SourceItem(
375 item_id="tt1", source="tiktok", title="T", body="B",
376 url="https://example.com",
377 engagement={"views": 50000, "likes": 3000, "comments": 200},
378 )
379 result = signals.engagement_raw(item)
380 self.assertIsNotNone(result)
381 expected = (
382 0.45 * math.log1p(50000)
383 + 0.27 * math.log1p(3000)
384 + 0.18 * math.log1p(200)
385 )
386 self.assertAlmostEqual(expected, result)
387
388 def test_tiktok_engagement_all_zero_returns_none(self):
389 item = schema.SourceItem(
390 item_id="tt2", source="tiktok", title="T", body="B",
391 url="https://example.com",
392 engagement={"views": 0, "likes": 0, "comments": 0},
393 )
394 self.assertIsNone(signals.engagement_raw(item))
395
396 def test_tiktok_engagement_missing_fields(self):
397 item = schema.SourceItem(
398 item_id="tt3", source="tiktok", title="T", body="B",
399 url="https://example.com",
400 engagement={"likes": 1000},
401 )
402 result = signals.engagement_raw(item)
403 self.assertIsNotNone(result)
404 expected = 0.27 * math.log1p(1000)
405 self.assertAlmostEqual(expected, result)
406
407 def test_instagram_engagement_dominant_weight(self):
408 item = schema.SourceItem(
409 item_id="ig1", source="instagram", title="T", body="B",
410 url="https://example.com",
411 engagement={"views": 8000, "likes": 1500, "comments": 100},
412 )
413 result = signals.engagement_raw(item)
414 self.assertIsNotNone(result)
415 # U2: IG now uses _instagram_engagement (video-shaped, with a 0.10
416 # top-comment carve-out); no top comment here so that term is 0.
417 expected = (
418 0.45 * math.log1p(8000)
419 + 0.27 * math.log1p(1500)
420 + 0.18 * math.log1p(100)
421 )
422 self.assertAlmostEqual(expected, result)
423
424 def test_instagram_engagement_all_zero_returns_none(self):
425 item = schema.SourceItem(
426 item_id="ig2", source="instagram", title="T", body="B",
427 url="https://example.com",
428 engagement={"views": 0, "likes": 0, "comments": 0},
429 )
430 self.assertIsNone(signals.engagement_raw(item))
431
432 def test_instagram_engagement_missing_fields(self):
433 item = schema.SourceItem(
434 item_id="ig3", source="instagram", title="T", body="B",
435 url="https://example.com",
436 engagement={"comments": 50},
437 )
438 result = signals.engagement_raw(item)
439 self.assertIsNotNone(result)
440 expected = 0.18 * math.log1p(50)
441 self.assertAlmostEqual(expected, result)
442
443 def test_hackernews_engagement_all_zero_returns_none(self):
444 item = schema.SourceItem(
445 item_id="hn2", source="hackernews", title="T", body="B",
446 url="https://example.com",
447 engagement={"points": 0, "comments": 0},
448 )
449 self.assertIsNone(signals.engagement_raw(item))
450
451 def test_hackernews_engagement_missing_fields(self):
452 item = schema.SourceItem(
453 item_id="hn3", source="hackernews", title="T", body="B",
454 url="https://example.com",
455 engagement={"points": 75},
456 )
457 result = signals.engagement_raw(item)
458 self.assertIsNotNone(result)
459 expected = 0.55 * math.log1p(75)
460 self.assertAlmostEqual(expected, result)
461
462 def test_bluesky_engagement_dominant_weight(self):
463 """Bluesky: likes at 0.40 should dominate over quotes at 0.10."""
464 item = schema.SourceItem(
465 item_id="bs1", source="bluesky", title="T", body="B",
466 url="https://example.com",
467 engagement={"likes": 200, "reposts": 50, "replies": 30, "quotes": 10},
468 )
469 result = signals.engagement_raw(item)
470 self.assertIsNotNone(result)
471 expected = (
472 0.40 * math.log1p(200)
473 + 0.30 * math.log1p(50)
474 + 0.20 * math.log1p(30)
475 + 0.10 * math.log1p(10)
476 )
477 self.assertAlmostEqual(expected, result)
478
479 def test_bluesky_engagement_all_zero_returns_none(self):
480 item = schema.SourceItem(
481 item_id="bs2", source="bluesky", title="T", body="B",
482 url="https://example.com",
483 engagement={"likes": 0, "reposts": 0, "replies": 0, "quotes": 0},
484 )
485 self.assertIsNone(signals.engagement_raw(item))
486
487 def test_bluesky_engagement_missing_fields(self):
488 item = schema.SourceItem(
489 item_id="bs3", source="bluesky", title="T", body="B",
490 url="https://example.com",
491 engagement={"likes": 100, "replies": 20},
492 )
493 result = signals.engagement_raw(item)
494 self.assertIsNotNone(result)
495 expected = 0.40 * math.log1p(100) + 0.20 * math.log1p(20)
496 self.assertAlmostEqual(expected, result)
497
498 def test_truthsocial_engagement_dominant_weight(self):
499 """Truth Social: likes at 0.45 should dominate over replies at 0.25."""
500 item = schema.SourceItem(
501 item_id="ts1", source="truthsocial", title="T", body="B",
502 url="https://example.com",
503 engagement={"likes": 500, "reposts": 100, "replies": 50},
504 )
505 result = signals.engagement_raw(item)
506 self.assertIsNotNone(result)
507 expected = (
508 0.45 * math.log1p(500)
509 + 0.30 * math.log1p(100)
510 + 0.25 * math.log1p(50)
511 )
512 self.assertAlmostEqual(expected, result)
513
514 def test_truthsocial_engagement_all_zero_returns_none(self):
515 item = schema.SourceItem(
516 item_id="ts2", source="truthsocial", title="T", body="B",
517 url="https://example.com",
518 engagement={"likes": 0, "reposts": 0, "replies": 0},
519 )
520 self.assertIsNone(signals.engagement_raw(item))
521
522 def test_truthsocial_engagement_missing_fields(self):
523 item = schema.SourceItem(
524 item_id="ts3", source="truthsocial", title="T", body="B",
525 url="https://example.com",
526 engagement={"reposts": 80},
527 )
528 result = signals.engagement_raw(item)
529 self.assertIsNotNone(result)
530 expected = 0.30 * math.log1p(80)
531 self.assertAlmostEqual(expected, result)
532
533 # -- Fix 5: Rebalance engagement weight --
534
535 def test_engagement_weight_meaningful_for_social_ranking(self):
536 """Engagement must have enough weight to differentiate otherwise-equal items."""
537 high_engagement = schema.SourceItem(
538 item_id="viral",
539 source="x",
540 title="Trending topic discussion",
541 body="Popular social post",
542 url="https://example.com/viral",
543 published_at="2026-03-15",
544 engagement={"likes": 50000, "reposts": 5000, "replies": 2000, "quotes": 500},
545 )
546 low_engagement = schema.SourceItem(
547 item_id="quiet",
548 source="x",
549 title="Trending topic discussion",
550 body="Popular social post",
551 url="https://example.com/quiet",
552 published_at="2026-03-15",
553 engagement={"likes": 10, "reposts": 1, "replies": 0, "quotes": 0},
554 )
555 ranked = signals.annotate_stream(
556 [low_engagement, high_engagement],
557 ranking_query="trending topic discussion",
558 freshness_mode="balanced_recent",
559 )
560 high_score = ranked[0].local_rank_score
561 low_score = ranked[1].local_rank_score
562 gap = high_score - low_score
563 # With 10% engagement weight, the gap should be >= 0.06
564 # With 5% weight, gap would be ~0.04
565 self.assertGreaterEqual(gap, 0.06,
566 f"Engagement gap should be >= 0.06 with 10% weight, got {gap:.4f}")
567
568 # -- Fix 4: Lower prune threshold for social media --
569
570 def test_prune_keeps_social_items_above_003(self):
571 """Social media items with low but non-trivial relevance should survive pruning."""
572 social = schema.SourceItem(
573 item_id="social",
574 source="x",
575 title="Viral tweet about topic",
576 body="Short social post",
577 url="https://example.com/social",
578 metadata={"local_relevance": 0.05},
579 )
580 strong = schema.SourceItem(
581 item_id="strong",
582 source="grounding",
583 title="Detailed article about topic",
584 body="In-depth analysis",
585 url="https://example.com/strong",
586 metadata={"local_relevance": 0.4},
587 )
588 pruned = signals.prune_low_relevance([strong, social])
589 ids = [item.item_id for item in pruned]
590 self.assertIn("social", ids, "Item with relevance 0.05 should survive pruning")
591 self.assertIn("strong", ids)
592
593 # -- Unit 3: YouTube high-engagement relevance floor --
594
595 def test_youtube_high_engagement_gets_relevance_floor(self):
596 """YouTube video with >100K views gets at least 0.3 relevance even with low text overlap."""
597 item = schema.SourceItem(
598 item_id="yt-official",
599 source="youtube",
600 title="YE - FATHER (feat. TRAVIS SCOTT)",
601 body="Official music video",
602 url="https://youtube.com/watch?v=abc",
603 engagement={"views": 8_000_000, "likes": 422_000, "comments": 5000},
604 )
605 rel = signals.local_relevance(item, "kanye west")
606 self.assertGreaterEqual(rel, 0.3, f"High-engagement YouTube should get >= 0.3 relevance, got {rel}")
607
608 def test_youtube_low_engagement_no_floor(self):
609 """YouTube video with <100K views does NOT get the relevance floor."""
610 item = schema.SourceItem(
611 item_id="yt-small",
612 source="youtube",
613 title="Random unrelated video title",
614 body="Nothing relevant here",
615 url="https://youtube.com/watch?v=xyz",
616 engagement={"views": 500, "likes": 10, "comments": 1},
617 )
618 rel = signals.local_relevance(item, "kanye west")
619 self.assertLess(rel, 0.3, f"Low-engagement YouTube should not get floor, got {rel}")
620
621 def test_non_youtube_high_engagement_no_floor(self):
622 """Non-YouTube items with high engagement do NOT get the YouTube floor."""
623 item = schema.SourceItem(
624 item_id="reddit-viral",
625 source="reddit",
626 title="Completely unrelated post",
627 body="Nothing about the topic",
628 url="https://reddit.com/r/test",
629 engagement={"score": 50000, "num_comments": 3000},
630 )
631 rel = signals.local_relevance(item, "kanye west")
632 self.assertLess(rel, 0.3, f"Non-YouTube item should not get YouTube floor, got {rel}")
633
634 # -- Unit 8: Engagement floor for TikTok/Instagram --
635
636 def test_tiktok_below_1000_views_pruned(self):
637 """TikTok items with <1000 views should be pruned when other sources exist."""
638 spam = schema.SourceItem(
639 item_id="tt-spam", source="tiktok", title="AI news clip", body="Generic",
640 url="https://tiktok.com/spam",
641 local_relevance=0.4, engagement={"views": 500, "likes": 10, "comments": 1},
642 )
643 good = schema.SourceItem(
644 item_id="r-good", source="reddit", title="Good discussion", body="Quality",
645 url="https://reddit.com/good",
646 local_relevance=0.5, engagement_score=50,
647 )
648 pruned = signals.prune_low_relevance([good, spam])
649 ids = [item.item_id for item in pruned]
650 self.assertNotIn("tt-spam", ids, "TikTok with 500 views should be pruned")
651 self.assertIn("r-good", ids)
652
653 def test_instagram_below_1000_views_pruned(self):
654 """Instagram items with <1000 views should be pruned when other sources exist."""
655 spam = schema.SourceItem(
656 item_id="ig-spam", source="instagram", title="Repost clip", body="Generic",
657 url="https://instagram.com/spam",
658 local_relevance=0.4, engagement={"views": 200, "likes": 5, "comments": 0},
659 )
660 good = schema.SourceItem(
661 item_id="x-good", source="x", title="Good tweet", body="Quality",
662 url="https://x.com/good",
663 local_relevance=0.5, engagement_score=50,
664 )
665 pruned = signals.prune_low_relevance([good, spam])
666 ids = [item.item_id for item in pruned]
667 self.assertNotIn("ig-spam", ids, "Instagram with 200 views should be pruned")
668
669 def test_tiktok_above_1000_views_kept(self):
670 """TikTok items with >=1000 views should survive pruning."""
671 good_tt = schema.SourceItem(
672 item_id="tt-good", source="tiktok", title="Popular clip", body="Relevant",
673 url="https://tiktok.com/good",
674 local_relevance=0.4, engagement={"views": 5000, "likes": 200, "comments": 30},
675 )
676 other = schema.SourceItem(
677 item_id="r-other", source="reddit", title="Reddit post", body="Relevant",
678 url="https://reddit.com/other",
679 local_relevance=0.5, engagement_score=50,
680 )
681 pruned = signals.prune_low_relevance([other, good_tt])
682 ids = [item.item_id for item in pruned]
683 self.assertIn("tt-good", ids, "TikTok with 5000 views should be kept")
684
685 def test_tiktok_sole_source_not_pruned(self):
686 """When TikTok is the only source, low-view items should NOT be pruned."""
687 items = [
688 schema.SourceItem(
689 item_id=f"tt-{i}", source="tiktok", title=f"Clip {i}", body="Content",
690 url=f"https://tiktok.com/{i}",
691 local_relevance=0.4, engagement={"views": 300, "likes": 5, "comments": 0},
692 )
693 for i in range(3)
694 ]
695 pruned = signals.prune_low_relevance(items)
696 self.assertEqual(len(pruned), 3, "Sole-source TikTok items should all survive")
697
698 def test_non_video_sources_unaffected_by_floor(self):
699 """Reddit/X items should not be affected by the video engagement floor."""
700 low_eng_x = schema.SourceItem(
701 item_id="x-low", source="x", title="Tweet", body="Topic discussion",
702 url="https://x.com/low",
703 local_relevance=0.5, engagement={"likes": 2, "reposts": 0},
704 engagement_score=5,
705 )
706 other = schema.SourceItem(
707 item_id="r-other", source="reddit", title="Post", body="Topic",
708 url="https://reddit.com/other",
709 local_relevance=0.5, engagement_score=50,
710 )
711 pruned = signals.prune_low_relevance([other, low_eng_x])
712 ids = [item.item_id for item in pruned]
713 self.assertIn("x-low", ids, "X items should not be affected by video floor")
714
715 def test_aspiresnippets_scenario(self):
716 """@aspiresnippets scenario: 5 TikTok items with 200-700 views all pruned."""
717 spam_items = [
718 schema.SourceItem(
719 item_id=f"aspire-{i}", source="tiktok", title=f"AI news {i}", body="Generic clip",
720 url=f"https://tiktok.com/aspire/{i}",
721 local_relevance=0.3, engagement={"views": 200 + i * 100, "likes": 5, "comments": 0},
722 )
723 for i in range(5)
724 ]
725 good = schema.SourceItem(
726 item_id="good-yt", source="youtube", title="In-depth analysis", body="Quality content",
727 url="https://youtube.com/good",
728 local_relevance=0.6, engagement_score=70,
729 )
730 pruned = signals.prune_low_relevance([good] + spam_items)
731 aspire_ids = [item.item_id for item in pruned if item.item_id.startswith("aspire")]
732 self.assertEqual(len(aspire_ids), 0, f"All @aspiresnippets items should be pruned, got {aspire_ids}")
733
734 # -- Fix 468: YouTube items with transcripts survive relevance pruning --
735
736 def test_youtube_with_transcript_survives_pruning_even_with_low_relevance(self):
737 """A YouTube item with a non-empty snippet (transcript) should not be
738 pruned even if its title-only relevance is below the threshold."""
739 has_transcript = schema.SourceItem(
740 item_id="yt-transcript",
741 source="youtube",
742 title="Short title",
743 body="Short body",
744 url="https://youtube.com/watch?v=abc",
745 snippet="This is a detailed transcript about the topic with substantive discussion...",
746 local_relevance=0.05,
747 )
748 strong = schema.SourceItem(
749 item_id="yt-strong",
750 source="youtube",
751 title="Strong video",
752 body="Detailed analysis of the topic",
753 url="https://youtube.com/watch?v=strong",
754 snippet="Detailed transcript content about the topic",
755 local_relevance=0.6,
756 )
757 pruned = signals.prune_low_relevance([strong, has_transcript], minimum=0.15)
758 ids = [item.item_id for item in pruned]
759 self.assertIn("yt-transcript", ids,
760 "YouTube item with transcript should survive pruning")
761 self.assertIn("yt-strong", ids, "Strong item should survive")
762
763 def test_youtube_without_transcript_is_pruned_normally(self):
764 """A YouTube item with no transcript (empty snippet) and low relevance
765 should still be pruned when stronger items exist."""
766 no_transcript = schema.SourceItem(
767 item_id="yt-no-transcript",
768 source="youtube",
769 title="Short title",
770 body="Short body",
771 url="https://youtube.com/watch?v=xyz",
772 snippet="",
773 local_relevance=0.05,
774 )
775 strong = schema.SourceItem(
776 item_id="yt-strong",
777 source="youtube",
778 title="Strong video",
779 body="Detailed analysis of the topic",
780 url="https://youtube.com/watch?v=strong",
781 snippet="Detailed transcript content about the topic",
782 local_relevance=0.6,
783 )
784 pruned = signals.prune_low_relevance([strong, no_transcript], minimum=0.15)
785 ids = [item.item_id for item in pruned]
786 self.assertIn("yt-strong", ids, "Strong item should survive")
787 self.assertNotIn("yt-no-transcript", ids,
788 "YouTube item without transcript should be pruned normally")
789
790 def test_youtube_transcript_exemption_does_not_affect_other_sources(self):
791 """Non-YouTube items with low relevance are still pruned even if they
792 have a non-empty snippet (the exemption is YouTube-specific)."""
793 reddit_with_snippet = schema.SourceItem(
794 item_id="reddit-snippet",
795 source="reddit",
796 title="Short title",
797 body="Short body",
798 url="https://reddit.com/r/test",
799 snippet="Some snippet content",
800 local_relevance=0.05,
801 )
802 strong = schema.SourceItem(
803 item_id="strong",
804 source="reddit",
805 title="Strong post",
806 body="Detailed analysis of the topic",
807 url="https://reddit.com/r/strong",
808 local_relevance=0.5,
809 )
810 pruned = signals.prune_low_relevance([strong, reddit_with_snippet], minimum=0.15)
811 ids = [item.item_id for item in pruned]
812 self.assertIn("strong", ids, "Strong item should survive")
813 self.assertNotIn("reddit-snippet", ids,
814 "Non-YouTube items should still be pruned by relevance threshold")
815
816
817 if __name__ == "__main__":
818 unittest.main()
819
820
821 def _reddit_unscored(item_id, *, engagement_source):
822 return schema.SourceItem(
823 item_id=item_id, source="reddit", title="kanye thread", body="kanye thread",
824 url=f"https://www.reddit.com/r/x/comments/{item_id}/t/", author=None,
825 container="x", published_at="2026-11-10", date_confidence="high",
826 engagement={}, local_relevance=0.2,
827 metadata={"engagement_source": engagement_source},
828 )
829
830
831 def test_pending_reddit_score_is_not_absent_engagement():
832 # 0.2 clears the 0.15 floor but not the 1.5x floor for zero-engagement
833 # social items: only the pending thread survives the on-topic count that
834 # decides whether the ScrapeCreators backfill runs.
835 pending = _reddit_unscored("p1", engagement_source="arctic-pending")
836 archived = _reddit_unscored("a1", engagement_source="arctic")
837 kept = signals.prune_low_relevance([pending, archived], minimum=0.15, passing_only=True)
838 assert [item.item_id for item in kept] == ["p1"]
839
839 lines PYTHON