clipwright/tests/e2e/pipeline/test_cli_full_pipeline.py
pedro-bento 97f813553d test: add full E2E coverage (API, pipeline, browser)
Three-level end-to-end suite, all deterministic (no retries, no sleeps, no
unmocked network):

- tests/e2e/api (209 tests): every route in clipwright/api/app.py +
  free_tools — health/config, uploads, process submit matrix + worker/webhook/
  auto-retry/resume, status/source/download-all, edit/effects, transcript/EDL/
  rerender, scenes/reframe, subtitle, hook, Postiz, thumbnails, free tools,
  and the security/path guards.
- tests/e2e/pipeline (136 tests): every stage with real ffmpeg and stubbed
  Gemini/yt-dlp/ASR/ML — download, transcribe, scene detect, selection,
  layout, reframe/render, subtitles, hooks, watermark, thumbnails, plus the
  real CLI full pipeline via stub_env.
- dashboard/e2e (19 Playwright specs): route-interception state machine
  covering every dashboard flow; one real-backend spec left test.fixme.
- Foundation: tests/e2e/conftest.py (state isolation, no-network guard, ffmpeg
  media factory, disk-seeded jobs), FakePopen worker harness, child-process
  stub_env, architecture-consistent fixtures.
- CI: unit + backend-e2e + dashboard-e2e jobs in the python+node container;
  ffmpeg + Playwright installed in-job; no setup-node/cache (act runner).

Verified: unit 790 passed; API E2E 209 passed; pipeline E2E 136 passed;
Playwright 19 passed / 1 skipped.
2026-10-04 22:12:21 +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"))