import sys
from pathlib import Path

import pytest
from clarabot_nano_common.utility.path.common import normalize_validate_absolute_path
from clarabot_nano_common.utility.path.restrict import PathRestrictionValidator
from clarabot_nano_common.utility.path.validate import ValidationError


def test_utility_path_restriction_validator():
    if sys.platform.startswith("win"):
        paths = [
            "C:\\path\\ab",
            "\\\\?\\C:\\path\\xy",
            "//?/C:/other/path",
        ]
        others = [
            "C:/not-in",
            "E:/other/path",
        ]
    else:
        paths = [
            "/mnt/path/ab",
            "/mnt/path/xy",
            "/mnt/other/path",
        ]
        others = [
            "/opt/asd",
        ]

    v = PathRestrictionValidator(paths)
    for p in paths:
        p2 = Path(normalize_validate_absolute_path(p))
        v(p2)
        v(p2 / "yes")
        with pytest.raises(ValidationError):
            v(p2.parent)
    for o in others:
        o2 = Path(normalize_validate_absolute_path(o))
        with pytest.raises(ValidationError):
            v(o2)
        with pytest.raises(ValidationError):
            v(o2 / "yes")
        with pytest.raises(ValidationError):
            v(o2.parent)

    v = PathRestrictionValidator([])
    for x in paths + others:
        x2 = Path(normalize_validate_absolute_path(x))
        v(x2)
        v(x2 / "yes")
        v(x2.parent)
