返回 last30days-skill
test_jobs_fusion_render.py
根目录 / tests / test_jobs_fusion_render.py
1 import copy
2 from urllib.parse import urlsplit
3
4 import pytest
5
6 from lib import cluster, fusion, hiring_signals, http, normalize, pipeline, render, rerank, schema
7
8
9 def _jobs(count):
10 return normalize.normalize_source_items(
11 "jobs",
12 [
13 {
14 "id": f"job-{index}",
15 "title": f"Software Engineer {index}",
16 # Role-unique text: a sentence every role shares is company
17 # boilerplate and themes nothing.
18 "description": f"Build Acme backend infrastructure for team {index}.",
19 "url": f"https://boards.greenhouse.io/acme/jobs/{index}",
20 "provider": "greenhouse",
21 "department": "Engineering",
22 "date": "2026-06-01",
23 }
24 for index in range(count)
25 ],
26 "2026-05-17",
27 "2026-06-16",
28 )
29
30
31 def _plan(sources):
32 return schema.QueryPlan(
33 intent="company",
34 freshness_mode="relaxed",
35 cluster_mode="story",
36 raw_topic="Acme",
37 subqueries=[schema.SubQuery(
38 label="primary",
39 search_query="Acme",
40 ranking_query="What is Acme hiring for?",
41 sources=sources,
42 )],
43 source_weights={source: 1.0 for source in sources},
44 )
45
46
47 def test_job_provider_does_not_cap_roles_or_exempt_social_author():
48 jobs = _jobs(6)
49 posts = [
50 schema.SourceItem(
51 item_id=f"post-{index}",
52 source="x",
53 title=f"Acme update {index}",
54 body="Acme update",
55 url=f"https://x.com/greenhouse/status/{index}",
56 author="greenhouse",
57 relevance_hint=0.8,
58 )
59 for index in range(6)
60 ]
61
62 candidates = fusion.weighted_rrf(
63 {("primary", "jobs"): jobs, ("primary", "x"): posts},
64 _plan(["jobs", "x"]),
65 pool_limit=40,
66 )
67
68 assert {item.item_id for item in candidates if item.source == "jobs"} == {
69 item.item_id for item in jobs
70 }
71 assert len([item for item in candidates if item.source == "x"]) == 3
72
73
74 def _report(jobs, pool_limit):
75 plan = _plan(["jobs"])
76 candidates = fusion.weighted_rrf(
77 {("primary", "jobs"): jobs}, plan, pool_limit=pool_limit,
78 )
79 ranked = rerank.rerank_candidates(
80 topic="Acme", plan=plan, candidates=candidates,
81 provider=None, model=None, shortlist_size=pool_limit,
82 )
83 return schema.Report(
84 topic="Acme",
85 range_from="2026-05-17",
86 range_to="2026-06-16",
87 generated_at="2026-06-16T00:00:00+00:00",
88 provider_runtime=schema.ProviderRuntime(
89 reasoning_provider=None, planner_model=None, rerank_model=None,
90 ),
91 query_plan=plan,
92 ranked_candidates=ranked,
93 clusters=cluster.cluster_candidates(ranked, plan),
94 items_by_source={"jobs": jobs},
95 errors_by_source={},
96 artifacts={"hiring_signals": hiring_signals.analyze(
97 jobs, explicit=True, topic="Acme",
98 )},
99 )
100
101
102 @pytest.mark.parametrize("renderer", [
103 lambda report: render.render_compact(report, cluster_limit=1),
104 lambda report: render.render_context(report, cluster_limit=1),
105 render.render_for_html,
106 lambda report: render.render_compact(report, register="exec", cluster_limit=1),
107 ], ids=["compact", "context", "html", "exec"])
108 @pytest.mark.parametrize("pool_limit", [5, 40])
109 def test_hiring_analysis_uses_board_beyond_ranking_and_display_limits(renderer, pool_limit):
110 jobs = _jobs(40)
111 jobs[-1].title = "Founding Research Scientist, Human Simulation"
112 jobs[-1].body = "Acme is founding a human simulation research team."
113 report = _report(jobs, pool_limit=pool_limit)
114
115 assert len(report.ranked_candidates) == pool_limit
116 if pool_limit == 5:
117 assert all(candidate.item_id != jobs[-1].item_id for candidate in report.ranked_candidates)
118 text = renderer(report)
119
120 assert "company-size tier: growth" in text
121 assert "Founding Research Scientist, Human Simulation" in text
122 assert "evidence: 39 roles" in text
123
124
125 @pytest.mark.parametrize("rejection", ["cluster", "candidate"])
126 def test_hiring_analysis_excludes_rejected_roles_from_full_board(rejection):
127 jobs = _jobs(6)
128 jobs[-1].title = "Founding Research Scientist, Human Simulation"
129 report = _report(jobs, pool_limit=6)
130 assert jobs[-1].title in render.render_compact(report)
131
132 rejected = next(
133 candidate for candidate in report.ranked_candidates
134 if candidate.item_id == jobs[-1].item_id
135 )
136 if rejection == "cluster":
137 next(
138 cluster for cluster in report.clusters
139 if rejected.candidate_id in cluster.candidate_ids
140 ).score = 0
141 else:
142 rejected.explanation = "fallback-local-score (entity-miss demotion)"
143
144 text = render.render_compact(report)
145
146 assert "## Hiring Signals" in text
147 assert jobs[-1].title not in text
148 assert "evidence: 5 roles" in text
149
150
151 @pytest.mark.parametrize("only_rejected", [False, True])
152 def test_hiring_analysis_does_not_revive_pruned_entity_miss(monkeypatch, only_rejected):
153 jobs = _jobs(6)
154 jobs[-1].title = "Founding Research Scientist, Human Simulation"
155 jobs[-1].body = "Research an unrelated company."
156 rejected_title = jobs[-1].title
157 if only_rejected:
158 jobs = jobs[-1:]
159
160 def careers_page(url, **kwargs):
161 assert url == "https://acme.com/careers"
162 return '<a href="https://boards.greenhouse.io/acme">Careers</a>'
163
164 def board_response(url, **kwargs):
165 assert url == "https://boards-api.greenhouse.io/v1/boards/acme/jobs"
166 return {"jobs": [
167 {
168 "id": job.item_id,
169 "title": job.title,
170 "content": job.body,
171 "absolute_url": job.url,
172 "updated_at": "2026-06-01",
173 "departments": [{"name": "Engineering"}],
174 }
175 for job in jobs
176 ]}
177
178 monkeypatch.setattr(http, "get_text", careers_page)
179 monkeypatch.setattr(http, "get", board_response)
180 report = pipeline.run(
181 topic="Acme",
182 config={"FROM_BROWSER": "off", "LAST30DAYS_X_BACKEND": "xai"},
183 depth="quick",
184 requested_sources=["jobs"],
185 hiring_signals_mode=True,
186 web_backend="none",
187 )
188
189 assert any(item.title == rejected_title for item in report.items_by_source["jobs"])
190 assert all(candidate.title != rejected_title for candidate in report.ranked_candidates)
191
192 text = render.render_compact(report)
193
194 assert rejected_title not in text
195 if not only_rejected:
196 assert "## Hiring Signals" in text
197 assert "evidence: 5 roles" in text
198
199
200 @pytest.mark.parametrize("rejected", [False, True])
201 def test_hiring_analysis_uses_canonical_job_urls(rejected):
202 jobs = _jobs(5)
203 jobs[-1].title = "Founding Research Scientist, Human Simulation"
204 if rejected:
205 jobs[-1].body = "Research an unrelated company."
206 jobs[-1].snippet = jobs[-1].body
207 report = _report(jobs, pool_limit=5)
208 original = jobs[-1]
209 variant = copy.deepcopy(original)
210 variant.item_id = "other-stream-id"
211 parts = urlsplit(original.url)
212 variant.url = parts._replace(
213 scheme=parts.scheme.upper(), netloc=parts.netloc.upper(),
214 path=parts.path + "/", query="utm_source=jobs",
215 ).geturl()
216 report.items_by_source["jobs"] = [*jobs[:-1], variant]
217 assert fusion.candidate_key(original) == fusion.candidate_key(variant)
218 if rejected:
219 candidate = next(
220 candidate for candidate in report.ranked_candidates
221 if candidate.item_id == original.item_id
222 )
223 assert "entity-miss" in candidate.explanation
224 assert not rerank.candidate_relevance_ok(candidate)
225
226 text = render.render_compact(report)
227
228 assert f"evidence: {4 if rejected else 5} roles" in text
229 if rejected:
230 assert original.title not in text
231
232
233 def test_hiring_analysis_keeps_case_sensitive_job_paths_distinct():
234 jobs = _jobs(5)
235 original = jobs[-1]
236 distinct = copy.deepcopy(original)
237 distinct.item_id = "case-sensitive-role"
238 distinct.title = "Software Engineer, Infrastructure"
239 parts = urlsplit(original.url)
240 distinct.url = parts._replace(path=parts.path.upper()).geturl()
241 report = _report([*jobs, distinct], pool_limit=6)
242
243 assert len(report.ranked_candidates) == 6
244 assert {original.url, distinct.url} <= {
245 candidate.url for candidate in report.ranked_candidates
246 }
247 assert "evidence: 6 roles" in render.render_compact(report)
248
249
250 def _stats_report(items_by_source):
251 report = _report(_jobs(1), pool_limit=1)
252 report.items_by_source = items_by_source
253 return report
254
255
256 def _posts(count):
257 return [
258 schema.SourceItem(
259 item_id=f"post-{index}",
260 source="x",
261 title=f"Acme update {index}",
262 body="Acme update",
263 url=f"https://x.com/acmefan/status/{index}",
264 author="acmefan",
265 )
266 for index in range(count)
267 ]
268
269
270 def test_stats_total_leaves_out_job_roles_and_reports_them_on_their_own_line():
271 threads = [
272 schema.SourceItem(
273 item_id=f"thread-{index}",
274 source="reddit",
275 title=f"Acme thread {index}",
276 body="Acme thread",
277 url=f"https://www.reddit.com/r/acme/comments/{index}",
278 container="acme",
279 )
280 for index in range(45)
281 ]
282 report = _stats_report(
283 {"jobs": _jobs(2900), "x": _posts(100), "reddit": threads}
284 )
285
286 stats = render._render_stats(report)
287
288 assert "- Total evidence: 145 items across 2 sources" in stats
289 assert "- Jobs: 2900 roles" in stats
290
291
292 def test_stats_top_voices_names_no_department_or_hiring_system():
293 report = _stats_report({"jobs": _jobs(40), "x": _posts(2)})
294
295 stats = render._render_stats(report)
296
297 assert "- Top voices: @acmefan" in stats
298 assert not any("Engineering" in line or "greenhouse" in line for line in stats)
299
300
301 def test_stats_top_voices_drop_a_zero_engagement_handle_but_keep_departments_out():
302 quiet = _posts(4)
303 loud = [
304 schema.SourceItem(
305 item_id="loud-1", source="x", title="Acme launch", body="Acme launch",
306 url="https://x.com/acmeceo/status/1", author="acmeceo",
307 engagement={"likes": 900, "reposts": 50, "replies": 20},
308 )
309 ]
310 for post in quiet:
311 post.engagement = {"likes": 0, "reposts": 0, "replies": 1}
312 report = _stats_report({"jobs": _jobs(40), "x": quiet + loud})
313
314 stats = render._render_stats(report)
315
316 assert "- Top voices: @acmeceo" in stats
317 assert not any("@acmefan" in line for line in stats if "Top voices" in line)
318
319
320 def test_stats_for_a_jobs_only_run_reports_roles_without_an_evidence_total():
321 report = _stats_report({"jobs": _jobs(1)})
322
323 assert render._render_stats(report) == ["## Stats", "", "- Jobs: 1 role", ""]
324
324 lines PYTHON