"""Pipeline E2E fixtures. The tests here run the REAL CLI (``python -m clipwright.pipeline.main``) with real ffmpeg, but the child interpreter is started with ``stub_env`` on ``PYTHONPATH``. ``stub_env/sitecustomize.py`` swaps the ML and network edges (ultralytics/mediapipe, ASR, Gemini) for deterministic fakes, so no model is downloaded and no request leaves the process. Mark ML-backed variants with ``pytest.mark.ml``; those need the heavy ML stack from ``requirements.txt``. """ import os import subprocess import sys from pathlib import Path import pytest pytestmark = pytest.mark.pipeline REPO_ROOT = Path(__file__).resolve().parents[3] STUB_ENV = Path(__file__).resolve().parent / "stub_env" @pytest.fixture(scope="session") def stub_env_dir(): """Directory holding ``sitecustomize.py`` for the pipeline child.""" assert (STUB_ENV / "sitecustomize.py").is_file() return STUB_ENV @pytest.fixture def pipeline_env(stub_env_dir, tmp_path): """Environment for a pipeline subprocess: stub env on PYTHONPATH, no LLM.""" env = dict(os.environ) env.pop("LLM_BASE_URL", None) env.pop("LLM_MODEL", None) env["GEMINI_API_KEY"] = "stub-gemini-key" env["PYTHONPATH"] = os.pathsep.join( [str(stub_env_dir), str(REPO_ROOT), env.get("PYTHONPATH", "")] ).strip(os.pathsep) env["PYTHONHASHSEED"] = "0" env["TZ"] = "UTC" # Keep output inside the test's tmp tree unless the caller overrides it. env.setdefault("TRANSCRIBE_BACKEND", "whisper") return env @pytest.fixture def run_pipeline(pipeline_env): """Run the real CLI; returns the CompletedProcess. Usage: ``run_pipeline(media.six, "-o", str(out))``. """ def _run(input_path, *extra, timeout=180): cmd = [ sys.executable, "-u", "-m", "clipwright.pipeline.main", "-i", str(input_path), *extra, ] return subprocess.run( cmd, cwd=str(REPO_ROOT), env=pipeline_env, capture_output=True, text=True, timeout=timeout, ) return _run