| 1 | """Tests for setup-wizard auto-install of the default-on Printing Press sources |
| 2 | (arxiv, techmeme, yahoo_finance; trustpilot stays opt-in) -- lib/setup_wizard.py.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from lib import setup_wizard as sw |
| 9 | |
| 10 | |
| 11 | @pytest.fixture |
| 12 | def no_off_path(monkeypatch): |
| 13 | # Default: no binary present on disk in known dirs. |
| 14 | monkeypatch.setattr(sw, "_pp_off_path_binary", lambda bin_name: None) |
| 15 | |
| 16 | |
| 17 | def test_already_installed_when_on_path(monkeypatch): |
| 18 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 19 | installed, action, stderr, off = sw._install_pp_cli("arxiv", "arxiv-pp-cli") |
| 20 | assert installed is True |
| 21 | assert action == "already_installed" |
| 22 | |
| 23 | |
| 24 | def test_installed_off_path(monkeypatch, no_off_path): |
| 25 | monkeypatch.setattr(sw.shutil, "which", lambda name: None) |
| 26 | monkeypatch.setattr(sw, "_pp_off_path_binary", lambda bin_name: "/home/u/.local/bin/" + bin_name) |
| 27 | installed, action, stderr, off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 28 | assert installed is False |
| 29 | assert action == "installed_off_path" |
| 30 | assert off.endswith("techmeme-pp-cli") |
| 31 | |
| 32 | |
| 33 | def test_no_npx(monkeypatch, no_off_path): |
| 34 | monkeypatch.setattr(sw.shutil, "which", lambda name: None) # neither bin nor npx |
| 35 | installed, action, stderr, off = sw._install_pp_cli("trustpilot", "trustpilot-pp-cli") |
| 36 | assert installed is False |
| 37 | assert action == "no_npx" |
| 38 | |
| 39 | |
| 40 | def test_install_success(monkeypatch, no_off_path): |
| 41 | # npx present; binary absent before, resolves after the install. |
| 42 | calls = {"n": 0} |
| 43 | |
| 44 | def fake_which(name): |
| 45 | if name == "npx": |
| 46 | return "/usr/bin/npx" |
| 47 | if name == "arxiv-pp-cli": |
| 48 | # absent on first check, present after install |
| 49 | calls["n"] += 1 |
| 50 | return None if calls["n"] == 1 else "/home/u/.local/bin/arxiv-pp-cli" |
| 51 | return None |
| 52 | |
| 53 | monkeypatch.setattr(sw.shutil, "which", fake_which) |
| 54 | monkeypatch.setattr(sw.subprocess, "run", |
| 55 | lambda *a, **k: type("P", (), {"returncode": 0, "stdout": "", "stderr": ""})()) |
| 56 | installed, action, stderr, off = sw._install_pp_cli("arxiv", "arxiv-pp-cli") |
| 57 | assert installed is True |
| 58 | assert action == "installed" |
| 59 | |
| 60 | |
| 61 | def test_install_uses_resolved_windows_npx_path(monkeypatch, no_off_path): |
| 62 | """Regression for #904: subprocess.run must receive the resolved npx |
| 63 | path (e.g. a Windows PATHEXT-resolved npx.CMD), not the bare "npx" |
| 64 | string -- bare "npx" fails with WinError 2 on Windows.""" |
| 65 | calls = {"n": 0} |
| 66 | windows_npx = r"C:\Program Files\nodejs\npx.CMD" |
| 67 | |
| 68 | def fake_which(name): |
| 69 | if name == "npx": |
| 70 | return windows_npx |
| 71 | if name == "techmeme-pp-cli": |
| 72 | calls["n"] += 1 |
| 73 | return None if calls["n"] == 1 else r"C:\Users\me\.local\bin\techmeme-pp-cli" |
| 74 | return None |
| 75 | |
| 76 | run_calls = [] |
| 77 | |
| 78 | def fake_run(cmd, **kwargs): |
| 79 | run_calls.append(cmd) |
| 80 | return type("P", (), {"returncode": 0, "stdout": "", "stderr": ""})() |
| 81 | |
| 82 | monkeypatch.setattr(sw.shutil, "which", fake_which) |
| 83 | monkeypatch.setattr(sw.subprocess, "run", fake_run) |
| 84 | installed, action, stderr, off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 85 | assert installed is True |
| 86 | assert action == "installed" |
| 87 | assert run_calls[0][0] == windows_npx |
| 88 | |
| 89 | |
| 90 | def test_install_failed_nonzero_rc(monkeypatch, no_off_path): |
| 91 | def fake_which(name): |
| 92 | return "/usr/bin/npx" if name == "npx" else None |
| 93 | |
| 94 | monkeypatch.setattr(sw.shutil, "which", fake_which) |
| 95 | monkeypatch.setattr(sw.subprocess, "run", |
| 96 | lambda *a, **k: type("P", (), {"returncode": 1, "stdout": "", "stderr": "boom"})()) |
| 97 | installed, action, stderr, off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 98 | assert installed is False |
| 99 | assert action == "install_failed" |
| 100 | assert "boom" in stderr |
| 101 | |
| 102 | |
| 103 | def test_install_default_pp_sources_covers_default_on_set(monkeypatch): |
| 104 | # Only the zero-auth default-on sources are auto-installed. Trustpilot is |
| 105 | # opt-in (INCLUDE_SOURCES=trustpilot) and intentionally excluded here. |
| 106 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 107 | out = sw.install_default_pp_sources() |
| 108 | assert set(out.keys()) == {"arxiv", "techmeme", "news_rss", "yahoo_finance"} |
| 109 | assert "trustpilot" not in out |
| 110 | for entry in out.values(): |
| 111 | assert entry["action"] == "already_installed" |
| 112 | assert entry["installed"] is True |
| 113 | |
| 114 | |
| 115 | def test_install_is_idempotent(monkeypatch): |
| 116 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 117 | run_calls = [] |
| 118 | monkeypatch.setattr(sw.subprocess, "run", lambda *a, **k: run_calls.append(a)) |
| 119 | sw.install_default_pp_sources() |
| 120 | sw.install_default_pp_sources() |
| 121 | # All already on PATH -> npx install never invoked. |
| 122 | assert run_calls == [] |
| 123 | |
| 124 | |
| 125 | def _version_stub(monkeypatch, line_for): |
| 126 | """Stub the helper version exec; ``line_for(argv)`` returns its stdout.""" |
| 127 | import subprocess |
| 128 | |
| 129 | from lib import health |
| 130 | |
| 131 | execs = [] |
| 132 | |
| 133 | def fake_version(cmd, **kwargs): |
| 134 | execs.append(cmd) |
| 135 | return subprocess.CompletedProcess(args=cmd, returncode=0, stdout=line_for(cmd), stderr="") |
| 136 | |
| 137 | monkeypatch.setattr(health.subproc, "run_with_timeout", fake_version) |
| 138 | return execs |
| 139 | |
| 140 | |
| 141 | def test_helper_with_no_minimum_is_never_upgraded(monkeypatch): |
| 142 | # yahoo-finance-pp-cli numbers its releases 1.0.0, far below Digg's |
| 143 | # date-shaped versions; with no table entry it is left alone and its |
| 144 | # version is not even read. |
| 145 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 146 | execs = _version_stub(monkeypatch, lambda cmd: "yahoo-finance-pp-cli 1.0.0\n") |
| 147 | run_calls = [] |
| 148 | monkeypatch.setattr(sw.subprocess, "run", lambda *a, **k: run_calls.append(a)) |
| 149 | installed, action, stderr, off = sw._install_pp_cli("yahoo-finance", YF_BIN) |
| 150 | assert (installed, action) == (True, "already_installed") |
| 151 | assert run_calls == [] |
| 152 | assert execs == [] |
| 153 | |
| 154 | |
| 155 | def test_pp_helper_below_its_minimum_is_upgraded(monkeypatch): |
| 156 | from lib import health |
| 157 | |
| 158 | monkeypatch.setitem(health.MIN_HELPER_VERSIONS, "arxiv-pp-cli", (2, 0, 0)) |
| 159 | state = {"installed": False} |
| 160 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 161 | _version_stub( |
| 162 | monkeypatch, |
| 163 | lambda cmd: ( |
| 164 | "techmeme-pp-cli 2026.11.1\n" if "techmeme" in cmd[0] |
| 165 | else "arxiv-pp-cli 2.1.0\n" if state["installed"] else "arxiv-pp-cli 1.9.9\n" |
| 166 | ), |
| 167 | ) |
| 168 | runs = [] |
| 169 | |
| 170 | def fake_run(cmd, **kwargs): |
| 171 | runs.append(cmd) |
| 172 | state["installed"] = True |
| 173 | return type("P", (), {"returncode": 0, "stdout": "", "stderr": ""})() |
| 174 | |
| 175 | monkeypatch.setattr(sw.subprocess, "run", fake_run) |
| 176 | out = sw.install_default_pp_sources() |
| 177 | assert out["arxiv"] == {"installed": True, "action": "upgraded"} |
| 178 | assert runs == [["/usr/bin/npx", "-y", sw.PRINTING_PRESS_NPM, "install", "arxiv", "--cli-only"]] |
| 179 | |
| 180 | |
| 181 | def _techmeme_setup(monkeypatch, current: str, after: str): |
| 182 | state = {"installed": False} |
| 183 | monkeypatch.setattr(sw.shutil, "which", lambda name: f"/usr/bin/{name}") |
| 184 | _version_stub( |
| 185 | monkeypatch, |
| 186 | lambda cmd: f"techmeme-pp-cli {after if state['installed'] else current}\n", |
| 187 | ) |
| 188 | runs = [] |
| 189 | |
| 190 | def fake_run(cmd, **kwargs): |
| 191 | runs.append(cmd) |
| 192 | state["installed"] = True |
| 193 | return type("P", (), {"returncode": 0, "stdout": "", "stderr": ""})() |
| 194 | |
| 195 | monkeypatch.setattr(sw.subprocess, "run", fake_run) |
| 196 | return runs |
| 197 | |
| 198 | |
| 199 | def test_techmeme_without_dates_is_upgraded_by_setup(monkeypatch): |
| 200 | # Real table entry, not monkeypatched: 2026.6.2 predates dated search. |
| 201 | runs = _techmeme_setup(monkeypatch, "2026.6.2", "2026.11.1") |
| 202 | installed, action, _stderr, _off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 203 | assert (installed, action) == (True, "upgraded") |
| 204 | assert runs == [["/usr/bin/npx", "-y", sw.PRINTING_PRESS_NPM, "install", "techmeme", "--cli-only"]] |
| 205 | |
| 206 | |
| 207 | def test_techmeme_at_the_minimum_is_left_alone(monkeypatch): |
| 208 | runs = _techmeme_setup(monkeypatch, "2026.11.1", "2026.11.1") |
| 209 | installed, action, _stderr, _off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 210 | assert (installed, action) == (True, "already_installed") |
| 211 | assert runs == [] |
| 212 | |
| 213 | |
| 214 | def test_techmeme_without_search_pages_is_upgraded_by_setup(monkeypatch): |
| 215 | # 2026.9.1 has dated search but reads only one page of 10. |
| 216 | runs = _techmeme_setup(monkeypatch, "2026.9.1", "2026.11.1") |
| 217 | installed, action, _stderr, _off = sw._install_pp_cli("techmeme", "techmeme-pp-cli") |
| 218 | assert (installed, action) == (True, "upgraded") |
| 219 | assert runs == [["/usr/bin/npx", "-y", sw.PRINTING_PRESS_NPM, "install", "techmeme", "--cli-only"]] |
| 220 | |
| 221 | |
| 222 | def test_status_text_pp_helper_upgraded(): |
| 223 | text = sw.get_setup_status_text({"pp_sources": {"arxiv": {"installed": True, "action": "upgraded"}}}) |
| 224 | assert "Upgraded arXiv CLI" in text |
| 225 | |
| 226 | |
| 227 | # --- Yahoo Finance: source key and catalog slug differ (underscore vs hyphen) --- |
| 228 | |
| 229 | YF_BIN = "yahoo-finance-pp-cli" |
| 230 | YF_OFF_PATH = "/home/u/.local/bin/" + YF_BIN |
| 231 | |
| 232 | |
| 233 | def _yahoo_outcome(monkeypatch, *, before, after, npx=True, rc=0, on_disk_before=False, on_disk_after=False): |
| 234 | """Run the default install with every other CLI already on PATH and the |
| 235 | Yahoo Finance binary in the given state before and after the npx run. |
| 236 | |
| 237 | Returns ``(yahoo entry, argv of each npx run)``. No real install runs: |
| 238 | ``shutil.which``, the off-PATH probe and ``subprocess.run`` are stubbed. |
| 239 | """ |
| 240 | state = {"ran": False} |
| 241 | runs = [] |
| 242 | |
| 243 | def fake_which(name): |
| 244 | if name == YF_BIN: |
| 245 | present = after if state["ran"] else before |
| 246 | return f"/usr/bin/{name}" if present else None |
| 247 | if name == "npx": |
| 248 | return "/usr/bin/npx" if npx else None |
| 249 | return f"/usr/bin/{name}" |
| 250 | |
| 251 | def fake_off_path(bin_name): |
| 252 | if bin_name != YF_BIN: |
| 253 | return None |
| 254 | return YF_OFF_PATH if (on_disk_after if state["ran"] else on_disk_before) else None |
| 255 | |
| 256 | def fake_run(cmd, **kwargs): |
| 257 | runs.append(cmd) |
| 258 | state["ran"] = True |
| 259 | return type("P", (), {"returncode": rc, "stdout": "", "stderr": "boom" if rc else ""})() |
| 260 | |
| 261 | monkeypatch.setattr(sw.shutil, "which", fake_which) |
| 262 | monkeypatch.setattr(sw, "_pp_off_path_binary", fake_off_path) |
| 263 | monkeypatch.setattr(sw.subprocess, "run", fake_run) |
| 264 | return sw.install_default_pp_sources()["yahoo_finance"], runs |
| 265 | |
| 266 | |
| 267 | def test_yahoo_finance_installed(monkeypatch): |
| 268 | entry, runs = _yahoo_outcome(monkeypatch, before=False, after=True) |
| 269 | assert entry == {"installed": True, "action": "installed"} |
| 270 | # The catalog install takes the hyphenated slug, not the source key. |
| 271 | assert runs == [["/usr/bin/npx", "-y", sw.PRINTING_PRESS_NPM, "install", "yahoo-finance", "--cli-only"]] |
| 272 | |
| 273 | |
| 274 | def test_yahoo_finance_already_installed(monkeypatch): |
| 275 | entry, runs = _yahoo_outcome(monkeypatch, before=True, after=True) |
| 276 | assert entry == {"installed": True, "action": "already_installed"} |
| 277 | assert runs == [] |
| 278 | |
| 279 | |
| 280 | def test_yahoo_finance_already_on_disk_off_path(monkeypatch): |
| 281 | # A binary on disk that the agent PATH cannot see is never reinstalled and |
| 282 | # never counted as active. |
| 283 | entry, runs = _yahoo_outcome(monkeypatch, before=False, after=False, on_disk_before=True) |
| 284 | assert entry == {"installed": False, "action": "installed_off_path", "path": YF_OFF_PATH} |
| 285 | assert runs == [] |
| 286 | |
| 287 | |
| 288 | def test_yahoo_finance_install_lands_off_path(monkeypatch): |
| 289 | entry, runs = _yahoo_outcome(monkeypatch, before=False, after=False, on_disk_after=True) |
| 290 | assert entry == {"installed": False, "action": "installed_off_path", "path": YF_OFF_PATH} |
| 291 | assert len(runs) == 1 |
| 292 | |
| 293 | |
| 294 | def test_yahoo_finance_install_failed(monkeypatch): |
| 295 | entry, runs = _yahoo_outcome(monkeypatch, before=False, after=False, rc=1) |
| 296 | assert entry == {"installed": False, "action": "install_failed", "stderr": "boom"} |
| 297 | |
| 298 | |
| 299 | def test_yahoo_finance_no_npx(monkeypatch): |
| 300 | entry, runs = _yahoo_outcome(monkeypatch, before=False, after=False, npx=False) |
| 301 | assert entry == {"installed": False, "action": "no_npx"} |
| 302 | assert runs == [] |
| 303 | |
| 304 | |
| 305 | def _status_text(entry): |
| 306 | return sw.get_setup_status_text({"pp_sources": {"yahoo_finance": entry}}) |
| 307 | |
| 308 | |
| 309 | @pytest.mark.parametrize("action", ["install_failed", "no_npx"]) |
| 310 | def test_manual_install_hint_prints_the_catalog_slug(action): |
| 311 | text = _status_text({"installed": False, "action": action}) |
| 312 | assert f"npx -y {sw.PRINTING_PRESS_NPM} install yahoo-finance --cli-only" in text |
| 313 | assert "install yahoo_finance" not in text |
| 314 | assert "Yahoo Finance CLI" in text |
| 315 | |
| 316 | |
| 317 | def test_status_text_names_yahoo_finance_active_only_on_path(): |
| 318 | assert "Installed Yahoo Finance CLI (Yahoo Finance source now active)" in _status_text( |
| 319 | {"installed": True, "action": "installed"} |
| 320 | ) |
| 321 | off_path = _status_text({"installed": False, "action": "installed_off_path", "path": YF_OFF_PATH}) |
| 322 | assert f"Yahoo Finance CLI at {YF_OFF_PATH} but not on PATH" in off_path |
| 323 | assert "now active" not in off_path |
| 324 | |
| 325 | |
| 326 | def test_every_default_source_has_a_display_name_and_slug(): |
| 327 | # A source missing from either map falls back to the source key, which is |
| 328 | # the wrong install argument for a hyphenated slug. |
| 329 | for source_key, slug, _bin_name in sw.PP_DEFAULT_SOURCES: |
| 330 | text = sw.get_setup_status_text( |
| 331 | {"pp_sources": {source_key: {"installed": False, "action": "install_failed"}}} |
| 332 | ) |
| 333 | assert f"install {slug} --cli-only" in text, source_key |
| 334 |