"""Tests for remote (git URL) fetching helpers — no network required."""
from abom import remote


def test_is_git_url_recognizes_schemes():
    assert remote.is_git_url("https://github.com/org/agent")
    assert remote.is_git_url("http://example.com/x/y")
    assert remote.is_git_url("git@github.com:org/agent.git")
    assert remote.is_git_url("git://example.com/x.git")
    assert remote.is_git_url("ssh://git@host/x.git")
    assert remote.is_git_url("org/agent")              # bare org/repo shorthand


def test_is_git_url_rejects_non_urls():
    assert not remote.is_git_url("just some text")
    assert not remote.is_git_url("/local/path")
    assert not remote.is_git_url("./rel")
    assert not remote.is_git_url("")


def test_normalize_expands_bare_repo():
    assert remote._normalize("org/agent") == "https://github.com/org/agent"
    # full URLs pass through unchanged
    assert remote._normalize("https://github.com/org/agent") == "https://github.com/org/agent"
    assert remote._normalize("git@github.com:org/agent.git") == "git@github.com:org/agent.git"


def test_is_mcp_url():
    assert remote.is_mcp_url("https://example.com/mcp")
    assert remote.is_mcp_url("http://localhost:8000/mcp")
    assert not remote.is_mcp_url("git@github.com:x/y.git")
    assert not remote.is_mcp_url("org/repo")


def test_mcp_to_scan_result_maps_capabilities():
    caps = {
        "server": {"name": "demo-agent", "version": "1.0.0"},
        "tools": [{"name": "lookup_order", "description": "look up"},
                  {"name": "issue_refund"}],
        "resources": [{"name": "policies", "uri": "file://kb/policies"}],
        "prompts": [{"name": "support_prompt"}],
    }
    res = remote.mcp_to_scan_result("https://x/mcp", caps)
    assert res["agent"]["name"] == "demo-agent"
    assert res["agent"]["version"] == "1.0.0"
    by_type = {}
    for c in res["components"]:
        by_type.setdefault(c["type"], []).append(c["name"])
    assert "lookup_order" in by_type["tool"] and "issue_refund" in by_type["tool"]
    assert "demo-agent" in by_type["mcpServer"]
    assert "policies" in by_type["dataSource"]
    assert "support_prompt" in by_type["prompt"]
    # provenance points at the live endpoint
    assert all(c["detected_from"] == "mcp:https://x/mcp" for c in res["components"])
