| 1 | """Tests for the uniform dependency-probe taxonomy in scripts/lib/health.py. |
| 2 | |
| 3 | Covers the doctor-command probe layer (issue #692 class): every probed external |
| 4 | dependency (yt-dlp, Printing Press CLIs, node, ffmpeg) must report |
| 5 | ok | missing | broken | timeout with a package-manager-aware prescription. |
| 6 | The stale-shim false negative — shutil.which resolves a binary that cannot |
| 7 | exec — must classify as ``broken`` with a *reinstall* prescription, never as |
| 8 | available. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import os |
| 14 | import stat |
| 15 | import subprocess |
| 16 | import sys |
| 17 | from pathlib import Path |
| 18 | from unittest import mock |
| 19 | |
| 20 | import pytest |
| 21 | |
| 22 | from lib import health |
| 23 | |
| 24 | |
| 25 | @pytest.fixture(autouse=True) |
| 26 | def _fresh_probe_cache(): |
| 27 | """Probes memoize per process; isolate every test.""" |
| 28 | health.clear_dependency_probe_cache() |
| 29 | yield |
| 30 | health.clear_dependency_probe_cache() |
| 31 | |
| 32 | |
| 33 | def _which_map(mapping): |
| 34 | """shutil.which side_effect resolving only the names in ``mapping``.""" |
| 35 | def _which(name, *args, **kwargs): |
| 36 | return mapping.get(name) |
| 37 | return _which |
| 38 | |
| 39 | |
| 40 | def _completed(rc=0, stdout="", stderr=""): |
| 41 | return subprocess.CompletedProcess(args=["x"], returncode=rc, stdout=stdout, stderr=stderr) |
| 42 | |
| 43 | |
| 44 | class TestMissing: |
| 45 | """Scenario 1: binary absent from PATH -> missing + installer prescription.""" |
| 46 | |
| 47 | def test_ytdlp_missing_brew_prescription(self): |
| 48 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"brew": "/opt/homebrew/bin/brew"})), \ |
| 49 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 50 | probe = health.probe_dependency("yt-dlp") |
| 51 | assert probe.status == health.MISSING |
| 52 | assert probe.off_path is False # genuinely absent, not merely off-PATH |
| 53 | assert probe.prescription == "brew install yt-dlp" |
| 54 | assert probe.owner_pkg_manager == "brew" |
| 55 | assert "not found on PATH" in probe.detail |
| 56 | |
| 57 | def test_ytdlp_missing_pipx_prescription_when_no_brew(self): |
| 58 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"pipx": "/usr/local/bin/pipx"})), \ |
| 59 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 60 | probe = health.probe_dependency("yt-dlp") |
| 61 | assert probe.status == health.MISSING |
| 62 | assert probe.prescription == "pipx install yt-dlp" |
| 63 | assert probe.owner_pkg_manager == "pipx" |
| 64 | |
| 65 | def test_pp_cli_missing_prescribes_printing_press_install(self): |
| 66 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 67 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 68 | probe = health.probe_dependency("digg-pp-cli") |
| 69 | assert probe.status == health.MISSING |
| 70 | assert "printing-press-library" in probe.prescription |
| 71 | assert "install digg --cli-only" in probe.prescription |
| 72 | assert probe.owner_pkg_manager == "npx" |
| 73 | |
| 74 | def test_node_missing_nvm_prescription_when_no_brew(self, monkeypatch, tmp_path): |
| 75 | monkeypatch.setenv("NVM_DIR", str(tmp_path)) |
| 76 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 77 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 78 | probe = health.probe_dependency("node") |
| 79 | assert probe.status == health.MISSING |
| 80 | assert "nvm install" in probe.prescription |
| 81 | assert probe.owner_pkg_manager == "nvm" |
| 82 | |
| 83 | def test_ffmpeg_missing_apt_prescription_when_no_brew(self): |
| 84 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"apt-get": "/usr/bin/apt-get"})), \ |
| 85 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 86 | probe = health.probe_dependency("ffmpeg") |
| 87 | assert probe.status == health.MISSING |
| 88 | assert "apt-get install" in probe.prescription |
| 89 | assert probe.owner_pkg_manager == "apt" |
| 90 | |
| 91 | def test_missing_with_no_manager_still_prescribes_something(self, monkeypatch): |
| 92 | monkeypatch.delenv("NVM_DIR", raising=False) |
| 93 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 94 | mock.patch.object(health, "_nvm_present", return_value=False), \ |
| 95 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 96 | probe = health.probe_dependency("yt-dlp") |
| 97 | assert probe.status == health.MISSING |
| 98 | assert probe.prescription # never an empty prescription for missing |
| 99 | assert probe.owner_pkg_manager == "" |
| 100 | |
| 101 | |
| 102 | class TestBroken: |
| 103 | """Scenario 2: which resolves but exec fails -> broken + REINSTALL prescription.""" |
| 104 | |
| 105 | def test_stale_shim_exec_oserror_is_broken_not_ok(self): |
| 106 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"yt-dlp": "/x/yt-dlp", "brew": "/x/brew"})), \ |
| 107 | mock.patch.object(health.subproc, "run_with_timeout", side_effect=OSError("exec format error")): |
| 108 | probe = health.probe_dependency("yt-dlp") |
| 109 | assert probe.status == health.BROKEN |
| 110 | assert probe.prescription == "brew reinstall yt-dlp" |
| 111 | assert "reinstall" in probe.prescription.lower() |
| 112 | |
| 113 | def test_nonzero_version_exit_is_broken(self): |
| 114 | fake = _completed(rc=1, stderr="ModuleNotFoundError: No module named 'yt_dlp'") |
| 115 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"yt-dlp": "/x/yt-dlp", "brew": "/x/brew"})), \ |
| 116 | mock.patch.object(health.subproc, "run_with_timeout", return_value=fake): |
| 117 | probe = health.probe_dependency("yt-dlp") |
| 118 | assert probe.status == health.BROKEN |
| 119 | assert "ModuleNotFoundError" in probe.detail |
| 120 | assert "reinstall" in probe.prescription.lower() |
| 121 | |
| 122 | def test_broken_pp_cli_prescribes_rerunning_install(self): |
| 123 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"digg-pp-cli": "/x/digg-pp-cli"})), \ |
| 124 | mock.patch.object(health.subproc, "run_with_timeout", side_effect=OSError("bad exec")): |
| 125 | probe = health.probe_dependency("digg-pp-cli") |
| 126 | assert probe.status == health.BROKEN |
| 127 | assert "printing-press-library" in probe.prescription |
| 128 | assert "install digg --cli-only" in probe.prescription |
| 129 | |
| 130 | def test_real_stale_shim_on_disk(self, tmp_path, monkeypatch): |
| 131 | """A launcher is discoverable on PATH but cannot execute (#692).""" |
| 132 | shim = tmp_path / ("fake-pp-cli.exe" if os.name == "nt" else "fake-pp-cli") |
| 133 | shim.write_text("#!/nonexistent-interpreter/python3\nprint('hi')\n") |
| 134 | shim.chmod(shim.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 135 | monkeypatch.setenv("PATH", str(tmp_path)) |
| 136 | probe = health.probe_dependency("fake-pp-cli") |
| 137 | assert probe.status == health.BROKEN |
| 138 | fix = probe.prescription.lower() |
| 139 | assert "reinstall" in fix or "re-run" in fix |
| 140 | |
| 141 | |
| 142 | class TestTimeout: |
| 143 | """Scenario 3: slow probe -> timeout, distinct message, bounded budget.""" |
| 144 | |
| 145 | def test_timeout_status_and_message(self): |
| 146 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"ffmpeg": "/x/ffmpeg", "brew": "/x/brew"})), \ |
| 147 | mock.patch.object( |
| 148 | health.subproc, "run_with_timeout", |
| 149 | side_effect=health.subproc.SubprocTimeout("ffmpeg timed out"), |
| 150 | ): |
| 151 | probe = health.probe_dependency("ffmpeg") |
| 152 | assert probe.status == health.TIMEOUT |
| 153 | assert "timed out" in probe.detail |
| 154 | |
| 155 | def test_probe_budget_is_bounded(self): |
| 156 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"node": "/x/node", "brew": "/x/brew"})), \ |
| 157 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="v22.1.0")) as run: |
| 158 | health.probe_dependency("node") |
| 159 | assert run.call_args.kwargs["timeout"] <= health.PROBE_TIMEOUT |
| 160 | |
| 161 | |
| 162 | class TestOk: |
| 163 | """Scenario 4: healthy binary -> ok, no prescription.""" |
| 164 | |
| 165 | def test_healthy_binary_ok_no_prescription(self): |
| 166 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"yt-dlp": "/x/yt-dlp", "brew": "/x/brew"})), \ |
| 167 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="2026.06.09\n")): |
| 168 | probe = health.probe_dependency("yt-dlp") |
| 169 | assert probe.status == health.OK |
| 170 | assert probe.ok |
| 171 | assert probe.prescription == "" |
| 172 | assert "2026.06.09" in probe.detail |
| 173 | |
| 174 | def test_real_healthy_binary_end_to_end(self, monkeypatch): |
| 175 | """Integration: a real executable on a real PATH, no mocks.""" |
| 176 | binary = Path(sys.executable) |
| 177 | monkeypatch.setenv("PATH", str(binary.parent)) |
| 178 | probe = health.probe_dependency(binary.name) |
| 179 | assert probe.status == health.OK |
| 180 | assert f"Python {sys.version_info.major}.{sys.version_info.minor}." in probe.detail |
| 181 | assert probe.prescription == "" |
| 182 | |
| 183 | |
| 184 | class TestOffPath: |
| 185 | """Scenario 5: on disk but off PATH -> missing + PATH-fix, never ok.""" |
| 186 | |
| 187 | def test_off_path_binary_is_missing_with_path_fix(self, tmp_path): |
| 188 | binary = tmp_path / "digg-pp-cli" |
| 189 | binary.write_text("#!/bin/sh\necho ok\n") |
| 190 | binary.chmod(binary.stat().st_mode | stat.S_IXUSR) |
| 191 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"npx": "/x/npx"})), \ |
| 192 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[tmp_path]): |
| 193 | probe = health.probe_dependency("digg-pp-cli") |
| 194 | assert probe.status == health.MISSING |
| 195 | assert probe.off_path is True |
| 196 | assert "PATH" in probe.prescription |
| 197 | assert str(tmp_path) in probe.prescription or "$HOME" in probe.prescription |
| 198 | assert str(binary) in probe.detail or "$HOME" in probe.detail |
| 199 | |
| 200 | def test_off_path_never_reports_ok(self, tmp_path): |
| 201 | binary = tmp_path / "yt-dlp" |
| 202 | binary.write_text("#!/bin/sh\necho 2026.06.09\n") |
| 203 | binary.chmod(binary.stat().st_mode | stat.S_IXUSR) |
| 204 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"brew": "/x/brew"})), \ |
| 205 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[tmp_path]): |
| 206 | probe = health.probe_dependency("yt-dlp") |
| 207 | assert probe.status != health.OK |
| 208 | assert not probe.ok |
| 209 | |
| 210 | |
| 211 | class TestCachingAndRegistry: |
| 212 | """Probes are memoized per process; the registry covers all doctor deps.""" |
| 213 | |
| 214 | def test_probe_memoized_single_subprocess(self): |
| 215 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"node": "/x/node", "brew": "/x/brew"})), \ |
| 216 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="v22.1.0")) as run: |
| 217 | first = health.probe_dependency("node") |
| 218 | second = health.probe_dependency("node") |
| 219 | assert run.call_count == 1 |
| 220 | assert first is second |
| 221 | |
| 222 | def test_clear_cache_reprobes(self): |
| 223 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"node": "/x/node", "brew": "/x/brew"})), \ |
| 224 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="v22.1.0")) as run: |
| 225 | health.probe_dependency("node") |
| 226 | health.clear_dependency_probe_cache() |
| 227 | health.probe_dependency("node") |
| 228 | assert run.call_count == 2 |
| 229 | |
| 230 | def test_probe_dependencies_covers_known_set(self): |
| 231 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 232 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 233 | probes = health.probe_dependencies() |
| 234 | assert set(probes) == set(health.KNOWN_DEPENDENCIES) |
| 235 | assert {"yt-dlp", "digg-pp-cli", "yahoo-finance-pp-cli", "node", "ffmpeg"} <= set(probes) |
| 236 | for probe in probes.values(): |
| 237 | assert probe.status == health.MISSING |
| 238 | assert probe.prescription |
| 239 | |
| 240 | def test_any_pp_cli_name_gets_printing_press_prescription(self): |
| 241 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 242 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 243 | probe = health.probe_dependency("espn-pp-cli") |
| 244 | assert probe.status == health.MISSING |
| 245 | assert "install espn --cli-only" in probe.prescription |
| 246 | assert probe.owner_pkg_manager == "npx" |
| 247 | |
| 248 | |
| 249 | def test_yahoo_finance_cli_prescribes_the_pinned_catalog_install(self): |
| 250 | from lib import setup_wizard |
| 251 | |
| 252 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({})), \ |
| 253 | mock.patch.object(health, "_off_path_candidate_dirs", return_value=[]): |
| 254 | probe = health.probe_dependency("yahoo-finance-pp-cli") |
| 255 | assert probe.status == health.MISSING |
| 256 | assert probe.off_path is False |
| 257 | assert probe.prescription == ( |
| 258 | f"npx -y {setup_wizard.PRINTING_PRESS_NPM} install yahoo-finance --cli-only" |
| 259 | ) |
| 260 | assert probe.owner_pkg_manager == "npx" |
| 261 | |
| 262 | |
| 263 | class TestWindowsPrintingPressCandidates: |
| 264 | """F15 regression (docs/solutions/integration-issues/ |
| 265 | digg-cli-agent-path-setup-wizard.md): the Windows managed install dir |
| 266 | (%LOCALAPPDATA%/Programs/PrintingPress/bin) must be in the shared |
| 267 | candidate-dir source so an installed-but-off-PATH digg-pp-cli gets a |
| 268 | PATH fix, never "never installed". |
| 269 | |
| 270 | os.name is patched only in health's namespace (a delegating stub) — |
| 271 | patching the global os.name would flip pathlib to WindowsPath and break |
| 272 | Path construction on posix. |
| 273 | """ |
| 274 | |
| 275 | class _NtOs: |
| 276 | """Delegates to the real os module but reports name == 'nt'.""" |
| 277 | name = "nt" |
| 278 | |
| 279 | def __getattr__(self, attr): |
| 280 | return getattr(os, attr) |
| 281 | |
| 282 | def _nt(self): |
| 283 | return mock.patch.object(health, "os", self._NtOs()) |
| 284 | |
| 285 | def test_windows_dir_in_candidates_when_localappdata_set(self, tmp_path): |
| 286 | with self._nt(), \ |
| 287 | mock.patch.dict(os.environ, {"LOCALAPPDATA": str(tmp_path)}): |
| 288 | dirs = health._off_path_candidate_dirs() |
| 289 | assert tmp_path / "Programs" / "PrintingPress" / "bin" in dirs |
| 290 | |
| 291 | def test_windows_dir_absent_without_localappdata(self): |
| 292 | env_clean = {k: v for k, v in os.environ.items() |
| 293 | if k.lower() != "localappdata"} |
| 294 | with self._nt(), \ |
| 295 | mock.patch.dict(os.environ, env_clean, clear=True): |
| 296 | assert health.windows_printing_press_bin_dir() is None |
| 297 | |
| 298 | def test_posix_has_no_windows_dir(self, tmp_path): |
| 299 | with mock.patch.object(health, "os") as host: |
| 300 | host.name = "posix" |
| 301 | host.environ = {"LOCALAPPDATA": str(tmp_path)} |
| 302 | assert health.windows_printing_press_bin_dir() is None |
| 303 | |
| 304 | def test_windows_off_path_digg_reports_path_fix(self, tmp_path): |
| 305 | # Binary present ONLY in the PrintingPress dir: missing + off_path + |
| 306 | # PATH prescription — the documented failure mode said "never installed". |
| 307 | pp_dir = tmp_path / "Programs" / "PrintingPress" / "bin" |
| 308 | pp_dir.mkdir(parents=True) |
| 309 | binary = pp_dir / "digg-pp-cli.exe" |
| 310 | binary.write_text("#!/bin/sh\necho ok\n") |
| 311 | binary.chmod(binary.stat().st_mode | stat.S_IXUSR) |
| 312 | fake_home = tmp_path / "home" |
| 313 | with self._nt(), \ |
| 314 | mock.patch.dict(os.environ, |
| 315 | {"LOCALAPPDATA": str(tmp_path), |
| 316 | "GOPATH": str(tmp_path / "gopath")}), \ |
| 317 | mock.patch.object(health.Path, "home", return_value=fake_home), \ |
| 318 | mock.patch.object(health.shutil, "which", |
| 319 | side_effect=_which_map({"npx": "/x/npx"})): |
| 320 | probe = health.probe_dependency("digg-pp-cli") |
| 321 | assert probe.status == health.MISSING |
| 322 | assert probe.off_path is True |
| 323 | assert "PATH" in probe.prescription |
| 324 | assert str(pp_dir) in probe.prescription or str(pp_dir) in probe.detail |
| 325 | |
| 326 | @pytest.mark.parametrize("layout", ["local", "go", "gopath", "managed"]) |
| 327 | def test_setup_recognizes_windows_pp_binaries_before_installing(self, tmp_path, monkeypatch, layout): |
| 328 | from lib import setup_wizard |
| 329 | |
| 330 | home = tmp_path / "home" |
| 331 | gopath = tmp_path / "custom go" |
| 332 | appdata = tmp_path / "LocalAppData" |
| 333 | directory = { |
| 334 | "local": home / ".local" / "bin", |
| 335 | "go": home / "go" / "bin", |
| 336 | "gopath": gopath / "bin", |
| 337 | "managed": appdata / "Programs" / "PrintingPress" / "bin", |
| 338 | }[layout] |
| 339 | directory.mkdir(parents=True) |
| 340 | for name in [setup_wizard.DIGG_CLI_BIN, *(item[2] for item in setup_wizard.PP_DEFAULT_SOURCES)]: |
| 341 | binary = directory / f"{name}.exe" |
| 342 | binary.touch() |
| 343 | binary.chmod(0o755) |
| 344 | monkeypatch.setenv("GOPATH", str(gopath)) |
| 345 | monkeypatch.setenv("LOCALAPPDATA", str(appdata)) |
| 346 | monkeypatch.setattr(Path, "home", lambda: home) |
| 347 | monkeypatch.setattr(setup_wizard.shutil, "which", lambda name: None) |
| 348 | with self._nt(), mock.patch.object(setup_wizard, "_run_npx_install") as install: |
| 349 | installed, action, _, path = setup_wizard._install_digg_cli() |
| 350 | sources = setup_wizard.install_default_pp_sources() |
| 351 | assert not installed and action == "installed_off_path" |
| 352 | assert Path(path) == directory / "digg-pp-cli.exe" |
| 353 | for source, _, name in setup_wizard.PP_DEFAULT_SOURCES: |
| 354 | assert sources[source] == { |
| 355 | "installed": False, "action": "installed_off_path", "path": str(directory / f"{name}.exe"), |
| 356 | } |
| 357 | install.assert_not_called() |
| 358 | |
| 359 | |
| 360 | class TestHelperMinimumVersion: |
| 361 | """A default helper below the engine's minimum reads as outdated, with the |
| 362 | upgrade command; a helper with no minimum or an unreadable version never |
| 363 | does.""" |
| 364 | |
| 365 | def _probe(self, name, version_line): |
| 366 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({name: f"/x/{name}"})), \ |
| 367 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout=version_line)): |
| 368 | return health.probe_dependency(name) |
| 369 | |
| 370 | def test_versions_parse_as_dotted_integer_tuples(self): |
| 371 | assert health.parse_version("digg-pp-cli 2026.11.1") == (2026, 11, 1) |
| 372 | assert health.parse_version("yahoo-finance-pp-cli 1.0.0") == (1, 0, 0) |
| 373 | assert health.parse_version("digg-pp-cli v2026.10.2\n") == (2026, 10, 2) |
| 374 | assert health.parse_version("digg-pp-cli dev") is None |
| 375 | assert health.parse_version("") is None |
| 376 | |
| 377 | def test_digg_and_techmeme_have_minimums(self): |
| 378 | assert set(health.MIN_HELPER_VERSIONS) == {"digg-pp-cli", "techmeme-pp-cli"} |
| 379 | # 2026.11.1 is the first techmeme-pp-cli whose search takes --pages. |
| 380 | assert health.MIN_HELPER_VERSIONS["techmeme-pp-cli"] == (2026, 11, 1) |
| 381 | |
| 382 | def test_techmeme_before_the_dates_fix_is_outdated(self): |
| 383 | probe = self._probe("techmeme-pp-cli", "techmeme-pp-cli 2026.6.2\n") |
| 384 | assert probe.ok |
| 385 | assert probe.outdated is True |
| 386 | assert probe.prescription == health.helper_upgrade_cmd("techmeme") |
| 387 | assert "older than 2026.11.1" in probe.detail |
| 388 | |
| 389 | def test_techmeme_with_dates_but_no_pages_is_outdated(self): |
| 390 | probe = self._probe("techmeme-pp-cli", "techmeme-pp-cli 2026.9.1\n") |
| 391 | assert probe.ok |
| 392 | assert probe.outdated is True |
| 393 | assert "older than 2026.11.1" in probe.detail |
| 394 | # It still works on one page, so doctor must not call it uncallable. |
| 395 | assert "the oldest the engine can call" not in probe.detail |
| 396 | assert "first 10 results" in probe.detail |
| 397 | |
| 398 | def test_techmeme_at_the_pages_release_is_current(self): |
| 399 | probe = self._probe("techmeme-pp-cli", "techmeme-pp-cli 2026.11.1\n") |
| 400 | assert probe.outdated is False |
| 401 | assert probe.prescription == "" |
| 402 | assert probe.detail == "techmeme-pp-cli 2026.11.1" |
| 403 | |
| 404 | def test_digg_below_minimum_is_outdated_with_the_upgrade_command(self): |
| 405 | probe = self._probe("digg-pp-cli", "digg-pp-cli 2026.7.1\n") |
| 406 | assert probe.ok # it still runs; outdated is not broken |
| 407 | assert probe.outdated is True |
| 408 | assert probe.prescription == health.helper_upgrade_cmd("digg") |
| 409 | assert "2026.7.1" in probe.detail |
| 410 | assert "older than" in probe.detail |
| 411 | |
| 412 | def test_digg_at_minimum_is_current(self): |
| 413 | minimum = ".".join(str(n) for n in health.MIN_HELPER_VERSIONS["digg-pp-cli"]) |
| 414 | probe = self._probe("digg-pp-cli", f"digg-pp-cli {minimum}\n") |
| 415 | assert probe.outdated is False |
| 416 | assert probe.prescription == "" |
| 417 | assert probe.detail == f"digg-pp-cli {minimum}" |
| 418 | |
| 419 | def test_unparseable_digg_version_is_unknown_not_outdated(self): |
| 420 | probe = self._probe("digg-pp-cli", "digg-pp-cli dev\n") |
| 421 | assert probe.ok |
| 422 | assert probe.outdated is None |
| 423 | assert probe.prescription == "" |
| 424 | assert "version unknown" in probe.detail |
| 425 | assert "older than" not in probe.detail |
| 426 | |
| 427 | def test_helper_with_no_minimum_is_never_outdated(self): |
| 428 | probe = self._probe("yahoo-finance-pp-cli", "yahoo-finance-pp-cli 1.0.0\n") |
| 429 | assert probe.ok |
| 430 | assert probe.outdated is None |
| 431 | assert probe.prescription == "" |
| 432 | assert probe.detail == "yahoo-finance-pp-cli 1.0.0" |
| 433 | |
| 434 | def test_upgrade_command_is_the_pinned_catalog_install(self): |
| 435 | assert health.PRINTING_PRESS_NPM == "@mvanhorn/printing-press-library@0.1.27" |
| 436 | assert health.helper_upgrade_cmd("digg") == ( |
| 437 | f"npx -y {health.PRINTING_PRESS_NPM} install digg --cli-only" |
| 438 | ) |
| 439 | |
| 440 | def test_outdated_check_runs_no_version_exec_without_a_minimum(self): |
| 441 | with mock.patch.object(health.subproc, "run_with_timeout") as run: |
| 442 | assert health.helper_outdated("yahoo-finance-pp-cli", "/x/yahoo-finance-pp-cli") is None |
| 443 | run.assert_not_called() |
| 444 | |
| 445 | def test_outdated_check_execs_the_resolved_path(self): |
| 446 | with mock.patch.object( |
| 447 | health.subproc, "run_with_timeout", return_value=_completed(stdout="digg-pp-cli 2026.7.1"), |
| 448 | ) as run: |
| 449 | assert health.helper_outdated("digg-pp-cli", "/x/digg-pp-cli") is True |
| 450 | assert run.call_args.args[0] == ["/x/digg-pp-cli", "--version"] |
| 451 | |
| 452 | def test_outdated_check_is_unknown_when_the_version_exec_fails(self): |
| 453 | with mock.patch.object(health.subproc, "run_with_timeout", side_effect=OSError("bad exec")): |
| 454 | assert health.helper_outdated("digg-pp-cli", "/x/digg-pp-cli") is None |
| 455 | |
| 456 | def test_doctor_reports_an_outdated_digg_with_the_upgrade_command(self): |
| 457 | from lib import doctor |
| 458 | |
| 459 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"digg-pp-cli": "/x/digg-pp-cli"})), \ |
| 460 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="digg-pp-cli 2026.7.1")), \ |
| 461 | mock.patch("lib.digg.cli_searches_all_topics", return_value=None): |
| 462 | record = doctor._digg_record({}) |
| 463 | # Its searches fail, so it is not ok, but it is present, not missing. |
| 464 | assert record["status"] == health.DEGRADED |
| 465 | assert health.helper_upgrade_cmd("digg") in record["fix"] |
| 466 | assert "older than" in record["detail"] |
| 467 | |
| 468 | def test_doctor_reports_an_unreadable_digg_version_as_unknown(self): |
| 469 | from lib import doctor |
| 470 | |
| 471 | with mock.patch.object(health.shutil, "which", side_effect=_which_map({"digg-pp-cli": "/x/digg-pp-cli"})), \ |
| 472 | mock.patch.object(health.subproc, "run_with_timeout", return_value=_completed(stdout="digg-pp-cli dev")), \ |
| 473 | mock.patch("lib.digg.cli_searches_all_topics", return_value=True): |
| 474 | record = doctor._digg_record({}) |
| 475 | assert record["status"] == health.OK |
| 476 | assert record["fix"] == "" |
| 477 | assert "version unknown" in record["detail"] |
| 478 | assert "older than" not in record["detail"] |
| 479 | |
| 480 | |
| 481 | class TestRunOutcomeTaxonomy: |
| 482 | """The per-run outcome vocabulary must stay enumerated in lockstep across |
| 483 | ``health`` (constants), ``schema.RunOutcomeState`` (the export Literal), |
| 484 | ``SourceOutcome`` validation, and the pipeline's failure-specificity |
| 485 | ladder. ``payment-required`` (credit exhaustion, HTTP 402) is its own |
| 486 | state, distinct from ``auth-failed``: "top up credits" is a different |
| 487 | user action from "re-authenticate". |
| 488 | """ |
| 489 | |
| 490 | def test_payment_required_constant_and_alias(self): |
| 491 | from lib import schema |
| 492 | |
| 493 | assert health.PAYMENT_REQUIRED == "payment-required" |
| 494 | assert schema.PAYMENT_REQUIRED == health.PAYMENT_REQUIRED |
| 495 | |
| 496 | def test_literal_and_validation_enumerate_every_run_state(self): |
| 497 | import typing |
| 498 | |
| 499 | from lib import schema |
| 500 | |
| 501 | literal_states = set(typing.get_args(schema.RunOutcomeState)) |
| 502 | assert "payment-required" in literal_states |
| 503 | # Every Literal member constructs; the Literal is the export contract. |
| 504 | for state in literal_states: |
| 505 | schema.SourceOutcome(source="x", state=state) |
| 506 | with pytest.raises(ValueError): |
| 507 | schema.SourceOutcome(source="x", state="not-a-state") |
| 508 | |
| 509 | def test_payment_required_is_a_failure_state_next_to_auth_failed(self): |
| 510 | from lib import pipeline |
| 511 | |
| 512 | ladder = pipeline._FAILURE_SPECIFICITY |
| 513 | assert health.PAYMENT_REQUIRED in ladder |
| 514 | assert ladder[health.AUTH_FAILED] < ladder[health.PAYMENT_REQUIRED] < ladder[health.RATE_LIMITED] |
| 515 | |
| 516 | def test_credits_exhausted_label_is_source_aware(self): |
| 517 | assert health.credits_exhausted_label("x") == "X API credits exhausted" |
| 518 | assert health.credits_exhausted_label("reddit") == "credits exhausted" |
| 519 |