"""L1 API core: the single-clip ZIP download bundle.""" import io import json import zipfile import pytest import clipwright.api.app as app pytestmark = pytest.mark.e2e async def test_clip_download_returns_folder_shaped_zip(client, seeded_job): job_id = seeded_job(job_id="job-clip", base="bundle", clips=1) resp = await client.get(f"/api/jobs/{job_id}/clip/0/download") assert resp.status_code == 200 assert resp.headers["content-type"] == "application/zip" with zipfile.ZipFile(io.BytesIO(resp.content)) as zf: assert sorted(zf.namelist()) == [ "Seeded clip 1/clip.mp4", "Seeded clip 1/description.txt", ] assert zf.read("Seeded clip 1/clip.mp4") assert zf.read("Seeded clip 1/description.txt").decode("utf-8") == "Seeded #test" async def test_clip_download_out_of_range_is_404(client, seeded_job): job_id = seeded_job(job_id="job-clip-range", base="bundle", clips=1) resp = await client.get(f"/api/jobs/{job_id}/clip/5/download") assert resp.status_code == 404 assert resp.json()["detail"] == "Clip not found" async def test_clip_download_without_metadata_is_404(client): resp = await client.get("/api/jobs/nobody-home/clip/0/download") assert resp.status_code == 404 assert resp.json()["detail"] == "Job not found" async def test_clip_download_without_clip_file_is_404(client, app_state): job_id = "job-clip-missing" job_dir = app_state.out / job_id job_dir.mkdir() # Metadata promises a clip; the file itself was never rendered. (job_dir / "missing_metadata.json").write_text(json.dumps({ "shorts": [{ "start": 0.0, "end": 5.0, "video_title_for_youtube_short": "Gone", }], })) resp = await client.get(f"/api/jobs/{job_id}/clip/0/download") assert resp.status_code == 404 assert resp.json()["detail"] == "Clip file not found" def test_safe_clip_bundle_name_sanitises(): name = app._safe_clip_bundle_name('A/B: c?') assert not (set(name) & set('/\\:*?"<>|')) assert name == name.strip(' .') assert app._safe_clip_bundle_name(' .. ') == 'clip' assert len(app._safe_clip_bundle_name('x' * 200)) <= 100