import os
import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
LAUNCHER = ROOT / "scripts" / "codex-state-hub-mcp.sh"


def test_codex_mcp_launcher_uses_configured_python(tmp_path: Path) -> None:
    fake_python = tmp_path / "python"
    fake_python.write_text(
        "#!/usr/bin/env bash\n"
        "printf 'python=%s\\nscript=%s\\ntransport=%s\\n' "
        '"$0" "$1" "$MCP_TRANSPORT"\n'
    )
    fake_python.chmod(0o755)
    env = os.environ.copy()
    env["STATE_HUB_PYTHON"] = str(fake_python)
    env["API_BASE"] = "http://127.0.0.1:8000"

    result = subprocess.run(
        [str(LAUNCHER)],
        cwd=ROOT,
        env=env,
        text=True,
        capture_output=True,
        check=True,
        timeout=5,
    )

    assert f"python={fake_python}" in result.stdout
    assert "script=mcp_server/codex_server.py" in result.stdout
    assert "transport=stdio" in result.stdout


def test_codex_mcp_launcher_has_no_uv_runtime_dependency() -> None:
    launcher = LAUNCHER.read_text()
    assert "uv run" not in launcher
    assert ".venv/bin/python" in launcher
