|
| 1 | +"""The interactive "update now?" prompt that the startup notice offers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | +import time |
| 7 | +import types |
| 8 | + |
| 9 | +from rich.console import Console |
| 10 | + |
| 11 | +from aai_cli.core import config, procs, stdio |
| 12 | +from aai_cli.ui import output, theme, update_check |
| 13 | + |
| 14 | + |
| 15 | +def _tty_console() -> tuple[Console, io.StringIO]: |
| 16 | + # A theme-aware console reporting as a terminal, color env pinned for stable output |
| 17 | + # (mirrors the helper in test_update_check.py). |
| 18 | + buf = io.StringIO() |
| 19 | + return theme.make_console(file=buf, force_terminal=True, width=80, _environ={}), buf |
| 20 | + |
| 21 | + |
| 22 | +def test_resolve_upgrade_command_uses_detected_channel(monkeypatch): |
| 23 | + monkeypatch.setattr(update_check, "detect_upgrade_command", lambda: "brew upgrade assembly") |
| 24 | + assert update_check.resolve_upgrade_command() == "brew upgrade assembly" |
| 25 | + |
| 26 | + |
| 27 | +def test_resolve_upgrade_command_falls_back_to_install_script(monkeypatch): |
| 28 | + # Unknown install channel -> the canonical curl|sh installer, not an empty string. |
| 29 | + monkeypatch.setattr(update_check, "detect_upgrade_command", lambda: "") |
| 30 | + command = update_check.resolve_upgrade_command() |
| 31 | + assert command == update_check._INSTALL_SCRIPT_COMMAND |
| 32 | + assert "install.sh" in command |
| 33 | + |
| 34 | + |
| 35 | +def test_upgrade_argv_runs_install_script_through_a_shell(): |
| 36 | + # The fallback is a pipeline (curl … | sh), so it must go through `sh -c`, not be |
| 37 | + # split into bare argv (which would hand `|` and `sh` to curl as arguments). |
| 38 | + argv = update_check._upgrade_argv(update_check._INSTALL_SCRIPT_COMMAND) |
| 39 | + assert argv == ["sh", "-c", update_check._INSTALL_SCRIPT_COMMAND] |
| 40 | + |
| 41 | + |
| 42 | +def test_upgrade_argv_splits_package_manager_command(): |
| 43 | + assert update_check._upgrade_argv("brew upgrade assembly") == ["brew", "upgrade", "assembly"] |
| 44 | + |
| 45 | + |
| 46 | +def test_run_foreground_inherits_stdio_and_returns_status(monkeypatch): |
| 47 | + calls = {} |
| 48 | + |
| 49 | + def fake_run(argv, *, check): |
| 50 | + calls["argv"] = argv |
| 51 | + calls["check"] = check |
| 52 | + return types.SimpleNamespace(returncode=7) |
| 53 | + |
| 54 | + monkeypatch.setattr("aai_cli.core.procs.subprocess.run", fake_run) |
| 55 | + |
| 56 | + assert procs.run_foreground(["brew", "upgrade", "assembly"]) == 7 |
| 57 | + assert calls["argv"] == ["brew", "upgrade", "assembly"] |
| 58 | + assert calls["check"] is False # exit status is inspected, never raised |
| 59 | + |
| 60 | + |
| 61 | +def _enable_prompt(tmp_path, monkeypatch) -> io.StringIO: |
| 62 | + """Cache a newer version, a tty stderr console, and an interactive stdin so the |
| 63 | + update notice renders and the upgrade prompt is reachable.""" |
| 64 | + monkeypatch.setattr(config, "config_dir", lambda: tmp_path) |
| 65 | + config.set_update_cache(last_check=time.time(), latest_version="9.9.9") |
| 66 | + con, buf = _tty_console() |
| 67 | + monkeypatch.setattr(output, "error_console", con) |
| 68 | + monkeypatch.delenv("CI", raising=False) |
| 69 | + monkeypatch.delenv(update_check.ENV_DISABLED, raising=False) |
| 70 | + monkeypatch.setattr(stdio, "stdin_is_tty", lambda: True) |
| 71 | + return buf |
| 72 | + |
| 73 | + |
| 74 | +def test_prompt_runs_upgrade_when_confirmed(tmp_path, monkeypatch): |
| 75 | + buf = _enable_prompt(tmp_path, monkeypatch) |
| 76 | + monkeypatch.setattr(update_check, "detect_upgrade_command", lambda: "brew upgrade assembly") |
| 77 | + |
| 78 | + confirm = {} |
| 79 | + |
| 80 | + def fake_confirm(text, *, default, err): |
| 81 | + confirm["text"] = text |
| 82 | + confirm["default"] = default |
| 83 | + confirm["err"] = err |
| 84 | + return True |
| 85 | + |
| 86 | + monkeypatch.setattr(update_check.typer, "confirm", fake_confirm) |
| 87 | + |
| 88 | + ran = {} |
| 89 | + |
| 90 | + def fake_run_foreground(argv): |
| 91 | + ran["argv"] = argv |
| 92 | + return 0 |
| 93 | + |
| 94 | + monkeypatch.setattr(procs, "run_foreground", fake_run_foreground) |
| 95 | + |
| 96 | + update_check.maybe_notify(json_mode=False) |
| 97 | + |
| 98 | + assert ran["argv"] == ["brew", "upgrade", "assembly"] # the detected channel ran |
| 99 | + assert "Update now?" in confirm["text"] # the prompt actually asks |
| 100 | + assert confirm["default"] is False # default-No: a bare Enter declines |
| 101 | + assert confirm["err"] is True # prompt rides stderr, like the notice |
| 102 | + out = buf.getvalue() |
| 103 | + assert "Updated to" in out |
| 104 | + assert "9.9.9" in out |
| 105 | + assert "Restart" in out # tells the user the new binary takes over next run |
| 106 | + |
| 107 | + |
| 108 | +def test_prompt_skips_upgrade_when_declined(tmp_path, monkeypatch): |
| 109 | + buf = _enable_prompt(tmp_path, monkeypatch) |
| 110 | + monkeypatch.setattr(update_check.typer, "confirm", lambda *a, **k: False) |
| 111 | + |
| 112 | + ran = [] |
| 113 | + |
| 114 | + def fake_run_foreground(argv): |
| 115 | + ran.append(argv) |
| 116 | + return 0 |
| 117 | + |
| 118 | + monkeypatch.setattr(procs, "run_foreground", fake_run_foreground) |
| 119 | + |
| 120 | + update_check.maybe_notify(json_mode=False) |
| 121 | + |
| 122 | + assert ran == [] # declining runs nothing |
| 123 | + assert "Update available" in buf.getvalue() # the notice still showed |
| 124 | + |
| 125 | + |
| 126 | +def test_no_upgrade_prompt_when_stdin_not_a_tty(tmp_path, monkeypatch): |
| 127 | + buf = _enable_prompt(tmp_path, monkeypatch) |
| 128 | + monkeypatch.setattr(stdio, "stdin_is_tty", lambda: False) # piped/redirected stdin |
| 129 | + |
| 130 | + asked = [] |
| 131 | + monkeypatch.setattr(update_check.typer, "confirm", lambda *a, **k: asked.append(True)) |
| 132 | + |
| 133 | + update_check.maybe_notify(json_mode=False) |
| 134 | + |
| 135 | + assert asked == [] # a non-interactive stdin is never prompted |
| 136 | + assert "Update available" in buf.getvalue() # but the notice still renders |
| 137 | + |
| 138 | + |
| 139 | +def test_prompt_reports_failure_when_upgrade_errors(tmp_path, monkeypatch): |
| 140 | + buf = _enable_prompt(tmp_path, monkeypatch) |
| 141 | + monkeypatch.setattr(update_check, "detect_upgrade_command", lambda: "brew upgrade assembly") |
| 142 | + monkeypatch.setattr(update_check.typer, "confirm", lambda *a, **k: True) |
| 143 | + monkeypatch.setattr(procs, "run_foreground", lambda argv: 3) # non-zero exit |
| 144 | + |
| 145 | + update_check.maybe_notify(json_mode=False) |
| 146 | + |
| 147 | + out = buf.getvalue() |
| 148 | + assert "Update failed" in out |
| 149 | + assert "brew upgrade assembly" in out # the command to re-run by hand |
| 150 | + |
| 151 | + |
| 152 | +def test_confirm_upgrade_treats_aborted_prompt_as_no(monkeypatch): |
| 153 | + # Ctrl-C (Abort) or Ctrl-D (EOFError) at the prompt must read as "no", never crash. |
| 154 | + for exc in (update_check.typer.Abort, EOFError): |
| 155 | + |
| 156 | + def boom(*a, _exc=exc, **k): |
| 157 | + raise _exc() |
| 158 | + |
| 159 | + monkeypatch.setattr(update_check.typer, "confirm", boom) |
| 160 | + assert update_check._confirm_upgrade() is False |
0 commit comments