| 1 | # fmt: off |
| 2 | import contextlib |
| 3 | import json |
| 4 | import io |
| 5 | import os |
| 6 | import shutil |
| 7 | import tempfile |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import types |
| 11 | import unittest |
| 12 | from contextlib import redirect_stderr, redirect_stdout |
| 13 | from datetime import datetime |
| 14 | from pathlib import Path |
| 15 | from unittest import mock |
| 16 | |
| 17 | import last30days as cli |
| 18 | from lib import schema |
| 19 | |
| 20 | REPO_ROOT = Path(__file__).resolve().parents[1] |
| 21 | |
| 22 | |
| 23 | class CliV3Tests(unittest.TestCase): |
| 24 | def make_report(self, topic: str = "OpenClaw vs NanoClaw") -> schema.Report: |
| 25 | return schema.Report( |
| 26 | topic=topic, |
| 27 | range_from="2026-02-14", |
| 28 | range_to="2026-03-16", |
| 29 | generated_at="2026-03-16T00:00:00+00:00", |
| 30 | provider_runtime=schema.ProviderRuntime( |
| 31 | reasoning_provider="gemini", |
| 32 | planner_model="gemini-3.1-flash-lite", |
| 33 | rerank_model="gemini-3.1-flash-lite", |
| 34 | ), |
| 35 | query_plan=schema.QueryPlan( |
| 36 | intent="comparison", |
| 37 | freshness_mode="balanced_recent", |
| 38 | cluster_mode="debate", |
| 39 | raw_topic=topic, |
| 40 | subqueries=[ |
| 41 | schema.SubQuery( |
| 42 | label="primary", |
| 43 | search_query=topic.lower(), |
| 44 | ranking_query=f"What are people saying about {topic}?", |
| 45 | sources=["grounding"], |
| 46 | ) |
| 47 | ], |
| 48 | source_weights={"grounding": 1.0}, |
| 49 | ), |
| 50 | clusters=[], |
| 51 | ranked_candidates=[], |
| 52 | items_by_source={"grounding": []}, |
| 53 | errors_by_source={}, |
| 54 | ) |
| 55 | |
| 56 | def test_mock_json_cli(self): |
| 57 | result = subprocess.run( |
| 58 | [sys.executable, "skills/last30days/scripts/last30days.py", "test topic", "--mock", "--emit=json"], |
| 59 | cwd=REPO_ROOT, |
| 60 | capture_output=True, |
| 61 | text=True, |
| 62 | encoding="utf-8", |
| 63 | check=False, |
| 64 | ) |
| 65 | self.assertEqual(0, result.returncode, result.stderr) |
| 66 | payload = json.loads(result.stdout) |
| 67 | self.assertEqual("1.3", payload["schema_version"]) |
| 68 | self.assertEqual("test topic", payload["query"]) |
| 69 | self.assertIn("results", payload) |
| 70 | self.assertIn("clusters", payload) |
| 71 | self.assertIn("source_status", payload) |
| 72 | |
| 73 | def test_invalid_plan_json_exits_nonzero(self): |
| 74 | """Malformed --plan JSON must fail fast, not silently fall back to the |
| 75 | internal planner and burn a paid run the user did not ask for.""" |
| 76 | result = subprocess.run( |
| 77 | [ |
| 78 | sys.executable, |
| 79 | "skills/last30days/scripts/last30days.py", |
| 80 | "test topic", |
| 81 | "--mock", |
| 82 | "--emit=json", |
| 83 | "--plan", |
| 84 | "{not valid json", |
| 85 | ], |
| 86 | cwd=REPO_ROOT, |
| 87 | capture_output=True, |
| 88 | text=True, |
| 89 | encoding="utf-8", |
| 90 | check=False, |
| 91 | ) |
| 92 | self.assertEqual(2, result.returncode, result.stderr) |
| 93 | self.assertIn("Invalid --plan JSON", result.stderr) |
| 94 | |
| 95 | def test_invalid_plan_structure_exits_nonzero_without_fallback(self): |
| 96 | result = subprocess.run( |
| 97 | [ |
| 98 | sys.executable, |
| 99 | "skills/last30days/scripts/last30days.py", |
| 100 | "test topic", |
| 101 | "--mock", |
| 102 | "--emit=json", |
| 103 | "--plan", |
| 104 | json.dumps({"queries": {"web": ["Berlin"]}}), |
| 105 | ], |
| 106 | cwd=REPO_ROOT, |
| 107 | capture_output=True, |
| 108 | text=True, |
| 109 | encoding="utf-8", |
| 110 | check=False, |
| 111 | ) |
| 112 | self.assertEqual(2, result.returncode, result.stderr) |
| 113 | self.assertIn("Invalid --plan schema", result.stderr) |
| 114 | self.assertNotIn("fallback-plan", result.stderr) |
| 115 | |
| 116 | def test_parse_search_flag_normalizes_aliases_and_dedupes(self): |
| 117 | self.assertEqual( |
| 118 | ["grounding", "reddit", "hackernews"], |
| 119 | cli.parse_search_flag("web, reddit, hn, web"), |
| 120 | ) |
| 121 | |
| 122 | def test_parse_search_flag_accepts_optional_social_sources(self): |
| 123 | self.assertEqual( |
| 124 | ["threads", "pinterest"], |
| 125 | cli.parse_search_flag("threads, pinterest"), |
| 126 | ) |
| 127 | |
| 128 | def test_explicit_threads_search_uses_scrapecreators_key_without_include_sources(self): |
| 129 | available = cli.pipeline.available_sources( |
| 130 | {"SCRAPECREATORS_API_KEY": "test-key", "INCLUDE_SOURCES": ""}, |
| 131 | requested_sources=["threads"], |
| 132 | ) |
| 133 | self.assertIn("threads", available) |
| 134 | |
| 135 | def test_explicit_perplexity_search_uses_openrouter_fallback(self): |
| 136 | available = cli.pipeline.available_sources( |
| 137 | {"OPENROUTER_API_KEY": "test-key", "INCLUDE_SOURCES": ""}, |
| 138 | requested_sources=["perplexity"], |
| 139 | ) |
| 140 | self.assertIn("perplexity", available) |
| 141 | |
| 142 | def test_explicit_perplexity_search_uses_direct_key_without_include_sources(self): |
| 143 | available = cli.pipeline.available_sources( |
| 144 | {"PERPLEXITY_API_KEY": "test-key", "INCLUDE_SOURCES": ""}, |
| 145 | requested_sources=["perplexity"], |
| 146 | ) |
| 147 | self.assertIn("perplexity", available) |
| 148 | |
| 149 | def test_parse_search_flag_rejects_invalid_or_empty_inputs(self): |
| 150 | with self.assertRaises(SystemExit): |
| 151 | cli.parse_search_flag("unknown") |
| 152 | with self.assertRaises(SystemExit): |
| 153 | cli.parse_search_flag(" , ") |
| 154 | |
| 155 | def test_resolve_requested_sources_flag_wins_over_config_default(self): |
| 156 | sources = cli.resolve_requested_sources( |
| 157 | "reddit", {"LAST30DAYS_DEFAULT_SEARCH": "x,youtube"}, |
| 158 | ) |
| 159 | self.assertEqual(["reddit"], sources) |
| 160 | |
| 161 | def test_resolve_requested_sources_falls_back_to_config_default(self): |
| 162 | sources = cli.resolve_requested_sources( |
| 163 | None, {"LAST30DAYS_DEFAULT_SEARCH": "web, reddit, hn"}, |
| 164 | ) |
| 165 | self.assertEqual(["grounding", "reddit", "hackernews"], sources) |
| 166 | |
| 167 | def test_resolve_requested_sources_none_when_neither_set(self): |
| 168 | self.assertIsNone(cli.resolve_requested_sources(None, {})) |
| 169 | self.assertIsNone( |
| 170 | cli.resolve_requested_sources(None, {"LAST30DAYS_DEFAULT_SEARCH": ""}) |
| 171 | ) |
| 172 | self.assertIsNone( |
| 173 | cli.resolve_requested_sources(None, {"LAST30DAYS_DEFAULT_SEARCH": " "}) |
| 174 | ) |
| 175 | |
| 176 | def test_resolve_requested_sources_invalid_config_default_names_env_var(self): |
| 177 | with self.assertRaises(SystemExit) as exc: |
| 178 | cli.resolve_requested_sources( |
| 179 | None, {"LAST30DAYS_DEFAULT_SEARCH": "notasource"}, |
| 180 | ) |
| 181 | self.assertIn("LAST30DAYS_DEFAULT_SEARCH", str(exc.exception)) |
| 182 | |
| 183 | def test_deep_research_preserves_default_source_selection(self): |
| 184 | self.assertIsNone(cli.add_deep_research_source(None)) |
| 185 | |
| 186 | def test_deep_research_extends_an_explicit_source_selection(self): |
| 187 | self.assertEqual( |
| 188 | ["reddit", "perplexity"], |
| 189 | cli.add_deep_research_source(["reddit"]), |
| 190 | ) |
| 191 | self.assertEqual( |
| 192 | ["reddit", "perplexity"], |
| 193 | cli.add_deep_research_source(["reddit", "perplexity"]), |
| 194 | ) |
| 195 | |
| 196 | def test_deep_research_enables_exact_include_token(self): |
| 197 | config = {"INCLUDE_SOURCES": "notperplexity,reddit"} |
| 198 | |
| 199 | cli.enable_deep_research_source(config) |
| 200 | |
| 201 | self.assertEqual( |
| 202 | ["notperplexity", "reddit", "perplexity"], |
| 203 | config["INCLUDE_SOURCES"].split(","), |
| 204 | ) |
| 205 | |
| 206 | def test_deep_research_rejects_exact_exclusion(self): |
| 207 | config = {"EXCLUDE_SOURCES": "reddit,Perplexity"} |
| 208 | |
| 209 | with self.assertRaisesRegex( |
| 210 | ValueError, |
| 211 | "conflicts with EXCLUDE_SOURCES=perplexity", |
| 212 | ): |
| 213 | cli.enable_deep_research_source(config) |
| 214 | |
| 215 | def test_build_parser_accepts_days_alias_and_preserves_topic_tokens(self): |
| 216 | parser = cli.build_parser() |
| 217 | args, extra = parser.parse_known_args(["--days", "7", "biosecurity", "ai", "agents"]) |
| 218 | self.assertEqual(7, args.lookback_days) |
| 219 | self.assertEqual(["biosecurity", "ai", "agents"], args.topic) |
| 220 | self.assertEqual([], extra) |
| 221 | |
| 222 | def test_build_parser_accepts_web_backend_keyless(self): |
| 223 | """Regression for #905: CONFIGURATION.md documents --web-backend=keyless |
| 224 | to force the zero-key floor, but the choices list rejected it.""" |
| 225 | parser = cli.build_parser() |
| 226 | args, extra = parser.parse_known_args(["--web-backend", "keyless", "biosecurity"]) |
| 227 | self.assertEqual("keyless", args.web_backend) |
| 228 | self.assertEqual([], extra) |
| 229 | |
| 230 | def test_deep_research_help_keeps_openrouter_fallback(self): |
| 231 | parser = cli.build_parser() |
| 232 | action = next( |
| 233 | candidate |
| 234 | for candidate in parser._actions |
| 235 | if "--deep-research" in candidate.option_strings |
| 236 | ) |
| 237 | self.assertIn("PERPLEXITY_API_KEY", action.help) |
| 238 | self.assertIn("OPENROUTER_API_KEY", action.help) |
| 239 | self.assertIn("cannot be combined with competitor or vs-mode", action.help) |
| 240 | |
| 241 | def test_deep_research_rejects_modes_without_a_positional_topic(self): |
| 242 | for argv in ( |
| 243 | ["last30days.py", "--discover", "agents", "--deep-research"], |
| 244 | ["last30days.py", "--drill", "cluster-1", "--deep-research"], |
| 245 | ): |
| 246 | with self.subTest(argv=argv), mock.patch.object( |
| 247 | cli.env, |
| 248 | "get_config", |
| 249 | return_value={}, |
| 250 | ), mock.patch.object( |
| 251 | cli, |
| 252 | "_run_discover", |
| 253 | ) as discover_mock, mock.patch.object( |
| 254 | cli, |
| 255 | "_run_drill", |
| 256 | ) as drill_mock, mock.patch.dict( |
| 257 | os.environ, |
| 258 | {"LAST30DAYS_SKIP_PREFLIGHT": "1"}, |
| 259 | clear=False, |
| 260 | ), mock.patch.object(sys, "argv", argv): |
| 261 | stderr = io.StringIO() |
| 262 | with redirect_stderr(stderr): |
| 263 | rc = cli.main() |
| 264 | |
| 265 | self.assertEqual(2, rc) |
| 266 | discover_mock.assert_not_called() |
| 267 | drill_mock.assert_not_called() |
| 268 | self.assertIn("requires a normal positional topic", stderr.getvalue()) |
| 269 | |
| 270 | def test_deep_research_rejects_competitor_fanout_before_pipeline_run(self): |
| 271 | diag = { |
| 272 | "available_sources": ["perplexity"], |
| 273 | "providers": {"google": False, "openai": False, "xai": False}, |
| 274 | "x_backend": None, |
| 275 | "bird_installed": True, |
| 276 | "bird_authenticated": False, |
| 277 | "bird_username": None, |
| 278 | "native_web_backend": None, |
| 279 | } |
| 280 | with mock.patch.object( |
| 281 | cli.env, |
| 282 | "get_config", |
| 283 | return_value={"PERPLEXITY_API_KEY": "pplx-test"}, |
| 284 | ), mock.patch.object( |
| 285 | cli.pipeline, |
| 286 | "diagnose", |
| 287 | return_value=diag, |
| 288 | ) as diagnose_mock, mock.patch.object( |
| 289 | cli.pipeline, |
| 290 | "run", |
| 291 | ) as run_mock, mock.patch.object( |
| 292 | cli.ui, |
| 293 | "ProgressDisplay", |
| 294 | return_value=mock.Mock(), |
| 295 | ), mock.patch.object( |
| 296 | sys, |
| 297 | "argv", |
| 298 | [ |
| 299 | "last30days.py", |
| 300 | "Alpha", |
| 301 | "vs", |
| 302 | "Beta", |
| 303 | "--mock", |
| 304 | "--deep-research", |
| 305 | ], |
| 306 | ): |
| 307 | stderr = io.StringIO() |
| 308 | with redirect_stderr(stderr): |
| 309 | rc = cli.main() |
| 310 | |
| 311 | self.assertEqual(2, rc) |
| 312 | diagnose_mock.assert_not_called() |
| 313 | run_mock.assert_not_called() |
| 314 | self.assertIn( |
| 315 | "one paid Deep Research run per user action", |
| 316 | stderr.getvalue(), |
| 317 | ) |
| 318 | |
| 319 | def test_openrouter_deep_research_bypasses_hosted_and_adds_source(self): |
| 320 | report = self.make_report(topic="why AI safety matters") |
| 321 | diag = { |
| 322 | "available_sources": ["reddit", "perplexity"], |
| 323 | "providers": {"google": False, "openai": False, "xai": False}, |
| 324 | "x_backend": None, |
| 325 | "bird_installed": True, |
| 326 | "bird_authenticated": False, |
| 327 | "bird_username": None, |
| 328 | "native_web_backend": None, |
| 329 | } |
| 330 | with mock.patch.object( |
| 331 | cli.env, |
| 332 | "get_config", |
| 333 | return_value={"OPENROUTER_API_KEY": "openrouter-test"}, |
| 334 | ), mock.patch.object( |
| 335 | cli.env, |
| 336 | "read_secret_env", |
| 337 | return_value="hosted-test-key", |
| 338 | ), mock.patch.object( |
| 339 | cli.pipeline, |
| 340 | "diagnose", |
| 341 | return_value=diag, |
| 342 | ), mock.patch.object( |
| 343 | cli.pipeline, |
| 344 | "run", |
| 345 | return_value=report, |
| 346 | ) as run_mock, mock.patch( |
| 347 | "lib.hosted.run_hosted", |
| 348 | ) as hosted_mock, mock.patch.object( |
| 349 | cli.ui, |
| 350 | "ProgressDisplay", |
| 351 | return_value=mock.Mock(), |
| 352 | ), mock.patch.object( |
| 353 | cli, |
| 354 | "emit_output", |
| 355 | return_value="# rendered", |
| 356 | ), mock.patch.dict( |
| 357 | os.environ, |
| 358 | { |
| 359 | "LAST30DAYS_API_BASE": "https://hosted.example.test", |
| 360 | "LAST30DAYS_SKIP_PREFLIGHT": "1", |
| 361 | }, |
| 362 | clear=False, |
| 363 | ), mock.patch.object( |
| 364 | sys, |
| 365 | "argv", |
| 366 | [ |
| 367 | "last30days.py", |
| 368 | "why", |
| 369 | "AI", |
| 370 | "safety", |
| 371 | "matters", |
| 372 | "--deep-research", |
| 373 | "--search", |
| 374 | "reddit", |
| 375 | ], |
| 376 | ): |
| 377 | with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 378 | rc = cli.main() |
| 379 | |
| 380 | self.assertEqual(0, rc) |
| 381 | hosted_mock.assert_not_called() |
| 382 | requested_sources = run_mock.call_args.kwargs["requested_sources"] |
| 383 | self.assertEqual(["reddit", "perplexity"], requested_sources) |
| 384 | self.assertTrue(run_mock.call_args.kwargs["config"]["_deep_research"]) |
| 385 | |
| 386 | def test_build_parser_still_accepts_other_web_backend_values(self): |
| 387 | parser = cli.build_parser() |
| 388 | for value in ("auto", "brave", "exa", "serper", "parallel", "parallel-mcp", "none"): |
| 389 | args, extra = parser.parse_known_args(["--web-backend", value, "biosecurity"]) |
| 390 | self.assertEqual(value, args.web_backend) |
| 391 | self.assertEqual([], extra) |
| 392 | |
| 393 | def test_build_parser_rejects_invalid_web_backend(self): |
| 394 | parser = cli.build_parser() |
| 395 | with self.assertRaises(SystemExit): |
| 396 | parser.parse_known_args(["--web-backend", "bogus", "biosecurity"]) |
| 397 | |
| 398 | def test_build_parser_accepts_explicit_output_file(self): |
| 399 | parser = cli.build_parser() |
| 400 | args, extra = parser.parse_known_args( |
| 401 | ["--emit", "json", "--output", "results/run.json", "biosecurity"] |
| 402 | ) |
| 403 | self.assertEqual("results/run.json", args.output) |
| 404 | self.assertEqual(["biosecurity"], args.topic) |
| 405 | self.assertEqual([], extra) |
| 406 | |
| 407 | def test_build_parser_accepts_result_cap_overrides(self): |
| 408 | parser = cli.build_parser() |
| 409 | args, extra = parser.parse_known_args( |
| 410 | ["--max-results", "200", "--max-per-source", "60", |
| 411 | "--max-source-fetches", "8", "figma config 2026"] |
| 412 | ) |
| 413 | self.assertEqual(200, args.max_results) |
| 414 | self.assertEqual(60, args.max_per_source) |
| 415 | self.assertEqual(8, args.max_source_fetches) |
| 416 | self.assertEqual(["figma config 2026"], args.topic) |
| 417 | self.assertEqual([], extra) |
| 418 | |
| 419 | def test_result_cap_overrides_default_to_none(self): |
| 420 | parser = cli.build_parser() |
| 421 | args, _ = parser.parse_known_args(["figma config 2026"]) |
| 422 | self.assertIsNone(args.max_results) |
| 423 | self.assertIsNone(args.max_per_source) |
| 424 | self.assertIsNone(args.max_source_fetches) |
| 425 | |
| 426 | def test_research_unknown_flag_fails_before_config_load(self): |
| 427 | with mock.patch.object( |
| 428 | cli.env, "get_config", side_effect=AssertionError("config should not load") |
| 429 | ), mock.patch.object(sys, "argv", ["last30days.py", "topic", "--save"]): |
| 430 | stderr = io.StringIO() |
| 431 | with redirect_stderr(stderr), self.assertRaises(SystemExit) as exc: |
| 432 | cli.main() |
| 433 | self.assertEqual(2, exc.exception.code) |
| 434 | self.assertIn("--save", stderr.getvalue()) |
| 435 | |
| 436 | def test_agent_is_skill_argument_not_python_cli_flag(self): |
| 437 | with mock.patch.object( |
| 438 | cli.env, "get_config", side_effect=AssertionError("config should not load") |
| 439 | ), mock.patch.object(sys, "argv", ["last30days.py", "topic", "--agent"]): |
| 440 | stderr = io.StringIO() |
| 441 | with redirect_stderr(stderr), self.assertRaises(SystemExit) as exc: |
| 442 | cli.main() |
| 443 | self.assertEqual(2, exc.exception.code) |
| 444 | self.assertIn("skill arguments", stderr.getvalue()) |
| 445 | |
| 446 | def test_agent_error_includes_other_unknown_flags(self): |
| 447 | with mock.patch.object( |
| 448 | cli.env, "get_config", side_effect=AssertionError("config should not load") |
| 449 | ), mock.patch.object(sys, "argv", ["last30days.py", "topic", "--agent", "--save"]): |
| 450 | stderr = io.StringIO() |
| 451 | with redirect_stderr(stderr), self.assertRaises(SystemExit) as exc: |
| 452 | cli.main() |
| 453 | self.assertEqual(2, exc.exception.code) |
| 454 | message = stderr.getvalue() |
| 455 | self.assertIn("--agent", message) |
| 456 | self.assertIn("--save", message) |
| 457 | |
| 458 | def test_setup_passthrough_flags_remain_scoped_to_setup(self): |
| 459 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 460 | mock.patch("lib.setup_wizard.run_github_auth", return_value={"status": "cancelled"}), \ |
| 461 | mock.patch.object(sys, "argv", ["last30days.py", "setup", "--github"]): |
| 462 | stdout = io.StringIO() |
| 463 | stderr = io.StringIO() |
| 464 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 465 | rc = cli.main() |
| 466 | self.assertEqual(0, rc) |
| 467 | |
| 468 | def test_setup_rejects_unknown_passthrough_flag_before_config_load(self): |
| 469 | with mock.patch.object( |
| 470 | cli.env, "get_config", side_effect=AssertionError("config should not load") |
| 471 | ), mock.patch.object(sys, "argv", ["last30days.py", "setup", "--bad"]): |
| 472 | stderr = io.StringIO() |
| 473 | with redirect_stderr(stderr), self.assertRaises(SystemExit) as exc: |
| 474 | cli.main() |
| 475 | self.assertEqual(2, exc.exception.code) |
| 476 | self.assertIn("--bad", stderr.getvalue()) |
| 477 | |
| 478 | def test_ensure_supported_python_rejects_old_interpreter_with_actionable_error(self): |
| 479 | stderr = io.StringIO() |
| 480 | with redirect_stderr(stderr): |
| 481 | with self.assertRaises(SystemExit) as exc: |
| 482 | cli.ensure_supported_python((3, 9, 6)) |
| 483 | self.assertEqual(1, exc.exception.code) |
| 484 | message = stderr.getvalue() |
| 485 | self.assertIn("last30days v3 requires Python 3.12+", message) |
| 486 | self.assertIn("Detected Python 3.9.6", message) |
| 487 | self.assertIn("python3.12", message) |
| 488 | |
| 489 | def test_ensure_supported_python_allows_supported_interpreter(self): |
| 490 | cli.ensure_supported_python((3, 12, 0)) |
| 491 | |
| 492 | def test_missing_sources_for_promo_treats_x_as_optional(self): |
| 493 | self.assertEqual( |
| 494 | "reddit", |
| 495 | cli._missing_sources_for_promo({"available_sources": ["youtube"]}), |
| 496 | ) |
| 497 | self.assertEqual( |
| 498 | "web", |
| 499 | cli._missing_sources_for_promo({"available_sources": ["reddit", "x"]}), |
| 500 | ) |
| 501 | # The web promo is satisfied by a paid backend (better web search), not |
| 502 | # by the keyless grounding floor — keyless web is always available now. |
| 503 | self.assertIsNone( |
| 504 | cli._missing_sources_for_promo( |
| 505 | {"available_sources": ["reddit", "x", "grounding"], "native_web_backend": "brave"} |
| 506 | ), |
| 507 | ) |
| 508 | |
| 509 | def test_optional_x_omission_is_post_result_copy_for_default_runs(self): |
| 510 | note = cli._optional_x_omission_text( |
| 511 | {"available_sources": ["reddit", "youtube", "grounding"]}, |
| 512 | None, |
| 513 | ) |
| 514 | self.assertEqual( |
| 515 | "Optional source omitted: X/Twitter was not enabled; research " |
| 516 | "continued with the available sources.", |
| 517 | note, |
| 518 | ) |
| 519 | |
| 520 | def test_optional_x_omission_is_suppressed_when_x_active_or_search_explicit(self): |
| 521 | self.assertIsNone( |
| 522 | cli._optional_x_omission_text( |
| 523 | {"available_sources": ["reddit", "x", "youtube"]}, |
| 524 | None, |
| 525 | ) |
| 526 | ) |
| 527 | self.assertIsNone( |
| 528 | cli._optional_x_omission_text( |
| 529 | {"available_sources": ["reddit", "youtube"]}, |
| 530 | ["reddit", "youtube"], |
| 531 | ) |
| 532 | ) |
| 533 | # ...or suppressed entirely on a native-search host. |
| 534 | self.assertIsNone( |
| 535 | cli._missing_sources_for_promo( |
| 536 | {"available_sources": ["reddit", "x", "grounding"], "native_search": True} |
| 537 | ), |
| 538 | ) |
| 539 | |
| 540 | def test_slugify_and_emit_output_cover_supported_modes(self): |
| 541 | report = self.make_report() |
| 542 | self.assertEqual("openclaw-vs-nanoclaw", cli.slugify(report.topic)) |
| 543 | self.assertEqual("last30days CLI.", cli.__doc__) |
| 544 | |
| 545 | compact = cli.emit_output(report, "compact") |
| 546 | json_output = cli.emit_output(report, "json") |
| 547 | context = cli.emit_output(report, "context") |
| 548 | brief = cli.emit_output(report, "brief") |
| 549 | |
| 550 | self.assertIn("# last30days v", compact) |
| 551 | self.assertIn('"query": "OpenClaw vs NanoClaw"', json_output) |
| 552 | self.assertIsInstance(context, str) |
| 553 | self.assertIn("# Production Brief:", brief) |
| 554 | |
| 555 | with self.assertRaises(SystemExit): |
| 556 | cli.emit_output(report, "bad-mode") |
| 557 | |
| 558 | def test_save_output_writes_expected_extension(self): |
| 559 | report = self.make_report() |
| 560 | with tempfile.TemporaryDirectory() as tmp: |
| 561 | path = cli.save_output(report, "json", tmp) |
| 562 | self.assertEqual(".json", path.suffix) |
| 563 | payload = json.loads(path.read_text()) |
| 564 | self.assertEqual("OpenClaw vs NanoClaw", payload["query"]) |
| 565 | |
| 566 | def test_compact_emit_saves_full_artifact_not_compact_render(self): |
| 567 | """A --emit=compact --save-dir run must save the complete debug |
| 568 | artifact (all clusters plus per-source items), not the compact stdout |
| 569 | render. Saving the compact render made most collected evidence |
| 570 | unrecoverable from the raw file (#923).""" |
| 571 | with tempfile.TemporaryDirectory() as tmp: |
| 572 | result = subprocess.run( |
| 573 | [ |
| 574 | sys.executable, |
| 575 | "skills/last30days/scripts/last30days.py", |
| 576 | "compact save probe", |
| 577 | "--mock", |
| 578 | "--emit=compact", |
| 579 | f"--save-dir={tmp}", |
| 580 | ], |
| 581 | cwd=REPO_ROOT, |
| 582 | capture_output=True, |
| 583 | text=True, |
| 584 | encoding="utf-8", |
| 585 | check=False, |
| 586 | ) |
| 587 | self.assertEqual(0, result.returncode, result.stderr) |
| 588 | saved = list(Path(tmp).glob("*.md")) |
| 589 | self.assertEqual(1, len(saved), saved) |
| 590 | content = saved[0].read_text(encoding="utf-8") |
| 591 | self.assertIn("## All Items by Source", content) |
| 592 | self.assertNotIn("## All Items by Source", result.stdout) |
| 593 | |
| 594 | def test_save_output_uses_unique_dated_fallback(self): |
| 595 | report = self.make_report() |
| 596 | with tempfile.TemporaryDirectory() as tmp: |
| 597 | save_dir = Path(tmp) |
| 598 | today = datetime.now().strftime("%Y-%m-%d") |
| 599 | base = save_dir / "openclaw-vs-nanoclaw-raw.md" |
| 600 | dated = save_dir / f"openclaw-vs-nanoclaw-raw-{today}.md" |
| 601 | base.write_text("base content", encoding="utf-8") |
| 602 | dated.write_text("dated content", encoding="utf-8") |
| 603 | |
| 604 | saved = cli.save_output(report, "md", tmp) |
| 605 | |
| 606 | self.assertEqual((save_dir / f"openclaw-vs-nanoclaw-raw-{today}-1.md").resolve(), saved) |
| 607 | self.assertEqual("base content", base.read_text(encoding="utf-8")) |
| 608 | self.assertEqual("dated content", dated.read_text(encoding="utf-8")) |
| 609 | self.assertTrue(saved.exists()) |
| 610 | |
| 611 | def test_save_output_render_fn_footer_names_actual_collision_path(self): |
| 612 | from lib import render as render_module |
| 613 | |
| 614 | report = self.make_report() |
| 615 | with tempfile.TemporaryDirectory() as tmp: |
| 616 | save_dir = Path(tmp) |
| 617 | today = datetime.now().strftime("%Y-%m-%d") |
| 618 | base = save_dir / "openclaw-vs-nanoclaw-raw.md" |
| 619 | dated = save_dir / f"openclaw-vs-nanoclaw-raw-{today}.md" |
| 620 | base.write_text("base content", encoding="utf-8") |
| 621 | dated.write_text("dated content", encoding="utf-8") |
| 622 | |
| 623 | def render_fn(actual_path: Path) -> str: |
| 624 | return render_module.render_compact(report, save_path=str(actual_path)) |
| 625 | |
| 626 | saved = cli.save_output(report, "md", tmp, render_fn=render_fn) |
| 627 | |
| 628 | expected = (save_dir / f"openclaw-vs-nanoclaw-raw-{today}-1.md").resolve() |
| 629 | self.assertEqual(expected, saved.resolve()) |
| 630 | content = saved.read_text(encoding="utf-8") |
| 631 | self.assertIn(f"Raw results saved to {saved}", content) |
| 632 | self.assertNotIn(f"Raw results saved to {base}", content) |
| 633 | self.assertNotIn(f"Raw results saved to {dated}", content) |
| 634 | self.assertEqual("base content", base.read_text(encoding="utf-8")) |
| 635 | self.assertEqual("dated content", dated.read_text(encoding="utf-8")) |
| 636 | |
| 637 | def test_save_output_removes_reserved_candidate_when_deferred_render_fails(self): |
| 638 | report = self.make_report() |
| 639 | with tempfile.TemporaryDirectory() as tmp: |
| 640 | save_dir = Path(tmp) |
| 641 | |
| 642 | def fail_render(_actual_path: Path) -> str: |
| 643 | raise RuntimeError("render failed") |
| 644 | |
| 645 | with self.assertRaisesRegex(RuntimeError, "render failed"): |
| 646 | cli.save_output(report, "md", tmp, render_fn=fail_render) |
| 647 | |
| 648 | self.assertEqual([], list(save_dir.iterdir())) |
| 649 | |
| 650 | def test_render_save_and_print_uses_actual_collision_path_in_file_and_stdout(self): |
| 651 | report = self.make_report(topic="Collision Topic") |
| 652 | with tempfile.TemporaryDirectory() as tmp: |
| 653 | save_dir = Path(tmp) |
| 654 | today = datetime.now().strftime("%Y-%m-%d") |
| 655 | base = save_dir / "collision-topic-raw.md" |
| 656 | dated = save_dir / f"collision-topic-raw-{today}.md" |
| 657 | expected = save_dir / f"collision-topic-raw-{today}-1.md" |
| 658 | base.write_text("base content", encoding="utf-8") |
| 659 | dated.write_text("dated content", encoding="utf-8") |
| 660 | args = types.SimpleNamespace( |
| 661 | topic=["Collision Topic"], |
| 662 | competitors=None, |
| 663 | competitors_list=None, |
| 664 | competitors_plan=None, |
| 665 | drill=False, |
| 666 | register=None, |
| 667 | emit="compact", |
| 668 | output=None, |
| 669 | save_dir=str(save_dir), |
| 670 | save_suffix="", |
| 671 | json_profile="agent", |
| 672 | publish_html=False, |
| 673 | ) |
| 674 | stdout = io.StringIO() |
| 675 | stderr = io.StringIO() |
| 676 | |
| 677 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 678 | rc = cli._render_save_and_print(args, report, None, None, {}) |
| 679 | |
| 680 | self.assertEqual(0, rc) |
| 681 | expected_display = cli.compute_output_path_display(str(expected)) |
| 682 | footer = f"Raw results saved to {expected_display}" |
| 683 | self.assertTrue(expected.exists()) |
| 684 | self.assertIn(footer, expected.read_text(encoding="utf-8")) |
| 685 | self.assertIn(footer, stdout.getvalue()) |
| 686 | self.assertEqual("base content", base.read_text(encoding="utf-8")) |
| 687 | self.assertEqual("dated content", dated.read_text(encoding="utf-8")) |
| 688 | |
| 689 | def test_save_output_writes_utf8_encoded_markdown(self): |
| 690 | report = self.make_report() |
| 691 | with tempfile.TemporaryDirectory() as tmp: |
| 692 | path = cli.save_output(report, "md", tmp) |
| 693 | raw = path.read_bytes() |
| 694 | content = path.read_text(encoding="utf-8") |
| 695 | self.assertIn(report.topic, content) |
| 696 | # Verify the raw bytes decode cleanly as UTF-8. |
| 697 | self.assertEqual(content, raw.decode("utf-8")) |
| 698 | |
| 699 | def test_save_rendered_output_writes_exact_file_path(self): |
| 700 | with tempfile.TemporaryDirectory() as tmp: |
| 701 | out_path = Path(tmp) / "nested" / "results.json" |
| 702 | saved = cli.save_rendered_output('{"ok": true}', str(out_path)) |
| 703 | self.assertEqual(out_path.resolve(), saved) |
| 704 | self.assertEqual('{"ok": true}', out_path.read_text(encoding="utf-8")) |
| 705 | |
| 706 | def test_compute_save_path_display_uses_posix_slashes_under_home(self): |
| 707 | # Regression: f"~/{relative}" stringified pathlib.Path with the |
| 708 | # OS-native separator, producing "~/Documents\\Last30Days\\..." on |
| 709 | # Windows that no shell or File Explorer could open. The fix is |
| 710 | # f"~/{relative.as_posix()}" which forces forward slashes regardless |
| 711 | # of host OS. On POSIX hosts this asserts the contract for |
| 712 | # cross-platform safety; on Windows hosts it would fail without the fix. |
| 713 | real_home = Path.home() |
| 714 | tmp_under_home = Path(tempfile.mkdtemp(prefix="l30d_save_path_", dir=str(real_home))) |
| 715 | try: |
| 716 | save_dir = tmp_under_home / "Documents" / "Last30Days" |
| 717 | save_dir.mkdir(parents=True, exist_ok=True) |
| 718 | display = cli.compute_save_path_display( |
| 719 | str(save_dir), "british airways middle east", "v3", "compact" |
| 720 | ) |
| 721 | self.assertTrue(display.startswith("~/"), f"Expected '~/' prefix, got: {display}") |
| 722 | self.assertNotIn("\\", display, f"Backslash leaked into display: {display}") |
| 723 | self.assertTrue( |
| 724 | display.endswith("british-airways-middle-east-raw-v3.md"), |
| 725 | f"Expected slug+suffix at end, got: {display}", |
| 726 | ) |
| 727 | finally: |
| 728 | shutil.rmtree(tmp_under_home, ignore_errors=True) |
| 729 | |
| 730 | def test_compute_output_path_display_uses_posix_slashes_under_home(self): |
| 731 | real_home = Path.home() |
| 732 | tmp_under_home = Path(tempfile.mkdtemp(prefix="l30d_output_path_", dir=str(real_home))) |
| 733 | try: |
| 734 | output_path = tmp_under_home / "Documents" / "Last30Days" / "run.json" |
| 735 | display = cli.compute_output_path_display(str(output_path)) |
| 736 | self.assertTrue(display.startswith("~/"), f"Expected '~/' prefix, got: {display}") |
| 737 | self.assertNotIn("\\", display, f"Backslash leaked into display: {display}") |
| 738 | self.assertTrue(display.endswith("Documents/Last30Days/run.json"), display) |
| 739 | finally: |
| 740 | shutil.rmtree(tmp_under_home, ignore_errors=True) |
| 741 | |
| 742 | def test_persist_report_updates_run_status_on_success_and_failure(self): |
| 743 | report = self.make_report() |
| 744 | |
| 745 | success_store = types.SimpleNamespace( |
| 746 | scoped_db=lambda _path: contextlib.nullcontext(), |
| 747 | init_db=mock.Mock(), |
| 748 | add_topic=mock.Mock(return_value={"id": 7}), |
| 749 | record_run=mock.Mock(return_value=11), |
| 750 | findings_from_report=mock.Mock(return_value=[{"title": "x"}]), |
| 751 | store_findings=mock.Mock(return_value={"new": 2, "updated": 1}), |
| 752 | update_run=mock.Mock(), |
| 753 | ) |
| 754 | with mock.patch.dict(sys.modules, {"store": success_store}): |
| 755 | counts = cli.persist_report(report) |
| 756 | self.assertEqual({"new": 2, "updated": 1}, counts) |
| 757 | success_store.update_run.assert_called_once_with( |
| 758 | 11, |
| 759 | status="completed", |
| 760 | findings_new=2, |
| 761 | findings_updated=1, |
| 762 | ) |
| 763 | |
| 764 | failure_store = types.SimpleNamespace( |
| 765 | scoped_db=lambda _path: contextlib.nullcontext(), |
| 766 | init_db=mock.Mock(), |
| 767 | add_topic=mock.Mock(return_value={"id": 7}), |
| 768 | record_run=mock.Mock(return_value=12), |
| 769 | findings_from_report=mock.Mock(side_effect=RuntimeError("boom")), |
| 770 | store_findings=mock.Mock(), |
| 771 | update_run=mock.Mock(), |
| 772 | ) |
| 773 | with mock.patch.dict(sys.modules, {"store": failure_store}): |
| 774 | with self.assertRaises(RuntimeError): |
| 775 | cli.persist_report(report) |
| 776 | failure_store.update_run.assert_called_once() |
| 777 | _, kwargs = failure_store.update_run.call_args |
| 778 | self.assertEqual("failed", kwargs["status"]) |
| 779 | self.assertIn("boom", kwargs["error_message"]) |
| 780 | |
| 781 | def test_main_wires_banner_and_progress_display(self): |
| 782 | report = self.make_report() |
| 783 | diag = { |
| 784 | "available_sources": ["grounding", "youtube"], |
| 785 | "providers": {"google": True, "openai": False, "xai": False}, |
| 786 | "x_backend": None, |
| 787 | "bird_installed": True, |
| 788 | "bird_authenticated": False, |
| 789 | "bird_username": None, |
| 790 | "native_web_backend": "brave", |
| 791 | } |
| 792 | fake_progress = mock.Mock() |
| 793 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 794 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 795 | mock.patch.object(cli.pipeline, "run", return_value=report), \ |
| 796 | mock.patch.object(cli.ui, "show_diagnostic_banner") as banner, \ |
| 797 | mock.patch.object(cli.ui, "ProgressDisplay", return_value=fake_progress) as progress_cls, \ |
| 798 | mock.patch.object(cli, "emit_output", return_value="# rendered"), \ |
| 799 | mock.patch.object(sys, "argv", ["last30days.py", "test", "topic"]): |
| 800 | stdout = io.StringIO() |
| 801 | stderr = io.StringIO() |
| 802 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 803 | rc = cli.main() |
| 804 | self.assertEqual(0, rc) |
| 805 | banner.assert_not_called() # Banner moved to post-research |
| 806 | progress_cls.assert_called_once_with("test topic", show_banner=True) |
| 807 | fake_progress.start_processing.assert_called_once() |
| 808 | fake_progress.end_processing.assert_called_once() |
| 809 | fake_progress.show_complete.assert_called_once_with( |
| 810 | source_counts={"grounding": 0}, |
| 811 | display_sources=["grounding"], |
| 812 | ) |
| 813 | fake_progress.show_promo.assert_called_once_with("reddit", diag=diag) |
| 814 | self.assertIn("# rendered", stdout.getvalue()) |
| 815 | |
| 816 | def test_main_writes_rendered_output_to_explicit_file(self): |
| 817 | report = self.make_report() |
| 818 | diag = { |
| 819 | "available_sources": ["grounding"], |
| 820 | "providers": {"google": True, "openai": False, "xai": False}, |
| 821 | "x_backend": None, |
| 822 | "bird_installed": True, |
| 823 | "bird_authenticated": False, |
| 824 | "bird_username": None, |
| 825 | "native_web_backend": "brave", |
| 826 | } |
| 827 | with tempfile.TemporaryDirectory() as tmp: |
| 828 | output_path = Path(tmp) / "exports" / "run.json" |
| 829 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 830 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 831 | mock.patch.object(cli.pipeline, "run", return_value=report), \ |
| 832 | mock.patch.object(cli, "emit_output", return_value='{"rendered": true}') as emit, \ |
| 833 | mock.patch.object(sys, "argv", [ |
| 834 | "last30days.py", |
| 835 | "test", |
| 836 | "topic", |
| 837 | "--emit=json", |
| 838 | "--output", |
| 839 | str(output_path), |
| 840 | ]): |
| 841 | stdout = io.StringIO() |
| 842 | stderr = io.StringIO() |
| 843 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 844 | rc = cli.main() |
| 845 | self.assertEqual(0, rc) |
| 846 | emit.assert_called_once() |
| 847 | self.assertEqual('{"rendered": true}\n', stdout.getvalue()) |
| 848 | self.assertEqual('{"rendered": true}', output_path.read_text(encoding="utf-8")) |
| 849 | self.assertIn(f"[last30days] Saved output to {output_path.resolve()}", stderr.getvalue()) |
| 850 | |
| 851 | def test_main_combines_output_and_save_dir_for_comparison_html(self): |
| 852 | diag = { |
| 853 | "available_sources": ["grounding"], |
| 854 | "providers": {"google": True, "openai": False, "xai": False}, |
| 855 | "x_backend": None, |
| 856 | "bird_installed": True, |
| 857 | "bird_authenticated": False, |
| 858 | "bird_username": None, |
| 859 | "native_web_backend": "brave", |
| 860 | } |
| 861 | fake_progress = mock.Mock() |
| 862 | |
| 863 | def run_report(*_args, **kwargs): |
| 864 | return self.make_report(topic=kwargs["topic"]) |
| 865 | |
| 866 | with tempfile.TemporaryDirectory() as tmp: |
| 867 | output_path = Path(tmp) / "exports" / "comparison.html" |
| 868 | save_dir = Path(tmp) / "saved" |
| 869 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 870 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 871 | mock.patch.object(cli.pipeline, "run", side_effect=run_report), \ |
| 872 | mock.patch.object(cli.ui, "ProgressDisplay", return_value=fake_progress), \ |
| 873 | mock.patch.object( |
| 874 | cli, "emit_comparison_output", return_value="<html>comparison</html>" |
| 875 | ) as emit_comparison, \ |
| 876 | mock.patch.object(cli, "emit_output", return_value="<html>peer</html>"), \ |
| 877 | mock.patch.object(sys, "argv", [ |
| 878 | "last30days.py", |
| 879 | "Alpha", |
| 880 | "vs", |
| 881 | "Beta", |
| 882 | "--mock", |
| 883 | "--emit=html", |
| 884 | "--output", |
| 885 | str(output_path), |
| 886 | "--save-dir", |
| 887 | str(save_dir), |
| 888 | ]): |
| 889 | stdout = io.StringIO() |
| 890 | stderr = io.StringIO() |
| 891 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 892 | rc = cli.main() |
| 893 | |
| 894 | self.assertEqual(0, rc) |
| 895 | output_display = cli.compute_output_path_display(str(output_path)) |
| 896 | comparison_saved = save_dir / "alpha-vs-beta-raw-html.html" |
| 897 | self.assertEqual(2, emit_comparison.call_count) |
| 898 | first_kwargs = emit_comparison.call_args_list[0].kwargs |
| 899 | second_kwargs = emit_comparison.call_args_list[1].kwargs |
| 900 | self.assertEqual(output_display, first_kwargs["save_path"]) |
| 901 | comparison_display = cli.compute_output_path_display(str(comparison_saved)) |
| 902 | self.assertEqual(comparison_display, second_kwargs["save_path"]) |
| 903 | self.assertEqual("<html>comparison</html>\n", stdout.getvalue()) |
| 904 | self.assertEqual("<html>comparison</html>", output_path.read_text(encoding="utf-8")) |
| 905 | self.assertEqual( |
| 906 | "<html>comparison</html>", |
| 907 | comparison_saved.read_text(encoding="utf-8"), |
| 908 | ) |
| 909 | peer_saved = save_dir / "beta-raw-html.html" |
| 910 | self.assertEqual("<html>peer</html>", peer_saved.read_text(encoding="utf-8")) |
| 911 | self.assertIn(f"[last30days] Saved output to {output_path.resolve()}", stderr.getvalue()) |
| 912 | self.assertIn(f"[last30days] Saved output to {comparison_saved.resolve()}", stderr.getvalue()) |
| 913 | self.assertIn(f"[last30days] Saved output to {peer_saved.resolve()}", stderr.getvalue()) |
| 914 | self.assertIn( |
| 915 | f"[last30days] Comparison artifact set: main={comparison_saved.resolve()}; " |
| 916 | f"peers={peer_saved.resolve()}", |
| 917 | stderr.getvalue(), |
| 918 | ) |
| 919 | |
| 920 | def test_main_canonicalizes_explicit_github_repo_flags(self): |
| 921 | report = self.make_report() |
| 922 | diag = { |
| 923 | "available_sources": ["grounding"], |
| 924 | "providers": {"google": True, "openai": False, "xai": False}, |
| 925 | "x_backend": None, |
| 926 | "bird_installed": True, |
| 927 | "bird_authenticated": False, |
| 928 | "bird_username": None, |
| 929 | "native_web_backend": "brave", |
| 930 | } |
| 931 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 932 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 933 | mock.patch.object(cli.pipeline, "run", return_value=report) as run_mock, \ |
| 934 | mock.patch.object(cli, "emit_output", return_value="# rendered"), \ |
| 935 | mock.patch.object(sys, "argv", [ |
| 936 | "last30days.py", |
| 937 | "claude", |
| 938 | "code", |
| 939 | "vs", |
| 940 | "codex", |
| 941 | "--github-repo", |
| 942 | "openai/codex,anthropics/claude-code-action", |
| 943 | ]): |
| 944 | stdout = io.StringIO() |
| 945 | stderr = io.StringIO() |
| 946 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 947 | rc = cli.main() |
| 948 | self.assertEqual(0, rc) |
| 949 | # In vs-mode main + competitors run in parallel via ThreadPoolExecutor, |
| 950 | # so the order of pipeline.run invocations is non-deterministic. Find |
| 951 | # the main runner's call by predicate on the canonicalized github_repos |
| 952 | # rather than by index. |
| 953 | expected_repos = ["openai/codex", "anthropics/claude-code"] |
| 954 | main_call = next( |
| 955 | (c for c in run_mock.call_args_list if c.kwargs.get("github_repos") == expected_repos), |
| 956 | None, |
| 957 | ) |
| 958 | self.assertIsNotNone( |
| 959 | main_call, |
| 960 | f"No pipeline.run call had github_repos={expected_repos}; " |
| 961 | f"saw {[c.kwargs.get('github_repos') for c in run_mock.call_args_list]}", |
| 962 | ) |
| 963 | self.assertIn("[GitHub] Canonicalized repos:", stderr.getvalue()) |
| 964 | |
| 965 | def test_main_passes_trustpilot_domain_to_pipeline_run(self): |
| 966 | """The user-set flag must reach pipeline.run verbatim with |
| 967 | provenance user-set (is_hint False) on the single-topic path.""" |
| 968 | report = self.make_report() |
| 969 | diag = { |
| 970 | "available_sources": ["grounding"], |
| 971 | "providers": {"google": True, "openai": False, "xai": False}, |
| 972 | "x_backend": None, |
| 973 | "bird_installed": True, |
| 974 | "bird_authenticated": False, |
| 975 | "bird_username": None, |
| 976 | "native_web_backend": "brave", |
| 977 | } |
| 978 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 979 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 980 | mock.patch.object(cli.pipeline, "run", return_value=report) as run_mock, \ |
| 981 | mock.patch.object(cli, "emit_output", return_value="# rendered"), \ |
| 982 | mock.patch.object(sys, "argv", [ |
| 983 | "last30days.py", |
| 984 | "ThriftBooks", |
| 985 | "--trustpilot-domain", |
| 986 | "www.thriftbooks.com", |
| 987 | ]): |
| 988 | stdout = io.StringIO() |
| 989 | stderr = io.StringIO() |
| 990 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 991 | rc = cli.main() |
| 992 | self.assertEqual(0, rc) |
| 993 | main_call = next( |
| 994 | (c for c in run_mock.call_args_list |
| 995 | if c.kwargs.get("trustpilot_domain") == "www.thriftbooks.com"), |
| 996 | None, |
| 997 | ) |
| 998 | self.assertIsNotNone( |
| 999 | main_call, |
| 1000 | f"No pipeline.run call carried trustpilot_domain; saw " |
| 1001 | f"{[c.kwargs.get('trustpilot_domain') for c in run_mock.call_args_list]}", |
| 1002 | ) |
| 1003 | self.assertFalse(main_call.kwargs.get("trustpilot_domain_is_hint")) |
| 1004 | |
| 1005 | def test_trustpilot_domain_auto_activates_include_sources(self): |
| 1006 | """Explicit --trustpilot-domain must activate Trustpilot even when |
| 1007 | INCLUDE_SOURCES omits it (#873) — otherwise the flag silently no-ops.""" |
| 1008 | report = self.make_report(topic="Weber grills") |
| 1009 | diag = { |
| 1010 | "available_sources": ["tiktok", "instagram"], |
| 1011 | "providers": {"google": True, "openai": False, "xai": False}, |
| 1012 | "x_backend": None, |
| 1013 | "bird_installed": True, |
| 1014 | "bird_authenticated": False, |
| 1015 | "bird_username": None, |
| 1016 | "native_web_backend": "brave", |
| 1017 | } |
| 1018 | config = {"INCLUDE_SOURCES": "tiktok,instagram"} |
| 1019 | with mock.patch.object(cli.env, "get_config", return_value=config), \ |
| 1020 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 1021 | mock.patch.object(cli.pipeline, "run", return_value=report) as run_mock, \ |
| 1022 | mock.patch.object(cli, "emit_output", return_value="# rendered"), \ |
| 1023 | mock.patch.object(sys, "argv", [ |
| 1024 | "last30days.py", |
| 1025 | "Weber grills", |
| 1026 | "--trustpilot-domain", |
| 1027 | "weber.co.uk", |
| 1028 | ]): |
| 1029 | stdout = io.StringIO() |
| 1030 | stderr = io.StringIO() |
| 1031 | with redirect_stdout(stdout), redirect_stderr(stderr): |
| 1032 | rc = cli.main() |
| 1033 | self.assertEqual(0, rc) |
| 1034 | self.assertIn("trustpilot", config["INCLUDE_SOURCES"].lower()) |
| 1035 | self.assertIn("[Trustpilot] --trustpilot-domain=weber.co.uk activated", stderr.getvalue()) |
| 1036 | main_call = run_mock.call_args_list[0] |
| 1037 | self.assertEqual(main_call.kwargs.get("trustpilot_domain"), "weber.co.uk") |
| 1038 | |
| 1039 | def test_trustpilot_domain_auto_activates_with_search_filter(self): |
| 1040 | """When --search omits trustpilot, the explicit domain flag must still |
| 1041 | append it to requested_sources so the intersection filter cannot drop it.""" |
| 1042 | report = self.make_report(topic="Weber grills") |
| 1043 | diag = { |
| 1044 | "available_sources": ["tiktok", "instagram", "trustpilot"], |
| 1045 | "providers": {"google": True, "openai": False, "xai": False}, |
| 1046 | "x_backend": None, |
| 1047 | "bird_installed": True, |
| 1048 | "bird_authenticated": False, |
| 1049 | "bird_username": None, |
| 1050 | "native_web_backend": "brave", |
| 1051 | } |
| 1052 | config = {"INCLUDE_SOURCES": "tiktok,instagram"} |
| 1053 | with mock.patch.object(cli.env, "get_config", return_value=config), \ |
| 1054 | mock.patch.object(cli.pipeline, "diagnose", return_value=diag), \ |
| 1055 | mock.patch.object(cli.pipeline, "run", return_value=report) as run_mock, \ |
| 1056 | mock.patch.object(cli, "emit_output", return_value="# rendered"), \ |
| 1057 | mock.patch.object(sys, "argv", [ |
| 1058 | "last30days.py", |
| 1059 | "Weber grills", |
| 1060 | "--search", |
| 1061 | "tiktok,instagram", |
| 1062 | "--trustpilot-domain", |
| 1063 | "weber.co.uk", |
| 1064 | ]): |
| 1065 | with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 1066 | rc = cli.main() |
| 1067 | self.assertEqual(0, rc) |
| 1068 | requested = run_mock.call_args_list[0].kwargs.get("requested_sources") or [] |
| 1069 | self.assertIn("trustpilot", requested) |
| 1070 | |
| 1071 | def test_trustpilot_domain_respects_exclude_sources(self): |
| 1072 | config = {"INCLUDE_SOURCES": "tiktok", "EXCLUDE_SOURCES": "trustpilot"} |
| 1073 | requested = cli.activate_trustpilot_for_explicit_domain( |
| 1074 | config, ["tiktok"], reason="--trustpilot-domain=weber.co.uk", |
| 1075 | ) |
| 1076 | self.assertEqual(requested, ["tiktok"]) |
| 1077 | self.assertNotIn("trustpilot", config["INCLUDE_SOURCES"].lower()) |
| 1078 | |
| 1079 | |
| 1080 | class ActivateTrustpilotHelperTests(unittest.TestCase): |
| 1081 | def test_plan_has_explicit_trustpilot_domain(self): |
| 1082 | self.assertTrue(cli.plan_has_explicit_trustpilot_domain({ |
| 1083 | "traeger": {"trustpilot_domain": "traeger.com"}, |
| 1084 | })) |
| 1085 | self.assertFalse(cli.plan_has_explicit_trustpilot_domain({ |
| 1086 | "traeger": {"x_handle": "Traeger"}, |
| 1087 | })) |
| 1088 | self.assertFalse(cli.plan_has_explicit_trustpilot_domain(None)) |
| 1089 | |
| 1090 | def test_activate_adds_include_and_requested(self): |
| 1091 | config = {"INCLUDE_SOURCES": "tiktok,instagram"} |
| 1092 | requested = cli.activate_trustpilot_for_explicit_domain( |
| 1093 | config, ["tiktok", "instagram"], reason="--trustpilot-domain=x.com", |
| 1094 | ) |
| 1095 | self.assertIn("trustpilot", config["INCLUDE_SOURCES"].lower()) |
| 1096 | self.assertEqual(requested, ["tiktok", "instagram", "trustpilot"]) |
| 1097 | |
| 1098 | def test_activate_noop_when_already_present(self): |
| 1099 | config = {"INCLUDE_SOURCES": "tiktok,trustpilot"} |
| 1100 | requested = cli.activate_trustpilot_for_explicit_domain( |
| 1101 | config, ["trustpilot"], reason="--trustpilot-domain=x.com", |
| 1102 | ) |
| 1103 | self.assertEqual(config["INCLUDE_SOURCES"], "tiktok,trustpilot") |
| 1104 | self.assertEqual(requested, ["trustpilot"]) |
| 1105 | |
| 1106 | |
| 1107 | if __name__ == "__main__": |
| 1108 | unittest.main() |
| 1109 |