返回 last30days-skill
test_normalize_v3.py
根目录 / tests / test_normalize_v3.py
1 import unittest
2
3 from lib import normalize
4
5
6 class NormalizeV3Tests(unittest.TestCase):
7 def test_x_post_text_is_unescaped_in_title_and_body(self):
8 items = [
9 {
10 "id": "1",
11 "text": "Insane W at Worlds for Tama & Sahra <3 it's over",
12 "url": "https://x.com/pickleball_com/status/1",
13 "author_handle": "pickleball_com",
14 "date": "2026-03-01",
15 }
16 ]
17 normalized = normalize.normalize_source_items("x", items, "2026-02-15", "2026-03-17")
18 self.assertEqual("Insane W at Worlds for Tama & Sahra <3 it's over", normalized[0].body)
19 self.assertEqual(normalized[0].body, normalized[0].title)
20
21 def test_youtube_evergreen_fallback_keeps_older_items_when_recent_pool_is_empty(self):
22 items = [
23 {
24 "video_id": "vid-1",
25 "title": "Deploy to Fly.io tutorial",
26 "url": "https://youtube.com/watch?v=vid-1",
27 "channel_name": "Example",
28 "date": "2026-01-10",
29 "engagement": {"views": 1000, "likes": 50, "comments": 10},
30 }
31 ]
32 normalized = normalize.normalize_source_items(
33 "youtube",
34 items,
35 "2026-02-15",
36 "2026-03-17",
37 freshness_mode="evergreen_ok",
38 )
39 self.assertEqual(1, len(normalized))
40 self.assertEqual("2026-01-10", normalized[0].published_at)
41
42 def test_grounding_still_drops_older_items_in_evergreen_mode(self):
43 items = [
44 {
45 "id": "g-1",
46 "title": "Fly.io guide",
47 "url": "https://example.com/fly-guide",
48 "date": "2026-01-08",
49 "date_confidence": "high",
50 "snippet": "Step-by-step guide.",
51 }
52 ]
53 normalized = normalize.normalize_source_items(
54 "grounding",
55 items,
56 "2026-02-15",
57 "2026-03-17",
58 freshness_mode="evergreen_ok",
59 )
60 self.assertEqual([], normalized)
61
62 def test_arxiv_keeps_adapter_valid_items_older_than_report_window(self):
63 items = [
64 {
65 "id": "http://arxiv.org/abs/2509.00001v1",
66 "title": "Reliable Agent Memory",
67 "url": "https://arxiv.org/abs/2509.00001v1",
68 "summary": "A study of memory systems for coding agents.",
69 "author": "Ada Lovelace",
70 "authors": ["Ada Lovelace"],
71 "date": "2025-09-04",
72 "relevance": 0.9,
73 }
74 ]
75
76 normalized = normalize.normalize_source_items(
77 "arxiv",
78 items,
79 "2026-07-06",
80 "2026-08-05",
81 )
82
83 self.assertEqual(1, len(normalized))
84 self.assertEqual("Reliable Agent Memory", normalized[0].title)
85 self.assertEqual("https://arxiv.org/abs/2509.00001v1", normalized[0].url)
86 self.assertEqual("2025-09-04", normalized[0].published_at)
87
88 def test_youtube_top_comments_passthrough_with_field_mapping(self):
89 """YT comments from enrich_with_comments use likes/text; normalize must
90 carry them into metadata as the Reddit-compatible {score, excerpt} shape."""
91 items = [
92 {
93 "video_id": "vid-1",
94 "title": "How to deploy",
95 "url": "https://youtube.com/watch?v=vid-1",
96 "channel_name": "Example",
97 "date": "2026-03-01",
98 "engagement": {"views": 10000, "likes": 500, "comments": 30},
99 "top_comments": [
100 {"author": "Alice", "text": "Best tutorial ever", "likes": 120, "date": "2026-03-02"},
101 {"author": "Bob", "text": "Helped me ship", "likes": 45, "date": "2026-03-03"},
102 {"author": "Carol", "text": "Solid walkthrough", "likes": 7, "date": "2026-03-04"},
103 ],
104 }
105 ]
106 normalized = normalize.normalize_source_items(
107 "youtube", items, "2026-02-15", "2026-03-17",
108 )
109 self.assertEqual(1, len(normalized))
110 top = normalized[0].metadata.get("top_comments")
111 self.assertIsNotNone(top)
112 self.assertEqual(3, len(top))
113 # First comment: likes->score, text->excerpt
114 self.assertEqual(120, top[0]["score"])
115 self.assertEqual("Best tutorial ever", top[0]["excerpt"])
116 self.assertEqual("Alice", top[0]["author"])
117 self.assertEqual("2026-03-02", top[0]["date"])
118 # Preserves ordering from input (already sorted desc upstream)
119 self.assertEqual(45, top[1]["score"])
120 self.assertEqual(7, top[2]["score"])
121
122 def test_instagram_comment_like_count_maps_to_score(self):
123 """U2: IG comments use comment_like_count as the vote; normalize must
124 carry it into the shared `score` field so it participates in ranking."""
125 items = [
126 {
127 "video_id": "ig-1",
128 "text": "reel caption",
129 "url": "https://www.instagram.com/reel/ABC/",
130 "author_name": "example",
131 "date": "2026-03-01",
132 "engagement": {"views": 10000, "likes": 500, "comments": 30},
133 "top_comments": [
134 {"author": "alice", "text": "gold take", "comment_like_count": 120, "date": "2026-03-02"},
135 {"author": "bob", "text": "mid", "comment_like_count": 5, "date": "2026-03-03"},
136 ],
137 }
138 ]
139 normalized = normalize.normalize_source_items(
140 "instagram", items, "2026-02-15", "2026-03-17",
141 )
142 self.assertEqual(1, len(normalized))
143 top = normalized[0].metadata.get("top_comments")
144 self.assertIsNotNone(top)
145 self.assertEqual(120, top[0]["score"])
146 self.assertEqual("gold take", top[0]["excerpt"])
147 self.assertEqual("alice", top[0]["author"])
148
149 def test_youtube_top_comments_empty_list_passes_through_cleanly(self):
150 items = [
151 {
152 "video_id": "vid-2",
153 "title": "Short clip",
154 "url": "https://youtube.com/watch?v=vid-2",
155 "channel_name": "Example",
156 "date": "2026-03-01",
157 "engagement": {"views": 50, "likes": 2},
158 "top_comments": [],
159 }
160 ]
161 normalized = normalize.normalize_source_items(
162 "youtube", items, "2026-02-15", "2026-03-17",
163 )
164 self.assertEqual(1, len(normalized))
165 # Empty list is fine; metadata may have empty top_comments or omit it.
166 top = normalized[0].metadata.get("top_comments", [])
167 self.assertEqual([], top)
168
169 def test_youtube_without_top_comments_key_does_not_crash(self):
170 items = [
171 {
172 "video_id": "vid-3",
173 "title": "No comments fetched",
174 "url": "https://youtube.com/watch?v=vid-3",
175 "channel_name": "Example",
176 "date": "2026-03-01",
177 "engagement": {"views": 100, "likes": 5},
178 }
179 ]
180 normalized = normalize.normalize_source_items(
181 "youtube", items, "2026-02-15", "2026-03-17",
182 )
183 self.assertEqual(1, len(normalized))
184 self.assertEqual([], normalized[0].metadata.get("top_comments", []))
185
186 def test_youtube_top_comments_feed_top_comment_score_signal(self):
187 """Integration: after normalize, signals._top_comment_score should
188 return log1p(first comment score) for YT, proving the full chain."""
189 from lib import signals
190 import math
191 items = [
192 {
193 "video_id": "vid-4",
194 "title": "Viral comment thread",
195 "url": "https://youtube.com/watch?v=vid-4",
196 "channel_name": "Example",
197 "date": "2026-03-01",
198 "engagement": {"views": 1000, "likes": 50, "comments": 10},
199 "top_comments": [
200 {"author": "A", "text": "Legendary", "likes": 9999, "date": "2026-03-02"},
201 ],
202 }
203 ]
204 normalized = normalize.normalize_source_items(
205 "youtube", items, "2026-02-15", "2026-03-17",
206 )
207 self.assertAlmostEqual(math.log1p(9999), signals._top_comment_score(normalized[0]), places=4)
208
209 def test_tiktok_top_comments_passthrough_with_digg_count_mapping(self):
210 """TikTok comments from enrich_with_comments use digg_count/text;
211 normalize must map to the shared {score, excerpt} shape."""
212 items = [
213 {
214 "id": "tt-1",
215 "text": "POV: shipping on Friday",
216 "url": "https://www.tiktok.com/@u/video/tt-1",
217 "author_name": "u",
218 "date": "2026-03-01",
219 "engagement": {"views": 50000, "likes": 2000, "comments": 300},
220 "top_comments": [
221 {"author": "Alice", "text": "dead", "digg_count": 1200, "date": "2026-03-02"},
222 {"author": "Bob", "text": "so real", "digg_count": 400, "date": "2026-03-03"},
223 ],
224 }
225 ]
226 normalized = normalize.normalize_source_items(
227 "tiktok", items, "2026-02-15", "2026-03-17",
228 )
229 self.assertEqual(1, len(normalized))
230 top = normalized[0].metadata.get("top_comments")
231 self.assertEqual(2, len(top))
232 self.assertEqual(1200, top[0]["score"])
233 self.assertEqual("dead", top[0]["excerpt"])
234 self.assertEqual("Alice", top[0]["author"])
235 self.assertEqual(400, top[1]["score"])
236
237 def test_tiktok_without_top_comments_does_not_crash(self):
238 items = [
239 {
240 "id": "tt-2",
241 "text": "plain clip",
242 "url": "https://www.tiktok.com/@u/video/tt-2",
243 "author_name": "u",
244 "date": "2026-03-01",
245 "engagement": {"views": 1000, "likes": 20},
246 }
247 ]
248 normalized = normalize.normalize_source_items(
249 "tiktok", items, "2026-02-15", "2026-03-17",
250 )
251 self.assertEqual([], normalized[0].metadata.get("top_comments", []))
252
253 def test_tiktok_top_comments_feed_top_comment_score_signal(self):
254 from lib import signals
255 import math
256 items = [
257 {
258 "id": "tt-3",
259 "text": "viral",
260 "url": "https://www.tiktok.com/@u/video/tt-3",
261 "author_name": "u",
262 "date": "2026-03-01",
263 "engagement": {"views": 100000, "likes": 5000, "comments": 500},
264 "top_comments": [
265 {"author": "A", "text": "this aged well", "digg_count": 50000, "date": "2026-03-02"},
266 ],
267 }
268 ]
269 normalized = normalize.normalize_source_items(
270 "tiktok", items, "2026-02-15", "2026-03-17",
271 )
272 self.assertAlmostEqual(math.log1p(50000), signals._top_comment_score(normalized[0]), places=4)
273
274 def test_grounding_requires_a_usable_date(self):
275 items = [
276 {
277 "id": "g-1",
278 "title": "Undated result",
279 "url": "https://example.com/post",
280 "snippet": "No date was returned.",
281 }
282 ]
283 normalized = normalize.normalize_source_items(
284 "grounding",
285 items,
286 "2026-02-15",
287 "2026-03-17",
288 )
289 self.assertEqual([], normalized)
290
291 def test_grounding_keeps_undated_keyless_current_window_results(self):
292 items = [
293 {
294 "id": "WK1",
295 "title": "Undated keyless result",
296 "url": "https://example.com/post",
297 "snippet": "Keyless backends return no date.",
298 "metadata": {"date_window_basis": "keyless_current"},
299 }
300 ]
301 normalized = normalize.normalize_source_items(
302 "grounding",
303 items,
304 "2026-02-15",
305 "2026-03-17",
306 )
307 self.assertEqual(1, len(normalized))
308 self.assertIsNone(normalized[0].published_at)
309 self.assertEqual("low", normalized[0].date_confidence)
310
311 def test_grounding_still_drops_known_out_of_window_dates(self):
312 items = [
313 {
314 "id": "g-old",
315 "title": "Old dated result",
316 "url": "https://example.com/old",
317 "snippet": "Dated before the window.",
318 "date": "2025-01-10",
319 },
320 {
321 "id": "g-new",
322 "title": "In-window dated result",
323 "url": "https://example.com/new",
324 "snippet": "Dated inside the window.",
325 "date": "2026-03-01",
326 },
327 ]
328 normalized = normalize.normalize_source_items(
329 "grounding",
330 items,
331 "2026-02-15",
332 "2026-03-17",
333 )
334 self.assertEqual(["https://example.com/new"], [item.url for item in normalized])
335
336 def test_youtube_recent_modes_drop_transcribed_older_items_when_date_window_is_empty(self):
337 """#1043 rescue is scoped to evergreen_ok: recent topics get an honest empty result."""
338 items = [
339 {
340 "video_id": "vid-old",
341 "title": "Ghost in the Shell review",
342 "url": "https://youtube.com/watch?v=vid-old",
343 "channel_name": "Example",
344 "date": "2017-03-01",
345 "transcript_snippet": "Fetched transcript about Ghost in the Shell.",
346 "engagement": {"views": 1000, "likes": 50, "comments": 10},
347 }
348 ]
349 for mode in ("balanced_recent", "strict_recent"):
350 normalized = normalize.normalize_source_items(
351 "youtube",
352 items,
353 "2026-02-15",
354 "2026-03-17",
355 freshness_mode=mode,
356 )
357 self.assertEqual([], normalized, mode)
358
359 def test_youtube_evergreen_keeps_transcribed_older_items_when_date_window_is_empty(self):
360 """#1043: evergreen search kept out-of-window videos so transcripts could run; normalize must not drop them."""
361 items = [
362 {
363 "video_id": "vid-old",
364 "title": "Informa TechTarget overview",
365 "url": "https://youtube.com/watch?v=vid-old",
366 "channel_name": "Example",
367 "date": "2025-01-10",
368 "transcript_snippet": "Fetched transcript about Informa TechTarget.",
369 "engagement": {"views": 1000, "likes": 50, "comments": 10},
370 }
371 ]
372 normalized = normalize.normalize_source_items(
373 "youtube",
374 items,
375 "2026-02-15",
376 "2026-03-17",
377 freshness_mode="evergreen_ok",
378 )
379 self.assertEqual(1, len(normalized))
380 self.assertEqual("vid-old", normalized[0].item_id)
381 self.assertIn("Fetched transcript", normalized[0].snippet)
382
383 def test_youtube_empty_window_fallback_keeps_only_transcribed_items(self):
384 """#1043 rescue is transcript-backed evidence, not stale metadata-only videos."""
385 items = [
386 {
387 "video_id": "vid-meta",
388 "title": "Old video without captions",
389 "url": "https://youtube.com/watch?v=vid-meta",
390 "channel_name": "Example",
391 "date": "2025-01-10",
392 "engagement": {"views": 1000, "likes": 50, "comments": 10},
393 },
394 {
395 "video_id": "vid-old",
396 "title": "Informa TechTarget overview",
397 "url": "https://youtube.com/watch?v=vid-old",
398 "channel_name": "Example",
399 "date": "2025-01-10",
400 "transcript_snippet": "Fetched transcript about Informa TechTarget.",
401 "engagement": {"views": 1000, "likes": 50, "comments": 10},
402 },
403 ]
404 normalized = normalize.normalize_source_items(
405 "youtube",
406 items,
407 "2026-02-15",
408 "2026-03-17",
409 freshness_mode="evergreen_ok",
410 )
411 self.assertEqual(1, len(normalized))
412 self.assertEqual("vid-old", normalized[0].item_id)
413
414 def test_youtube_empty_window_fallback_drops_all_transcript_free_videos(self):
415 items = [
416 {
417 "video_id": "vid-meta",
418 "title": "Old video without captions",
419 "url": "https://youtube.com/watch?v=vid-meta",
420 "channel_name": "Example",
421 "date": "2025-01-10",
422 }
423 ]
424 normalized = normalize.normalize_source_items(
425 "youtube",
426 items,
427 "2026-02-15",
428 "2026-03-17",
429 freshness_mode="strict_recent",
430 )
431 self.assertEqual([], normalized)
432
433
434 if __name__ == "__main__":
435 unittest.main()
436
436 lines PYTHON