import sys

import pytest
from clarabot_nano_common.utility.path.validate import ValidationError, sanitize_name


def test_utility_path_validate_sanitize_name_universal():
    insane = "".join(chr(i) for i in range(128))
    sane = (
        "________________________________ !_#$%&'()_+,-._0123456789_;_=__"
        "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[_]___abcdefghijklmnopqrstuvwxyz{_}~_"
    )
    assert sanitize_name(insane) == sane
    assert (
        sanitize_name(insane, "")
        == " !#$%&'()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz{}~"
    )
    for n in ["|", ".|", ".||.|"]:
        with pytest.raises(ValidationError):
            sanitize_name(n, "")
        assert sanitize_name(n) == n.replace("|", "_")
    for n in [""]:
        with pytest.raises(ValidationError):
            sanitize_name(n, "")
        with pytest.raises(ValidationError):
            sanitize_name(n)


@pytest.mark.skipif(sys.platform.startswith("win"), reason="for posix only")
def test_utility_path_validate_sanitize_name_posix():
    for n in [".|."]:
        with pytest.raises(ValidationError):
            sanitize_name(n, "")
        assert sanitize_name(n) == n.replace("|", "_")
    assert sanitize_name(".| ", "") == ". "
    assert sanitize_name(".|. ", "") == ".. "
    assert sanitize_name(".| .", "") == ". ."


@pytest.mark.skipif(not sys.platform.startswith("win"), reason="for windows only")
def test_utility_path_validate_sanitize_name_nt():
    for n in [".| ", ".|.", ".|. ", ".| ."]:
        with pytest.raises(ValidationError):
            sanitize_name(n, "")
        with pytest.raises(ValidationError):
            sanitize_name(n)
