coreason-meta-engineering 0.1.0

Rust port of the CoReason Agentic Forge & AST Manipulation Layer
Documentation
# Copyright (c) 2026 CoReason, Inc
#
# This software is proprietary and dual-licensed
# Licensed under the Prosperity Public License 3.0 (the "License")
# A copy of the license is available at <https://prosperitylicense.com/versions/3.0.0>
# For details, see the LICENSE file
# Commercial use beyond a 30-day trial requires a separate license
#
# Source Code: <https://github.com/CoReason-AI/coreason-meta-engineering>

"""Tests for mcp_server.py."""

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:
        # Case 1: Environment variable set and exists
        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())

        # Case 2: Environment variable set but does not exist
        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

        # Case 3: Sibling target directory
        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())

        # Case 4: Fallback to shutil.which
        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)