import sys
import unicodedata
from pathlib import Path

import pytest
from clarabot_nano_common.utility.path.unicode import PLATFORM_UNICODE_NORMALIZATION

from clarabot_nano_tests.testing.file import temp_path
from clarabot_nano_tests.utility.path.utility import get_platform_alternative_acute_e


@pytest.mark.skipif(not sys.platform.startswith("darwin"), reason="for darwin only")
def test_utility_path_unicode_darwin(tmpdir):
    assert PLATFORM_UNICODE_NORMALIZATION == "NFD"

    aep, aea = get_platform_alternative_acute_e()
    assert aep != aea
    assert unicodedata.normalize(PLATFORM_UNICODE_NORMALIZATION, aep) == aep
    assert unicodedata.normalize(PLATFORM_UNICODE_NORMALIZATION, aea) == aep

    d = Path(str(temp_path(tmpdir)))
    d.mkdir()

    # FS should be normalization coercing
    (d / aep).write_bytes(b"platform")
    (d / aea).write_bytes(b"alternative")
    assert set(item.name for item in d.iterdir()) == {aep}
    assert (d / aep).read_bytes() == b"alternative"
    assert (d / aea).read_bytes() == b"alternative"


@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="for not darwin only")
def test_utility_path_unicode_other(tmpdir):
    assert PLATFORM_UNICODE_NORMALIZATION == "NFC"

    aep, aea = get_platform_alternative_acute_e()
    assert aep != aea
    assert unicodedata.normalize(PLATFORM_UNICODE_NORMALIZATION, aep) == aep
    assert unicodedata.normalize(PLATFORM_UNICODE_NORMALIZATION, aea) == aep

    d = Path(str(temp_path(tmpdir)))
    d.mkdir()

    # FS should be normalization agnostic
    (d / aep).write_bytes(b"platform")
    (d / aea).write_bytes(b"alternative")
    assert set(item.name for item in d.iterdir()) == {aep, aea}
    assert (d / aep).read_bytes() == b"platform"
    assert (d / aea).read_bytes() == b"alternative"
