返回 last30days-skill
test_render_content_quotes.py
根目录 / tests / test_render_content_quotes.py
1 """The quote pool holds what was said in videos, not only the comments on them.
2
3 A YouTube transcript highlight or a reel's spoken line joins the pool as a
4 ``said`` entry with the video's like count, ready to copy as a quote line. A
5 small on-topic Reddit thread joins with a few votes on its top comment.
6 """
7
8 from lib import render, schema
9
10
11 def _item(source, url, *, author="", comments=None, likes=None, snippet="",
12 highlights=None, container=None, score=None, spoken=False):
13 metadata = {}
14 if spoken:
15 metadata["spoken_caption"] = True
16 if comments:
17 metadata["top_comments"] = [{"depth": 0, **c} for c in comments]
18 if highlights:
19 metadata["transcript_highlights"] = highlights
20 engagement = {}
21 if likes is not None:
22 engagement["likes"] = likes
23 if score is not None:
24 engagement["score"] = score
25 return schema.SourceItem(
26 item_id=f"i-{url}",
27 source=source,
28 title="Sequoia Capital post",
29 body=snippet,
30 url=url,
31 author=author,
32 container=container,
33 snippet=snippet,
34 engagement=engagement,
35 metadata=metadata,
36 )
37
38
39 def _candidate(item):
40 candidate = schema.Candidate(
41 candidate_id=f"c-{item.url}",
42 item_id=item.item_id,
43 source=item.source,
44 title=item.title,
45 url=item.url,
46 snippet="",
47 subquery_labels=["q1"],
48 native_ranks={item.source: 1},
49 local_relevance=0.8,
50 freshness=50,
51 engagement=0,
52 source_quality=0.5,
53 rrf_score=0.01,
54 source_items=[item],
55 )
56 candidate.final_score = 50.0
57 return candidate
58
59
60 def _report(*items):
61 return schema.Report(
62 topic="Sequoia Capital",
63 range_from="2026-10-12",
64 range_to="2026-11-11",
65 generated_at="2026-11-11T00:00:00Z",
66 provider_runtime=schema.ProviderRuntime(
67 reasoning_provider="test", planner_model="test", rerank_model="test"
68 ),
69 query_plan=schema.QueryPlan(
70 intent="breaking_news",
71 freshness_mode="strict_recent",
72 cluster_mode="story",
73 raw_topic="Sequoia Capital",
74 subqueries=[],
75 source_weights={},
76 ),
77 ranked_candidates=[_candidate(item) for item in items],
78 clusters=[],
79 items_by_source={},
80 errors_by_source={},
81 )
82
83
84 WSJ = _item(
85 "youtube",
86 "https://www.youtube.com/watch?v=Gmp1_uPoIS0",
87 author="WSJ Events",
88 likes=386,
89 highlights=[
90 ">> Given the results of the midterms, do you think the venture "
91 "capital industry has tied itself too close to Trump?",
92 ],
93 )
94 REEL = _item(
95 "instagram",
96 "https://www.instagram.com/reel/DfYRA2ghafT/",
97 author="atlasberry008",
98 likes=1943,
99 snippet=(
100 "Let me get this straight. Sequoia just raised $10 billion and in the same "
101 "letter to announce it, they shut down their London office, their scout "
102 "program and their accelerator program, the most famous VC fund in the world "
103 "Marie Kondoed itself, and when the best venture firm in venture starts "
104 "cutting, it's telling you about the new world order they're getting ready for."
105 ),
106 comments=[{"author": "@fan", "score": 0, "excerpt": "Sequoia"}],
107 spoken=True,
108 )
109 NEOLIBERAL = _item(
110 "reddit",
111 "https://www.reddit.com/r/neoliberal/comments/1xntipt/sequoia_shuts_london/",
112 container="neoliberal",
113 score=23,
114 comments=[
115 {"author": "ldn6", "score": 4,
116 "excerpt": "This is definitely an outlying move because London is recording a surge in AI investment."},
117 ],
118 )
119
120
121 def _lines(*items):
122 return render._render_top_comments(_report(*items))
123
124
125 def test_youtube_highlight_joins_the_pool_as_said_with_likes():
126 lines = _lines(WSJ, NEOLIBERAL)
127 (line,) = [line for line in lines if "midterms" in line]
128 assert line.startswith('- "Given the results of the midterms')
129 assert ">" not in line and ">>" not in line
130 assert "🔴 WSJ Events on YouTube · 386 likes" in line
131 assert line.endswith("— said")
132
133
134 def test_reel_spoken_caption_joins_the_pool_and_its_bait_comments_do_not():
135 lines = _lines(REEL, NEOLIBERAL)
136 said = [line for line in lines if "Marie Kondoed" in line]
137 assert len(said) == 1
138 assert "atlasberry008 on Instagram · 1,943 likes" in said[0]
139 assert not any(line.startswith('- "Sequoia"') for line in lines)
140
141
142 def test_a_video_under_the_like_floor_says_nothing():
143 quiet = _item(
144 "youtube", "https://www.youtube.com/watch?v=quiet", author="Small",
145 likes=3, highlights=["Sequoia Capital raised ten billion dollars this week in new funds."],
146 )
147 lines = _lines(quiet, NEOLIBERAL)
148 assert not any("ten billion" in line for line in lines)
149
150
151 def test_small_reddit_thread_joins_with_a_four_vote_top_comment():
152 lines = _lines(NEOLIBERAL, WSJ)
153 assert any("outlying move" in line and "top comment" in line for line in lines)
154
155
156 def test_large_reddit_thread_still_needs_five_votes():
157 big = _item(
158 "reddit", "https://www.reddit.com/r/startups/comments/big/thread/",
159 container="startups", score=900,
160 comments=[{"author": "a", "score": 4, "excerpt": "Four votes on a huge thread is noise."}],
161 )
162 lines = _lines(big, WSJ)
163 assert not any("huge thread" in line for line in lines)
164
165
166 def test_linkedin_post_with_placeholder_author_is_skipped():
167 placeholder = _item(
168 "linkedin", "https://www.linkedin.com/posts/x", author="LinkedIn", likes=87,
169 snippet="Sequoia just ended its scouts program and the firm that made scouting famous shut it down.",
170 )
171 named = _item(
172 "linkedin", "https://www.linkedin.com/posts/y", author="Jane Doe", likes=231,
173 snippet="A week has passed since the news about Sequoia Capital's European office closing.",
174 )
175 lines = _lines(placeholder, named, WSJ)
176 assert not any("scouts program" in line for line in lines)
177 assert any("European office" in line and line.endswith("— post") for line in lines)
178
179
180 def test_a_video_gives_its_top_comment_before_what_it_said():
181 video = _item(
182 "youtube", "https://www.youtube.com/watch?v=both", author="WSJ Events",
183 likes=386,
184 highlights=["Sequoia Capital raised ten billion dollars and closed its London office."],
185 comments=[{"author": "viewer", "score": 120, "excerpt": "Bigger is not better, apparently."}],
186 )
187 lines = [line for line in _lines(video, NEOLIBERAL) if line.startswith("- ")]
188 video_labels = [line.rsplit("— ", 1)[1] for line in lines if "watch?v=both" in line]
189 assert video_labels == ["top comment", "said"]
190
191
192 def test_a_reel_without_a_transcript_says_nothing():
193 described = _item(
194 "instagram", "https://www.instagram.com/reel/written/", author="writer",
195 likes=500,
196 snippet="Sequoia Capital closed its London office and ended the scout program this month, link in bio.",
197 )
198 lines = _lines(described, NEOLIBERAL)
199 assert not any("link in bio" in line for line in lines)
200
201
202 def test_one_long_spoken_sentence_stays_whole():
203 sentence = (
204 "Sequoia just raised $10 billion and in the same letter to announce it they "
205 "shut down their London office and their scout program and their accelerator "
206 "program because the most famous VC fund in the world Marie Kondoed itself "
207 "and the best venture firm is telling you about the new world order it sees"
208 )
209 assert 50 < len(sentence.split()) <= 70
210 reel = _item(
211 "tiktok", "https://www.tiktok.com/@x/video/1", author="x", likes=900,
212 snippet=sentence + ".", spoken=True,
213 )
214 assert render._content_quotes(reel, "Sequoia Capital") == [sentence + "."]
215
216
217 def test_normalize_marks_only_transcripts_as_spoken():
218 from lib import normalize
219
220 base = {"video_id": "1", "url": "https://www.tiktok.com/@x/video/1", "text": "desc"}
221 written = normalize._normalize_shortform_video(
222 "tiktok", {**base, "caption_snippet": "desc"}, 0, "2026-10-12", "2026-11-11", "tk", "TikTok"
223 )
224 spoken = normalize._normalize_shortform_video(
225 "tiktok", {**base, "caption_snippet": "words said", "caption_is_transcript": True},
226 0, "2026-10-12", "2026-11-11", "tk", "TikTok",
227 )
228 assert written.metadata["spoken_caption"] is False
229 assert spoken.metadata["spoken_caption"] is True
230
231
232 # Duplicate copies of one reel keep the transcript and its `spoken_caption`
233 # mark together, in either merge order.
234 def _reel(snippet, spoken):
235 return _item(
236 "instagram",
237 "https://www.instagram.com/reel/abc/",
238 author="vcbrags",
239 likes=1204,
240 snippet=snippet,
241 spoken=spoken,
242 )
243
244
245 _TRANSCRIPT = "The most famous VC fund in the world Marie Kondoed itself last week."
246 _DESCRIPTION = (
247 "Sequoia Capital news roundup. Follow for more venture capital stories every single "
248 "day of the week. #sequoia #vc #startups #funding #founders #investing"
249 )
250
251
252 def test_merge_keeps_a_later_transcript_over_a_longer_written_description():
253 from lib import fusion
254
255 merged = fusion.merge_source_items(_reel(_DESCRIPTION, False), _reel(_TRANSCRIPT, True))
256 assert merged.snippet == _TRANSCRIPT
257 assert merged.metadata["spoken_caption"] is True
258
259
260 def test_merge_never_replaces_a_transcript_with_a_longer_written_description():
261 from lib import fusion
262
263 merged = fusion.merge_source_items(_reel(_TRANSCRIPT, True), _reel(_DESCRIPTION, False))
264 assert merged.snippet == _TRANSCRIPT
265 assert merged.metadata["spoken_caption"] is True
266
267
268 def test_merge_still_keeps_the_longer_snippet_when_neither_copy_is_spoken():
269 from lib import fusion
270
271 merged = fusion.merge_source_items(_reel("Short.", False), _reel(_DESCRIPTION, False))
272 assert merged.snippet == _DESCRIPTION
273 assert not merged.metadata.get("spoken_caption")
274
274 lines PYTHON