import json
from importlib.metadata import distribution
from pathlib import Path

import unified_agent


def test_unified_agent_is_loaded_from_the_external_dependency() -> None:
    imported = Path(unified_agent.__file__).resolve()
    old_embedded_copy = Path(__file__).resolve().parents[2] / "unified_agent"
    installed = distribution("unified-agent")
    direct_url = json.loads(installed.read_text("direct_url.json") or "{}")
    vcs_info = direct_url.get("vcs_info", {})

    # unified_agent must resolve to the installed external dependency, never the
    # repo-root vendored copy that can shadow it when Python runs from the root.
    assert not imported.is_relative_to(old_embedded_copy)
    # It is installed from the git wrapper repo (a direct VCS URL), not a path.
    assert vcs_info.get("vcs") == "git"
    assert "UnifedAgentWrapper" in direct_url.get("url", "")
    assert unified_agent.__version__ == installed.version == "0.3.0"
