Files
wehub-resource-sync b7f52be4c9
CI / Run CI (push) Has been cancelled
CI / check-backend (push) Has been cancelled
CI / check-frontend (push) Has been cancelled
CI / tests (push) Has been cancelled
CI / e2e-tests (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:48:47 +08:00

128 lines
4.4 KiB
Python

import json
from pathlib import Path
import pytest
from chainlit import config as chainlit_config
from chainlit.config import ChainlitConfig
@pytest.fixture
def translation_dir(tmp_path: Path) -> Path:
"""Minimal translation directory with a controlled set of locale files."""
t_dir = tmp_path / "translations"
t_dir.mkdir()
files: dict[str, dict] = {
"en-US.json": {"greeting": "Hello"},
"es.json": {"greeting": "Hola"},
"da-DK.json": {"greeting": "Hej"},
"de-DE.json": {"greeting": "Hallo"},
"zh-CN.json": {"greeting": "你好 CN"},
"zh-TW.json": {"greeting": "你好 TW"},
}
for filename, content in files.items():
(t_dir / filename).write_text(json.dumps(content), encoding="utf-8")
return t_dir
class TestLoadTranslation:
"""Regression tests for the load_translation fallback chain."""
def test_exact_match_regional(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Exact regional locale (da-DK) resolves directly to its file."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("da-DK") == {"greeting": "Hej"}
def test_exact_match_base(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Exact base locale (es) resolves directly to its file."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("es") == {"greeting": "Hola"}
def test_parent_fallback(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Regional locale (es-419) falls back to base file (es.json) when no exact match."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("es-419") == {"greeting": "Hola"}
def test_regional_variant_lookup(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Base locale (da) resolves to regional file (da-DK.json) when no exact match exists."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("da") == {"greeting": "Hej"}
def test_regional_variant_lookup_de(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Base locale (de) resolves to regional file (de-DE.json) via variant lookup."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("de") == {"greeting": "Hallo"}
def test_regional_variant_sorted_deterministic(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""When multiple regional variants exist, the first sorted match (zh-CN) is returned."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("zh") == {"greeting": "你好 CN"}
def test_default_fallback_unknown_locale(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Completely unknown locale (xx) falls back to en-US."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("xx") == {"greeting": "Hello"}
def test_default_fallback_base_without_regional_variant(
self,
test_config: ChainlitConfig,
translation_dir: Path,
monkeypatch: pytest.MonkeyPatch,
):
"""Base locale (fr) with no matching file at all falls back to en-US."""
monkeypatch.setattr(
chainlit_config, "config_translation_dir", str(translation_dir)
)
assert test_config.load_translation("fr") == {"greeting": "Hello"}