# ABOUTME: Tests for locale detection and string lookup. # ABOUTME: Verifies DE/EN string tables and locale fallback behavior. from pathlib import Path from unittest.mock import patch from moonset.i18n import Strings, detect_locale, load_strings class TestDetectLocale: """Tests for locale detection from environment and config files.""" @patch.dict("os.environ", {"LANG": "de_DE.UTF-8"}) def test_detects_german_from_env(self) -> None: assert detect_locale() == "de" @patch.dict("os.environ", {"LANG": "en_US.UTF-8"}) def test_detects_english_from_env(self) -> None: assert detect_locale() == "en" @patch.dict("os.environ", {"LANG": ""}) def test_reads_locale_conf_when_env_empty(self, tmp_path: Path) -> None: conf = tmp_path / "locale.conf" conf.write_text("LANG=de_DE.UTF-8\n") assert detect_locale(locale_conf_path=conf) == "de" @patch.dict("os.environ", {}, clear=True) def test_reads_locale_conf_when_env_unset(self, tmp_path: Path) -> None: conf = tmp_path / "locale.conf" conf.write_text("LANG=en_GB.UTF-8\n") assert detect_locale(locale_conf_path=conf) == "en" @patch.dict("os.environ", {"LANG": "C"}) def test_c_locale_falls_back_to_english(self) -> None: assert detect_locale() == "en" @patch.dict("os.environ", {"LANG": "POSIX"}) def test_posix_locale_falls_back_to_english(self) -> None: assert detect_locale() == "en" @patch.dict("os.environ", {}, clear=True) def test_missing_conf_falls_back_to_english(self) -> None: assert detect_locale(locale_conf_path=Path("/nonexistent")) == "en" @patch.dict("os.environ", {"LANG": "fr_FR.UTF-8"}) def test_detects_unsupported_locale(self) -> None: assert detect_locale() == "fr" class TestLoadStrings: """Tests for string table loading.""" def test_loads_german_strings(self) -> None: strings = load_strings("de") assert isinstance(strings, Strings) assert strings.lock_label == "Sperren" def test_loads_english_strings(self) -> None: strings = load_strings("en") assert isinstance(strings, Strings) assert strings.lock_label == "Lock" def test_unknown_locale_falls_back_to_english(self) -> None: strings = load_strings("fr") assert strings.lock_label == "Lock" def test_all_string_fields_are_nonempty(self) -> None: for locale in ("de", "en"): strings = load_strings(locale) for field_name in Strings.__dataclass_fields__: value = getattr(strings, field_name) assert value, f"{locale}: {field_name} is empty" def test_confirm_yes_no_present(self) -> None: strings = load_strings("de") assert strings.confirm_yes == "Ja" assert strings.confirm_no == "Abbrechen" def test_error_messages_present(self) -> None: strings = load_strings("en") assert "failed" in strings.lock_failed.lower() assert "failed" in strings.logout_failed.lower() assert "failed" in strings.hibernate_failed.lower() assert "failed" in strings.reboot_failed.lower() assert "failed" in strings.shutdown_failed.lower()