"""``_pyenv`` — the project-interpreter resolver shared by the catalog + warden.

The whole point is that a project's tests run under *its own* ``.venv`` (its
test-deps, its ``minversion``), not under whatever runs testctl — so these pin
the venv preference and the current-interpreter fallback.
"""

from __future__ import annotations

import sys
from pathlib import Path

from mgf.test_web._pyenv import project_python, pytest_cmd


def _make_venv(root: Path) -> Path:
    venv_py = root / ".venv" / "bin" / "python"
    venv_py.parent.mkdir(parents=True)
    venv_py.write_text("#!/bin/sh\n", encoding="utf-8")
    return venv_py


def test_prefers_the_project_venv_interpreter_when_present(tmp_path: Path) -> None:
    venv_py = _make_venv(tmp_path)
    assert project_python(tmp_path) == str(venv_py)
    assert pytest_cmd(tmp_path) == [str(venv_py), "-m", "pytest"]


def test_falls_back_to_the_current_interpreter_without_a_venv(tmp_path: Path) -> None:
    assert project_python(tmp_path) == sys.executable
    assert pytest_cmd(tmp_path) == [sys.executable, "-m", "pytest"]
