from pathlib import Path
import pytest
def test_find_rust_binary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
import os
import shutil
from coreason_meta_engineering.mcp_server import find_rust_binary
old_binary = os.environ.get("COREASON_META_MCP_BINARY")
old_root = os.environ.get("COREASON_WORKSPACE_ROOT")
try:
test_bin = tmp_path / "custom-meta-mcp"
test_bin.write_text("")
os.environ["COREASON_META_MCP_BINARY"] = str(test_bin)
assert find_rust_binary() == str(test_bin.resolve())
os.environ["COREASON_META_MCP_BINARY"] = str(tmp_path / "nonexistent-bin")
if "COREASON_WORKSPACE_ROOT" in os.environ:
del os.environ["COREASON_WORKSPACE_ROOT"]
monkeypatch.setattr(shutil, "which", lambda _x: None)
assert find_rust_binary() is None
os.environ.pop("COREASON_META_MCP_BINARY", None)
workspace_dir = tmp_path / "workspace"
os.environ["COREASON_WORKSPACE_ROOT"] = str(workspace_dir)
target_dir = workspace_dir / "coreason-meta-engineering" / "target" / "release"
target_dir.mkdir(parents=True, exist_ok=True)
sibling_bin = target_dir / "coreason-meta-mcp"
sibling_bin.write_text("")
assert find_rust_binary() == str(sibling_bin.resolve())
os.environ.pop("COREASON_WORKSPACE_ROOT", None)
monkeypatch.setattr(shutil, "which", lambda _x: "/usr/bin/coreason-meta-mcp")
assert find_rust_binary() == "/usr/bin/coreason-meta-mcp"
finally:
if old_binary is not None:
os.environ["COREASON_META_MCP_BINARY"] = old_binary
else:
os.environ.pop("COREASON_META_MCP_BINARY", None)
if old_root is not None:
os.environ["COREASON_WORKSPACE_ROOT"] = old_root
else:
os.environ.pop("COREASON_WORKSPACE_ROOT", None)