84 行
3.1 KiB
Python
84 行
3.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import importlib
|
||
|
|
import inspect
|
||
|
|
import pkgutil
|
||
|
|
import re
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
import backend.routes
|
||
|
|
from backend.services.workflow_service import WorkflowService
|
||
|
|
|
||
|
|
|
||
|
|
def _route_modules():
|
||
|
|
for info in pkgutil.iter_modules(backend.routes.__path__):
|
||
|
|
if info.name in {"common", "params"}:
|
||
|
|
continue
|
||
|
|
yield importlib.import_module(f"backend.routes.{info.name}")
|
||
|
|
|
||
|
|
|
||
|
|
def test_no_route_module_hand_assembles_workflow_service() -> None:
|
||
|
|
"""Assembly belongs to the composition root, not to each route module.
|
||
|
|
|
||
|
|
Seven route modules each built their own ``WorkflowService``; four of them
|
||
|
|
forgot ``event_bus``, so whether a workflow mutation published an event
|
||
|
|
depended on which route the request entered through. Branch events and
|
||
|
|
material-binding events were silently dropped as a result.
|
||
|
|
"""
|
||
|
|
|
||
|
|
# Word boundary matters: CharacterWorkflowService(...) and
|
||
|
|
# WorkflowNodeMaterialService(...) are consumers, not assemblers.
|
||
|
|
direct_construction = re.compile(r"(?<![A-Za-z_])WorkflowService\s*\(")
|
||
|
|
|
||
|
|
offenders: list[str] = []
|
||
|
|
for module in _route_modules():
|
||
|
|
try:
|
||
|
|
source = inspect.getsource(module)
|
||
|
|
except OSError: # pragma: no cover - source always available here
|
||
|
|
continue
|
||
|
|
if direct_construction.search(source):
|
||
|
|
offenders.append(module.__name__)
|
||
|
|
|
||
|
|
assert offenders == [], (
|
||
|
|
"these route modules construct WorkflowService directly instead of "
|
||
|
|
f"calling backend.services.service_registry.create_workflow_service: {offenders}"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize(
|
||
|
|
("module_name", "factory", "unwrap"),
|
||
|
|
[
|
||
|
|
("backend.routes.branch_routes", "_create_workflow_service", lambda s: s),
|
||
|
|
("backend.routes.workflow_routes", "_create_workflow_service", lambda s: s),
|
||
|
|
("backend.routes.profile_routes", "_create_workflow_service", lambda s: s),
|
||
|
|
("backend.routes.review_routes", "_create_review_service", lambda s: s.workflow_service),
|
||
|
|
("backend.routes.history_routes", "_create_history_service", lambda s: s.workflow_service),
|
||
|
|
("backend.routes.node_material_routes", "_create_service", lambda s: s.workflow_service),
|
||
|
|
],
|
||
|
|
)
|
||
|
|
def test_every_route_factory_wires_an_event_bus(module_name, factory, unwrap) -> None:
|
||
|
|
module = importlib.import_module(module_name)
|
||
|
|
builder = getattr(module, factory, None)
|
||
|
|
if builder is None:
|
||
|
|
pytest.skip(f"{module_name} has no {factory}")
|
||
|
|
|
||
|
|
service = unwrap(builder())
|
||
|
|
|
||
|
|
assert isinstance(service, WorkflowService)
|
||
|
|
assert service.event_bus is not None, (
|
||
|
|
f"{module_name}.{factory} assembles a WorkflowService without an "
|
||
|
|
"event_bus; every publish() from this route becomes a silent no-op"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_composition_root_supplies_the_full_collaborator_set() -> None:
|
||
|
|
from backend.services.service_registry import create_workflow_service
|
||
|
|
|
||
|
|
service = create_workflow_service()
|
||
|
|
|
||
|
|
assert service.workflow_repository is not None
|
||
|
|
assert service.profile_repository is not None
|
||
|
|
assert service.audit_service is not None
|
||
|
|
assert service.event_bus is not None
|