"""L1 API: cadence handoff upload + submit (PLAN #8). The bundle rides the existing /api/uploads slot flow (``kind="handoff"``); the test asserts the staged manifest/transcript land in the job dir and the queued child command carries ``--handoff``. The worker never runs here. """ import io import json import os import zipfile import pytest pytestmark = pytest.mark.e2e KEY = {"X-Gemini-Key": "test-key"} def _zip_bytes(): manifest = { "schema": "clipwright.handoff/1", "generator": {"name": "cadence", "version": "0.1.0"}, "edited": {"duration": 6.0, "master": None}, "sources": [{"index": 0, "path": "/emitter/local/rec.mkv"}], "transcript": {"path": "rec.transcript.json"}, "candidates": [], } transcript = { "text": " hello world", "language": "en", "segments": [{"start": 0.0, "end": 1.0, "text": " hello world", "words": [{"word": " hello", "start": 0.0, "end": 0.4}, {"word": " world", "start": 0.4, "end": 1.0}]}], } buf = io.BytesIO() with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: zf.writestr("rec.handoff.json", json.dumps(manifest)) zf.writestr("rec.transcript.json", json.dumps(transcript)) return buf.getvalue() async def _master_upload(client, media_factory): reserved = await client.post("/api/uploads", json={"filename": "master.mp4"}) uid = reserved.json()["upload_id"] assert (await client.put(f"/api/uploads/{uid}", content=media_factory.six.read_bytes())).status_code == 200 return uid async def _handoff_upload(client, payload): reserved = await client.post("/api/uploads", json={"filename": "bundle.zip", "kind": "handoff"}) assert reserved.status_code == 200 assert reserved.json()["kind"] == "handoff" uid = reserved.json()["upload_id"] put = await client.put(f"/api/uploads/{uid}", content=payload) assert put.status_code == 200 assert put.json()["kind"] == "handoff" return uid async def test_handoff_id_stages_bundle_and_queues_the_flag(client, app_state, media_factory): master_id = await _master_upload(client, media_factory) handoff_id = await _handoff_upload(client, _zip_bytes()) resp = await client.post("/api/process", headers=KEY, json={ "upload_id": master_id, "handoff_id": handoff_id, "acknowledged": True}) assert resp.status_code == 200, resp.text job = app_state.app.jobs[resp.json()["job_id"]] assert "--handoff" in job["cmd"] manifest_path = job["cmd"][job["cmd"].index("--handoff") + 1] assert os.path.exists(manifest_path) staged = json.loads(open(manifest_path, encoding="utf-8").read()) assert staged["transcript"]["path"] == "source_transcript.json" assert os.path.exists(os.path.join(os.path.dirname(manifest_path), "source_transcript.json")) # The slot is consumed; the master upload still follows its own path. assert handoff_id not in app_state.app.pending_uploads async def test_non_zip_handoff_bundle_is_400(client, app_state, media_factory): master_id = await _master_upload(client, media_factory) handoff_id = await _handoff_upload(client, b"not a zip") resp = await client.post("/api/process", headers=KEY, json={ "upload_id": master_id, "handoff_id": handoff_id, "acknowledged": True}) assert resp.status_code == 400 assert "ZIP" in resp.json()["detail"] async def test_unknown_handoff_id_is_404(client, app_state, media_factory): master_id = await _master_upload(client, media_factory) resp = await client.post("/api/process", headers=KEY, json={ "upload_id": master_id, "handoff_id": "nope", "acknowledged": True}) assert resp.status_code == 404