clipwright/tests/test_package_layout.py
pedro-bento 83ad1feaba
All checks were successful
CI / backend-tests (pull_request) Successful in 30s
CI / frontend-build (pull_request) Successful in 29s
refactor: move backend into the clipwright package
Restructure the 37 root-level Python modules into a proper package:
  clipwright/{core,api,pipeline,layouts,media,integrations,tools}

- Rewrite every local import (module-level and deferred) to absolute
  clipwright.<group>.<mod> form; no legacy root shims or re-exports.
- Add clipwright/core/paths.py and route fonts/assets/cookies through it.
- Entry points: uvicorn clipwright.api.app:app; jobs run as
  python -m clipwright.pipeline.main; probe as -m ...quality_probe.
- child_env() appends REPO_ROOT to PYTHONPATH so the -m workers work.
- transcribe_backends: sys.modules key now clipwright.media.scene_detection.
- Tooling: root pyproject.toml (pytest pythonpath=[.], ruff), shared
  requirements-test.txt, CI installs it, Docker PYTHONPATH=/app.
- Tests: all imports/fakes/source-scans repointed; new tests/test_package_layout.py
  enforces no root modules, docstring-only __init__, and no legacy imports.
- Docs/comments updated for the new layout.

Backend: 744 passed, 19 skipped. Frontend lint+build green.
2026-10-04 20:40:38 +01:00

94 lines
3.6 KiB
Python

"""Architecture guard for the package-layout refactor.
The backend's 37 top-level modules moved under ``clipwright/``. These tests pin
the invariants that made the move worth doing:
1. the repo root holds no importable ``*.py`` module any more;
2. every package ``__init__`` is a pure docstring (no code, no re-exports);
3. nothing under ``clipwright/`` or ``tests/`` still imports a module by its
old bare top-level name.
Failure messages name the offending files.
"""
import ast
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
PACKAGE = REPO_ROOT / "clipwright"
TESTS = REPO_ROOT / "tests"
# The old top-level module names. ``from clipwright.<group> import <name>`` is
# the supported spelling; importing any of these bare names is a regression.
LEGACY_MODULES = frozenset({
"active_speaker", "app", "camera_inset", "clip_selection", "edit_builder",
"editor", "ffmpeg_utils", "file_hosts", "frame_sampler", "free_tools",
"gemini_worker", "hook_grounding", "hooks", "layout_picker",
"layout_ranges", "llm_backend", "main", "media_auth", "panel_layout",
"postiz", "punch_in", "quality_probe", "recut", "reframe_v2",
"restoring_static", "scene_detection", "screencast_layout",
"security_utils", "split_layout", "subtitles", "thumbnail",
"transcribe_backends", "verify_aesthetic", "verify_custom_hook",
"verify_hooks", "watermarked", "yt_clients",
})
def _legacy_imports(path):
"""The legacy bare module names imported by the source at ``path``."""
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
found = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
root = alias.name.split(".")[0]
if root in LEGACY_MODULES:
found.add(root)
elif isinstance(node, ast.ImportFrom):
# A relative import inside the package (``from . import x``) is
# not a bare top-level import; only absolute ones can be legacy.
if node.level:
continue
root = (node.module or "").split(".")[0]
if root in LEGACY_MODULES:
found.add(root)
return found
def test_repo_root_has_no_python_modules():
offenders = sorted(p.name for p in REPO_ROOT.glob("*.py"))
assert not offenders, (
"stray top-level modules at the repo root (move them under clipwright/): "
+ ", ".join(offenders)
)
def test_package_inits_are_docstring_only():
offenders = []
for init in sorted(PACKAGE.rglob("__init__.py")):
tree = ast.parse(init.read_text(encoding="utf-8"), filename=str(init))
for node in tree.body:
is_docstring = (
isinstance(node, ast.Expr)
and isinstance(node.value, ast.Constant)
and isinstance(node.value.value, str)
)
if not is_docstring:
offenders.append(f"{init.relative_to(REPO_ROOT)}:{node.lineno}")
assert not offenders, (
"package __init__ files must contain only a docstring: "
+ ", ".join(offenders)
)
def test_no_legacy_bare_module_imports():
offenders = {}
sources = sorted(PACKAGE.rglob("*.py")) + sorted(TESTS.glob("*.py"))
for path in sources:
found = _legacy_imports(path)
if found:
offenders[str(path.relative_to(REPO_ROOT))] = sorted(found)
assert not offenders, (
"legacy bare module imports (use clipwright.<group> instead): "
+ ", ".join(
f"{name} -> {mods}" for name, mods in sorted(offenders.items())
)
)