150 lines
8.7 KiB
Python
150 lines
8.7 KiB
Python
"""Final classifications and private review history are different outputs."""
|
|
import csv
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from backend.operators.builtin.package import operator as package
|
|
from tests.backend.test_delivery_v3_package import inputs_for
|
|
from tests.backend.test_part_index_package import helpers
|
|
from tests.backend.test_slice_discard_package import accepted_inputs, fixture
|
|
|
|
NODE = {
|
|
"outputs": [{"id": n, "name": n} for n in ("deliveryDirectory", "deliverySummary", "deliveryAudit")],
|
|
"hyperparams": [{"key": k, "value": v} for k, v in {
|
|
"PART_INDEX_DELIVERY": "true", "DELIVERY_JSON_V4": "true", "PROCESSING_PROFILE": "raw_human"
|
|
}.items()],
|
|
}
|
|
BASE = {"id": "slice-word", "kind": "slice", "version": 2, "code": "base", "name": "物品操作", "tags": ["商品", "手部"]}
|
|
ACTION = {"id": "action-word", "kind": "action", "version": 3, "code": "pick", "name": "拿取", "tags": ["抓取"]}
|
|
|
|
|
|
def original_source(capture="capture-A", part="part0001"):
|
|
root=f"incoming/{capture}"
|
|
paths=[f"{part}/{capture}_camera_{side}_{part}{suffix}" for side in ("left","right") for suffix in (".mp4","_pts.csv")]
|
|
paths += [f"{part}/{capture}_imu_{part}.csv",f"{capture}_calibration_camera.yaml",f"{capture}_calibration_imu.yaml",f"meta_{capture}.json"]
|
|
return {"provider":"BOS","bucket":"test-bucket","prefix":root,"partPrefix":root+"/"+part,"captureUnit":capture+"/"+part,"objectKeys":[root+"/"+p for p in paths],"snapshotFingerprint":"synthetic-source-version"}
|
|
|
|
|
|
def add_synthetic_discard_sync(root, manifest):
|
|
"""Complete only the v4 synthetic fixture from its existing PTS evidence."""
|
|
for row in manifest["items"]:
|
|
if row["deliveryDisposition"] != "discard":
|
|
continue
|
|
folder = root / row["directory"]
|
|
with (folder / "left_pts.csv").open() as source:
|
|
points = list(csv.DictReader(source))
|
|
seconds = [float(point["clip_time_ms"]) / 1000 for point in points]
|
|
stamps = [int(float(point["timestamp_ms"]) * 1000) for point in points]
|
|
assert seconds[0] == 0 and stamps[0] == row["startTimestampUs"]
|
|
assert seconds[-1] < row["endSecond"] - row["startSecond"]
|
|
assert stamps[-1] < row["endTimestampUs"]
|
|
path = folder / "synchronized-slice.json"
|
|
sync = json.loads(path.read_text())
|
|
sync.update(
|
|
boundarySeconds=seconds + [row["endSecond"] - row["startSecond"]],
|
|
boundaryTimestampUs=stamps + [row["endTimestampUs"]],
|
|
rightBoundarySeconds=seconds + [row["endSecond"] - row["startSecond"]],
|
|
window={"startTimestampUs": row["startTimestampUs"],
|
|
"endTimestampUs": row["endTimestampUs"], "semantics": "half-open"},
|
|
)
|
|
path.write_text(json.dumps(sync))
|
|
|
|
|
|
def prepare(tmp_path, actions):
|
|
inputs = inputs_for(tmp_path, actions)
|
|
root = Path(inputs[0]["path"])
|
|
sync = root / "slices/slice-000001/synchronized-slice.json"
|
|
doc = json.loads(sync.read_text())
|
|
doc["dictionary"] = BASE
|
|
n=max(1,len(actions))
|
|
for i,action in enumerate(actions):
|
|
action.setdefault("id",f"action-{i}")
|
|
action.setdefault("clipStartMs",i*1000/n)
|
|
action.setdefault("clipEndMs",(i+1)*1000/n)
|
|
action.setdefault("startTimestampUs",1000000+round(action["clipStartMs"]*1000))
|
|
action.setdefault("endTimestampUs",1000000+round(action["clipEndMs"]*1000))
|
|
(root/"slices/slice-000001/annotation.json").write_text(json.dumps({"actions":actions}))
|
|
points=sorted({0.0,1.0,*[a[k]/1000 for a in actions for k in ("clipStartMs","clipEndMs")]})
|
|
doc["window"] = {"startTimestampUs":1000000,"endTimestampUs":2000000,"semantics":"half-open"}
|
|
doc.update(boundarySeconds=points,boundaryTimestampUs=[1000000+round(p*1000000) for p in points],rightBoundarySeconds=points)
|
|
sync.write_text(json.dumps(doc))
|
|
manifest_path = root / "revision-manifest.json"
|
|
m = json.loads(manifest_path.read_text())
|
|
add_synthetic_discard_sync(root, m)
|
|
m["runId"] = "source-run"
|
|
m["originalSource"] = original_source()
|
|
m["items"][0]["sliceName"] = BASE["name"]
|
|
reason = m["items"][1]["discardEvidence"]
|
|
reason.update(reasonLabelId="discard-word", reasonVersion=4, reasonName="遮挡", reasonNote="持续遮挡", at="2026-10-20T00:00:00Z", dictionary={"id":"discard-word","kind":"discard_reason","version":4,"code":"occluded","name":"遮挡","tags":["不可用"]})
|
|
manifest_path.write_text(json.dumps(m))
|
|
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"] = hashlib.sha256(manifest_path.read_bytes()).hexdigest()
|
|
inputs[1]["data"]["qualityReviews"] = [{"reviewRound":0,"decision":"rejected","comment":"PRIVATE_REWORK"},{"reviewRound":1,"decision":"approved","confirmations":[{"opinionId":"old","resolved":True}]}]
|
|
inputs[1]["data"]["trace"] = [{"humanItemId":"item","reworkBaselines":[{"round":1,"comment":"PRIVATE_BASELINE"}],"reworkHandling":[{"round":1}],"events":[{"type":"rework_submitted"}]}]
|
|
return inputs
|
|
|
|
|
|
def read_outputs(tmp_path, inputs, out="out"):
|
|
result = package.run("r", "p", tmp_path / out, NODE, inputs, helpers())
|
|
assert len(result) == 2
|
|
directory = Path(result[0]["path"])
|
|
assert {p.name for p in directory.iterdir()} == {"part.json"}
|
|
raw = (directory / "part.json").read_bytes()
|
|
audit = json.loads(Path(result[1]["path"]).read_text())
|
|
assert result[1]["portId"] == "deliveryAudit"
|
|
assert Path(result[1]["path"]).parent != directory
|
|
assert audit["partSha256"] == hashlib.sha256(raw).hexdigest()
|
|
assert audit["revision"] == json.loads(raw)["revision"]
|
|
return json.loads(raw), audit, raw
|
|
|
|
|
|
@pytest.mark.parametrize("actions,kinds,status", [
|
|
([{"description":"", "status":"pending"}], ["gap_placeholder"], "empty_confirmed"),
|
|
([{"description":"拿起杯子", "dictionary":ACTION},{"description":""},{"description":"无效", "isInvalid":True}], ["annotated","gap_placeholder","invalid_marked"], "described"),
|
|
([{"description":"无效", "isInvalid":True}], ["invalid_marked"], "invalid_only"),
|
|
])
|
|
def test_v4_freezes_classification_without_exporting_review_process(tmp_path, actions, kinds, status):
|
|
inputs = prepare(tmp_path, actions)
|
|
doc, audit, raw = read_outputs(tmp_path, inputs)
|
|
first = doc["slices"][0]
|
|
assert doc["schemaVersion"] == "droboticflow.delivery.part-index/v4"
|
|
assert first["classification"]["dictionary"] == BASE
|
|
assert first["classification"]["descriptionStatus"] == status
|
|
assert [a["contentKind"] for a in first["segments"]] == kinds
|
|
if kinds[0] == "annotated": assert first["segments"][0]["dictionary"] == ACTION
|
|
discard = doc["discardedSlices"][0]["classification"]["discardEvidence"]
|
|
assert discard["note"] == "持续遮挡" and discard["dictionary"]["tags"] == ["不可用"]
|
|
assert discard["reasonVersion"] == 4
|
|
for key in ("acceptance","reviewHistory","lineage"): assert key not in doc
|
|
for text in (b"PRIVATE_REWORK",b"PRIVATE_BASELINE",b"reviewerUserId",b"sampling",b"discardedAt",b"confirmedByUserId"):
|
|
assert text not in raw
|
|
assert audit["qualityReviews"][0]["comment"] == "PRIVATE_REWORK"
|
|
assert audit["trace"][0]["reworkBaselines"]
|
|
assert audit["lineage"]["sourceRunId"] == "source-run"
|
|
assert read_outputs(tmp_path, inputs, "retry")[2] == raw
|
|
# Intermediate audit-only changes cannot change final business bytes.
|
|
inputs[1]["data"]["qualityReviews"][0]["comment"] = "OTHER_REVIEW_NOTE"
|
|
assert read_outputs(tmp_path, inputs, "audit-change")[2] == raw
|
|
|
|
|
|
def test_v4_all_discard_outputs_classification_and_legacy_stays_unchanged(tmp_path):
|
|
inputs = fixture(tmp_path,["discard","discard"])
|
|
inputs[1]["data"].update(capture={"captureId":"capture-A/part0001","prefix":"incoming/capture-A/part0001"},trace=[])
|
|
inputs[1]["data"]["finalAcceptance"]["acceptedAt"] = "2026-10-20T01:00:00Z"
|
|
root=Path(inputs[0]["path"]);p=root/"revision-manifest.json";m=json.loads(p.read_text());m["originalSource"]=original_source();p.write_text(json.dumps(m));inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"]=hashlib.sha256(p.read_bytes()).hexdigest()
|
|
add_synthetic_discard_sync(root, m)
|
|
doc, _, _ = read_outputs(tmp_path, inputs)
|
|
assert doc["slices"] == [] and len(doc["discardedSlices"]) == 2
|
|
assert doc["deliveryClassification"]["discardedSliceCount"] == 2
|
|
legacy={**NODE,"hyperparams":[p for p in NODE["hyperparams"] if p["key"]!="DELIVERY_JSON_V4"]}
|
|
result=package.run("r","p",tmp_path/"legacy",legacy,inputs,helpers())
|
|
assert len(result)==1 and result[0]["data"]["outcome"]=="no_deliverables"
|
|
|
|
|
|
def test_v4_rejects_unaccepted_or_wrong_revision(tmp_path):
|
|
inputs = prepare(tmp_path,[])
|
|
inputs[1]["data"]["acceptedMaterializations"][0]["revision"] = 999
|
|
with pytest.raises(RuntimeError,match="not pinned"):
|
|
read_outputs(tmp_path, inputs)
|