| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- from __future__ import annotations
- import ast
- from pathlib import Path
- PROJECT_ROOT = Path(__file__).resolve().parents[1]
- def _has_chinese(value: str) -> bool:
- return any("\u4e00" <= char <= "\u9fff" for char in value)
- def _assigned_literal(path: Path, name: str):
- tree = ast.parse(path.read_text(encoding="utf-8"))
- for node in tree.body:
- if not isinstance(node, ast.Assign):
- continue
- if any(isinstance(target, ast.Name) and target.id == name for target in node.targets):
- return ast.literal_eval(node.value)
- raise AssertionError(f"{path} 中未找到 {name}")
- def test_default_loaded_modules_only_expose_chinese_management_features():
- module_names = _assigned_literal(PROJECT_ROOT / "wbb" / "__init__.py", "MOD_LOAD")
- assert "couple" not in module_names
- assert "music" not in module_names
- assert "sudoers" not in module_names
- for module_name in module_names:
- path = PROJECT_ROOT / "wbb" / "modules" / f"{module_name}.py"
- source = path.read_text(encoding="utf-8")
- tree = ast.parse(source)
- assignments = {
- target.id: ast.literal_eval(node.value)
- for node in tree.body
- if isinstance(node, ast.Assign)
- for target in node.targets
- if isinstance(target, ast.Name)
- and target.id in {"__MODULE__", "__HELP__"}
- }
- if "__MODULE__" in assignments:
- assert _has_chinese(assignments["__MODULE__"])
- if "__HELP__" in assignments:
- assert _has_chinese(assignments["__HELP__"])
- def test_registered_telegram_command_descriptions_are_chinese():
- source = (PROJECT_ROOT / "wbb" / "__main__.py").read_text(encoding="utf-8")
- tree = ast.parse(source)
- descriptions = []
- for node in ast.walk(tree):
- if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Name):
- continue
- if node.func.id != "BotCommand" or len(node.args) < 2:
- continue
- descriptions.append(ast.literal_eval(node.args[1]))
- assert descriptions
- assert all(_has_chinese(description) for description in descriptions)
- assert "项目源码" not in source
- def test_display_mappings_cover_all_point_sources(app_modules):
- points = app_modules.load("wbb.utils.dbpoints")
- i18n = app_modules.load("wbb.utils.i18n")
- assert set(i18n.POINT_SOURCE_LABELS) == points.POINT_SOURCES
- assert all(_has_chinese(label) for label in i18n.POINT_SOURCE_LABELS.values())
|