返回 last30days-skill
test_render_badge.py
根目录 / tests / test_render_badge.py
1 """The report's first line names the search, its window and the version."""
2
3 from __future__ import annotations
4
5 import pytest
6
7 from lib import dates, html_render, render, schema
8
9
10 def _report(
11 topic: str = "SpaceX",
12 range_from: str = "2026-10-11",
13 range_to: str = "2026-11-10",
14 ) -> schema.Report:
15 return schema.Report(
16 topic=topic,
17 range_from=range_from,
18 range_to=range_to,
19 generated_at=f"{range_to}T12:00:00+00:00",
20 provider_runtime=schema.ProviderRuntime(
21 reasoning_provider="local",
22 planner_model="mock-planner",
23 rerank_model="mock-rerank",
24 ),
25 query_plan=schema.QueryPlan(
26 intent="research",
27 freshness_mode="balanced_recent",
28 cluster_mode="story",
29 raw_topic=topic,
30 subqueries=[
31 schema.SubQuery(
32 label="primary",
33 search_query=topic,
34 ranking_query=topic,
35 sources=["grounding"],
36 )
37 ],
38 source_weights={"grounding": 1.0},
39 ),
40 clusters=[],
41 ranked_candidates=[],
42 items_by_source={},
43 errors_by_source={},
44 )
45
46
47 def _discovery(domain: str) -> schema.DiscoveryReport:
48 return schema.DiscoveryReport(
49 domain=domain,
50 range_from="2026-10-11",
51 range_to="2026-11-10",
52 generated_at="2026-11-10T00:00:00+00:00",
53 plan=schema.DiscoveryPlan(
54 domain=domain, category=None, subreddits=["all"],
55 sources=["reddit", "hackernews"],
56 ),
57 topics=[],
58 source_status={},
59 warnings=[],
60 )
61
62
63 def _first_line(rendered: str) -> str:
64 return rendered.splitlines()[0]
65
66
67 @pytest.fixture(autouse=True)
68 def _fixed_version(monkeypatch):
69 monkeypatch.setattr(render, "_skill_version", lambda: "9.9.9")
70
71
72 def test_format_window_same_year_prints_the_year_once():
73 assert dates.format_window("2026-10-11", "2026-11-10") == "Oct 11 to Nov 10, 2026"
74
75
76 def test_format_window_across_years_prints_both_years():
77 assert (
78 dates.format_window("2025-12-20", "2026-01-09")
79 == "Dec 20, 2025 to Jan 9, 2026"
80 )
81
82
83 def test_compact_report_badge_names_topic_window_and_version():
84 line = _first_line(render.render_compact(_report()))
85 assert line == "🌐 last30days · SpaceX · Oct 11 to Nov 10, 2026 · v9.9.9"
86
87
88 def test_html_report_markdown_carries_the_same_badge():
89 line = _first_line(render.render_for_html(_report()))
90 assert line == "🌐 last30days · SpaceX · Oct 11 to Nov 10, 2026 · v9.9.9"
91
92
93 def test_window_crossing_a_year_boundary_prints_both_years():
94 report = _report(range_from="2025-12-20", range_to="2026-01-19")
95 line = _first_line(render.render_compact(report))
96 assert line == "🌐 last30days · SpaceX · Dec 20, 2025 to Jan 19, 2026 · v9.9.9"
97
98
99 def test_comparison_badge_joins_three_entities_with_vs():
100 reports = [(name, _report(name)) for name in ("OpenAI", "Anthropic", "xAI")]
101 expected = (
102 "🌐 last30days · OpenAI vs Anthropic vs xAI · Oct 11 to Nov 10, 2026 · v9.9.9"
103 )
104 assert _first_line(render.render_comparison_multi(reports)) == expected
105 assert _first_line(render.render_for_html_comparison(reports)) == expected
106
107
108 def test_discovery_badge_prints_the_domain():
109 line = _first_line(render.render_discovery(_discovery("AI agents")))
110 assert line == "🌐 last30days · AI agents · Oct 11 to Nov 10, 2026 · v9.9.9"
111
112
113 def test_discovery_badge_without_a_domain_omits_the_topic_segment():
114 line = _first_line(render.render_discovery(_discovery("")))
115 assert line == "🌐 last30days · Oct 11 to Nov 10, 2026 · v9.9.9"
116 assert "· ·" not in line
117
118
119 def test_twenty_word_topic_is_capped():
120 topic = " ".join(f"word{index}" for index in range(1, 21))
121 line = _first_line(render.render_compact(_report(topic)))
122 segment = line.split(" · ")[1]
123 assert len(segment) <= 60
124 assert segment.startswith("word1 word2 word3")
125 assert segment.endswith("...")
126 assert line.endswith(" · Oct 11 to Nov 10, 2026 · v9.9.9")
127
128
129 def test_topic_with_markdown_characters_stays_on_one_defanged_line():
130 report = _report("Rust\n```\n# Fake heading <!-- note -->")
131 rendered = render.render_compact(report)
132 assert _first_line(rendered) == (
133 "🌐 last30days · Rust ''' # Fake heading <!- - note - -> "
134 "· Oct 11 to Nov 10, 2026 · v9.9.9"
135 )
136 assert rendered.splitlines()[1] == ""
137
138
139 def test_empty_version_leaves_no_dangling_v(monkeypatch):
140 monkeypatch.setattr(render, "_skill_version", lambda: "")
141 line = _first_line(render.render_for_html(_report()))
142 assert line == "🌐 last30days · SpaceX · Oct 11 to Nov 10, 2026"
143
144
145 def test_html_brief_wraps_the_badge_line_in_the_badge_element():
146 rendered = html_render.render_html(_report())
147 assert (
148 '<div class="badge"><span class="accent">🌐</span> '
149 "last30days · SpaceX · Oct 11 to Nov 10, 2026 · v9.9.9</div>"
150 ) in rendered
151
152
153 # --- The window ends on the reader's date, not on tomorrow ---
154 #
155 # On the evening of Nov 10 in Los Angeles every report's first line said
156 # "Oct 12 to Nov 11, 2026": the window ends on the UTC date, which was
157 # already Nov 11.
158
159 from datetime import datetime, timedelta, timezone
160
161 PACIFIC = timezone(timedelta(hours=-8))
162 TOKYO = timezone(timedelta(hours=9))
163 EVENING_NOV_10_PACIFIC = datetime(2026, 11, 10, 17, 47, tzinfo=PACIFIC) # Nov 11 01:47 UTC
164
165
166 def test_window_ending_today_in_utc_shows_the_readers_date():
167 assert (
168 dates.format_window("2026-10-12", "2026-11-11", now=EVENING_NOV_10_PACIFIC)
169 == "Oct 12 to Nov 10, 2026"
170 )
171
172
173 def test_badge_line_never_says_tomorrow(monkeypatch):
174 monkeypatch.setattr(dates, "_local_now", lambda: EVENING_NOV_10_PACIFIC)
175
176 badge = render._render_badge("Disney", "2026-10-12", "2026-11-11")[0]
177
178 assert "Oct 12 to Nov 10, 2026" in badge
179 assert "Nov 11" not in badge
180
181
182 def test_same_date_on_both_clocks_is_unchanged():
183 morning = datetime(2026, 11, 10, 9, 0, tzinfo=PACIFIC) # Nov 10 17:00 UTC
184
185 assert dates.format_window("2026-10-11", "2026-11-10", now=morning) == "Oct 11 to Nov 10, 2026"
186
187
188 def test_reader_ahead_of_utc_keeps_the_utc_date():
189 # Nov 11 08:00 in Tokyo is Nov 10 23:00 UTC: the UTC end is not the future.
190 tokyo_morning = datetime(2026, 11, 11, 8, 0, tzinfo=TOKYO)
191
192 assert (
193 dates.format_window("2026-10-11", "2026-11-10", now=tokyo_morning)
194 == "Oct 11 to Nov 10, 2026"
195 )
196
197
198 def test_as_of_window_is_shown_as_asked():
199 # --as-of names the end date; it is not today's UTC date, so it stays.
200 assert (
201 dates.format_window("2026-09-01", "2026-10-01", now=EVENING_NOV_10_PACIFIC)
202 == "Sep 1 to Oct 1, 2026"
203 )
204
205
206 def test_zero_day_window_does_not_start_after_it_ends():
207 assert (
208 dates.format_window("2026-11-11", "2026-11-11", now=EVENING_NOV_10_PACIFIC)
209 == "Nov 10 to Nov 10, 2026"
210 )
211
212
213 def test_query_window_still_ends_on_the_utc_date(monkeypatch):
214 # The clamp is display only: the queried range keeps its UTC upper bound
215 # so a post from the last hours is not lost.
216 class _Clock(datetime):
217 @classmethod
218 def now(cls, tz=None):
219 return EVENING_NOV_10_PACIFIC.astimezone(tz)
220
221 monkeypatch.setattr(dates, "datetime", _Clock)
222
223 assert dates.get_date_range(30) == ("2026-10-12", "2026-11-11")
224
224 lines PYTHON