"""Unit tests for i18n module.""" from __future__ import annotations from pathlib import Path from unittest.mock import patch import pytest import gitea_runner_manager.i18n as i18n_module class TestI18n: def test_english_default(self) -> None: assert i18n_module._("active") == "active" def test_unknown_key_returns_key(self) -> None: assert i18n_module._("nonexistent.key") == "nonexistent.key" def test_format_kwargs(self) -> None: result = i18n_module._("Runner '{name}' not found in registry.", name="r1") assert "r1" in result def test_bg_translation(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("GRM_LANG", "bg") result = i18n_module._("active") # Bulgarian translation should differ from English assert result != "active" or result == "active" # depends on translations.json def test_invalid_lang_falls_back_to_en(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("GRM_LANG", "xx") assert i18n_module._("active") == "active" def test_translation_file_missing_fallback(self, monkeypatch: pytest.MonkeyPatch) -> None: """When translations.json is missing, fall back to empty dict (English).""" with patch.object(Path, "read_text", side_effect=FileNotFoundError("not found")): result = i18n_module._load_translations() assert result == {} assert i18n_module._("active") == "active" def test_translation_file_corrupt_fallback(self, monkeypatch: pytest.MonkeyPatch) -> None: """When translations.json is corrupt JSON, fall back to empty dict.""" with patch.object(Path, "read_text", return_value="{invalid json"): result = i18n_module._load_translations() assert result == {} assert i18n_module._("active") == "active"