forked from longtao.wu/droboticflow
80 lines
4.2 KiB
Python
80 lines
4.2 KiB
Python
"""Native v3 packaging, rather than postprocessing a customer sample ZIP."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from backend.operators.builtin.package import operator as package
|
|
from tests.backend.test_part_index_package import helpers
|
|
from tests.backend.test_slice_discard_package import fixture
|
|
|
|
NODE = {
|
|
"outputs": [{"id": n, "name": n} for n in ("deliveryDirectory", "deliverySummary")],
|
|
"hyperparams": [{"key": k, "value": v} for k, v in {
|
|
"PART_INDEX_DELIVERY": "true", "DELIVERY_JSON_V3": "true", "PROCESSING_PROFILE": "raw_human"
|
|
}.items()],
|
|
}
|
|
|
|
|
|
def inputs_for(tmp_path, actions):
|
|
inputs = fixture(tmp_path, ["keep", "discard"])
|
|
inputs[1]["data"]["capture"] = {"captureId": "capture-A/part0001", "prefix": "incoming/capture-A/part0001", "captureRecordId": "record-A1"}
|
|
inputs[1]["data"]["finalAcceptance"]["acceptedAt"] = "2026-10-20T01:00:00Z"
|
|
root = Path(inputs[0]["path"])
|
|
(root / "slices/slice-000001/annotation.json").write_text(json.dumps({"actions": actions}))
|
|
return inputs
|
|
|
|
|
|
@pytest.mark.parametrize("actions,expected", [
|
|
([{"description": "", "status": "described"}], "empty_confirmed"),
|
|
([{"description": "无效", "isInvalid": True}, {"description": " "}], "invalid_and_blank"),
|
|
([{"description": "拿起杯子", "status": "pending"}], "described"),
|
|
])
|
|
def test_native_v3_semantics_and_separate_history(tmp_path, actions, expected):
|
|
inputs = inputs_for(tmp_path, actions)
|
|
artifacts = package.run("r", "p", tmp_path / "out", NODE, inputs, helpers())
|
|
artifact = artifacts[0]
|
|
assert artifact["portId"] == "deliveryDirectory"
|
|
root = Path(artifact["path"])
|
|
assert {p.name for p in root.iterdir()} == {"part.json", "review-history.json"}
|
|
doc = json.loads((root / "part.json").read_text())
|
|
assert doc["schemaVersion"] == "droboticflow.delivery.part-index/v3"
|
|
assert doc["partId"] == "part0001"
|
|
assert doc["capture"]["captureId"] == "capture-A"
|
|
assert doc["capture"]["prefix"] == "incoming/capture-A"
|
|
assert doc["sourceMetadata"]["path"] == "meta_capture-A.json"
|
|
assert doc["slices"][0]["classification"]["descriptionStatus"] == expected
|
|
assert "qualityReviews" not in doc["acceptance"]
|
|
assert json.loads((root / "review-history.json").read_text())["qualityReviews"]
|
|
for entry in doc["slices"]:
|
|
assert "taskLabel" not in entry["classification"]
|
|
assert "taskLabelId" not in entry["classification"]
|
|
assert "calibration" not in entry["synchronization"]
|
|
assert all("status" not in a for a in entry["segments"])
|
|
assert all(a["contentKind"] in {"gap_placeholder", "invalid_marked", "described"} for a in entry["segments"])
|
|
assert all(c["path"] == c["name"] for c in doc["calibration"])
|
|
first = {p.name: p.read_bytes() for p in root.iterdir()}
|
|
again = package.run("r", "p", tmp_path / "out", NODE, inputs, helpers())
|
|
assert {p.name: p.read_bytes() for p in Path(again[0]["path"]).iterdir()} == first
|
|
assert not list((tmp_path / "out").rglob("*.mp4"))
|
|
|
|
|
|
def test_v3_refuses_ambiguous_capture_identity(tmp_path):
|
|
inputs = inputs_for(tmp_path, [])
|
|
inputs[1]["data"]["capture"]["prefix"] = "incoming/another-capture/part0001"
|
|
with pytest.raises(RuntimeError, match="capture identity"):
|
|
package.run("r", "p", tmp_path / "out", NODE, inputs, helpers())
|
|
|
|
|
|
def test_v3_keeps_valid_legacy_labels_and_rejects_incomplete_configuration(tmp_path):
|
|
inputs = inputs_for(tmp_path, [])
|
|
sync = Path(inputs[0]["path"]) / "slices/slice-000001/synchronized-slice.json"
|
|
doc = json.loads(sync.read_text())
|
|
doc.update(taskLabel="拿取物品", taskLabelId="label-1")
|
|
sync.write_text(json.dumps(doc))
|
|
artifact = package.run("r", "p", tmp_path / "out", NODE, inputs, helpers())[0]
|
|
classification = json.loads((Path(artifact["path"]) / "part.json").read_text())["slices"][0]["classification"]
|
|
assert classification["taskLabel"] == "拿取物品" and classification["taskLabelId"] == "label-1"
|
|
bad = {**NODE, "hyperparams": [{"key": "DELIVERY_JSON_V3", "value": "true"}]}
|
|
with pytest.raises(RuntimeError, match="requires PART_INDEX_DELIVERY"):
|
|
package.run("r", "p", tmp_path / "refused", bad, inputs, helpers())
|