test_architecture_artifacts.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 isinstance(function.value, ast.Name)
  21. and function.value.id == "bp"
  22. and (
  23. function.attr == "route"
  24. or function.attr
  25. in {"get", "post", "put", "patch", "delete"}
  26. )
  27. ):
  28. count += 1
  29. return count
  30. def test_required_architecture_artifacts_exist():
  31. required = {
  32. "ARCHITECTURE_OVERVIEW.md",
  33. "OPENAPI.yaml",
  34. "FRONTEND_REMOVAL_INVENTORY.md",
  35. "DATA_MODEL.md",
  36. "DEPLOYMENT_SOURCE_OF_TRUTH.md",
  37. "ADR-001-authentication.md",
  38. "ADR-002-workflow-engine.md",
  39. "ADR-003-cross-store-consistency.md",
  40. "ADR-004-ai-first-kestra-orchestration.md",
  41. "NEXT_ITERATION_ROADMAP.md",
  42. }
  43. assert required <= {path.name for path in ARCH.glob("*")}
  44. def test_openapi_route_inventory_matches_flask_source():
  45. source_count = _route_count()
  46. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  47. assert source_count > 100
  48. assert "openapi: 3.1.0" in contract
  49. assert f"x-route-count: {source_count}" in contract
  50. assert contract.count("operationId:") == source_count
  51. def test_openapi_generator_is_reproducible(tmp_path):
  52. generated = tmp_path / "OPENAPI.yaml"
  53. subprocess.run(
  54. [
  55. sys.executable,
  56. str(ROOT / "scripts" / "generate_openapi.py"),
  57. "--output",
  58. str(generated),
  59. ],
  60. cwd=ROOT,
  61. check=True,
  62. )
  63. assert generated.read_bytes() == (ARCH / "OPENAPI.yaml").read_bytes()
  64. def test_wp09_contract_and_migration_are_documented():
  65. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  66. data_model = (ARCH / "DATA_MODEL.md").read_text(encoding="utf-8")
  67. migration = (
  68. ROOT
  69. / "migrations/versions/20260729_350_device_observability.py"
  70. )
  71. for path in (
  72. "/api/development/v1/device-observability/events",
  73. "/api/development/v1/device-observability/import",
  74. "/api/development/v1/device-observability/graph",
  75. "/api/development/v1/device-observability/root-cause",
  76. ):
  77. assert path in contract
  78. assert migration.exists()
  79. assert "device_operational_events" in data_model
  80. assert "device_evidence_relations" in data_model
  81. assert "证据不足,无法确认根因" in data_model
  82. def test_wp10_device_knowledge_contract_and_boundaries_are_documented():
  83. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  84. data_model = (ARCH / "DATA_MODEL.md").read_text(encoding="utf-8")
  85. golden_set = (
  86. ROOT
  87. / "docs/acceptance/WP10_DEVICE_KNOWLEDGE_GOLDEN_SET.json"
  88. )
  89. for path in (
  90. "/api/knowledge/search",
  91. "/api/knowledge/ask",
  92. "/api/knowledge/sources/{source_uid}",
  93. "/api/knowledge/admin/device-sources",
  94. "/api/knowledge/admin/device-sources/{source_uid}/scope",
  95. "/api/knowledge/admin/query-audits",
  96. ):
  97. assert path in contract
  98. assert "x-response-fields: [" in contract
  99. assert "citations" in contract
  100. assert "evidence" in contract
  101. assert "knowledge_query_audits" in data_model
  102. assert "authorized_sources" in data_model
  103. assert "LightRAG 仍是影子投影" in data_model
  104. assert golden_set.exists()
  105. def test_wp11_governance_metric_contract_and_boundaries_are_documented():
  106. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  107. data_model = (ARCH / "DATA_MODEL.md").read_text(encoding="utf-8")
  108. for path in (
  109. "/api/development/v1/governance-metrics/summary",
  110. "/api/development/v1/governance-metrics/details",
  111. ):
  112. assert path in contract
  113. for field in ("scope", "metrics", "metric", "state", "records", "total"):
  114. assert field in contract
  115. for term in (
  116. "台账完整率",
  117. "责任覆盖率",
  118. "实体映射率",
  119. "问题闭环率",
  120. "问题复发率",
  121. "rate = null",
  122. "authorized_sources",
  123. "不依赖前端过滤",
  124. ):
  125. assert term in data_model
  126. def test_wp12_security_audit_contract_and_boundaries_are_documented():
  127. contract = (ARCH / "OPENAPI.yaml").read_text(encoding="utf-8")
  128. data_model = (ARCH / "DATA_MODEL.md").read_text(encoding="utf-8")
  129. validation = ROOT / "docs/validation/WP12_SECURITY_AUDIT_EVIDENCE.md"
  130. for path in (
  131. "/api/system/governance-audit/security-checks",
  132. "/api/system/governance-audit/coverage",
  133. "/api/system/governance-audit/events",
  134. "/api/system/governance-audit/seals",
  135. "/api/system/governance-audit/seals/{seal_uid}/verify",
  136. ):
  137. assert path in contract
  138. for term in (
  139. "governance_audit_seals",
  140. "HMAC-SHA256",
  141. "authentication",
  142. "ingestion",
  143. "entity_resolution",
  144. "publication",
  145. "remediation",
  146. "knowledge_query",
  147. "不等于阻止数据库管理员修改",
  148. "50,000",
  149. ):
  150. assert term in data_model
  151. assert validation.exists()
  152. def test_wp12_release_copy_matches_authoritative_backend_files():
  153. for relative in (
  154. "app/__init__.py",
  155. "app/api/system/__init__.py",
  156. "app/api/system/governance_audit.py",
  157. "app/config/config.py",
  158. "app/core/data_source/redaction.py",
  159. "app/core/system/governance_audit.py",
  160. "app/core/system/governance_audit_repository.py",
  161. "app/core/system/permissions.py",
  162. ):
  163. source = ROOT / relative
  164. release = ROOT / "deployment" / relative
  165. assert release.read_bytes() == source.read_bytes(), relative
  166. def test_contract_ci_regenerates_and_checks_the_committed_inventory():
  167. workflow = (ROOT / ".github" / "workflows" / "contracts.yml").read_text(
  168. encoding="utf-8"
  169. )
  170. assert "scripts/generate_openapi.py" in workflow
  171. assert "git diff --exit-code" in workflow
  172. assert "tests/test_architecture_artifacts.py" in workflow
  173. def test_architecture_decisions_capture_product_boundaries():
  174. combined = "\n".join(
  175. (ARCH / name).read_text(encoding="utf-8")
  176. for name in (
  177. "DATA_MODEL.md",
  178. "DEPLOYMENT_SOURCE_OF_TRUTH.md",
  179. "ADR-001-authentication.md",
  180. "ADR-002-workflow-engine.md",
  181. "ADR-003-cross-store-consistency.md",
  182. "ADR-004-ai-first-kestra-orchestration.md",
  183. "NEXT_ITERATION_ROADMAP.md",
  184. )
  185. )
  186. for required_term in (
  187. "管理员",
  188. "编辑者",
  189. "查看者",
  190. "DataFlow",
  191. "n8n Workflow",
  192. "同一环境只允许一个当前生效版本",
  193. "Qwen",
  194. "DeepSeek",
  195. "每日全量一致性巡检",
  196. "app/",
  197. "deployment/app/",
  198. ):
  199. assert required_term in combined
  200. def test_removal_inventory_is_bound_to_the_recovery_backup():
  201. inventory = (ARCH / "FRONTEND_REMOVAL_INVENTORY.md").read_text(
  202. encoding="utf-8"
  203. )
  204. assert "DataOps-platform-backup-before-cleanup-20260716-170321.tar.gz" in inventory
  205. assert "866c6c4aff127f88497a5ddf39ac8d8da93008cc784ae6f276f80a7cd17d4ef0" in inventory
  206. assert "348" in inventory
  207. def test_release_sync_and_package_include_versioned_migrations():
  208. sync = (ROOT / "deployment" / "sync_release.sh").read_text(encoding="utf-8")
  209. package = (ROOT / "deployment" / "package_release.sh").read_text(
  210. encoding="utf-8"
  211. )
  212. run_script = (ROOT / "scripts" / "run_dataops.sh").read_text(encoding="utf-8")
  213. assert 'copy_tree "${ROOT_DIR}/migrations"' in sync
  214. assert 'copy_file "${ROOT_DIR}/alembic.ini"' in sync
  215. assert 'copy_into_release "${SCRIPT_DIR}/migrations"' in package
  216. assert 'copy_into_release "${SCRIPT_DIR}/alembic.ini"' in package
  217. assert 'alembic" -c "${APP_DIR}/alembic.ini" upgrade head' in run_script