clipwright/tests/e2e/pipeline/test_cli_full_pipeline.py
pedro-bento 0040aa49da
All checks were successful
CI / dashboard-e2e (push) Successful in 1m44s
CI / unit (push) Successful in 29s
CI / backend-e2e (push) Successful in 1m17s
test/e2e-coverage (#5)
Reviewed-on: #5
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-04 22:28:46 +01:00

119 lines
4.2 KiB
Python

"""L4 — full CLI pipeline (the keystone test).
Runs the REAL ``python -m clipwright.pipeline.main`` end to end with real
ffmpeg. The child interpreter is started with ``stub_env`` first on
``PYTHONPATH``, so ``sitecustomize.py`` injects the fake ultralytics/mediapipe
modules, stubs ASR, and patches the Gemini client. The test asserts on exit
code, metadata JSON, real clip files (ffprobe, never bytes) and the
``CLIP_READY`` markers — plus a failure variant that must write no metadata.
"""
import json
import subprocess
from pathlib import Path
import pytest
pytestmark = [pytest.mark.e2e, pytest.mark.pipeline]
def _cli_env(pipeline_env, **overrides):
"""Pin the deterministic, no-ML pipeline environment for the child."""
pipeline_env.update({
"GEMINI_API_KEY": "test",
"AUTO_LAYOUT": "0",
"SCENE_ENGINE": "pyscenedetect",
"FFMPEG_ENCODER": "x264",
"ASR_HOST_SLOTS": "0",
"CLIP_WORKERS": "2",
})
for var in ("AUTO_HOOK", "WATERMARK", "MAX_SOURCE_MINUTES",
"SOURCE_CAP_MINUTES", "DOWNLOAD_SKIP_STATICS"):
pipeline_env.pop(var, None)
pipeline_env.update(overrides)
return pipeline_env
def _probe(path):
out = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "v:0",
"-show_entries", "stream=width,height", "-of", "csv=p=0:s=x", str(path)],
capture_output=True, text=True, check=True)
width, height = out.stdout.strip().split("x")
return int(width), int(height)
def _ready_lines(stdout):
return [line for line in stdout.splitlines() if line.startswith("CLIP_READY ")]
def _assert_success(result, out, media, fmt):
assert result.returncode == 0, (
f"CLI failed ({result.returncode})\nSTDOUT:\n{result.stdout[-4000:]}\n"
f"STDERR:\n{result.stderr[-4000:]}")
metadata_files = list(out.glob("*_metadata.json"))
assert len(metadata_files) == 1
meta = json.loads(metadata_files[0].read_text())
assert meta["shorts"], "expected at least one clip"
assert meta["transcript"]["segments"], "expected the transcript in metadata"
assert meta["source_video"] == media.name
assert meta["output_format"] == fmt
base = media.stem
clips = sorted(out.glob(f"{base}_clip_*.mp4"))
assert len(clips) == len(meta["shorts"])
for clip in clips:
assert clip.stat().st_size > 1024
assert _probe(clip)[0] > 0
ready = _ready_lines(result.stdout)
assert len(ready) == len(meta["shorts"])
return meta, clips
def test_cli_full_pipeline_vertical(run_pipeline, pipeline_env, media_factory, tmp_path):
_cli_env(pipeline_env)
out = tmp_path / "out"
result = run_pipeline(media_factory.six, "-o", str(out), "--format", "vertical")
meta, clips = _assert_success(result, out, media_factory.six, "vertical")
width, height = _probe(clips[0])
assert height > width, f"vertical clip should be portrait, got {width}x{height}"
def test_cli_full_pipeline_horizontal(run_pipeline, pipeline_env, media_factory, tmp_path):
_cli_env(pipeline_env)
out = tmp_path / "out"
result = run_pipeline(media_factory.six, "-o", str(out), "--format", "horizontal")
meta, clips = _assert_success(result, out, media_factory.six, "horizontal")
width, height = _probe(clips[0])
assert width > height, f"horizontal clip should be landscape, got {width}x{height}"
def test_cli_failure_writes_no_metadata(run_pipeline, pipeline_env, media_factory, tmp_path):
# No Gemini key + no clips available -> the pipeline raises instead of
# shipping a silent render; nothing must be written to the output dir.
_cli_env(pipeline_env, GEMINI_API_KEY="")
out = tmp_path / "out"
result = run_pipeline(media_factory.silent, "-o", str(out), "--format", "vertical")
assert result.returncode != 0
assert not list(out.glob("*_metadata.json"))
assert not list(out.glob("*_clip_*.mp4"))
def test_cli_missing_input_exits_nonzero(run_pipeline, pipeline_env, tmp_path):
_cli_env(pipeline_env)
out = tmp_path / "out"
result = run_pipeline(tmp_path / "does-not-exist.mp4", "-o", str(out))
assert result.returncode != 0
assert not list(out.glob("*_metadata.json"))