test_architecture_artifacts.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import ast
  2. import subprocess
  3. import sys
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parents[1]
  6. ARCH = ROOT / "docs" / "architecture"
  7. def _route_count() -> int:
  8. count = 0
  9. for route_file in (ROOT / "app" / "api").glob("*/*.py"):
  10. tree = ast.parse(route_file.read_text(encoding="utf-8"))
  11. for node in ast.walk(tree):
  12. if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
  13. continue
  14. for decorator in node.decorator_list:
  15. if not isinstance(decorator, ast.Call):
  16. continue
  17. function = decorator.func
  18. if (
  19. isinstance(function, ast.Attribute)
  20. and function.attr == "route"
  21. and isinstance(function.value, ast.Name)
  22. and function.value.id == "bp"
  23. ):
  24. count += 1
  25. return count
  26. def test_required_architecture_artifacts_exist():
  27. required = {
  28. "ARCHITECTURE_OVERVIEW.md",
  29. "OPENAPI.yaml",
  30. "FRONTEND_REMOVAL_INVENTORY.md",
  31. "DATA_MODEL.md",
  32. "DEPLOYMENT_SOURCE_OF_TRUTH.md",
  33. "ADR-001-authentication.md",
  34. "ADR-002-workflow-engine.md",
  35. "ADR-003-cross-store-consistency.md",
  36. "ADR-004-ai-first-kestra-orchestration.md",
  37. "NEXT_ITERATION_ROADMAP.md",
  38. }
  39. assert required <= {path.name for path in ARCH.glob("*")}
  40. def test_openapi_route_inventory_matches_flask_source():
  41. source_count = _route_count()
  42. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  43. assert source_count > 100
  44. assert "openapi: 3.1.0" in contract
  45. assert f"x-route-count: {source_count}" in contract
  46. assert contract.count("operationId:") == source_count
  47. def test_openapi_generator_is_reproducible(tmp_path):
  48. generated = tmp_path / "OPENAPI.yaml"
  49. subprocess.run(
  50. [
  51. sys.executable,
  52. str(ROOT / "scripts" / "generate_openapi.py"),
  53. "--output",
  54. str(generated),
  55. ],
  56. cwd=ROOT,
  57. check=True,
  58. )
  59. assert generated.read_bytes() == (ARCH / "OPENAPI.yaml").read_bytes()
  60. def test_wp09_contract_and_migration_are_documented():
  61. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  62. data_model = (ARCH / "DATA_MODEL.md").read_text(encoding="utf-8")
  63. migration = (
  64. ROOT
  65. / "migrations/versions/20260729_350_device_observability.py"
  66. )
  67. for path in (
  68. "/api/development/v1/device-observability/events",
  69. "/api/development/v1/device-observability/import",
  70. "/api/development/v1/device-observability/graph",
  71. "/api/development/v1/device-observability/root-cause",
  72. ):
  73. assert path in contract
  74. assert migration.exists()
  75. assert "device_operational_events" in data_model
  76. assert "device_evidence_relations" in data_model
  77. assert "证据不足,无法确认根因" in data_model
  78. def test_contract_ci_regenerates_and_checks_the_committed_inventory():
  79. workflow = (ROOT / ".github" / "workflows" / "contracts.yml").read_text(
  80. encoding="utf-8"
  81. )
  82. assert "scripts/generate_openapi.py" in workflow
  83. assert "git diff --exit-code" in workflow
  84. assert "tests/test_architecture_artifacts.py" in workflow
  85. def test_architecture_decisions_capture_product_boundaries():
  86. combined = "\n".join(
  87. (ARCH / name).read_text(encoding="utf-8")
  88. for name in (
  89. "DATA_MODEL.md",
  90. "DEPLOYMENT_SOURCE_OF_TRUTH.md",
  91. "ADR-001-authentication.md",
  92. "ADR-002-workflow-engine.md",
  93. "ADR-003-cross-store-consistency.md",
  94. "ADR-004-ai-first-kestra-orchestration.md",
  95. "NEXT_ITERATION_ROADMAP.md",
  96. )
  97. )
  98. for required_term in (
  99. "管理员",
  100. "编辑者",
  101. "查看者",
  102. "DataFlow",
  103. "n8n Workflow",
  104. "同一环境只允许一个当前生效版本",
  105. "Qwen",
  106. "DeepSeek",
  107. "每日全量一致性巡检",
  108. "app/",
  109. "deployment/app/",
  110. ):
  111. assert required_term in combined
  112. def test_removal_inventory_is_bound_to_the_recovery_backup():
  113. inventory = (ARCH / "FRONTEND_REMOVAL_INVENTORY.md").read_text(
  114. encoding="utf-8"
  115. )
  116. assert "DataOps-platform-backup-before-cleanup-20260716-170321.tar.gz" in inventory
  117. assert "866c6c4aff127f88497a5ddf39ac8d8da93008cc784ae6f276f80a7cd17d4ef0" in inventory
  118. assert "348" in inventory
  119. def test_release_sync_and_package_include_versioned_migrations():
  120. sync = (ROOT / "deployment" / "sync_release.sh").read_text(encoding="utf-8")
  121. package = (ROOT / "deployment" / "package_release.sh").read_text(
  122. encoding="utf-8"
  123. )
  124. run_script = (ROOT / "scripts" / "run_dataops.sh").read_text(encoding="utf-8")
  125. assert 'copy_tree "${ROOT_DIR}/migrations"' in sync
  126. assert 'copy_file "${ROOT_DIR}/alembic.ini"' in sync
  127. assert 'copy_into_release "${SCRIPT_DIR}/migrations"' in package
  128. assert 'copy_into_release "${SCRIPT_DIR}/alembic.ini"' in package
  129. assert 'alembic" -c "${APP_DIR}/alembic.ini" upgrade head' in run_script