Skip to content

The agent that grows with you

test(i18n): make the zh-Hans Bot Mode parity checks invariants, not change-detectors

teknium1668a778606a4
diff --git a/tests/website/test_bot_mode_zh_hans.py b/tests/website/test_bot_mode_zh_hans.py
index afc55d5e230..b52cc7cf52b 100644
--- a/tests/website/test_bot_mode_zh_hans.py
+++ b/tests/website/test_bot_mode_zh_hans.py
@@ -1,10 +1,9 @@
"""Regression for issue #94366.
The zh-Hans docs tree had no translation of the Bot Mode user guide, so
-zh-Hans readers silently fell back to the English page. This keeps the
-translation structurally aligned with the English source: same headings
-in the same order, and machine-readable values in code blocks (commands,
-config keys, RPC payloads) preserved byte-for-byte.
+zh-Hans readers silently fell back to the English page. These checks are
+invariants of the translation, not a mirror of the English page: an
+English-only edit (new heading, new code block) must not turn main red.
"""
from __future__ import annotations
@@ -25,30 +24,39 @@ ZH_DOC = (
/ "bot-mode.md"
)
-HEADING_RE = re.compile(r"^(#{1,6})\s+(.*)$", re.MULTILINE)
+FRONT_MATTER_RE = re.compile(r"\A---\n(.*?)\n---\n", re.DOTALL)
CODE_BLOCK_RE = re.compile(r"```[a-zA-Z]*\n(.*?)```", re.DOTALL)
+# Front matter keys Docusaurus uses for routing; they must agree between the
+# source and its translation or the zh-Hans page lands on another URL.
+ROUTING_KEYS = ("id", "slug")
-def _heading_levels(text: str) -> list[int]:
- return [len(level) for level, _ in HEADING_RE.findall(text)]
+def _front_matter(text: str) -> dict[str, str]:
+ match = FRONT_MATTER_RE.match(text)
+ assert match, "page must start with a front matter block"
+ pairs = (line.split(":", 1) for line in match.group(1).splitlines() if ":" in line)
+ return {key.strip(): value.strip() for key, value in pairs}
-def test_heading_structure_matches_english():
- en_levels = _heading_levels(EN_DOC.read_text(encoding="utf-8"))
- zh_levels = _heading_levels(ZH_DOC.read_text(encoding="utf-8"))
- assert zh_levels == en_levels, (
- "zh-Hans heading levels/order must mirror the English source "
- f"(english={en_levels}, zh-Hans={zh_levels})"
- )
+def _machine_readable_lines(block: str) -> tuple[str, ...]:
+ # Comments are translated; commands, paths, config keys and payloads are not.
+ return tuple(line.split("#", 1)[0].rstrip() for line in block.strip().splitlines())
+
+def test_translation_exists_and_routes_like_the_english_page():
+ en_meta = _front_matter(EN_DOC.read_text(encoding="utf-8"))
+ zh_meta = _front_matter(ZH_DOC.read_text(encoding="utf-8"))
+ assert zh_meta.get("title"), "zh-Hans page needs its own title"
+ for key in ROUTING_KEYS:
+ assert zh_meta.get(key) == en_meta.get(key), f"front matter '{key}' must match the English page"
-def test_code_blocks_preserve_machine_readable_values():
- en_blocks = [b.strip() for b in CODE_BLOCK_RE.findall(EN_DOC.read_text(encoding="utf-8"))]
- zh_blocks = [b.strip() for b in CODE_BLOCK_RE.findall(ZH_DOC.read_text(encoding="utf-8"))]
- assert len(en_blocks) == len(zh_blocks)
- for en_block, zh_block in zip(en_blocks, zh_blocks):
- # Compare non-comment lines verbatim: commands, paths, and config
- # keys must stay byte-accurate even though comments are translated.
- en_code_lines = [line.split("#", 1)[0].rstrip() for line in en_block.splitlines()]
- zh_code_lines = [line.split("#", 1)[0].rstrip() for line in zh_block.splitlines()]
- assert en_code_lines == zh_code_lines
+
+def test_code_blocks_are_copied_from_the_english_page():
+ en_blocks = {_machine_readable_lines(b) for b in CODE_BLOCK_RE.findall(EN_DOC.read_text(encoding="utf-8"))}
+ zh_blocks = [_machine_readable_lines(b) for b in CODE_BLOCK_RE.findall(ZH_DOC.read_text(encoding="utf-8"))]
+ assert zh_blocks, "translation lost its code blocks"
+ stale = [b for b in zh_blocks if b not in en_blocks]
+ assert not stale, (
+ "zh-Hans code blocks must be byte-accurate copies of blocks that still exist "
+ f"in the English page (translation stale for: {stale})"
+ )