|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import json |
| 4 | +import subprocess |
4 | 5 | from typing import Any |
5 | 6 |
|
6 | 7 | from typer.testing import CliRunner |
@@ -135,3 +136,76 @@ async def fake_doctor() -> dict[str, object]: |
135 | 136 |
|
136 | 137 | assert result.exit_code == 1 |
137 | 138 | assert json.loads(result.output) == {"ok": False, "message": "broken", "hint": "fix it"} |
| 139 | + |
| 140 | + |
| 141 | +def test_install_json_success(monkeypatch: Any) -> None: |
| 142 | + calls: list[tuple[str, ...]] = [] |
| 143 | + |
| 144 | + def fake_run(command: tuple[str, ...], **_kwargs: Any) -> subprocess.CompletedProcess[str]: |
| 145 | + calls.append(command) |
| 146 | + return subprocess.CompletedProcess(command, 0, stdout="installed", stderr="") |
| 147 | + |
| 148 | + monkeypatch.setattr(cli.subprocess, "run", fake_run) |
| 149 | + |
| 150 | + result = runner.invoke(cli.app, ["install", "--format", "json"]) |
| 151 | + |
| 152 | + assert result.exit_code == 0, result.output |
| 153 | + payload = json.loads(result.output) |
| 154 | + assert payload["ok"] is True |
| 155 | + assert [tuple(step["command"]) for step in payload["steps"]] == list(cli.INSTALL_COMMANDS) |
| 156 | + assert calls == list(cli.INSTALL_COMMANDS) |
| 157 | + |
| 158 | + |
| 159 | +def test_install_json_failure(monkeypatch: Any) -> None: |
| 160 | + def fake_run(command: tuple[str, ...], **_kwargs: Any) -> subprocess.CompletedProcess[str]: |
| 161 | + return_code = 1 if command[0] == "patchright" else 0 |
| 162 | + return subprocess.CompletedProcess( |
| 163 | + command, |
| 164 | + return_code, |
| 165 | + stdout="", |
| 166 | + stderr="missing browser", |
| 167 | + ) |
| 168 | + |
| 169 | + monkeypatch.setattr(cli.subprocess, "run", fake_run) |
| 170 | + |
| 171 | + result = runner.invoke(cli.app, ["install", "--format", "json"]) |
| 172 | + |
| 173 | + assert result.exit_code == 1 |
| 174 | + payload = json.loads(result.output) |
| 175 | + assert payload["ok"] is False |
| 176 | + assert payload["steps"][0]["ok"] is True |
| 177 | + assert payload["steps"][1]["ok"] is False |
| 178 | + assert payload["steps"][1]["message"] == "missing browser" |
| 179 | + |
| 180 | + |
| 181 | +def test_install_json_handles_missing_executable(monkeypatch: Any) -> None: |
| 182 | + def fake_run(command: tuple[str, ...], **_kwargs: Any) -> subprocess.CompletedProcess[str]: |
| 183 | + if command[0] == "patchright": |
| 184 | + raise FileNotFoundError("missing patchright") |
| 185 | + return subprocess.CompletedProcess(command, 0, stdout="installed", stderr="") |
| 186 | + |
| 187 | + monkeypatch.setattr(cli.subprocess, "run", fake_run) |
| 188 | + |
| 189 | + result = runner.invoke(cli.app, ["install", "--format", "json"]) |
| 190 | + |
| 191 | + assert result.exit_code == 1 |
| 192 | + payload = json.loads(result.output) |
| 193 | + assert payload["ok"] is False |
| 194 | + assert payload["steps"][1]["ok"] is False |
| 195 | + assert payload["steps"][1]["message"] == "missing patchright" |
| 196 | + |
| 197 | + |
| 198 | +def test_install_human_output(monkeypatch: Any) -> None: |
| 199 | + def fake_run(command: tuple[str, ...], **_kwargs: Any) -> subprocess.CompletedProcess[str]: |
| 200 | + return subprocess.CompletedProcess(command, 0, stdout="installed", stderr="") |
| 201 | + |
| 202 | + monkeypatch.setattr(cli.subprocess, "run", fake_run) |
| 203 | + |
| 204 | + result = runner.invoke(cli.app, ["install"]) |
| 205 | + |
| 206 | + assert result.exit_code == 0, result.output |
| 207 | + assert "OK:" in result.output |
| 208 | + assert "playwright" in result.output |
| 209 | + assert "install" in result.output |
| 210 | + assert "chromium" in result.output |
| 211 | + assert "patchright install chromium" in result.output |
0 commit comments