import pytest
import mock

from voluptuous import Schema, Any

from dvc import analytics
from dvc.utils.compat import str, builtin_str


@pytest.fixture
def tmp_global_config(tmp_path):
    """
    Fixture to prevent modifying the actual global config
    """
    with mock.patch(
        "dvc.config.Config.get_global_config_dir", return_value=tmp_path
    ):
        yield


def test_collect(tmp_global_config):
    schema = Schema(
        {
            "cmd_class": Any(str, None),
            "cmd_return_code": Any(int, None),
            "dvc_version": Any(builtin_str, str),
            "is_binary": bool,
            "scm_class": Any("Git", None),
            "user_id": Any(builtin_str, str),
            "system_info": dict,
        }
    )

    report = analytics.collect(return_code=0)
    assert schema(report)


@mock.patch("requests.post")
def test_send(mock_post, tmp_path):
    url = "https://analytics.dvc.org"
    report = {"name": "dummy report"}

    analytics.send(report)
    mock_post.assert_called_with(url, json=report, timeout=5)


@pytest.mark.parametrize(
    "config, result",
    [
        ({}, True),
        ({"analytics": "false"}, False),
        ({"analytics": "true"}, True),
        ({"unknown": "broken"}, True),
        ({"analytics": "false", "unknown": "broken"}, False),
    ],
)
def test_is_enabled(dvc_repo, config, result, monkeypatch):
    configobj = dvc_repo.config._repo_config
    configobj["core"] = config
    configobj.write()

    # reset DVC_TEST env var, which affects `is_enabled()`
    monkeypatch.delenv("DVC_TEST")

    assert result == analytics.is_enabled()


def test_find_or_create_user_id(tmp_global_config):
    created = analytics.find_or_create_user_id()
    found = analytics.find_or_create_user_id()

    assert created == found

@@ -18,16 +18,44 @@
 ort mock
+%0Aimport platform%0Aimport json
 %0A%0Afrom v
@@ -1645,16 +1645,792 @@
 led()%0A%0A%0A
+def test_system_info():%0A    schema = Schema(%7B%22os%22: Any(%22windows%22, %22mac%22, %22linux%22)%7D)%0A%0A    system = platform.system()%0A%0A    if system == %22Windows%22:%0A        schema = schema.extend(%0A            %7B%0A                %22windows_version_build%22: int,%0A                %22windows_version_major%22: int,%0A                %22windows_version_minor%22: int,%0A                %22windows_version_service_pack%22: str,%0A            %7D%0A        )%0A%0A    if system == %22Darwin%22:%0A        schema = schema.extend(%7B%22mac_version%22: str%7D)%0A%0A    if system == %22Linux%22:%0A        schema = schema.extend(%0A            %7B%0A                %22linux_distro%22: str,%0A                %22linux_distro_like%22: Any(str, None),%0A                %22linux_distro_version%22: Any(str, None),%0A            %7D%0A        )%0A%0A    assert schema(analytics.system_info())%0A%0A%0A
 def test

add unit test for analytics.system_info