"""LanguageValidator — case-insensitive codes and canonical form (zh-CN fix)."""

import pytest

from middleware.validation import LanguageValidator, ValidationError


class TestLanguageValidatorCaseInsensitive:
    def test_zh_cn_mixed_case_accepted(self):
        assert LanguageValidator.validate("zh-CN") == "zh-CN"

    def test_zh_cn_lowercase_normalized(self):
        assert LanguageValidator.validate("zh-cn") == "zh-CN"

    def test_zh_cn_uppercase_normalized(self):
        assert LanguageValidator.validate("ZH-CN") == "zh-CN"

    def test_zh_tw_accepted(self):
        assert LanguageValidator.validate("zh-TW") == "zh-TW"
        assert LanguageValidator.validate("zh-tw") == "zh-TW"

    def test_alias_chinese(self):
        assert LanguageValidator.validate("chinese") == "zh-CN"

    def test_alias_tw(self):
        assert LanguageValidator.validate("tw") == "zh-TW"

    def test_plain_codes_unchanged(self):
        assert LanguageValidator.validate("en") == "en"
        assert LanguageValidator.validate("fr") == "fr"

    def test_auto_accepted(self):
        assert LanguageValidator.validate("auto") == "auto"

    def test_unknown_code_rejected(self):
        with pytest.raises(ValidationError):
            LanguageValidator.validate("xx")

    def test_unknown_variant_rejected(self):
        with pytest.raises(ValidationError):
            LanguageValidator.validate("zz-ZZ")

    def test_empty_rejected(self):
        with pytest.raises(ValidationError):
            LanguageValidator.validate("")
