test_chinese_ui.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from __future__ import annotations
  2. import ast
  3. from pathlib import Path
  4. PROJECT_ROOT = Path(__file__).resolve().parents[1]
  5. def _has_chinese(value: str) -> bool:
  6. return any("\u4e00" <= char <= "\u9fff" for char in value)
  7. def _assigned_literal(path: Path, name: str):
  8. tree = ast.parse(path.read_text(encoding="utf-8"))
  9. for node in tree.body:
  10. if not isinstance(node, ast.Assign):
  11. continue
  12. if any(isinstance(target, ast.Name) and target.id == name for target in node.targets):
  13. return ast.literal_eval(node.value)
  14. raise AssertionError(f"{path} 中未找到 {name}")
  15. def test_default_loaded_modules_only_expose_chinese_management_features():
  16. module_names = _assigned_literal(PROJECT_ROOT / "wbb" / "__init__.py", "MOD_LOAD")
  17. assert "couple" not in module_names
  18. assert "music" not in module_names
  19. assert "sudoers" not in module_names
  20. for module_name in module_names:
  21. path = PROJECT_ROOT / "wbb" / "modules" / f"{module_name}.py"
  22. source = path.read_text(encoding="utf-8")
  23. tree = ast.parse(source)
  24. assignments = {
  25. target.id: ast.literal_eval(node.value)
  26. for node in tree.body
  27. if isinstance(node, ast.Assign)
  28. for target in node.targets
  29. if isinstance(target, ast.Name)
  30. and target.id in {"__MODULE__", "__HELP__"}
  31. }
  32. if "__MODULE__" in assignments:
  33. assert _has_chinese(assignments["__MODULE__"])
  34. if "__HELP__" in assignments:
  35. assert _has_chinese(assignments["__HELP__"])
  36. def test_registered_telegram_command_descriptions_are_chinese():
  37. source = (PROJECT_ROOT / "wbb" / "__main__.py").read_text(encoding="utf-8")
  38. tree = ast.parse(source)
  39. descriptions = []
  40. for node in ast.walk(tree):
  41. if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Name):
  42. continue
  43. if node.func.id != "BotCommand" or len(node.args) < 2:
  44. continue
  45. descriptions.append(ast.literal_eval(node.args[1]))
  46. assert descriptions
  47. assert all(_has_chinese(description) for description in descriptions)
  48. assert "项目源码" not in source
  49. def test_display_mappings_cover_all_point_sources(app_modules):
  50. points = app_modules.load("wbb.utils.dbpoints")
  51. i18n = app_modules.load("wbb.utils.i18n")
  52. assert set(i18n.POINT_SOURCE_LABELS) == points.POINT_SOURCES
  53. assert all(_has_chinese(label) for label in i18n.POINT_SOURCE_LABELS.values())