返回 last30days-skill
test_digg_bar.py
根目录 / tests / test_digg_bar.py
1 """Tests for the Digg bar: which Digg stories are prominent, ordinary or dropped.
2
3 The bar is pure. Nothing here starts ``digg-pp-cli`` or touches the network.
4 """
5
6 from __future__ import annotations
7
8 import pytest
9
10 from lib import digg, normalize
11
12
13 def _story(
14 title: str,
15 summary: str = "A short summary of what is happening.",
16 *,
17 score: float | None = 226,
18 sources: int = 44,
19 story_id: str | None = "uuid-1",
20 url_id: str = "abc123xy",
21 ):
22 """A Digg story as the pipeline holds it after normalize."""
23 engagement: dict = {"postCount": sources, "uniqueAuthors": 3}
24 if score is not None:
25 engagement["digg_score"] = score
26 raw = {
27 "id": url_id,
28 "title": title,
29 "tldr": summary,
30 "url": f"https://di.gg/tech/{url_id}",
31 "date": "2026-11-01",
32 "engagement": engagement,
33 "relevance": 0.5,
34 }
35 if story_id:
36 raw["cluster_id"] = story_id
37 return normalize._normalize_digg("digg", raw, 0, "2026-10-13", "2026-11-12")
38
39
40 def _outcome(topic: str, story) -> str:
41 """Which of the bar's lists the one story landed in."""
42 result = digg.apply_bar(topic, [story])
43 names = [
44 name
45 for name in ("prominent", "ordinary", "dropped", "legacy")
46 if getattr(result, name)
47 ]
48 assert len(names) == 1, names
49 assert getattr(result, names[0]) == [story]
50 return names[0]
51
52
53 def test_big_story_naming_the_topic_in_its_title_is_prominent():
54 story = _story("OpenAI Releases GPT-6.1 Terra", score=226, sources=44)
55 assert _outcome("OpenAI", story) == "prominent"
56
57
58 def test_story_naming_the_topic_in_its_title_but_too_small_is_ordinary():
59 low_score = _story("OpenAI Releases GPT-6.1 Terra", score=30, sources=44)
60 few_sources = _story("OpenAI Releases GPT-6.1 Terra", score=60, sources=3)
61 assert _outcome("OpenAI", low_score) == "ordinary"
62 assert _outcome("OpenAI", few_sources) == "ordinary"
63
64
65 def test_story_at_exactly_the_thresholds_is_prominent():
66 story = _story(
67 "OpenAI Releases GPT-6.1 Terra",
68 score=digg.PROMINENT_MIN_SCORE,
69 sources=digg.PROMINENT_MIN_SOURCES,
70 )
71 assert (digg.PROMINENT_MIN_SCORE, digg.PROMINENT_MIN_SOURCES) == (40, 5)
72 assert _outcome("OpenAI", story) == "prominent"
73
74
75 def test_big_story_naming_the_topic_only_in_its_summary_is_dropped():
76 story = _story(
77 "Kevin Weil launches Warp Sciences",
78 "The former OpenAI product chief is starting a science company.",
79 score=92,
80 sources=20,
81 )
82 assert _outcome("OpenAI", story) == "dropped"
83
84
85 @pytest.mark.parametrize("title, summary, outcome", [
86 (
87 "The Weeknd's 'After Hours Til Dawn' sets male solo touring record",
88 "The tour trails only Taylor Swift and Coldplay on the all-time list.",
89 "dropped",
90 ),
91 (
92 "Swift's Eras Tour film returns to theaters",
93 "Taylor Swift is bringing the concert film back for one weekend.",
94 "ordinary",
95 ),
96 (
97 "Taylor Swift becomes Nashville Songwriters Hall of Fame inductee",
98 "She is the youngest writer the hall has inducted.",
99 "prominent",
100 ),
101 ])
102 def test_story_must_carry_the_person_in_its_title_to_be_kept(title, summary, outcome):
103 story = _story(title, summary, score=300, sources=40)
104 assert _outcome("Taylor Swift", story) == outcome
105
106
107 def test_big_story_that_never_names_the_person_is_dropped():
108 gala = _story(
109 "Honoree John Carpenter kicks off the Academy Museum Gala carpet",
110 "Stars arrived in Los Angeles for the annual fundraiser.",
111 score=652,
112 sources=80,
113 )
114 other_taylor = _story("Taylor Sheridan signs deal", "The deal covers new series.")
115 assert _outcome("Taylor Swift", gala) == "dropped"
116 assert _outcome("Taylor Swift", other_taylor) == "dropped"
117
118
119 def test_two_letter_topic_matches_only_as_a_whole_word():
120 named = _story("AI is forcing us to pay off technical debt")
121 maine = _story("Maine fishing season opens", "Anglers wait for the rain to clear.")
122 paid = _story("Maintaining a paid mailing list", "Email remains a main channel.")
123 assert _outcome("AI", named) == "prominent"
124 assert _outcome("AI", maine) == "dropped"
125 assert _outcome("AI", paid) == "dropped"
126
127
128 def test_topic_inside_a_longer_word_does_not_name_it():
129 metadata = _story(
130 "Understanding metadata in Postgres", "How the catalog stores metadata."
131 )
132 named = _story("Meta ships new glasses")
133 assert _outcome("Meta", metadata) == "dropped"
134 assert _outcome("Meta", named) == "prominent"
135
136
137 def test_two_word_topic_needs_one_word_in_the_title_and_the_phrase_in_the_text():
138 in_title = _story("World Series Game 5 preview")
139 one_word_in_title = _story(
140 "Brewers take Series lead",
141 "Milwaukee is one win from its first World Series title.",
142 )
143 in_summary = _story(
144 "Brewers sweep Dodgers in NLCS upset",
145 "Milwaukee reaches the World Series for the first time since 1982.",
146 )
147 apart = _story(
148 "Brewers sweep Dodgers in NLCS upset",
149 "The baseball world is stunned. Milwaukee won the series in four games.",
150 )
151 # A title word with no full phrase anywhere is not enough.
152 word_without_phrase = _story(
153 "Brewers take Series lead",
154 "Milwaukee is one win from the pennant.",
155 )
156 assert _outcome("World Series", in_title) == "prominent"
157 assert _outcome("World Series", one_word_in_title) == "ordinary"
158 assert _outcome("World Series", in_summary) == "dropped"
159 assert _outcome("World Series", apart) == "dropped"
160 assert _outcome("World Series", word_without_phrase) == "dropped"
161
162 surname = _story(
163 "Altman says the next model is close",
164 "Sam Altman told staff the release is weeks away.",
165 )
166 assert _outcome("Sam Altman", surname) == "ordinary"
167
168
169 def test_name_hidden_inside_another_word_is_dropped():
170 story = _story(
171 "Prosecutors file felony charges in fraud case",
172 "The filing must be answered within thirty days.",
173 )
174 assert _outcome("Elon Musk", story) == "dropped"
175
176
177 def test_full_name_in_title_is_prominent_beside_a_lookalike_word():
178 story = _story("Sam Altman meets Samsung executives")
179 assert _outcome("Sam Altman", story) == "prominent"
180
181
182 def test_question_topic_is_matched_on_its_subject():
183 story = _story("Twitter restores verification")
184 assert _outcome("what happened to Twitter", story) == "prominent"
185
186
187 def test_comparison_topic_counts_a_story_naming_either_side():
188 anthropic = _story("Anthropic ships Claude update", "The model is faster.")
189 neither = _story("Mistral ships new model", "The model is faster.")
190 assert _outcome("OpenAI vs Anthropic", anthropic) == "prominent"
191 assert _outcome("OpenAI versus Anthropic", anthropic) == "prominent"
192 assert _outcome("OpenAI vs Anthropic", neither) == "dropped"
193
194
195 def test_topic_with_no_subject_keeps_every_story_as_ordinary():
196 stories = [
197 _story("OpenAI Releases GPT-6.1 Terra", story_id="uuid-1"),
198 _story("Maine fishing season opens", story_id="uuid-2"),
199 ]
200 for topic in ("what is the latest", ""):
201 result = digg.apply_bar(topic, stories)
202 assert result.ordinary == stories
203 assert result.prominent == []
204 assert result.dropped == []
205
206
207 def test_legacy_cluster_without_a_story_id_is_returned_untouched():
208 on_topic = _story("OpenAI Releases GPT-6.1 Terra", story_id=None, url_id="old1")
209 off_topic = _story("Maine fishing season opens", story_id=None, url_id="old2")
210 result = digg.apply_bar("OpenAI", [off_topic, on_topic])
211 assert result.legacy == [off_topic, on_topic]
212 assert result.prominent == result.ordinary == result.dropped == []
213 assert digg.STORY_ID_KEY not in on_topic.metadata
214
215
216 def test_story_without_a_digg_score_is_ordinary_when_named_and_dropped_when_not():
217 named = _story("OpenAI Releases GPT-6.1 Terra", score=None, sources=44)
218 unnamed = _story("Maine fishing season opens", score=None, sources=44)
219 assert _outcome("OpenAI", named) == "ordinary"
220 assert _outcome("OpenAI", unnamed) == "dropped"
221
222
223 def test_prominent_stories_are_ordered_by_digg_score_then_story_id():
224 small = _story("OpenAI hires a new CFO", score=50, story_id="uuid-a")
225 tie_b = _story("OpenAI opens London office", score=226, story_id="uuid-b")
226 tie_a = _story("OpenAI Releases GPT-6.1 Terra", score=226, story_id="uuid-a2")
227 biggest = _story("OpenAI raises new round", score=1286, story_id="uuid-z")
228 ordinary_1 = _story("OpenAI adds a setting", score=10, story_id="uuid-o1")
229 ordinary_2 = _story("OpenAI fixes a bug", score=30, story_id="uuid-o2")
230 result = digg.apply_bar(
231 "OpenAI", [ordinary_1, small, tie_b, ordinary_2, tie_a, biggest]
232 )
233 assert result.prominent == [biggest, tie_a, tie_b, small]
234 assert result.ordinary == [ordinary_1, ordinary_2]
235
236
237 def test_verdict_is_the_same_when_asked_again_and_leaves_items_unchanged():
238 big = _story("OpenAI Releases GPT-6.1 Terra", story_id="uuid-1")
239 loose = _story("Maine fishing season opens", story_id="uuid-2")
240 before = (dict(big.metadata), dict(big.engagement))
241 first = digg.apply_bar("OpenAI", [big, loose])
242 # Asked later about the kept stories alone, as the source fetch does.
243 again = digg.apply_bar("OpenAI", first.prominent + first.ordinary)
244 assert again.prominent == first.prominent == [big]
245 assert (dict(big.metadata), dict(big.engagement)) == before
246
247
248 def test_short_and_symbol_topics_match_as_whole_tokens():
249 assert _outcome("C++", _story("C++ 26 adds reflection")) == "prominent"
250 assert _outcome("C++", _story("C compilers get faster", "No plus here.")) == "dropped"
251 assert _outcome("Go", _story("Go 1.26 lands generics fix")) == "prominent"
252 assert _outcome("Go", _story("Google ships update", "Good news.")) == "dropped"
253 assert _outcome("X", _story("X rolls out long posts")) == "prominent"
254 assert _outcome("X", _story("Xbox prices rise", "Next year too.")) == "dropped"
255
256
257 def test_phrase_matches_across_a_hyphen_or_extra_space():
258 assert _outcome("Claude Code", _story("Claude-Code gets plugins")) == "prominent"
259 assert _outcome("claude code", _story("CLAUDE CODE gets plugins")) == "prominent"
260
261
262 @pytest.mark.parametrize("topic, title, kept", [
263 ("why is Nvidia stock down", "Nvidia shares fall after export ruling", True),
264 ("why is Nvidia stock down", "Netflix renews Haunted Hotel", False),
265 ("Taylor Swift new album", "Taylor Swift announces a record", True),
266 ("best noise cancelling headphones", "Sony ships new headphones", True),
267 ("best noise cancelling headphones", "Best pizza in Naples", False),
268 ("how to use Cursor for refactoring", "Cursor adds background agents", True),
269 ])
270 def test_descriptive_topic_keeps_stories_that_share_a_telling_word(topic, title, kept):
271 story = _story(title, score=300, sources=40)
272 assert _outcome(topic, story) == ("ordinary" if kept else "dropped")
273
274
275 @pytest.mark.parametrize("topic, title, summary, outcome", [
276 ("Tesla earnings", "Tesla reports record Q3 profit", "Tesla posted earnings above estimates.", "ordinary"),
277 ("Nvidia stock", "Nvidia shares fall 5%", "The stock slid after the export ruling.", "ordinary"),
278 ("Tesla earnings", "Tesla recalls 200,000 cars", "A software fault is behind the recall.", "dropped"),
279 ("Tesla earnings", "Ford posts earnings beat", "Ford outsold Tesla in trucks.", "ordinary"),
280 ("Tesla earnings", "Rivian opens a new plant", "Tesla earnings are due next week.", "dropped"),
281 ])
282 def test_two_word_topic_that_is_a_subject_and_an_aspect(topic, title, summary, outcome):
283 assert _outcome(topic, _story(title, summary, score=300, sources=40)) == outcome
284
285
286 @pytest.mark.parametrize("topic, title, outcome", [
287 # A name that starts with a word stripped elsewhere keeps all its words.
288 ("Will Smith", "Will Smith announces a world tour", "prominent"),
289 ("Will Smith", "Smith College names a new president", "dropped"),
290 ("New York", "New York approves congestion pricing", "prominent"),
291 ("New York", "York Minster reopens after repairs", "dropped"),
292 # A trailing filler word is not part of the subject.
293 ("OpenAI news", "OpenAI ships a new model", "prominent"),
294 ])
295 def test_filler_words_are_stripped_only_where_they_are_filler(topic, title, outcome):
296 assert _outcome(topic, _story(title, score=300, sources=40)) == outcome
297
298
299 def test_topic_made_only_of_filler_words_judges_nothing():
300 story = _story("Netflix renews Haunted Hotel", score=300, sources=40)
301 assert _outcome("what is the latest", story) == "ordinary"
302
302 lines PYTHON