81 行
3 KiB
Python
81 行
3 KiB
Python
from __future__ import annotations
|
|
|
|
import ast
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
PLUGIN_ROOT = Path(__file__).resolve().parents[1]
|
|
SOURCE_DIRS = ("backend", "core", "database", "engines", "adapters")
|
|
|
|
|
|
def _python_files(*directories: str):
|
|
for directory in directories:
|
|
for path in (PLUGIN_ROOT / directory).rglob("*.py"):
|
|
if "__pycache__" in path.parts:
|
|
continue
|
|
yield path
|
|
|
|
|
|
def _imported_modules(path: Path) -> set[str]:
|
|
try:
|
|
tree = ast.parse(path.read_text(encoding="utf-8"))
|
|
except SyntaxError: # pragma: no cover
|
|
return set()
|
|
modules: set[str] = set()
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
|
|
modules.add(node.module)
|
|
elif isinstance(node, ast.Import):
|
|
modules.update(alias.name for alias in node.names)
|
|
return modules
|
|
|
|
|
|
def test_no_module_below_the_route_layer_imports_a_route() -> None:
|
|
"""Routes are the entry layer; nothing underneath may depend on them.
|
|
|
|
``workflow_round_worker_runtime`` -- a background worker with no HTTP
|
|
involvement -- imported ``create_generation_service`` from
|
|
``backend.routes.node_routes`` purely to assemble its dependencies, which
|
|
dragged the whole route layer into worker startup. Assembly now lives in
|
|
``backend.services.service_registry``.
|
|
"""
|
|
|
|
offenders: list[str] = []
|
|
for path in _python_files("backend/services", "core", "database", "engines", "adapters"):
|
|
for module in _imported_modules(path):
|
|
if module.startswith("backend.routes"):
|
|
offenders.append(f"{path.relative_to(PLUGIN_ROOT).as_posix()} -> {module}")
|
|
|
|
assert offenders == [], f"service/core/database/engine modules importing routes: {offenders}"
|
|
|
|
|
|
def test_database_layer_does_not_import_services_or_engines() -> None:
|
|
"""Repositories may speak DTOs, but not call business services.
|
|
|
|
``backend.dto`` is deliberately allowed: repositories returning typed DTOs
|
|
is the intended design. Importing ``backend.services`` or ``engines`` from
|
|
a repository inverts the dependency and makes the data layer un-testable
|
|
in isolation.
|
|
"""
|
|
|
|
offenders: list[str] = []
|
|
for path in _python_files("database"):
|
|
for module in _imported_modules(path):
|
|
if module.startswith("backend.services") or module.startswith("engines"):
|
|
offenders.append(f"{path.relative_to(PLUGIN_ROOT).as_posix()} -> {module}")
|
|
|
|
assert offenders == [], f"database layer reaching upward: {offenders}"
|
|
|
|
|
|
def test_core_does_not_depend_on_backend_services() -> None:
|
|
"""``core`` is the shared substrate; it must not know about services."""
|
|
|
|
offenders: list[str] = []
|
|
for path in _python_files("core"):
|
|
for module in _imported_modules(path):
|
|
if module.startswith("backend.services"):
|
|
offenders.append(f"{path.relative_to(PLUGIN_ROOT).as_posix()} -> {module}")
|
|
|
|
assert offenders == [], f"core reaching into services: {offenders}"
|