forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_locales.py
More file actions
88 lines (63 loc) · 3.25 KB
/
Copy pathtest_locales.py
File metadata and controls
88 lines (63 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Tests for the per-language data bundles and completeness gate (FIX-09).
``docs/ideation/02-large-scale-fixes.md`` FIX-09: EN/ES data lives in one
``locales/<lang>/bundle.yaml`` per language, and a load-time completeness validator
keyed off ``languages.supported`` fails config load — not render — when a supported
language's bundle is missing or missing a key the reference bundle defines.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from sprout import locales
from sprout.config import Config
def test_available_languages_has_reference_first() -> None:
langs = locales.available_languages()
assert langs[0] == locales.REFERENCE_LANGUAGE == "en"
assert set(langs) == {"en", "es"}
def test_load_bundle_en_and_es_share_the_same_key_shape() -> None:
en_keys = locales._flatten_keys(locales.load_bundle("en"))
es_keys = locales._flatten_keys(locales.load_bundle("es"))
# es may carry no *more* required-schema keys than en defines missing, but must
# carry at least everything en defines (the completeness gate's own invariant).
assert en_keys <= es_keys
def test_load_bundle_missing_language_raises() -> None:
with pytest.raises(locales.LocaleCompletenessError, match="no locale bundle"):
locales.load_bundle("xx")
def test_validate_completeness_passes_for_default_languages() -> None:
locales.validate_completeness(["en", "es"])
def test_validate_completeness_empty_is_a_noop() -> None:
locales.validate_completeness([])
def test_validate_completeness_fails_for_language_with_no_bundle() -> None:
with pytest.raises(locales.LocaleCompletenessError, match="no locale bundle"):
locales.validate_completeness(["en", "fr"])
def test_validate_completeness_fails_for_incomplete_stub_bundle(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""A stub ``fr/`` bundle that exists but is missing keys the reference (en) bundle
defines fails the completeness gate rather than silently falling back to English —
this is FIX-09's stated "excellent looks like" bar.
"""
en_dir = tmp_path / "en"
en_dir.mkdir()
(en_dir / "bundle.yaml").write_text(
"prompts:\n refusal: 'no'\n disclosure: 'no'\n", encoding="utf-8"
)
fr_dir = tmp_path / "fr"
fr_dir.mkdir()
(fr_dir / "bundle.yaml").write_text("prompts:\n refusal: 'non'\n", encoding="utf-8")
monkeypatch.setattr(locales, "_LOCALES_DIR", tmp_path)
locales.load_bundle.cache_clear()
try:
with pytest.raises(locales.LocaleCompletenessError, match="missing keys"):
locales.validate_completeness(["en", "fr"])
finally:
locales.load_bundle.cache_clear()
def test_config_rejects_unsupported_language_with_no_bundle() -> None:
with pytest.raises(locales.LocaleCompletenessError):
Config.model_validate({"languages": {"supported": ["en", "fr"]}})
def test_merged_list_unions_and_dedupes_preserving_order() -> None:
merged = locales.merged_list("eval", "poison_terms", ("en", "es"))
assert merged == ["poison", "envenen", "intoxica"]
def test_by_lang_returns_one_value_per_language() -> None:
values = locales.by_lang("prompts", "refusal", ("en", "es"))
assert set(values) == {"en", "es"}
assert values["en"] != values["es"]