forked from longtao.wu/droboticflow
214 lines
10 KiB
Python
214 lines
10 KiB
Python
"""Part-index delivery contract: one JSON per part, no sliced media tree."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from backend.operators.builtin.package import operator as package
|
|
|
|
from tests.backend.test_slice_discard_package import NODE as BASE_NODE
|
|
from tests.backend.test_slice_discard_package import fixture
|
|
|
|
|
|
PART_NODE = {
|
|
"outputs": [{"id": name, "name": name} for name in ("deliveryPartIndex", "deliverySummary")],
|
|
"hyperparams": [{"key": "PART_INDEX_DELIVERY", "value": "true"}],
|
|
}
|
|
|
|
|
|
def helpers():
|
|
def write(run, node, directory, name, payload, output=None):
|
|
path = Path(directory) / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload), encoding="utf8")
|
|
return {"id": run + name, "name": name, "path": str(path), "data": payload, "portId": output["id"]}
|
|
|
|
def build(run, node, path, artifact_type, data=None, metadata=None, output=None):
|
|
return {
|
|
"id": run + Path(path).stem,
|
|
"name": Path(path).name,
|
|
"path": str(path),
|
|
"type": artifact_type,
|
|
"metadata": dict(metadata or {}),
|
|
"portId": output["id"] if output else "",
|
|
}
|
|
|
|
return {
|
|
"write_json_artifact": write,
|
|
"build_artifact": build,
|
|
"build_virtual_artifact": lambda run, node, name, path, kind, size, data=None, output=None, **kw: {
|
|
"id": run + name,
|
|
"name": name,
|
|
"path": path,
|
|
"data": data,
|
|
"portId": output["id"] if output else "",
|
|
},
|
|
"payloads_from_inputs": lambda items: [i.get("data", {}) for i in items],
|
|
"hyperparams": lambda node: {str(p.get("key")): str(p.get("value")) for p in node.get("hyperparams", []) if isinstance(p, dict)},
|
|
"source_path_from_item": lambda item, *args: Path(item.get("path") or item["source"]["path"]),
|
|
"source_ref_from_artifact": lambda item: {"path": item.get("path")},
|
|
}
|
|
|
|
|
|
def run_part_index(tmp_path, dispositions, node=None, root=None):
|
|
inputs = fixture(tmp_path, dispositions)
|
|
if node is None:
|
|
# The shared fixture is a raw-human revision; give it the pose decision
|
|
# the accepted-slice contract requires so the part-index branch runs.
|
|
inputs[1]["data"]["finalAcceptance"]["poseDelivery"] = {"headpose": "exclude", "handpose": "exclude"}
|
|
artifacts = package.run("r", "p", root or tmp_path / "output", node or PART_NODE, inputs, helpers())
|
|
return inputs, artifacts
|
|
|
|
|
|
def read_index(artifact):
|
|
return json.loads(Path(artifact["path"]).read_text(encoding="utf-8"))
|
|
|
|
|
|
def test_part_index_is_a_single_json_delivery_with_both_split_levels(tmp_path):
|
|
_, artifacts = run_part_index(tmp_path, ["keep", "discard", "keep"])
|
|
assert len(artifacts) == 1
|
|
artifact = artifacts[0]
|
|
assert artifact["portId"] == "deliveryPartIndex"
|
|
assert artifact["name"] == "part.json"
|
|
# The artifact path is the deliverable itself; no sliced media tree exists.
|
|
assert Path(artifact["path"]).is_file()
|
|
assert not list((tmp_path / "output").rglob("*.mp4"))
|
|
|
|
index = read_index(artifact)
|
|
assert index["schemaVersion"] == "droboticflow.delivery.part-index/v1"
|
|
assert index["delivery"] == {
|
|
"kind": "part_index",
|
|
"fileName": "part.json",
|
|
"artifactName": "delivery.part-index",
|
|
}
|
|
|
|
# Task-level split carries every delivered interval with its classification.
|
|
assert [entry["sliceId"] for entry in index["slices"]] == ["slice-000001", "slice-000003"]
|
|
assert [entry["classification"]["deliveryDisposition"] for entry in index["slices"]] == ["keep", "keep"]
|
|
assert all(entry["taskSplit"]["semantics"] == "half-open" for entry in index["slices"])
|
|
assert index["deliveryClassification"]["keptSliceCount"] == 2
|
|
assert index["deliveryClassification"]["discardedSliceCount"] == 1
|
|
# The pinned review revision still covers every reviewed interval.
|
|
assert index["coverage"]["sliceCount"] == 3
|
|
|
|
# Action-level split is part of the same document.
|
|
first = index["slices"][0]
|
|
assert first["segments"][0]["description"] == "无效片段"
|
|
assert first["segments"][0]["isInvalid"] is True
|
|
assert first["classification"]["descriptionStatus"] == "invalid_only"
|
|
|
|
|
|
def test_pose_frames_stay_out_of_the_part_index(tmp_path):
|
|
"""The index carries the pose delivery decision, never the frame bytes.
|
|
|
|
A include decision must not pull per-frame pose into part.json: a real
|
|
segment would otherwise embed megabytes of frames into a kilobyte index.
|
|
"""
|
|
inputs = fixture(tmp_path, ["keep"])
|
|
inputs[1]["data"]["finalAcceptance"]["poseDelivery"] = {"headpose": "include", "handpose": "include"}
|
|
pose = {
|
|
"schemaVersion": "droboticflow.headpose/v1",
|
|
"enabled": True,
|
|
"frames": [
|
|
{"timestampSeconds": 0.04, "frameIndex": 1, "values": [0.1, 0.2, 0.3, 0.4]},
|
|
{"timestampSeconds": 0.08, "frameIndex": 2, "values": [0.1, 0.2, 0.3, 0.4]},
|
|
],
|
|
}
|
|
hand = {
|
|
"schemaVersion": "droboticflow.wilor.hand_pose_result/v1",
|
|
"enabled": True,
|
|
"result": {"frames": [{"timestampSeconds": 0.04, "sourceFrame": 1}]},
|
|
}
|
|
inputs.append({"id": "headpose", "portId": "headpose", "type": "json", "data": pose})
|
|
inputs.append({"id": "handpose", "portId": "handpose", "type": "json", "data": hand})
|
|
artifacts = package.run("r", "p", tmp_path / "output", PART_NODE, inputs, helpers())
|
|
index = read_index(artifacts[0])
|
|
entry = index["slices"][0]
|
|
# The decision is exported for downstream resolution.
|
|
assert entry["classification"]["poseDelivery"] == {"headpose": "include", "handpose": "include"}
|
|
# The frames themselves never enter the document.
|
|
assert "pose" not in entry
|
|
body = Path(artifacts[0]["path"]).read_text(encoding="utf-8")
|
|
assert "timestampSeconds" not in body and "frameIndex" not in body
|
|
assert index["poseDelivery"] == {"headpose": "include", "handpose": "include"}
|
|
|
|
|
|
def test_discarded_slices_are_delivered_as_their_own_classification(tmp_path):
|
|
_, artifacts = run_part_index(tmp_path, ["keep", "discard"])
|
|
index = read_index(artifacts[0])
|
|
assert [entry["sliceId"] for entry in index["slices"]] == ["slice-000001"]
|
|
assert [entry["sliceId"] for entry in index["discardedSlices"]] == ["slice-000002"]
|
|
discarded = index["discardedSlices"][0]
|
|
assert discarded["classification"]["deliveryDisposition"] == "discard"
|
|
assert discarded["classification"]["discardEvidence"]["reason"] == "PRIVATE_DISCARD_REASON"
|
|
# No media members: the index only classifies, it does not ship bytes.
|
|
assert "files" not in discarded and "mediaKey" not in discarded
|
|
|
|
|
|
def test_calibration_is_referenced_with_checksums(tmp_path):
|
|
_, artifacts = run_part_index(tmp_path, ["keep"])
|
|
index = read_index(artifacts[0])
|
|
assert {entry["name"] for entry in index["calibration"]} == {"calibration_camera.yaml", "calibration_imu.yaml"}
|
|
for entry in index["calibration"]:
|
|
assert len(entry["sha256"]) == 64
|
|
assert entry["bytes"] > 0
|
|
for entry in index["slices"]:
|
|
assert len(entry["synchronization"]["calibration"]) == 2
|
|
|
|
|
|
def test_review_comments_and_acceptance_statistics_stay_internal(tmp_path):
|
|
_, artifacts = run_part_index(tmp_path, ["keep", "discard"])
|
|
body = Path(artifacts[0]["path"]).read_text(encoding="utf-8")
|
|
# The quality review comment shares its text with the discard reason here,
|
|
# so assert on the review projection itself rather than raw text.
|
|
index = json.loads(body)
|
|
assert "statistics" not in index["acceptance"]["finalAcceptance"]
|
|
assert all("comment" not in review for review in index["acceptance"]["qualityReviews"])
|
|
|
|
|
|
def test_part_index_bytes_are_reproducible_for_the_same_revision(tmp_path):
|
|
inputs, _ = run_part_index(tmp_path, ["keep", "discard"], root=tmp_path / "a")
|
|
first = package.run("r", "p", tmp_path / "a", PART_NODE, inputs, helpers())
|
|
second = package.run("r", "p", tmp_path / "b", PART_NODE, inputs, helpers())
|
|
assert Path(first[0]["path"]).read_bytes() == Path(second[0]["path"]).read_bytes()
|
|
|
|
|
|
def test_disposition_disagreement_fails_closed(tmp_path):
|
|
inputs = fixture(tmp_path, ["keep"])
|
|
inputs[1]["data"]["finalAcceptance"]["poseDelivery"] = {"headpose": "exclude", "handpose": "exclude"}
|
|
synchronization = Path(inputs[0]["path"]) / "slices/slice-000001/synchronized-slice.json"
|
|
synchronization.write_text(json.dumps({"schemaVersion": "droboticflow.synchronized-slice/v1", "deliveryDisposition": "discard"}))
|
|
with pytest.raises(RuntimeError, match="disposition disagrees"):
|
|
package.run("r", "p", tmp_path / "output", PART_NODE, inputs, helpers())
|
|
|
|
|
|
def test_part_index_keeps_requiring_an_approved_revision(tmp_path):
|
|
inputs = fixture(tmp_path, ["keep", "discard", "keep"])
|
|
inputs[1]["data"]["finalAcceptance"]["confirmed"] = False
|
|
inputs[1]["data"]["finalAcceptance"]["poseDelivery"] = {"headpose": "exclude", "handpose": "exclude"}
|
|
with pytest.raises(RuntimeError):
|
|
package.run("r", "p", tmp_path / "output", PART_NODE, inputs, helpers())
|
|
|
|
|
|
def test_all_discard_still_emits_no_deliverable(tmp_path):
|
|
_, artifacts = run_part_index(tmp_path, ["discard", "discard"])
|
|
assert len(artifacts) == 1 and artifacts[0]["portId"] == "deliverySummary"
|
|
assert artifacts[0]["data"]["outcome"] == "no_deliverables"
|
|
assert not list((tmp_path / "output").glob("part-index"))
|
|
|
|
|
|
def test_missing_calibration_fails_closed(tmp_path):
|
|
inputs = fixture(tmp_path, ["keep"])
|
|
inputs[1]["data"]["finalAcceptance"]["poseDelivery"] = {"headpose": "exclude", "handpose": "exclude"}
|
|
trimmed = [item for item in inputs if item.get("portId") != "calibrations"]
|
|
with pytest.raises(RuntimeError, match="calibration"):
|
|
package.run("r", "p", tmp_path / "output", PART_NODE, trimmed, helpers())
|
|
|
|
|
|
def test_directory_contract_is_unchanged_when_the_switch_is_off(tmp_path, monkeypatch):
|
|
inputs = fixture(tmp_path, ["keep", "discard", "keep"])
|
|
monkeypatch.setattr(package, "_ffprobe_frame_count", lambda path: 2)
|
|
artifacts = package.run("r", "p", tmp_path / "output", BASE_NODE, inputs, helpers())
|
|
directory = Path(next(a["path"] for a in artifacts if a["portId"] == "deliveryDirectory"))
|
|
assert {entry.name for entry in (directory / "clips").iterdir()} == {"slice-000001", "slice-000003"}
|