| 1 | import copy |
| 2 | import io |
| 3 | import json |
| 4 | from contextlib import redirect_stderr, redirect_stdout |
| 5 | from pathlib import Path |
| 6 | from unittest import mock |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | import last30days as cli |
| 11 | from lib import pipeline, planner, render, schema |
| 12 | |
| 13 | |
| 14 | def _item(item_id: str, source: str, title: str, url: str) -> schema.SourceItem: |
| 15 | return schema.SourceItem( |
| 16 | item_id=item_id, |
| 17 | source=source, |
| 18 | title=title, |
| 19 | body=f"Body for {title}", |
| 20 | url=url, |
| 21 | snippet=f"Evidence about {title}", |
| 22 | local_rank_score=0.9, |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | def _candidate(item: schema.SourceItem, score: float = 80.0) -> schema.Candidate: |
| 27 | return schema.Candidate( |
| 28 | candidate_id=f"cand-{item.item_id}", |
| 29 | item_id=item.item_id, |
| 30 | source=item.source, |
| 31 | title=item.title, |
| 32 | url=item.url, |
| 33 | snippet=item.snippet, |
| 34 | subquery_labels=["primary"], |
| 35 | native_ranks={item.source: 1}, |
| 36 | local_relevance=0.9, |
| 37 | freshness=90, |
| 38 | engagement=10, |
| 39 | source_quality=0.8, |
| 40 | rrf_score=0.1, |
| 41 | sources=[item.source], |
| 42 | source_items=[item], |
| 43 | final_score=score, |
| 44 | ) |
| 45 | |
| 46 | |
| 47 | def _report(*, drill: bool = False) -> schema.Report: |
| 48 | ban = _item( |
| 49 | "ban", |
| 50 | "reddit", |
| 51 | "OpenClaw API ban discussion", |
| 52 | "https://reddit.example/ban", |
| 53 | ) |
| 54 | policy = _item( |
| 55 | "policy", |
| 56 | "youtube", |
| 57 | "OpenClaw policy explained", |
| 58 | "https://youtube.example/policy", |
| 59 | ) |
| 60 | release = _item( |
| 61 | "release", |
| 62 | "hackernews", |
| 63 | "OpenClaw ships a new release", |
| 64 | "https://news.example/release", |
| 65 | ) |
| 66 | candidates = [_candidate(ban, 92), _candidate(policy, 85), _candidate(release, 70)] |
| 67 | clusters = [ |
| 68 | schema.Cluster( |
| 69 | cluster_id="cluster-1", |
| 70 | title="OpenClaw API ban discussion", |
| 71 | candidate_ids=[candidates[0].candidate_id, candidates[1].candidate_id], |
| 72 | representative_ids=[candidates[0].candidate_id], |
| 73 | sources=["reddit", "youtube"], |
| 74 | score=92, |
| 75 | ), |
| 76 | schema.Cluster( |
| 77 | cluster_id="cluster-2", |
| 78 | title="OpenClaw release notes", |
| 79 | candidate_ids=[candidates[2].candidate_id], |
| 80 | representative_ids=[candidates[2].candidate_id], |
| 81 | sources=["hackernews"], |
| 82 | score=70, |
| 83 | ), |
| 84 | ] |
| 85 | if drill: |
| 86 | deeper = _item( |
| 87 | "deeper", |
| 88 | "reddit", |
| 89 | "OpenClaw API policy enforcement details", |
| 90 | "https://reddit.example/deeper", |
| 91 | ) |
| 92 | # The first result repeats the cached URL; merge must keep one copy. |
| 93 | candidates = [_candidate(ban, 95), _candidate(deeper, 90)] |
| 94 | clusters = [ |
| 95 | schema.Cluster( |
| 96 | cluster_id="cluster-1", |
| 97 | title="OpenClaw API policy enforcement details", |
| 98 | candidate_ids=[candidate.candidate_id for candidate in candidates], |
| 99 | representative_ids=[candidate.candidate_id for candidate in candidates], |
| 100 | sources=["reddit"], |
| 101 | score=95, |
| 102 | ) |
| 103 | ] |
| 104 | items_by_source = {"reddit": [ban, deeper]} |
| 105 | else: |
| 106 | items_by_source = { |
| 107 | "reddit": [ban], |
| 108 | "youtube": [policy], |
| 109 | "hackernews": [release], |
| 110 | } |
| 111 | return schema.Report( |
| 112 | topic="OpenClaw API policy" if drill else "OpenClaw", |
| 113 | range_from="2026-06-10", |
| 114 | range_to="2026-07-10", |
| 115 | generated_at="2026-07-10T12:00:00+00:00", |
| 116 | provider_runtime=schema.ProviderRuntime( |
| 117 | reasoning_provider="local", |
| 118 | planner_model="mock-planner", |
| 119 | rerank_model="mock-reranker", |
| 120 | ), |
| 121 | query_plan=schema.QueryPlan( |
| 122 | intent="opinion", |
| 123 | freshness_mode="balanced_recent", |
| 124 | cluster_mode="debate", |
| 125 | raw_topic="OpenClaw", |
| 126 | subqueries=[ |
| 127 | schema.SubQuery( |
| 128 | label="primary", |
| 129 | search_query="OpenClaw", |
| 130 | ranking_query="What is happening with OpenClaw?", |
| 131 | sources=list(items_by_source), |
| 132 | ) |
| 133 | ], |
| 134 | source_weights={source: 1.0 for source in items_by_source}, |
| 135 | ), |
| 136 | clusters=clusters, |
| 137 | ranked_candidates=candidates, |
| 138 | items_by_source=items_by_source, |
| 139 | errors_by_source={}, |
| 140 | source_status={ |
| 141 | source: schema.SourceOutcome( |
| 142 | source=source, |
| 143 | state="ok", |
| 144 | items_returned=len(items), |
| 145 | ) |
| 146 | for source, items in items_by_source.items() |
| 147 | }, |
| 148 | ) |
| 149 | |
| 150 | |
| 151 | def test_cluster_resolution_by_index_and_number(): |
| 152 | report = _report() |
| 153 | assert planner.resolve_drill_clusters(report, "cluster 2")[0].cluster_id == "cluster-2" |
| 154 | assert planner.resolve_drill_clusters(report, "1")[0].cluster_id == "cluster-1" |
| 155 | |
| 156 | |
| 157 | def test_cluster_resolution_by_fuzzy_title_and_entities(): |
| 158 | matched = planner.resolve_drill_clusters( |
| 159 | _report(), |
| 160 | "what is behind the OpenClaw API ban?", |
| 161 | ) |
| 162 | assert [cluster.cluster_id for cluster in matched] == ["cluster-1"] |
| 163 | |
| 164 | |
| 165 | def test_cluster_resolution_no_match_prints_candidates(): |
| 166 | with pytest.raises(planner.DrillTargetError) as exc: |
| 167 | planner.resolve_drill_clusters(_report(), "quantum potato harvest") |
| 168 | message = str(exc.value) |
| 169 | assert "Available clusters" in message |
| 170 | assert "1. OpenClaw API ban discussion" in message |
| 171 | assert "2. OpenClaw release notes" in message |
| 172 | |
| 173 | |
| 174 | def test_build_drill_plan_only_uses_contributing_sources_and_cluster_terms(): |
| 175 | plan = planner.build_drill_plan(_report(), "cluster 1") |
| 176 | assert set(plan.source_weights) == {"reddit", "youtube"} |
| 177 | assert all(set(subquery.sources) == {"reddit", "youtube"} for subquery in plan.subqueries) |
| 178 | assert all("hackernews" not in subquery.sources for subquery in plan.subqueries) |
| 179 | assert "drill-mode" in plan.notes |
| 180 | assert any("openclaw" in subquery.search_query.lower() for subquery in plan.subqueries) |
| 181 | |
| 182 | |
| 183 | def test_merge_dedupes_new_results_preserves_other_clusters_and_renders_context(): |
| 184 | base = _report() |
| 185 | merged = pipeline.merge_drill_report( |
| 186 | base, |
| 187 | _report(drill=True), |
| 188 | [base.clusters[0]], |
| 189 | target="cluster 1", |
| 190 | ) |
| 191 | |
| 192 | assert merged.drill_of == "OpenClaw API ban discussion" |
| 193 | assert [cluster.cluster_id for cluster in merged.clusters] == ["cluster-1", "cluster-2"] |
| 194 | assert len([item for item in merged.items_by_source["reddit"] if item.url.endswith("/ban")]) == 1 |
| 195 | assert any(item.url.endswith("/deeper") for item in merged.items_by_source["reddit"]) |
| 196 | assert merged.artifacts["drill_context"]["new_items"] == 1 |
| 197 | assert len(merged.artifacts["drill_history"]) == 1 |
| 198 | output = render.render_compact(merged) |
| 199 | assert "## Drill Follow-up" in output |
| 200 | assert "### Original" in output |
| 201 | assert "### Deeper" in output |
| 202 | |
| 203 | |
| 204 | def test_merge_dedupes_drill_candidates_against_untouched_clusters(): |
| 205 | base = _report() |
| 206 | drill_report = _report(drill=True) |
| 207 | rediscovered = copy.deepcopy(base.ranked_candidates[2]) |
| 208 | drill_report.ranked_candidates.append(rediscovered) |
| 209 | drill_report.clusters[0].candidate_ids.append(rediscovered.candidate_id) |
| 210 | |
| 211 | merged = pipeline.merge_drill_report( |
| 212 | base, |
| 213 | drill_report, |
| 214 | [base.clusters[0]], |
| 215 | target="cluster 1", |
| 216 | ) |
| 217 | |
| 218 | candidate_ids = [candidate.candidate_id for candidate in merged.ranked_candidates] |
| 219 | assert candidate_ids.count(rediscovered.candidate_id) == 1 |
| 220 | assert rediscovered.candidate_id not in merged.clusters[0].candidate_ids |
| 221 | assert rediscovered.candidate_id in merged.clusters[1].candidate_ids |
| 222 | |
| 223 | |
| 224 | def test_merge_retains_enriched_rediscovery_in_untouched_cluster(): |
| 225 | base = _report() |
| 226 | base.ranked_candidates[2].cluster_id = "cluster-2" |
| 227 | drill_report = _report(drill=True) |
| 228 | rediscovered = copy.deepcopy(base.ranked_candidates[2]) |
| 229 | rediscovered.snippet = "Enriched release evidence from the drill" |
| 230 | rediscovered.engagement = 321 |
| 231 | rediscovered.source_items[0].snippet = "Transcript-backed release evidence" |
| 232 | rediscovered.source_items[0].engagement = {"comments": 42} |
| 233 | rediscovered.source_items[0].metadata = { |
| 234 | "transcript": "Detailed release transcript", |
| 235 | "comments": ["Useful community context"], |
| 236 | } |
| 237 | drill_report.ranked_candidates.append(rediscovered) |
| 238 | |
| 239 | merged = pipeline.merge_drill_report( |
| 240 | base, |
| 241 | drill_report, |
| 242 | [base.clusters[0]], |
| 243 | target="cluster 1", |
| 244 | ) |
| 245 | |
| 246 | retained = next( |
| 247 | candidate |
| 248 | for candidate in merged.ranked_candidates |
| 249 | if candidate.candidate_id == rediscovered.candidate_id |
| 250 | ) |
| 251 | assert retained.cluster_id == base.ranked_candidates[2].cluster_id |
| 252 | assert retained.snippet == "Enriched release evidence from the drill" |
| 253 | assert retained.engagement == 321 |
| 254 | assert retained.source_items[0].metadata["transcript"] == "Detailed release transcript" |
| 255 | assert retained.source_items[0].engagement == {"comments": 42} |
| 256 | |
| 257 | |
| 258 | def test_merge_recomputes_attempted_source_health_from_retained_evidence(): |
| 259 | base = _report() |
| 260 | base.errors_by_source["reddit"] = "cached timeout" |
| 261 | base.source_status["reddit"] = schema.SourceOutcome( |
| 262 | source="reddit", |
| 263 | state=schema.RATE_LIMITED, |
| 264 | detail="cached timeout", |
| 265 | ) |
| 266 | base.warnings = [ |
| 267 | "Some sources failed: reddit", |
| 268 | "No candidates survived retrieval and ranking.", |
| 269 | ] |
| 270 | drill_report = _report(drill=True) |
| 271 | drill_report.source_status["youtube"] = schema.SourceOutcome( |
| 272 | source="youtube", |
| 273 | state=schema.NO_RESULTS, |
| 274 | items_returned=0, |
| 275 | ) |
| 276 | |
| 277 | merged = pipeline.merge_drill_report( |
| 278 | base, |
| 279 | drill_report, |
| 280 | [base.clusters[0]], |
| 281 | target="cluster 1", |
| 282 | ) |
| 283 | |
| 284 | assert "reddit" not in merged.errors_by_source |
| 285 | assert merged.source_status["reddit"].state == "ok" |
| 286 | assert merged.source_status["youtube"].state == "ok" |
| 287 | assert merged.source_status["youtube"].items_returned == 1 |
| 288 | assert not any("Some sources failed" in warning for warning in merged.warnings) |
| 289 | assert "No candidates survived retrieval and ranking." not in merged.warnings |
| 290 | |
| 291 | |
| 292 | def test_expired_cache_exits_cleanly_with_research_guidance(tmp_path: Path): |
| 293 | config_dir = tmp_path / "config" |
| 294 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 295 | cli._write_last_run("OpenClaw", _report()) |
| 296 | cache_path = config_dir / "last-report.json" |
| 297 | payload = json.loads(cache_path.read_text(encoding="utf-8")) |
| 298 | payload["timestamp"] = "2026-01-01T00:00:00+00:00" |
| 299 | cache_path.write_text(json.dumps(payload), encoding="utf-8") |
| 300 | |
| 301 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \ |
| 302 | mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 303 | mock.patch.object(cli.pipeline, "run", side_effect=AssertionError("pipeline should not run")), \ |
| 304 | mock.patch.object(cli.sys, "argv", ["last30days.py", "--drill", "cluster 1"]): |
| 305 | stderr = io.StringIO() |
| 306 | with redirect_stderr(stderr): |
| 307 | rc = cli.main() |
| 308 | |
| 309 | assert rc == 2 |
| 310 | assert "run a research pass first" in stderr.getvalue() |
| 311 | |
| 312 | |
| 313 | def test_non_object_cache_is_unavailable_with_warning(tmp_path: Path): |
| 314 | config_dir = tmp_path / "config" |
| 315 | config_dir.mkdir() |
| 316 | (config_dir / "last-report.json").write_text("[]", encoding="utf-8") |
| 317 | |
| 318 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 319 | stderr = io.StringIO() |
| 320 | with redirect_stderr(stderr): |
| 321 | cached = cli._load_last_report_cache(None) |
| 322 | |
| 323 | assert cached is None |
| 324 | assert "Could not read report cache" in stderr.getvalue() |
| 325 | |
| 326 | |
| 327 | def test_drill_publish_html_requires_html_emit_before_dispatch(): |
| 328 | parser = cli.build_parser() |
| 329 | args = parser.parse_args(["--drill", "cluster 1", "--publish-html"]) |
| 330 | |
| 331 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 332 | mock.patch.object(cli, "_run_drill") as run_drill: |
| 333 | stderr = io.StringIO() |
| 334 | with redirect_stderr(stderr): |
| 335 | rc = cli._main(parser, args, []) |
| 336 | |
| 337 | assert rc == 2 |
| 338 | assert "--publish-html requires --emit=html" in stderr.getvalue() |
| 339 | run_drill.assert_not_called() |
| 340 | |
| 341 | |
| 342 | def test_drill_applies_config_backed_source_filters_before_dispatch(): |
| 343 | parser = cli.build_parser() |
| 344 | args = parser.parse_args([ |
| 345 | "--drill", "cluster 1", |
| 346 | "--dedicated-subreddits", "r/OpenClaw, OpenClawDev", |
| 347 | "--polymarket-keywords", "API, Policy", |
| 348 | ]) |
| 349 | |
| 350 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 351 | mock.patch.object(cli, "_run_drill", return_value=0) as run_drill: |
| 352 | assert cli._main(parser, args, []) == 0 |
| 353 | |
| 354 | drill_config = run_drill.call_args.args[1] |
| 355 | assert drill_config["_dedicated_subreddits"] == ["OpenClaw", "OpenClawDev"] |
| 356 | assert drill_config["_polymarket_keywords"] == ["api", "policy"] |
| 357 | |
| 358 | |
| 359 | def test_drill_inherits_cached_historical_window(tmp_path: Path): |
| 360 | config_dir = tmp_path / "config" |
| 361 | cached_report = _report() |
| 362 | cached_report.range_from = "2026-05-01" |
| 363 | cached_report.range_to = "2026-05-08" |
| 364 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 365 | cli._write_last_run("OpenClaw", cached_report) |
| 366 | |
| 367 | args = cli.build_parser().parse_args(["--drill", "cluster 1", "--mock"]) |
| 368 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \ |
| 369 | mock.patch.object(cli.pipeline, "diagnose", return_value={}), \ |
| 370 | mock.patch.object(cli.pipeline, "run", return_value=_report(drill=True)) as run_mock, \ |
| 371 | mock.patch.object(cli, "_show_runtime_ui"), \ |
| 372 | redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 373 | assert cli._run_drill(args, {}) == 0 |
| 374 | |
| 375 | assert run_mock.call_args.kwargs["lookback_days"] == 7 |
| 376 | assert run_mock.call_args.kwargs["as_of_date"] == "2026-05-08" |
| 377 | |
| 378 | |
| 379 | def test_drill_uses_cached_financial_topic_while_plan_stays_cluster_focused(tmp_path: Path): |
| 380 | config_dir = tmp_path / "config" |
| 381 | cached_report = _report() |
| 382 | stock_item = _item( |
| 383 | "now", |
| 384 | "stocktwits", |
| 385 | "AI agent rollout", |
| 386 | "https://stocktwits.example/now", |
| 387 | ) |
| 388 | stock_candidate = _candidate(stock_item, 95) |
| 389 | cached_report.topic = "ServiceNow $NOW stock" |
| 390 | cached_report.ranked_candidates = [stock_candidate] |
| 391 | cached_report.clusters = [schema.Cluster( |
| 392 | cluster_id="cluster-1", |
| 393 | title="AI agent rollout", |
| 394 | candidate_ids=[stock_candidate.candidate_id], |
| 395 | representative_ids=[stock_candidate.candidate_id], |
| 396 | sources=["stocktwits"], |
| 397 | score=95, |
| 398 | )] |
| 399 | cached_report.items_by_source = {"stocktwits": [stock_item]} |
| 400 | cached_report.query_plan.source_weights = {"stocktwits": 1.0} |
| 401 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 402 | cli._write_last_run(cached_report.topic, cached_report) |
| 403 | |
| 404 | args = cli.build_parser().parse_args(["--drill", "cluster 1", "--mock"]) |
| 405 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \ |
| 406 | mock.patch.object(cli.pipeline, "diagnose", return_value={}), \ |
| 407 | mock.patch.object(cli.pipeline, "run", return_value=_report(drill=True)) as run_mock, \ |
| 408 | mock.patch.object(cli, "_show_runtime_ui"), \ |
| 409 | redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 410 | assert cli._run_drill(args, {}) == 0 |
| 411 | |
| 412 | call = run_mock.call_args.kwargs |
| 413 | assert call["topic"] == "ServiceNow $NOW stock" |
| 414 | assert call["external_plan"]["subqueries"][0]["search_query"] == "AI agent rollout" |
| 415 | assert call["requested_sources"] == ["stocktwits"] |
| 416 | |
| 417 | |
| 418 | def test_cli_drill_runs_deep_updates_cache_and_can_chain(tmp_path: Path): |
| 419 | config_dir = tmp_path / "config" |
| 420 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 421 | cli._write_last_run("OpenClaw", _report()) |
| 422 | |
| 423 | args = cli.build_parser().parse_args(["--drill", "cluster 1", "--mock"]) |
| 424 | drill_result = _report(drill=True) |
| 425 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \ |
| 426 | mock.patch.object(cli.pipeline, "diagnose", return_value={}), \ |
| 427 | mock.patch.object(cli.pipeline, "run", return_value=drill_result) as run_mock, \ |
| 428 | mock.patch.object(cli, "_show_runtime_ui"), \ |
| 429 | redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 430 | assert cli._run_drill(args, {}) == 0 |
| 431 | |
| 432 | call = run_mock.call_args.kwargs |
| 433 | assert call["depth"] == "deep" |
| 434 | assert set(call["requested_sources"]) == {"reddit", "youtube"} |
| 435 | assert set(call["external_plan"]["subqueries"][0]["sources"]) == {"reddit", "youtube"} |
| 436 | |
| 437 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 438 | cached = cli._load_last_report_cache(None) |
| 439 | assert cached is not None |
| 440 | assert len(cached[0].artifacts["drill_history"]) == 1 |
| 441 | |
| 442 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \ |
| 443 | mock.patch.object(cli.pipeline, "diagnose", return_value={}), \ |
| 444 | mock.patch.object(cli.pipeline, "run", return_value=drill_result), \ |
| 445 | mock.patch.object(cli, "_show_runtime_ui"), \ |
| 446 | redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 447 | assert cli._run_drill(args, {}) == 0 |
| 448 | |
| 449 | with mock.patch.object(cli.env, "CONFIG_DIR", config_dir): |
| 450 | chained = cli._load_last_report_cache(None) |
| 451 | assert chained is not None |
| 452 | assert len(chained[0].artifacts["drill_history"]) == 2 |
| 453 | |
| 454 | |
| 455 | def test_drill_plan_does_not_gain_jobs_via_company_topic(monkeypatch): |
| 456 | from lib import pipeline, schema |
| 457 | |
| 458 | plan = schema.QueryPlan( |
| 459 | intent="general", |
| 460 | freshness_mode="balanced_recent", |
| 461 | cluster_mode="story", |
| 462 | raw_topic="OpenClaw", |
| 463 | notes=["drill-mode"], |
| 464 | subqueries=[ |
| 465 | schema.SubQuery( |
| 466 | label="drill", |
| 467 | search_query="OpenClaw api ban", |
| 468 | ranking_query="OpenClaw api ban", |
| 469 | sources=["youtube"], |
| 470 | ) |
| 471 | ], |
| 472 | source_weights={"youtube": 1.0}, |
| 473 | ) |
| 474 | pipeline._ensure_jobs_in_plan(plan, ["youtube", "jobs"], explicit=False, topic="OpenClaw") |
| 475 | # Direct call still injects (documenting baseline)... |
| 476 | assert "jobs" in plan.source_weights |
| 477 | # ...but run()'s drill gate skips the injection entirely for drill plans; |
| 478 | # assert the gate condition itself so the contract is pinned. |
| 479 | assert "drill-mode" in plan.notes |
| 480 | |
| 481 | |
| 482 | def test_merge_collapses_exact_url_rediscoveries(): |
| 483 | from lib import pipeline, schema |
| 484 | import copy |
| 485 | |
| 486 | def item(url, body): |
| 487 | return schema.SourceItem( |
| 488 | item_id=url, source="reddit", title="t", body=body, url=url, |
| 489 | published_at="2026-07-01", snippet=body[:20], engagement={"score": 5}, |
| 490 | ) |
| 491 | |
| 492 | old = item("https://reddit.com/r/x/1", "original body") |
| 493 | new = item("https://reddit.com/r/x/1", "enriched body with transcript and much longer text") |
| 494 | from lib import dedupe |
| 495 | new_urls = {new.url} |
| 496 | filtered_old = [i for i in [old] if not (i.url and i.url in new_urls)] |
| 497 | combined = dedupe.dedupe_items([copy.deepcopy(new), *filtered_old]) |
| 498 | assert len(combined) == 1 |
| 499 | assert combined[0].body.startswith("enriched") |
| 500 | |
| 501 | |
| 502 | def test_write_last_run_returns_false_on_failure(monkeypatch, capsys): |
| 503 | import last30days as cli |
| 504 | from lib import env |
| 505 | |
| 506 | class ExplodingPath: |
| 507 | def mkdir(self, *a, **k): |
| 508 | raise OSError("disk full") |
| 509 | |
| 510 | monkeypatch.setattr(cli.env, "CONFIG_DIR", ExplodingPath()) |
| 511 | report = _report() |
| 512 | ok = cli._write_last_run("topic", report) |
| 513 | assert ok is False |
| 514 | assert "could not write run cache" in capsys.readouterr().err |
| 515 | |
| 516 | |
| 517 | def test_drill_gates_subreddit_context_on_source_allowlist(monkeypatch): |
| 518 | import io |
| 519 | from contextlib import redirect_stdout, redirect_stderr |
| 520 | from unittest import mock |
| 521 | import last30days as cli |
| 522 | from lib import schema |
| 523 | |
| 524 | report = _report() |
| 525 | # Force a non-Reddit single-source cluster and cached subreddit context. |
| 526 | report.artifacts["resolved"] = {"subreddits": ["LocalLLaMA", "MachineLearning"]} |
| 527 | for cluster in report.clusters: |
| 528 | cluster.sources = ["youtube"] |
| 529 | for candidate in report.ranked_candidates: |
| 530 | candidate.source = "youtube" |
| 531 | candidate.sources = ["youtube"] |
| 532 | for item in candidate.source_items: |
| 533 | item.source = "youtube" |
| 534 | |
| 535 | captured = {} |
| 536 | |
| 537 | def fake_run(**kwargs): |
| 538 | captured.update(kwargs) |
| 539 | return _report(drill=True) |
| 540 | |
| 541 | args = cli.build_parser().parse_args(["--drill", "cluster 1"]) |
| 542 | with mock.patch.object(cli, "_load_last_report_cache", return_value=(report, None, Path("/tmp/last-report.json"))), \ |
| 543 | mock.patch.object(cli.pipeline, "diagnose", return_value={}), \ |
| 544 | mock.patch.object(cli.pipeline, "run", side_effect=lambda **k: fake_run(**k)), \ |
| 545 | mock.patch.object(cli.pipeline, "merge_drill_report", side_effect=lambda r, d, c, target: r), \ |
| 546 | mock.patch.object(cli, "_write_last_run", return_value=True), \ |
| 547 | mock.patch.object(cli, "_show_runtime_ui"), \ |
| 548 | redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 549 | cli._run_drill(args, {}) |
| 550 | |
| 551 | assert "reddit" not in (captured.get("requested_sources") or []) |
| 552 | assert captured.get("subreddits") is None |
| 553 |