"""Thumbnail analyze cadence handoff ingest (server-side). Exercises :func:`_read_handoff_transcript` directly: the multipart upload is read into a throwaway dir, validated by ``_stage_handoff_bundle`` and its edited transcript returned so titles/description can skip Whisper. The helper must not pull in the heavy pipeline, so this module deliberately only imports the app. """ import asyncio import io import json import zipfile import pytest from starlette.datastructures import UploadFile app_module = pytest.importorskip("clipwright.api.app") def _zip_bytes(entries): buf = io.BytesIO() with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: for name, data in entries.items(): zf.writestr(name, data) return buf.getvalue() def _manifest_bytes(transcript_ref="rec.transcript.json"): return json.dumps({ "schema": "clipwright.handoff/1", "transcript": {"path": transcript_ref}, "edited": {"duration": 2.0}, }).encode("utf-8") def _transcript_bytes(): return json.dumps({ "text": "edited text", "language": "en", "segments": [{"start": 0.0, "end": 2.0, "text": "hi"}], }).encode("utf-8") def _upload(payload, filename="bundle.zip"): return UploadFile(file=io.BytesIO(payload), filename=filename) def _read(payload): return asyncio.run(app_module._read_handoff_transcript(_upload(payload))) def test_valid_bundle_returns_the_edited_transcript(): bundle = _zip_bytes({ "rec.handoff.json": _manifest_bytes(), "rec.transcript.json": _transcript_bytes(), }) transcript = _read(bundle) assert transcript["language"] == "en" assert transcript["segments"] == [{"start": 0.0, "end": 2.0, "text": "hi"}] def test_non_zip_is_400(): with pytest.raises(app_module.HTTPException) as exc: _read(b"this is not a zip") assert exc.value.status_code == 400 assert "ZIP" in exc.value.detail def test_zip_without_a_manifest_is_400(): bundle = _zip_bytes({"rec.transcript.json": _transcript_bytes()}) with pytest.raises(app_module.HTTPException) as exc: _read(bundle) assert exc.value.status_code == 400 assert "manifest" in exc.value.detail def test_oversized_bundle_is_413(monkeypatch): monkeypatch.setattr(app_module, "MAX_FILE_SIZE_MB", 1) limit_bytes = app_module.MAX_FILE_SIZE_MB * 1024 * 1024 with pytest.raises(app_module.HTTPException) as exc: _read(b"x" * (limit_bytes + 1)) assert exc.value.status_code == 413