| 1 | """Report finalize collapses same-URL copies of a thread and keeps enrichment.""" |
| 2 | |
| 3 | from lib import pipeline, schema |
| 4 | |
| 5 | |
| 6 | def _item(item_id, url, rank, comments=None): |
| 7 | item = schema.SourceItem( |
| 8 | item_id=item_id, |
| 9 | source="reddit", |
| 10 | title="Kanye West's sold-out Russia shows canceled", |
| 11 | body="Kanye West's sold-out Russia shows canceled", |
| 12 | url=url, |
| 13 | metadata={"top_comments": comments or []}, |
| 14 | ) |
| 15 | item.local_rank_score = rank |
| 16 | return item |
| 17 | |
| 18 | |
| 19 | def test_finalize_merges_same_url_copies_and_keeps_comments(): |
| 20 | url = "https://www.reddit.com/r/Music/comments/1vy0ilk/kanye_wests_soldout_russia/" |
| 21 | bare = _item("R1", url, 0.677) |
| 22 | rich = _item("R1", url, 0.487, comments=[{"score": 3293, "author": "u/tableleg7", "excerpt": "Putin"}]) |
| 23 | |
| 24 | finalized = pipeline._finalize_items_by_source({"reddit": [bare, rich]}, topic="kanye west", mock=True) |
| 25 | |
| 26 | items = finalized["reddit"] |
| 27 | assert len(items) == 1 |
| 28 | assert items[0].metadata["top_comments"][0]["score"] == 3293 |
| 29 | |
| 30 | |
| 31 | def _hn(story_id, points, comments, rank): |
| 32 | item = schema.SourceItem( |
| 33 | item_id=story_id, |
| 34 | source="hackernews", |
| 35 | title="SQLite 3.54.0 Released", |
| 36 | body="SQLite 3.54.0 Released", |
| 37 | url="https://sqlite.org/releaselog/3_54_0.html", |
| 38 | engagement={"points": points, "comments": len(comments)}, |
| 39 | metadata={"hn_url": f"https://news.ycombinator.com/item?id={story_id}", "top_comments": comments}, |
| 40 | ) |
| 41 | item.local_rank_score = rank |
| 42 | return item |
| 43 | |
| 44 | |
| 45 | def test_finalize_keeps_two_hn_submissions_of_one_url_apart(): |
| 46 | comment = {"score": 0, "author": "ciupicri", "excerpt": "How nice of them to support Windows XP until now."} |
| 47 | first = _hn("50040360", 11, [], 0.9) |
| 48 | second = _hn("50032781", 6, [comment], 0.8) |
| 49 | |
| 50 | finalized = pipeline._finalize_items_by_source({"hackernews": [first, second]}, topic="sqlite", mock=True) |
| 51 | |
| 52 | # Text dedupe may drop the near-identical second story, but no story may |
| 53 | # carry another story's comment count or comments. |
| 54 | for item in finalized["hackernews"]: |
| 55 | assert item.metadata["hn_url"].endswith(item.item_id) |
| 56 | assert item.engagement["comments"] == len(item.metadata["top_comments"]) |
| 57 | by_id = {item.item_id: item for item in finalized["hackernews"]} |
| 58 | assert by_id["50040360"].engagement == {"points": 11, "comments": 0} |
| 59 |