import zipfile from io import BytesIO def _seed_export_data(client): project = client.post("/api/projects", json={"name": "Export Project"}).json() frame = client.post(f"/api/projects/{project['id']}/frames", json={ "project_id": project["id"], "frame_index": 0, "image_url": "frames/0.jpg", "width": 100, "height": 50, }).json() template = client.post("/api/templates", json={ "name": "Category", "color": "#06b6d4", "z_index": 0, "classes": [], "rules": [], }).json() annotation = client.post("/api/ai/annotate", json={ "project_id": project["id"], "frame_id": frame["id"], "template_id": template["id"], "mask_data": {"polygons": [[[0.1, 0.2], [0.9, 0.2], [0.9, 0.8], [0.1, 0.8]]]}, "points": [[0.5, 0.5]], "bbox": [0.1, 0.2, 0.8, 0.6], }).json() return project, frame, template, annotation def test_export_coco_json_structure(client): project, frame, _, _ = _seed_export_data(client) response = client.get(f"/api/export/{project['id']}/coco") assert response.status_code == 200 assert response.headers["content-type"].startswith("application/json") data = response.json() assert data["info"]["description"] == "Annotations for Export Project" assert data["images"][0] == { "id": frame["id"], "file_name": "frames/0.jpg", "width": 100, "height": 50, "frame_index": 0, } assert data["annotations"][0]["segmentation"] == [[10.0, 10.0, 90.0, 10.0, 90.0, 40.0, 10.0, 40.0]] assert data["annotations"][0]["bbox"] == [10.0, 10.0, 80.0, 30.000000000000004] assert data["categories"][0]["name"] == "Category" def test_export_masks_zip(client): project, _, _, annotation = _seed_export_data(client) response = client.get(f"/api/export/{project['id']}/masks") assert response.status_code == 200 assert response.headers["content-type"].startswith("application/zip") with zipfile.ZipFile(BytesIO(response.content)) as archive: assert archive.namelist() == [f"mask_{annotation['id']:06d}.png"] def test_export_missing_project_returns_404(client): assert client.get("/api/export/999/coco").status_code == 404 assert client.get("/api/export/999/masks").status_code == 404