"""Static checks: translation keys and preference defaults stay in sync.""" from __future__ import annotations import json import re from pathlib import Path APP_ROOT = Path(__file__).resolve().parents[1] SRC_DIR = APP_ROOT / "src" LOCALES_DIR = APP_ROOT / "config" / "locales" DEFAULTS_JSON = APP_ROOT / "config" / "preferences.default.json" REPO_ROOT = APP_ROOT.parents[1] STORE_PY = REPO_ROOT / "shared" / "src" / "orisys_app_shared" / "prefs" / "store.py" _T_CALL = re.compile(r"""\bt\(\s*["']([A-Za-z0-9_.]+)["']""") _DEFAULT_BLOCK = re.compile(r"_DEFAULTS[^=]*=\s*\{(?P.*?)\n\}", re.DOTALL) _KEY = re.compile(r"""["']([A-Za-z0-9_]+)["']\s*:""") def _load_locale(name: str) -> dict: return json.loads((LOCALES_DIR / f"{name}.json").read_text(encoding="utf-8")) def _flatten(data: dict, prefix: str = "") -> set[str]: keys: set[str] = set() for key, value in data.items(): dotted = f"{prefix}{key}" if isinstance(value, dict): keys |= _flatten(value, f"{dotted}.") else: keys.add(dotted) return keys def _referenced_keys() -> set[str]: found: set[str] = set() for path in sorted(SRC_DIR.rglob("*.py")): found |= set(_T_CALL.findall(path.read_text(encoding="utf-8"))) return found def test_locales_parity_and_used_keys(): zh = _flatten(_load_locale("zh_CN")) en = _flatten(_load_locale("en_US")) assert zh == en, { "zh_only": sorted(zh - en), "en_only": sorted(en - zh), } referenced = _referenced_keys() assert referenced, "no t() calls found — the source scan is broken" missing = sorted(key for key in referenced if key not in zh) assert not missing, f"t() keys missing from locale files: {missing}" def test_preference_defaults_match_store_defaults(): json_keys = set(json.loads(DEFAULTS_JSON.read_text(encoding="utf-8"))) source = STORE_PY.read_text(encoding="utf-8") match = _DEFAULT_BLOCK.search(source) assert match, "could not locate _DEFAULTS in prefs/store.py" store_keys = set(_KEY.findall(match.group("body"))) assert json_keys == store_keys, { "json_only": sorted(json_keys - store_keys), "store_only": sorted(store_keys - json_keys), } def test_locale_files_are_valid_utf8_json(): for name in ("zh_CN", "en_US"): data = _load_locale(name) assert isinstance(data, dict) assert data.get("app", {}).get("title")