-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_i18n.py
More file actions
437 lines (339 loc) · 14.6 KB
/
Copy pathtest_i18n.py
File metadata and controls
437 lines (339 loc) · 14.6 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""The localization gates, and the negative controls that prove they bite.
The rule this file exists to enforce is narrow and non-negotiable: a machine
translation must never render as though a person checked it. Every assertion
below either pins that rule or pins a gate that would catch it being broken,
and each gate has a case where it fails — a gate nobody has watched fail is a
gate nobody should trust.
"""
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
from types import ModuleType
from typing import Any
import pytest
from contextsafe.canonical import canonical_json
from contextsafe.errors import ContextSafeError
from contextsafe.i18n import (
PSEUDO_LOCALE,
SOURCE_LOCALE,
Catalog,
Message,
ReviewClaim,
ReviewStatus,
Surface,
TranslationReview,
available_locales,
load_catalog,
pseudolocalize,
source_catalog,
)
REPO_ROOT = Path(__file__).resolve().parents[1]
GATE_PATH = REPO_ROOT / "tools" / "i18n_gate.py"
def _load_gate() -> ModuleType:
spec = importlib.util.spec_from_file_location("i18n_gate", GATE_PATH)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
gate = _load_gate()
def _reviewed_catalog() -> Catalog:
"""A catalog identical to the source one but marked human-reviewed."""
source = source_catalog()
return Catalog(
locale="xx-XX",
source_locale=SOURCE_LOCALE,
review=TranslationReview(
status=ReviewStatus.HUMAN,
reviewed_by="A named reviewer",
reviewed_at="2026-01-01",
note="Fixture catalog used only by tests.",
),
messages={
key: Message(
key=key,
text=message.text,
review=ReviewStatus.HUMAN,
locale="xx-XX",
)
for key, message in source.messages.items()
},
)
def test_the_source_locale_contains_no_translation() -> None:
"""en-US is authored, so nothing in it may claim to be translated."""
catalog = source_catalog()
assert catalog.messages
assert all(
message.review is ReviewStatus.SOURCE for message in catalog.messages.values()
)
assert not catalog.is_machine_translated
def test_the_only_shipped_translation_is_unreviewed_today() -> None:
"""B-042 has not happened, and the catalog must not pretend otherwise."""
catalog = load_catalog("es-US")
assert catalog.is_machine_translated
assert catalog.review.status is ReviewStatus.MACHINE
assert catalog.review.reviewed_by is None
assert catalog.review.reviewed_at is None
assert not catalog.review.human_reviewed
def test_a_surface_claiming_review_refuses_an_unreviewed_string() -> None:
"""The gate, at its smallest: the claim and the string must agree."""
surface = Surface(
name="test", catalog=load_catalog("es-US"), claim=ReviewClaim.HUMAN_REVIEWED
)
with pytest.raises(ContextSafeError) as excinfo:
surface.message("page.heading")
assert excinfo.value.code == "unreviewed_string_on_reviewed_surface"
def test_a_surface_claiming_review_accepts_a_reviewed_string() -> None:
"""The other half: a real review record must not be refused."""
surface = Surface(
name="test", catalog=_reviewed_catalog(), claim=ReviewClaim.HUMAN_REVIEWED
)
assert surface.text("page.heading")
def test_a_surface_claiming_nothing_still_has_to_disclose() -> None:
"""Not claiming review is not the same as being allowed to stay quiet."""
surface = Surface(name="test", catalog=load_catalog("es-US"))
assert surface.must_disclose_machine_translation
target, original = surface.disclosure()
assert target.locale == "es-US"
assert target.review is ReviewStatus.MACHINE
assert original.locale == SOURCE_LOCALE
assert original.review is ReviewStatus.SOURCE
assert target.text != original.text
def test_disclosure_is_refused_when_there_is_nothing_to_disclose() -> None:
"""A notice on a reviewed page is noise that teaches readers to skip it."""
surface = Surface(name="test", catalog=source_catalog())
assert not surface.must_disclose_machine_translation
with pytest.raises(ContextSafeError) as excinfo:
surface.disclosure()
assert excinfo.value.code == "disclosure_not_applicable"
def test_an_unknown_key_fails_closed_instead_of_falling_back() -> None:
"""Silent source-locale fallback is how a half-translated page ships."""
with pytest.raises(ContextSafeError) as excinfo:
load_catalog("es-US").message("no.such.key")
assert excinfo.value.code == "unknown_message_key"
def test_an_unknown_locale_fails_closed() -> None:
"""A locale with no catalog is an error, not an empty page."""
with pytest.raises(ContextSafeError) as excinfo:
load_catalog("zz-ZZ")
assert excinfo.value.code == "unknown_locale"
def test_placeholder_mismatch_fails_closed_in_both_directions() -> None:
"""Supplying the wrong values must not silently drop or invent one."""
message = source_catalog().message("footer.locale")
with pytest.raises(ContextSafeError) as missing:
message.format()
assert missing.value.code == "message_placeholder_mismatch"
with pytest.raises(ContextSafeError) as extra:
message.format(locale="en-US", surplus="x")
assert extra.value.code == "message_placeholder_mismatch"
assert message.format(locale="en-US").text.endswith("en-US")
def test_formatting_preserves_review_status() -> None:
"""Substituting a value must not launder an unreviewed string."""
message = load_catalog("es-US").message("footer.locale").format(locale="es-US")
assert message.review is ReviewStatus.MACHINE
assert not message.reviewed
def test_pseudolocale_transforms_text_and_preserves_placeholders() -> None:
"""The pseudolocale has to stay substitutable, or it tests only itself."""
rendered = pseudolocalize("Interface locale: {locale}")
assert "{locale}" in rendered
assert "Interface locale" not in rendered
assert rendered.startswith("⟦") and rendered.endswith("⟧")
assert len(rendered) > len("Interface locale: {locale}")
catalog = load_catalog(PSEUDO_LOCALE)
assert catalog.keys() == source_catalog().keys()
assert catalog.is_machine_translated
def test_available_locales_lists_what_can_be_rendered() -> None:
"""``--lang`` offers exactly the locales that exist."""
locales = available_locales()
assert SOURCE_LOCALE in locales
assert "es-US" in locales
assert PSEUDO_LOCALE in locales
@pytest.mark.parametrize(
("payload", "code"),
[
([], "invalid_catalog"),
({"locale": "other"}, "catalog_locale_mismatch"),
({"locale": "en-US"}, "invalid_catalog"),
(
{
"locale": "en-US",
"source_locale": "en-US",
"translation_review": {"status": "nope", "note": "n"},
"messages": {},
},
"invalid_catalog",
),
(
{
"locale": "en-US",
"source_locale": "en-US",
"translation_review": {"status": "source", "note": ""},
"messages": {},
},
"invalid_catalog",
),
(
{
"locale": "en-US",
"source_locale": "en-US",
"translation_review": {"status": "source", "note": "n"},
"messages": {"a": {"review": "source"}},
},
"invalid_catalog",
),
],
)
def test_a_malformed_catalog_is_rejected(payload: Any, code: str) -> None:
"""Catalogs are contracts; a broken one is a rejection, not a guess."""
from contextsafe import i18n
with pytest.raises(ContextSafeError) as excinfo:
i18n._parse_catalog(payload, locale="en-US")
assert excinfo.value.code == code
def test_the_machine_artifact_never_varies_by_locale() -> None:
"""Hash-covered output stays in one language, whatever the interface does.
Localizing a receipt payload would change its bytes and therefore its
hash, so the split is structural rather than a convention: catalogs reach
the rendered page and nothing else.
"""
document = gate.reference_document()
serialized = canonical_json(document)
spanish = load_catalog("es-US")
for message in spanish.messages.values():
assert message.text not in serialized
for limitation in document["payload"]["limitations"]: # type: ignore[index]
assert source_catalog().message
assert isinstance(limitation, str)
assert limitation.isascii()
def test_every_mandated_limitation_has_a_catalog_entry() -> None:
"""A limitation with no entry renders untranslated, so pin the coverage."""
document = gate.reference_document()
limitations = document["payload"]["limitations"] # type: ignore[index]
source = source_catalog()
keyed = {
message.text
for key, message in source.messages.items()
if key.startswith("limitation.")
}
assert set(limitations) <= keyed
def test_the_gate_is_clean_on_the_shipped_catalogs() -> None:
"""The repository has to pass its own gate."""
assert gate.run_gate(gate.shipped_catalogs()) == []
def test_the_gate_refuses_to_pass_on_nothing() -> None:
"""A gate that reports success having examined nothing is worthless."""
findings = gate.run_gate(())
assert [finding.rule for finding in findings] == ["no-catalogs"]
def test_the_gate_catches_a_missing_key(tmp_path: Path) -> None:
"""Negative control for ``catalog-parity``."""
catalog = _mutated_catalog(tmp_path, drop="column.rule")
rules = {finding.rule for finding in gate.check_parity(catalog)}
assert "catalog-parity" in rules
def test_the_gate_catches_a_changed_placeholder(tmp_path: Path) -> None:
"""Negative control for ``placeholder-parity``."""
catalog = _mutated_catalog(tmp_path, retext=("footer.locale", "Idioma: {idioma}"))
rules = {finding.rule for finding in gate.check_parity(catalog)}
assert "placeholder-parity" in rules
def test_the_gate_catches_an_empty_message(tmp_path: Path) -> None:
"""Negative control for ``message-quality`` at the parse boundary."""
with pytest.raises(ContextSafeError) as excinfo:
_mutated_catalog(tmp_path, retext=("footer.locale", " "))
assert excinfo.value.code == "invalid_catalog"
def test_the_gate_catches_a_source_entry_claiming_translation() -> None:
"""Negative control for ``message-quality`` in the source locale."""
source = source_catalog()
tainted = Catalog(
locale=SOURCE_LOCALE,
source_locale=SOURCE_LOCALE,
review=source.review,
messages={
**source.messages,
"page.title": Message(
key="page.title",
text="ContextSafe evaluation receipt",
review=ReviewStatus.MACHINE,
locale=SOURCE_LOCALE,
),
},
)
rules = {finding.rule for finding in gate.check_message_quality(tainted)}
assert "message-quality" in rules
def test_the_gate_catches_a_review_record_nobody_signed(tmp_path: Path) -> None:
"""Negative control for ``review-consistency``."""
catalog = _mutated_catalog(tmp_path, review_status="human")
findings = list(gate.check_review_consistency(catalog))
details = " ".join(finding.detail for finding in findings)
assert findings
assert "have not been reviewed" in details
assert "without naming a reviewer" in details
def test_the_gate_catches_a_reviewed_catalog_being_refused() -> None:
"""Negative control for the accepting half of the claiming-surface rule."""
catalog = Catalog(
locale="xx-XX",
source_locale=SOURCE_LOCALE,
review=TranslationReview(
status=ReviewStatus.HUMAN,
reviewed_by="A named reviewer",
reviewed_at="2026-01-01",
note="Fixture.",
),
messages={
key: Message(
key=key,
text=message.text,
review=(
ReviewStatus.MACHINE if key == "page.title" else ReviewStatus.HUMAN
),
locale="xx-XX",
)
for key, message in source_catalog().messages.items()
},
)
rules = {finding.rule for finding in gate.check_claiming_surface(catalog)}
assert rules == set()
findings = list(gate.check_review_consistency(catalog))
assert findings
def test_the_gate_catches_a_surface_that_stopped_refusing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Negative control for ``unreviewed-on-reviewed-surface``.
If :class:`Surface` ever stopped enforcing its claim, every other check
here would still pass, so the gate is pointed at that regression directly.
"""
monkeypatch.setattr(
gate.Surface, "message", lambda self, key, **values: self.catalog.message(key)
)
findings = list(gate.check_claiming_surface(load_catalog("es-US")))
assert [finding.rule for finding in findings] == ["unreviewed-on-reviewed-surface"]
def test_the_gate_reports_a_locale_that_cannot_render(tmp_path: Path) -> None:
"""A locale that fails to render is a finding, not a traceback."""
catalog = _mutated_catalog(tmp_path, drop="column.rule")
findings = list(gate.check_disclosure(catalog, gate.reference_document()))
assert [finding.rule for finding in findings] == ["render-failed"]
def _mutated_catalog(
tmp_path: Path,
*,
drop: str | None = None,
retext: tuple[str, str] | None = None,
review_status: str | None = None,
) -> Catalog:
"""Write a mutated copy of the Spanish catalog and load it."""
from contextsafe import i18n
raw = json.loads((i18n.LOCALES_ROOT / "es-US.json").read_text(encoding="utf-8"))
if drop is not None:
del raw["messages"][drop]
if retext is not None:
raw["messages"][retext[0]]["text"] = retext[1]
if review_status is not None:
raw["translation_review"]["status"] = review_status
return i18n._parse_catalog(raw, locale="es-US")
def test_the_gate_cli_reports_clean_and_dirty(
capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
) -> None:
"""The command-line entry point has to agree with the library."""
assert gate.main([]) == 0
assert "clean over 2 catalog(s)" in capsys.readouterr().out
monkeypatch.setattr(
gate, "run_gate", lambda catalogs: [gate.Finding("r", "l", "d")]
)
assert gate.main(["--locale", "es-US"]) == 1
assert "1 finding(s)" in capsys.readouterr().out