droboticflow/tests/backend/test_delivery_pose_contracts.py

688 lines
31 KiB
Python

import hashlib
import json
import pathlib
import shutil
from types import SimpleNamespace
import pytest
from backend.operators.builtin.delivery_options import operator as delivery_options
from backend.operators.builtin.head_pose import operator as head_pose
from backend.operators.builtin.package import operator as package_operator
from backend.operators.builtin.wilor_hand_pose import operator as wilor
def helpers(params=None, payloads=None):
params = params or {}
payloads = payloads or []
def write_json(run_id, node_id, output_dir, name, data, output=None):
pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True)
return {"name": name, "data": data, "path": str(pathlib.Path(output_dir) / name)}
return {
"hyperparams": lambda node_data: params,
"payloads_from_inputs": lambda inputs: payloads,
"output_binding": lambda node_data: None,
"write_json_artifact": write_json,
"first_payload_with_keys": lambda inputs, *keys: None,
"collect_output_artifacts": lambda run_id, node_id, output_dir, **kwargs: [
{"id": f"{run_id}-{node_id}-{path.name}", "name": path.name, "path": str(path)}
for path in pathlib.Path(output_dir).rglob("*")
if path.is_file()
],
}
def test_delivery_options_defaults_handpose_off(tmp_path):
artifacts = delivery_options.run("run", "node", tmp_path, {}, [], helpers(payloads=[{}]))
assert artifacts[0]["data"] == {
"schemaVersion": "droboticflow.delivery-options/v1",
"includeHeadpose": True,
"includeHandpose": False,
}
def test_wilor_disabled_does_not_resolve_media_or_call_service(tmp_path):
artifacts = wilor.run(
"run",
"node",
tmp_path,
{},
[],
helpers(payloads=[{"schemaVersion": "droboticflow.delivery-options/v1", "includeHandpose": False}]),
)
assert artifacts[0]["data"]["enabled"] is False
assert artifacts[0]["data"]["status"] == "skipped_by_delivery_options"
assert artifacts[0]["data"]["presentation"] == "3d_hand_pose"
assert artifacts[0]["data"]["requiresHumanReview"] is False
def test_headpose_requires_real_service_metadata_and_validates_frames(tmp_path, monkeypatch):
annotated = tmp_path / "annotated.mp4"
annotated.write_bytes(b"real-annotated-video")
monkeypatch.setattr(
head_pose,
"_post_json",
lambda url, payload, timeout: {
"frames": [
{
"frameIndex": 3,
"timestampSeconds": 0.1,
"yawDeg": 1.0,
"pitchDeg": 2.0,
"rollDeg": 3.0,
"confidence": 0.9,
}
],
"annotatedMediaUrl": str(annotated),
"runtimeEvidence": {"gpu": "A800", "modelSha256": "sha256:" + "b" * 64},
},
)
artifacts = head_pose.run(
"run",
"node",
tmp_path,
{},
[{"previewURL": "https://example.test/input.mp4"}],
helpers(
params={
"HEADPOSE_API_BASE_URL": "https://headpose.example.test",
"HEADPOSE_SERVICE_IMAGE_ID": "registry/headpose@sha256:" + "a" * 64,
"HEADPOSE_MODEL_SHA256": "sha256:" + "b" * 64,
"HEADPOSE_LICENSE_ID": "MIT",
}
),
)
manifest = artifacts[0]["data"]
assert manifest["schemaVersion"] == "droboticflow.headpose/v1"
assert manifest["frames"][0]["yawDeg"] == 1.0
assert manifest["frames"][0]["timestamp"] == 0.1
assert manifest["summary"]["yawDeg"]["mean"] == 1.0
assert manifest["rendering"]["browserPlayable"] is True
assert artifacts[1]["name"] == "headpose-annotated.mp4"
def test_headpose_prefers_nested_presigned_url_over_bos_path():
payload = {
"path": "bos://bucket/capture_camera_right_rgb.mp4",
"source": {
"path": "bos://bucket/capture_camera_right_rgb.mp4",
"previewURL": "https://bucket.example/capture_camera_right_rgb.mp4?signature=real",
},
}
assert head_pose._media_reference(payload).startswith("https://")
def test_headpose_prefers_inline_manifest_local_media_before_bos_artifact(tmp_path):
local_video = tmp_path / "blurred-left-camera.mp4"
local_video.write_bytes(b"real-shared-workspace-video")
inputs = [
{
"name": "blurred-manifest.json",
"path": str(tmp_path / "blurred-manifest.json"),
"payload": {
"mode": "inline",
"type": "json",
"inline": {"items": [{"path": str(local_video), "source": {"path": str(local_video)}}]},
},
},
{"name": "blurred-left-camera.mp4", "path": "bos://bucket/blurred-left-camera.mp4"},
]
assert head_pose._input_reference(inputs) == str(local_video)
def test_headpose_materializes_bos_video_into_shared_workspace(tmp_path):
local_video = tmp_path / "materialized-left.mp4"
local_video.write_bytes(b"shared-video")
artifact = {"name": "left.mp4", "path": "bos://bucket/left.mp4", "type": "video"}
observed = {}
def materialize(item, name, output, archives=None):
observed.update(item=item, name=name, output=pathlib.Path(output))
return local_video
result = head_pose._materialized_input_reference(
[artifact],
tmp_path / "node-output",
{"source_path_from_item": materialize, "source_ref_from_artifact": lambda item: {"path": item["path"]}},
)
assert result == str(local_video)
assert observed["item"]["source"]["path"] == "bos://bucket/left.mp4"
def test_wilor_materializes_bos_video_before_service_upload(tmp_path):
local_video = tmp_path / "materialized-hand.mp4"
local_video.write_bytes(b"shared-video")
artifact = {"name": "left.mp4", "path": "bos://bucket/left.mp4", "type": "video"}
assert wilor._input_ref({}, [artifact]) == artifact["path"]
result = wilor._materialized_input_ref(
artifact["path"],
[artifact],
tmp_path / "node-output",
{
"source_path_from_item": lambda item, name, output, archives=None: local_video,
"source_ref_from_artifact": lambda item: {"path": item["path"]},
},
)
assert result == str(local_video)
def test_wilor_concat_list_is_technical_and_removed_after_success(tmp_path, monkeypatch):
source = tmp_path / "annotated-chunk.mp4"
source.write_bytes(b"chunk")
destination = tmp_path / "outputs" / "wilor-annotated-video.mp4"
def fake_run(command, **kwargs):
pathlib.Path(command[-1]).write_bytes(b"merged-video")
return SimpleNamespace(returncode=0, stdout="", stderr="")
monkeypatch.setattr(wilor.subprocess, "run", fake_run)
wilor._concat_annotated_videos([source], destination)
assert destination.read_bytes() == b"merged-video"
assert not (destination.parent / "_annotated-chunks.txt").exists()
def test_wilor_full_video_success_path_removes_private_input_and_chunk_directories():
source = pathlib.Path("backend/operators/builtin/wilor_hand_pose/operator.py").read_text(encoding="utf-8")
assert "shutil.rmtree(chunk_root)" in source
assert "shutil.rmtree(input_dir)" in source
def test_headpose_service_skips_empty_sampled_frames_but_keeps_video_fail_closed():
source = pathlib.Path("services/headpose/app.py").read_text(encoding="utf-8")
assert 'if str(error) != "no face detected in sampled frame"' in source
assert 'raise RuntimeError("no sampled video frame produced a headpose result")' in source
assert "encode_browser_video" in source
assert '"libx264"' in source
assert '"+faststart"' in source
assert '"browserPlayable": True' in source
assert 'NamedTemporaryFile(prefix="headpose-browser-"' in source
assert "shutil.copy2(local_output, destination)" in source
def test_package_v3_rejects_missing_required_headpose(tmp_path):
with pytest.raises(RuntimeError, match="headpose"):
package_operator.run(
"run",
"node",
tmp_path,
{},
[],
helpers(params={"REQUIRE_HEADPOSE": "true"}, payloads=[]),
)
def test_v5_pose_free_package_writes_only_video_semantic_and_manifests(tmp_path):
clip = tmp_path / "accepted.mp4"
clip.write_bytes(b"real-final-video")
canonical = '{"grade":"A"}'
acceptance = {
"canonical": canonical,
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
"confirmedByUserId": "acceptance-user",
}
accepted = {
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
"annotationCount": 1,
"items": [{
"clipId": "slice-001",
"clipName": "slice-001.mp4",
"clipPath": str(clip),
"startSecond": 0,
"endSecond": 1,
"sourceStartSecond": 0,
"sourceEndSecond": 1,
"sourceVideo": "blurred-source.mp4",
"finalAcceptanceConfirmed": True,
"finalAcceptance": acceptance,
"transformChain": [
{"kind": "opencv-undistort", "applied": True},
{"kind": "face-anonymization", "applied": True, "detector": {"modelId": "opencv-zoo/yunet-2023mar"}},
],
"annotation": {"actions": [{"labelId": "06", "label": "热食加工与出餐", "start": 0, "end": 1}]},
}],
}
package_helpers = helpers(
params={"REQUIRE_HEADPOSE": "false", "POSE_FREE": "true", "REQUIRE_FINAL_ACCEPTANCE": "true", "GENERATE_SEGMENT_MP4": "true"},
payloads=[accepted],
)
def write_json(run_id, node_id, output_dir, name, data, output=None):
path = pathlib.Path(output_dir) / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
package_helpers.update({
"write_json_artifact": write_json,
"write_video_segment_with_ffmpeg": lambda source, destination, start, end: shutil.copy2(source, destination),
"write_video_segment": lambda source, destination, start, end: shutil.copy2(source, destination),
"inspect_video_file": lambda path: {"duration": 1.0},
})
with pytest.MonkeyPatch.context() as monkeypatch:
monkeypatch.setattr(package_operator, "_write_precise_final_segment", lambda source, destination, start, end: shutil.copy2(source, destination))
artifacts = package_operator.run("run", "package", tmp_path / "output", {}, [], package_helpers)
package_dir = tmp_path / "output" / "packages" / "slice-001_package"
assert (package_dir / "slice-001.mp4").is_file()
assert (package_dir / "semantic.json").is_file()
assert (package_dir / "segment.json").is_file()
assert not (package_dir / "headpose.json").exists()
assert not (package_dir / "handpose.json").exists()
segment = json.loads((package_dir / "segment.json").read_text(encoding="utf-8"))
assert segment["semantic"]["file"] == "semantic.json"
assert [item["kind"] for item in segment["transformChain"]] == ["opencv-undistort", "face-anonymization"]
assert "headpose" not in segment and "handpose" not in segment
package_manifest = json.loads((tmp_path / "output" / "packages-manifest.json").read_text(encoding="utf-8"))
assert package_manifest["trainingEnabled"] is False
assert "trainingProvenance" not in package_manifest
assert package_manifest["items"][0]["clipDuration"] == 1
assert package_manifest["items"][0]["sourceVideo"] == "blurred-source.mp4"
assert package_manifest["items"][0]["quality"]["source_video"] == "blurred-source.mp4"
artifact_ids = [artifact["id"] for artifact in artifacts]
assert len(artifact_ids) == len(set(artifact_ids)) == 4
def test_v56_stereo_package_crops_both_cameras_and_pose_frames_with_same_boundary(tmp_path):
left_source = tmp_path / "capture_camera_left_part0001.mp4"
right_source = tmp_path / "capture_camera_right_part0001.mp4"
left_source.write_bytes(b"left-rectified-and-blurred")
right_source.write_bytes(b"right-rectified-and-blurred")
canonical = '{"accepted":true}'
accepted = {
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
"annotationCount": 1,
"items": [{
"clipId": "stereo-001",
"clipName": "stereo-001-left.mp4",
"clipPath": str(left_source),
"collectionId": "capture",
"sourceVideo": left_source.name,
"startSecond": 0.0,
"endSecond": 1.0,
"sourceStartSecond": 0.0,
"sourceEndSecond": 1.0,
"finalAcceptanceConfirmed": True,
"finalAcceptance": {
"canonical": canonical,
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
"confirmedByUserId": "acceptance-user",
},
"annotation": {"actions": []},
}, {
"clipId": "<nil>_final_001",
"clipName": "headpose-annotated.mp4",
"clipPath": str(left_source),
"sourceVideo": "headpose-annotated.mp4",
"startSecond": 0.0,
"endSecond": 1.0,
"sourceStartSecond": 0.0,
"sourceEndSecond": 1.0,
"finalAcceptanceConfirmed": True,
"finalAcceptance": {
"canonical": canonical,
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
"confirmedByUserId": "acceptance-user",
},
"annotation": {"actions": []},
}],
}
headpose = {
"schemaVersion": "droboticflow.headpose/v1",
"enabled": True,
"model": "6DRepNet",
"frames": [{"frameIndex": 3, "timestampSeconds": 0.2, "yawDeg": 1, "pitchDeg": 2, "rollDeg": 3, "confidence": 0.9}],
}
handpose = {
"schemaVersion": "droboticflow.wilor.hand_pose_result/v1",
"status": "success",
"result": {"frames": [{"sourceFrame": 3, "timestampMs": 200, "detectedHands": 1}]},
}
package_helpers = helpers(
params={
"REQUIRE_HEADPOSE": "true",
"REQUIRE_HANDPOSE": "true",
"REQUIRE_STEREO_DELIVERY": "true",
"POSE_FREE": "false",
"REQUIRE_FINAL_ACCEPTANCE": "true",
"GENERATE_SEGMENT_MP4": "true",
},
payloads=[accepted, headpose, handpose],
)
def write_json(run_id, node_id, output_dir, name, data, output=None):
path = pathlib.Path(output_dir) / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
package_helpers.update({
"write_json_artifact": write_json,
"source_path_from_item": lambda item, name, output, archives=None: pathlib.Path(str((item.get("source") or {}).get("path") or item.get("path"))),
"source_ref_from_artifact": lambda artifact: {"path": artifact.get("path", "")},
"write_video_segment_with_ffmpeg": lambda source, destination, start, end: shutil.copy2(source, destination),
"write_video_segment": lambda source, destination, start, end: shutil.copy2(source, destination),
"inspect_video_file": lambda path: {"duration": 1.0},
})
right_artifact = {
"name": right_source.name,
"path": str(right_source),
"type": "video",
"portId": "rightVideos",
"data": {"collectionId": "capture", "cameraSide": "right"},
}
with pytest.MonkeyPatch.context() as monkeypatch:
monkeypatch.setattr(package_operator, "_write_precise_final_segment", lambda source, destination, start, end: (pathlib.Path(destination).parent.mkdir(parents=True, exist_ok=True), shutil.copy2(source, destination)))
package_operator._run_fanyu_v5(
"run-v56",
"package",
tmp_path / "stereo-output",
{},
[{"name": "accepted.json", "data": accepted}, right_artifact, {"name": "headpose.json", "data": headpose}, {"name": "handpose.json", "data": handpose}],
package_helpers,
)
package_dir = tmp_path / "stereo-output" / "packages" / "stereo-001_package"
assert (package_dir / "stereo-001-left.mp4").is_file()
assert (package_dir / "stereo-001_right.mp4").is_file()
assert (package_dir / "headpose.json").is_file()
assert (package_dir / "handpose.json").is_file()
segment = json.loads((package_dir / "segment.json").read_text(encoding="utf-8"))
assert set(segment["videos"]) == {"left", "right"}
assert segment["videos"]["right"]["durationSeconds"] == 1.0
assert segment["headpose"]["frameCount"] == 1
assert segment["handpose"]["frameCount"] == 1
manifest = json.loads((tmp_path / "stereo-output" / "packages-manifest.json").read_text(encoding="utf-8"))
assert manifest["schemaVersion"] == "droboticflow.delivery.manifest/v3"
assert manifest["stereoDelivery"] is True
assert manifest["headposeIncluded"] is True
assert manifest["handposeIncluded"] is True
def test_v56_package_reloads_externalized_pose_manifests_from_json_files(tmp_path):
headpose_path = tmp_path / "headpose-manifest.json"
full_headpose = {
"schemaVersion": "droboticflow.headpose/v1",
"enabled": True,
"frames": [{"frameIndex": 1, "timestampSeconds": 0.1}],
}
headpose_path.write_text(json.dumps(full_headpose), encoding="utf-8")
handpose_path = tmp_path / "wilor-result.json"
full_handpose = {
"schemaVersion": "droboticflow.wilor.hand_pose_result/v1",
"enabled": True,
"result": {"frames": [{"sourceFrame": 1, "timestampMs": 100}]},
}
handpose_path.write_text(json.dumps(full_handpose), encoding="utf-8")
inputs = [
{
"name": headpose_path.name,
"type": "json",
"path": str(headpose_path),
"data": {"schemaVersion": "droboticflow.headpose/v1", "frameCount": 1},
},
{
"name": handpose_path.name,
"type": "json",
"path": str(handpose_path),
"data": {"schemaVersion": "droboticflow.wilor.hand_pose_result/v1", "frameCount": 1},
},
]
package_helpers = helpers(payloads=[item["data"] for item in inputs])
payloads = package_operator._payloads_with_full_pose_manifests(inputs, tmp_path / "output", package_helpers)
assert payloads == [full_headpose, full_handpose]
def test_v56_package_recognizes_right_camera_artifact_before_target_port_remap():
artifact = {
"name": "blurred_capture_camera_right_part0001.mp4",
"type": "video",
"portId": "blurredVideos",
"data": {
"collectionId": "capture",
"transformChain": [{"kind": "opencv-undistort", "cameraSide": "right"}],
},
}
assert package_operator._right_video_artifacts([artifact]) == [artifact]
def test_v58_package_synchronously_cuts_stereo_pts_and_imu_and_excludes_pose(tmp_path, monkeypatch):
left = tmp_path / "capture_camera_left_part0001.mp4"
right = tmp_path / "capture_camera_right_part0001.mp4"
left.write_bytes(b"left-video")
right.write_bytes(b"right-video")
left_pts = tmp_path / "capture_camera_left_part0001_pts.csv"
right_pts = tmp_path / "capture_camera_right_part0001_pts.csv"
left_pts.write_text("timestamp_us\n1000000\n1100000\n1200000\n1300000\n", encoding="utf-8")
right_pts.write_text("timestamp_us\n1050000\n1150000\n1250000\n1350000\n", encoding="utf-8")
imu = tmp_path / "capture_imu.csv"
imu.write_text(
"timestamp_us\t,x\t,y\t,z\t,type\n"
"900000,0,0,9.8,accel\n900000,0,0,0,gyro\n"
"1100000,0,0,9.8,accel\n1100000,0,0,0,gyro\n"
"1200000,0,0,9.8,accel\n1200000,0,0,0,gyro\n"
"1400000,0,0,9.8,accel\n1400000,0,0,0,gyro\n",
encoding="utf-8",
)
camera_calibration = tmp_path / "capture_calibration_camera.yaml"
imu_calibration = tmp_path / "capture_calibration_imu.yaml"
camera_calibration.write_text("camera: real\n", encoding="utf-8")
imu_calibration.write_text("imu: real\n", encoding="utf-8")
canonical = '{"accepted":true,"pose":"policy"}'
acceptance = {
"canonical": canonical,
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
"confirmedByUserId": "acceptance-user",
"poseDelivery": {"headpose": "exclude", "handpose": "include", "reasonCodes": ["headpose_unqualified"]},
}
accepted = {
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
"annotationCount": 1,
"items": [{
"clipId": "capture-final-001",
"clipName": "capture-final-001-left.mp4",
"clipPath": str(left),
"collectionId": "capture",
"sourceVideo": left.name,
"startSecond": 0.0,
"endSecond": 0.2,
"sourceStartSecond": 0.0,
"sourceEndSecond": 0.2,
"finalAcceptanceConfirmed": True,
"finalAcceptance": acceptance,
"annotation": {"actions": [{"labelId": "06", "start": 0.0, "end": 0.2}]},
}],
}
headpose = {"schemaVersion": "droboticflow.headpose/v1", "enabled": True, "frames": [{"timestampSeconds": 0.1}]}
handpose = {"schemaVersion": "droboticflow.wilor.hand_pose_result/v1", "enabled": True, "result": {"frames": [{"timestampMs": 100}]}}
inputs = [
{"name": "accepted.json", "type": "json", "data": accepted},
{"name": right.name, "path": str(right), "type": "video", "portId": "rightVideos", "data": {"collectionId": "capture", "cameraSide": "right"}},
{"name": left_pts.name, "path": str(left_pts), "type": "file", "portId": "videoPts", "data": {"collectionId": "capture"}},
{"name": right_pts.name, "path": str(right_pts), "type": "file", "portId": "videoPts", "data": {"collectionId": "capture"}},
{"name": imu.name, "path": str(imu), "type": "file", "portId": "imu", "data": {"collectionId": "capture"}},
{"name": camera_calibration.name, "path": str(camera_calibration), "type": "file", "portId": "calibrations", "data": {"collectionId": "capture"}},
{"name": imu_calibration.name, "path": str(imu_calibration), "type": "file", "portId": "calibrations", "data": {"collectionId": "capture"}},
{"name": "headpose.json", "type": "json", "data": headpose},
{"name": "handpose.json", "type": "json", "data": handpose},
]
package_helpers = helpers(
params={
"REQUIRE_HEADPOSE": "true",
"REQUIRE_HANDPOSE": "true",
"REQUIRE_STEREO_DELIVERY": "true",
"REQUIRE_FINAL_ACCEPTANCE": "true",
"SYNCHRONIZED_SIDECARS": "true",
"ALLOW_POSE_EXCLUSION": "true",
},
payloads=[accepted, {}, {}, {}, {}, {}, {}, headpose, handpose],
)
def write_json(run_id, node_id, output_dir, name, data, output=None):
path = pathlib.Path(output_dir) / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
package_helpers.update({
"write_json_artifact": write_json,
"source_path_from_item": lambda item, name, output, archives=None: pathlib.Path(str((item.get("source") or {}).get("path") or item.get("path"))),
"source_ref_from_artifact": lambda artifact: {"path": artifact.get("path", "")},
})
monkeypatch.setattr(package_operator, "_write_frame_segment", lambda source, destination, start, end: pathlib.Path(destination).write_bytes(b"frame-accurate-video"))
monkeypatch.setattr(package_operator, "_ffprobe_frame_count", lambda path: 2 if "final-001" in pathlib.Path(path).name else 4)
package_operator._run_fanyu_v5("run-v58", "package", tmp_path / "output", {}, inputs, package_helpers)
package_dir = tmp_path / "output" / "packages" / "capture-final-001_package"
synchronized = json.loads((package_dir / "synchronized-slice.json").read_text(encoding="utf-8"))
semantic = json.loads((package_dir / "semantic.json").read_text(encoding="utf-8"))
assert synchronized["schemaVersion"] == "droboticflow.synchronized-slice/v1"
assert synchronized["left"]["pts"]["rowCount"] == synchronized["left"]["video"]["frameCount"] == 2
assert synchronized["right"]["pts"]["rowCount"] == synchronized["right"]["video"]["frameCount"] == 2
assert synchronized["imu"]["sampleCount"] == 4
assert (package_dir / "capture-final-001_left_pts.csv").is_file()
assert (package_dir / "capture-final-001_right_pts.csv").is_file()
assert (package_dir / "capture-final-001_imu.csv").is_file()
assert semantic["schemaVersion"] == "droboticflow.timestamped-annotation/v1"
assert semantic["timebase"]["startTimestampUs"] == synchronized["window"]["startTimestampUs"]
assert semantic["timebase"]["endTimestampUs"] == synchronized["window"]["endTimestampUs"]
assert semantic["actions"][0]["startTimestampUs"] == synchronized["window"]["startTimestampUs"]
assert semantic["actions"][0]["endTimestampUs"] == synchronized["window"]["endTimestampUs"]
assert not (package_dir / "headpose.json").exists()
assert (package_dir / "handpose.json").is_file()
manifest = json.loads((tmp_path / "output" / "packages-manifest.json").read_text(encoding="utf-8"))
assert manifest["schemaVersion"] == "droboticflow.delivery.manifest/v4"
assert manifest["audioIncluded"] is False
assert manifest["headposeIncluded"] is False
assert manifest["handposeIncluded"] is True
assert len(manifest["calibration"]) == 2
def test_timestamped_description_contract_uses_clip_milliseconds():
synchronized = {"window": {"startTimestampUs": 1_000_000, "endTimestampUs": 2_000_000}}
annotation = {
"schemaVersion": "droboticflow.timestamped-description/v3",
"descriptions": [{"id": "description-1", "description": "拿取商品", "clipStartMs": 100, "clipEndMs": 600}],
}
result = package_operator._timestamped_annotation(annotation, synchronized)
assert result["schemaVersion"] == "droboticflow.timestamped-description/v1"
assert result["descriptions"][0]["startTimestampUs"] == 1_100_000
assert result["descriptions"][0]["endTimestampUs"] == 1_600_000
def test_unified_descriptions_preserve_hardware_edges_not_media_duration():
synchronized = {"window": {"startTimestampUs": 1_000_000, "endTimestampUs": 2_000_000},
"boundaryTimestampUs": [1_000_000, 1_400_000, 2_000_000]}
annotation = {"boundaryContract": "hardware-frame-v1", "descriptions": [
{"clipStartMs": 0, "clipEndMs": 300, "startTimestampUs": 1_000_000, "endTimestampUs": 1_400_000},
{"clipStartMs": 300, "clipEndMs": 900, "startTimestampUs": 1_400_000, "endTimestampUs": 2_000_000}]}
result = package_operator._timestamped_annotation(annotation, synchronized)
assert result["descriptions"][0]["endTimestampUs"] == result["descriptions"][1]["startTimestampUs"] == 1_400_000
assert result["descriptions"][1]["endTimestampUs"] == 2_000_000
annotation["descriptions"].pop(0)
with pytest.raises(RuntimeError, match="consecutive"):
package_operator._timestamped_annotation(annotation, synchronized)
@pytest.mark.parametrize("schema", ["droboticflow.headpose/v1", "droboticflow.wilor.hand_pose_result/v1"])
def test_v5_pose_free_package_rejects_unexpected_pose_payloads(tmp_path, schema):
accepted = {
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
"annotationCount": 1,
"items": [],
}
with pytest.raises(RuntimeError, match="pose-free"):
package_operator.run(
"run",
"package",
tmp_path,
{},
[],
helpers(
params={"REQUIRE_HEADPOSE": "false", "POSE_FREE": "true", "REQUIRE_FINAL_ACCEPTANCE": "true"},
payloads=[accepted, {"schemaVersion": schema, "enabled": True, "frames": []}],
),
)
def test_v5_virtual_rough_cut_materializes_full_source_then_cuts_final_range(tmp_path):
full_source = tmp_path / "full-sanitized.mp4"
full_source.write_bytes(b"real-full-sanitized-video")
canonical = '{"confirmed":true}'
accepted = {
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
"annotationCount": 1,
"items": [{
"clipId": "rough-final-001",
"clipName": "rough-final-001.mp4",
"clipPath": "bos://testxyz/sanitized/full.mp4",
"source": {"path": "bos://testxyz/sanitized/full.mp4"},
"sourceIsFullVideo": True,
"startSecond": 8.5,
"endSecond": 11.25,
"sourceStartSecond": 8.5,
"sourceEndSecond": 11.25,
"finalAcceptanceConfirmed": True,
"finalAcceptance": {
"canonical": canonical,
"sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
"confirmedByUserId": "acceptance-user",
},
"annotation": {"actions": []},
}],
}
observed = {}
package_helpers = helpers(
params={"REQUIRE_HEADPOSE": "false", "REQUIRE_FINAL_ACCEPTANCE": "true", "GENERATE_SEGMENT_MP4": "true"},
payloads=[accepted],
)
def write_json(run_id, node_id, output_dir, name, data, output=None):
path = pathlib.Path(output_dir) / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
return {"id": f"{run_id}-{node_id}-{name}", "name": name, "data": data, "path": str(path)}
def cut(source, destination, start, end):
observed.update(source=pathlib.Path(source), start=start, end=end)
pathlib.Path(destination).parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, destination)
return {"duration": end - start}
package_helpers.update({
"write_json_artifact": write_json,
"source_path_from_item": lambda item, name, output, archives=None: full_source,
"write_video_segment_with_ffmpeg": cut,
"write_video_segment": cut,
"inspect_video_file": lambda path: {"duration": 2.75},
})
with pytest.MonkeyPatch.context() as monkeypatch:
monkeypatch.setattr(package_operator, "_write_precise_final_segment", cut)
package_operator.run("run", "package", tmp_path / "virtual-output", {}, [], package_helpers)
assert observed == {"source": full_source, "start": 8.5, "end": 11.25}
def test_precise_final_segment_reencodes_and_resets_timestamps(tmp_path, monkeypatch):
source = tmp_path / "source.mp4"
destination = tmp_path / "final.mp4"
source.write_bytes(b"source")
observed = {}
def fake_run(args, **kwargs):
observed["args"] = args
destination.write_bytes(b"final")
return type("Completed", (), {"returncode": 0, "stdout": "", "stderr": ""})()
monkeypatch.setattr(package_operator.shutil, "which", lambda name: "/usr/bin/ffmpeg")
monkeypatch.setattr(package_operator.subprocess, "run", fake_run)
package_operator._write_precise_final_segment(source, destination, 9.6, 20.5)
assert destination.is_file()
assert observed["args"][observed["args"].index("-ss") + 1] == "9.600000"
assert observed["args"][observed["args"].index("-t") + 1] == "10.900000"
assert "libx264" in observed["args"]
assert observed["args"][observed["args"].index("-reset_timestamps") + 1] == "1"
assert "-movflags" not in observed["args"]