clipwright/tests/test_thumbnail_handoff.py
pedro-bento 980dae98e8
Some checks failed
CI / dashboard-e2e (push) Waiting to run
CI / backend-e2e (push) Waiting to run
CI / unit (push) Has been cancelled
feat(youtube-studio): accept a cadence handoff bundle alongside the video (#11)
The Clip Generator already takes an optional cadence handoff ZIP and uses its
edited transcript instead of Whisper. YouTube Studio now does the same: an
optional `.zip` on the Analyze Video step rides along as a `handoff` field on
POST /api/thumbnail/analyze.

- `_read_handoff_transcript` stages the upload under the existing size cap and
  validates it with `_stage_handoff_bundle`, returning the edited transcript
  without importing the heavy pipeline.
- `thumbnail_analyze` passes that transcript as the pre-transcript and skips the
  background Whisper wait/error path when a handoff is present; behavior is
  unchanged when it is not.
- The video is still required for frames and publishing; the handoff only
  replaces transcription.
- UI: optional handoff drop zone on Step 0, skipped early Whisper when a bundle
  is attached, and reset clears it.

Reviewed-on: #11
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-14 09:53:57 +01:00

83 lines
2.5 KiB
Python

"""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