Reviewed-on: #8 Co-authored-by: pedro-bento <mail@pbento.pt> Co-committed-by: pedro-bento <mail@pbento.pt>
177 lines
6 KiB
Python
177 lines
6 KiB
Python
"""Cadence handoff bundle staging at the API edge (PLAN #8).
|
|
|
|
Only the bundle transport is exercised here (ZIP in an /api/uploads slot); the
|
|
CLI validation of the manifest is covered by tests/test_handoff.py and the
|
|
tiny-video run under tests/e2e/pipeline.
|
|
"""
|
|
import asyncio
|
|
import io
|
|
import json
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
app = 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",
|
|
schema="clipwright.handoff/1"):
|
|
return json.dumps({
|
|
"schema": schema,
|
|
"generator": {"name": "cadence", "version": "0.1.0"},
|
|
"edited": {"duration": 6.0, "master": None},
|
|
"sources": [{"index": 0, "path": "/abs/rec.mkv"}],
|
|
"transcript": {"path": transcript_ref},
|
|
"candidates": [],
|
|
}).encode("utf-8")
|
|
|
|
|
|
def _transcript_bytes():
|
|
return json.dumps({
|
|
"text": "hello",
|
|
"language": "en",
|
|
"segments": [{"start": 0.0, "end": 1.0, "text": " hello",
|
|
"words": [{"word": " hello", "start": 0.0, "end": 1.0}]}],
|
|
}).encode("utf-8")
|
|
|
|
|
|
def _job_dir(tmp_path):
|
|
job = tmp_path / "job"
|
|
job.mkdir()
|
|
return job
|
|
|
|
|
|
# --- staging ---------------------------------------------------------------
|
|
|
|
def test_stages_manifest_and_transcript_into_the_job_dir(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({
|
|
"rec.handoff.json": _manifest_bytes(),
|
|
"rec.transcript.json": _transcript_bytes(),
|
|
}))
|
|
|
|
manifest_path = app._stage_handoff_bundle(str(bundle), str(job))
|
|
|
|
assert manifest_path == str(job / "cadence_handoff.json")
|
|
staged = json.loads(Path(manifest_path).read_text())
|
|
assert staged["transcript"]["path"] == "source_transcript.json"
|
|
assert staged["edited"]["duration"] == 6.0
|
|
transcript = json.loads((job / "source_transcript.json").read_text())
|
|
assert transcript["segments"]
|
|
|
|
|
|
def test_manifest_found_by_schema_when_not_named_handoff_json(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({
|
|
"cadence-manifest.json": _manifest_bytes(),
|
|
"rec.transcript.json": _transcript_bytes(),
|
|
}))
|
|
manifest_path = app._stage_handoff_bundle(str(bundle), str(job))
|
|
assert Path(manifest_path).exists()
|
|
|
|
|
|
def test_member_paths_cannot_escape_the_job_dir(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({
|
|
"../evil.handoff.json": _manifest_bytes(),
|
|
"../rec.transcript.json": _transcript_bytes(),
|
|
}))
|
|
app._stage_handoff_bundle(str(bundle), str(job))
|
|
assert not (tmp_path / "evil.handoff.json").exists()
|
|
assert not (tmp_path / "rec.transcript.json").exists()
|
|
assert (job / "cadence_handoff.json").exists()
|
|
|
|
|
|
def test_non_zip_bundle_is_400(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
not_a_zip = tmp_path / "bundle.bin"
|
|
not_a_zip.write_bytes(b"not a zip")
|
|
with pytest.raises(app.HTTPException) as exc:
|
|
app._stage_handoff_bundle(str(not_a_zip), str(job))
|
|
assert exc.value.status_code == 400
|
|
assert "ZIP" in exc.value.detail
|
|
|
|
|
|
def test_bundle_without_a_manifest_is_400(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({"rec.transcript.json": _transcript_bytes()}))
|
|
with pytest.raises(app.HTTPException) as exc:
|
|
app._stage_handoff_bundle(str(bundle), str(job))
|
|
assert exc.value.status_code == 400
|
|
assert "manifest" in exc.value.detail
|
|
|
|
|
|
def test_manifest_referencing_a_missing_transcript_is_400(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({"rec.handoff.json": _manifest_bytes()}))
|
|
with pytest.raises(app.HTTPException) as exc:
|
|
app._stage_handoff_bundle(str(bundle), str(job))
|
|
assert exc.value.status_code == 400
|
|
assert "not in the bundle" in exc.value.detail
|
|
|
|
|
|
def test_transcript_without_segments_is_400(tmp_path):
|
|
job = _job_dir(tmp_path)
|
|
bundle = tmp_path / "bundle.zip"
|
|
bundle.write_bytes(_zip_bytes({
|
|
"rec.handoff.json": _manifest_bytes(),
|
|
"rec.transcript.json": json.dumps({"segments": []}).encode(),
|
|
}))
|
|
with pytest.raises(app.HTTPException) as exc:
|
|
app._stage_handoff_bundle(str(bundle), str(job))
|
|
assert exc.value.status_code == 400
|
|
assert "no segments" in exc.value.detail
|
|
|
|
|
|
# --- /api/uploads handoff slot ---------------------------------------------
|
|
|
|
def _client_call(method, path, **kw):
|
|
async def _do():
|
|
transport = httpx.ASGITransport(app=app.app)
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as c:
|
|
return await getattr(c, method)(path, **kw)
|
|
return asyncio.run(_do())
|
|
|
|
|
|
def test_handoff_slot_stores_a_zip_without_a_duration_probe(tmp_path, monkeypatch):
|
|
uploads = tmp_path / "uploads"
|
|
uploads.mkdir()
|
|
monkeypatch.setattr(app, "UPLOAD_DIR", str(uploads))
|
|
monkeypatch.setattr(app, "pending_uploads", {})
|
|
|
|
created = _client_call("post", "/api/uploads",
|
|
json={"filename": "bundle.zip", "kind": "handoff"})
|
|
assert created.status_code == 200
|
|
assert created.json()["kind"] == "handoff"
|
|
upload_id = created.json()["upload_id"]
|
|
|
|
bundle = _zip_bytes({
|
|
"rec.handoff.json": _manifest_bytes(),
|
|
"rec.transcript.json": _transcript_bytes(),
|
|
})
|
|
put = _client_call("put", f"/api/uploads/{upload_id}", content=bundle)
|
|
assert put.status_code == 200
|
|
assert put.json()["kind"] == "handoff"
|
|
assert "duration_seconds" not in put.json()
|
|
|
|
|
|
def test_unknown_upload_kind_is_400(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(app, "pending_uploads", {})
|
|
resp = _client_call("post", "/api/uploads",
|
|
json={"filename": "x.mp4", "kind": "nonsense"})
|
|
assert resp.status_code == 400
|