from __future__ import annotations import json from pathlib import Path from bench_builder.lite_runtime import inspect_lite_runtime class _Completed: def __init__(self, stdout: str, *, returncode: int = 0, stderr: str = "") -> None: self.stdout = stdout self.returncode = returncode self.stderr = stderr def _ready_capabilities(tmp_path: Path): project = tmp_path / "project" python = project / ".venv" / "Scripts" / "python.exe" node = project / ".node" / "node.exe" python.parent.mkdir(parents=True) node.parent.mkdir(parents=True) python.write_bytes(b"python") node.write_bytes(b"node") calls: list[tuple[tuple[str, ...], dict[str, object]]] = [] def fake_run(argv, **kwargs): calls.append((tuple(argv), kwargs)) assert kwargs["shell"] is False if tuple(argv) == (str(python.resolve()), "--version"): return _Completed("Python 3.12.4\n") if tuple(argv) == (str(node.resolve()), "--version"): return _Completed("v22.18.0\n") assert tuple(argv[:2]) == (str(python.resolve()), "-c") return _Completed( json.dumps( { "playwright_version": "1.62.0", "chromium_installed": True, } ) ) report = inspect_lite_runtime( project, python_executable=python, node_executable=node, runner=fake_run, ) return report, calls, project def test_capability_inspection_uses_argv_and_returns_frontend_safe_report(tmp_path, monkeypatch): monkeypatch.setenv("ULTRA_SECRET_TOKEN", "do-not-leak") report, calls, project = _ready_capabilities(tmp_path) assert report.project_venv_ready is True assert report.node_ready is True assert report.playwright_ready is True assert report.chromium_ready is True assert report.static_web_ready is True assert report.node_web_ready is True assert report.python_version == "3.12.4" assert report.node_version == "22.18.0" assert report.playwright_version == "1.62.0" assert len(calls) == 3 serialized = json.dumps(report.to_dict(), sort_keys=True) assert str(project) not in serialized assert "do-not-leak" not in serialized assert "executable" not in serialized def test_external_python_is_not_admitted_as_project_venv(tmp_path): project = tmp_path / "project" project.mkdir() external_python = tmp_path / "elsewhere" / "python.exe" external_python.parent.mkdir() external_python.write_bytes(b"python") def must_not_run(*_args, **_kwargs): raise AssertionError("an external Python executable must not be invoked") report = inspect_lite_runtime( project, python_executable=external_python, runner=must_not_run, ) assert report.project_venv_ready is False assert report.static_web_ready is False assert report.diagnostics == ("Project-owned Python venv is unavailable.",) assert str(external_python) not in json.dumps(report.to_dict())