| 1 | """grok as a CLI-gated dependency in the shared health layer. |
| 2 | |
| 3 | The off-PATH case is confirmed real, not theoretical: on a GrokBot machine the |
| 4 | official installer placed the binary at ~/.grok/bin/grok and it had to be |
| 5 | invoked by absolute path. That is the class the Digg learning documents -- |
| 6 | "present on disk" is not "installed". |
| 7 | """ |
| 8 | |
| 9 | from lib import health, prescriptions |
| 10 | |
| 11 | |
| 12 | def test_grok_is_a_known_dependency(): |
| 13 | assert "grok" in health.KNOWN_DEPENDENCIES |
| 14 | |
| 15 | |
| 16 | def test_missing_binary_yields_an_install_prescription(monkeypatch): |
| 17 | monkeypatch.setattr(health.shutil, "which", lambda name: None) |
| 18 | monkeypatch.setattr(health, "_off_path_binary", lambda name: None) |
| 19 | probe = health.probe_dependency("grok") |
| 20 | assert probe.status == health.MISSING |
| 21 | assert probe.prescription and not probe.off_path |
| 22 | assert "reinstall" not in probe.prescription.lower() |
| 23 | |
| 24 | |
| 25 | def test_off_path_binary_yields_a_path_edit_not_an_install(monkeypatch, tmp_path): |
| 26 | binary = tmp_path / "grok" |
| 27 | binary.write_text("#!/bin/sh\n") |
| 28 | binary.chmod(0o755) |
| 29 | monkeypatch.setattr(health.shutil, "which", lambda name: None) |
| 30 | monkeypatch.setattr(health, "_off_path_binary", lambda name: binary) |
| 31 | probe = health.probe_dependency("grok") |
| 32 | assert probe.status == health.MISSING |
| 33 | assert probe.off_path is True |
| 34 | assert "PATH" in probe.prescription |
| 35 | assert "install" not in probe.prescription.lower().replace("installed", "") |
| 36 | |
| 37 | |
| 38 | def test_broken_binary_yields_a_reinstall_prescription(monkeypatch): |
| 39 | monkeypatch.setattr(health.shutil, "which", lambda name: "/usr/bin/grok") |
| 40 | |
| 41 | def boom(*a, **k): |
| 42 | raise OSError("exec format error") |
| 43 | |
| 44 | monkeypatch.setattr(health.subprocess, "run", boom) |
| 45 | probe = health.probe_dependency("grok") |
| 46 | assert probe.status == health.BROKEN |
| 47 | assert "reinstall" in probe.prescription.lower() |
| 48 | |
| 49 | |
| 50 | def test_fallback_prescription_names_the_official_installer(): |
| 51 | install, reinstall = health._FALLBACK_PRESCRIPTIONS["grok"] |
| 52 | assert "x.ai/cli/install.sh" in install |
| 53 | assert "grok login" in install |
| 54 | assert "reinstall" in reinstall.lower() |
| 55 | |
| 56 | |
| 57 | def test_registry_covers_missing_and_unauthenticated(): |
| 58 | """The not-logged-in state has no equivalent in the Digg pattern and is |
| 59 | the one users will actually hit after installing.""" |
| 60 | assert ("x", "grok_cli_missing") in prescriptions.REGISTRY |
| 61 | assert ("x", "grok_not_authenticated") in prescriptions.REGISTRY |
| 62 | |
| 63 | |
| 64 | def test_not_authenticated_fix_is_a_login_not_an_install(): |
| 65 | entry = prescriptions.REGISTRY[("x", "grok_not_authenticated")] |
| 66 | assert entry.fix_cli.startswith("grok login") |
| 67 | assert "no X account" in entry.fix_nl |
| 68 | |
| 69 | |
| 70 | def test_new_entries_carry_no_secret_looking_values(): |
| 71 | for key in (("x", "grok_cli_missing"), ("x", "grok_not_authenticated")): |
| 72 | entry = prescriptions.REGISTRY[key] |
| 73 | blob = f"{entry.cause} {entry.fix_nl} {entry.fix_cli}" |
| 74 | assert "AUTH_TOKEN=" not in blob and "API_KEY=" not in blob |
| 75 |