| 1 | """Tests for the BRAVE/SERPER web-promo suppression when hosting-model-driven.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | from pathlib import Path |
| 11 | |
| 12 | REPO_ROOT = Path(__file__).resolve().parents[1] |
| 13 | |
| 14 | |
| 15 | def _engine() -> Path: |
| 16 | return REPO_ROOT / "skills" / "last30days" / "scripts" / "last30days.py" |
| 17 | |
| 18 | |
| 19 | class FooterNudgeSuppressionTests(unittest.TestCase): |
| 20 | def _run(self, *argv: str, topic: str) -> subprocess.CompletedProcess: |
| 21 | cmd = [ |
| 22 | sys.executable, |
| 23 | str(_engine()), |
| 24 | topic, |
| 25 | "--mock", |
| 26 | "--emit=md", |
| 27 | *argv, |
| 28 | ] |
| 29 | env = { |
| 30 | **os.environ, |
| 31 | "LAST30DAYS_SKIP_PREFLIGHT": "1", |
| 32 | # Skip ~/.config/last30days/.env so a contributor's saved |
| 33 | # BRAVE/EXA/SERPER/PARALLEL key doesn't make grounding "available" |
| 34 | # and suppress the promo we're checking for. |
| 35 | "LAST30DAYS_CONFIG_DIR": "", |
| 36 | # Pin X as available so _missing_sources_for_promo selects "web" |
| 37 | # (otherwise the "x" promo wins and the BRAVE_API_KEY string never |
| 38 | # appears). |
| 39 | "XAI_API_KEY": "test-stub", |
| 40 | } |
| 41 | # Strip any grounded-web keys the host might have so the promo path |
| 42 | # triggers deterministically in mock + no-backend. Also strip X cookie |
| 43 | # credentials so XAI_API_KEY is the unambiguous X backend. |
| 44 | for key in ("BRAVE_API_KEY", "EXA_API_KEY", "SERPER_API_KEY", |
| 45 | "PARALLEL_API_KEY", "OPENROUTER_API_KEY", "PERPLEXITY_API_KEY", |
| 46 | "AUTH_TOKEN", "CT0", "LAST30DAYS_X_BACKEND"): |
| 47 | env.pop(key, None) |
| 48 | # Run from a tmpdir so _find_project_env() can't walk up into any |
| 49 | # .claude/last30days.env above the repo on the contributor's machine. |
| 50 | with tempfile.TemporaryDirectory() as tmp: |
| 51 | return subprocess.run( |
| 52 | cmd, capture_output=True, text=True, encoding="utf-8", env=env, cwd=tmp, |
| 53 | ) |
| 54 | |
| 55 | def test_bare_run_emits_web_promo(self): |
| 56 | result = self._run(topic="OpenAI") |
| 57 | combined = result.stdout + result.stderr |
| 58 | # Mock mode still shows the promo when nothing indicates a hosting |
| 59 | # model is driving. Check both streams since the UI may emit to stderr. |
| 60 | self.assertIn("BRAVE_API_KEY", combined) |
| 61 | |
| 62 | def test_competitors_plan_suppresses_web_promo(self): |
| 63 | result = self._run( |
| 64 | "--competitors-list", "Anthropic", |
| 65 | "--competitors-plan", |
| 66 | '{"Anthropic":{"x_handle":"AnthropicAI","subreddits":["ClaudeAI"]}}', |
| 67 | topic="OpenAI", |
| 68 | ) |
| 69 | combined = result.stdout + result.stderr |
| 70 | self.assertNotIn( |
| 71 | "unlock native grounded web search", |
| 72 | combined, |
| 73 | msg="web promo should be suppressed when --competitors-plan is passed", |
| 74 | ) |
| 75 | |
| 76 | def test_plan_suppresses_web_promo(self): |
| 77 | plan = ( |
| 78 | '{"intent":"concept","freshness_mode":"balanced_recent",' |
| 79 | '"cluster_mode":"none","subqueries":[{"label":"primary",' |
| 80 | '"search_query":"OpenAI","ranking_query":"OpenAI",' |
| 81 | '"sources":["grounding"]}],"source_weights":{"grounding":1.0}}' |
| 82 | ) |
| 83 | result = self._run("--plan", plan, topic="OpenAI") |
| 84 | combined = result.stdout + result.stderr |
| 85 | self.assertNotIn( |
| 86 | "unlock native grounded web search", |
| 87 | combined, |
| 88 | msg="web promo should be suppressed when --plan is passed", |
| 89 | ) |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | unittest.main() |
| 93 |