forked from longtao.wu/droboticflow
170 lines
9.0 KiB
Python
170 lines
9.0 KiB
Python
"""Byte compatibility with 3221ac65, including new metadata on old workflows."""
|
|
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from backend.operators.builtin.package import operator as package
|
|
from tests.backend.test_delivery_v4_package import ACTION, BASE, NODE, prepare
|
|
from tests.backend.test_part_index_package import helpers
|
|
|
|
|
|
# SHA-256 of actual run() output at 3221ac65 for the synthetic inputs below.
|
|
# Each pair is (alias-only/absent metadata, canonical legacy note/timestamp).
|
|
# v3 also pins its separate historical review file, not just the main JSON.
|
|
BASELINE_BYTES = {
|
|
"v1": ("f2b178e17be9edf9ca5c6fee02af30f4a4cfcca2e84089aa45de4afce9cbfb5b", "c89f0511319d051531139ba73da18db5121881399ca7906236a846f61b672613"),
|
|
"v2": ("0668b1d273c84825485b006ace08c866ddcafa855bca646b0ae7646469cb75f2", "bd9eb8de44bdcc012a03c52aa8165d7b1b3e49d61e08c56d26ac3aae351d42a8"),
|
|
"v3": ("bcae3a13831d0c98aa836b93e9014f2d0ccc9da38b41d14e0e83c37a550a6ed9", "ab2161ae68f11cb970df5bc6be44edad2eca67b6af3fe9115bd1680944004560"),
|
|
"source-v3": ("0f60b917100934ea33da3a0f5932dd75e215b65cc06c34e5579e3f001568031c", "cbb994707f67ed01a24d770e36566395a471072583d808c1e75d3e4ec7d16bb3"),
|
|
}
|
|
BASELINE_HISTORY = "1cb4f6018ad9935b9692b2579d5917340b8309619154b15785d57894788e4597"
|
|
|
|
|
|
@pytest.fixture(scope="module", params=["backend", "operator-repo"])
|
|
def implementation(request):
|
|
if request.param == "backend":
|
|
return package
|
|
path = Path(__file__).resolve().parents[2] / "operator-repos/builtin/package/src/operator.py"
|
|
spec = importlib.util.spec_from_file_location("mirrored_package_compatibility", path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def legacy_node(version, explicit_off=False):
|
|
params = {"PART_INDEX_DELIVERY": "true", "PROCESSING_PROFILE": "raw_human"}
|
|
if version in {"v2", "source-v3"}:
|
|
params["SOURCE_INDEX_DELIVERY"] = "true"
|
|
if version in {"v3", "source-v3"}:
|
|
params["DELIVERY_JSON_V3"] = "true"
|
|
if explicit_off:
|
|
params["DELIVERY_JSON_V4"] = "false"
|
|
return {
|
|
"outputs": [{"id": name, "name": name} for name in ("deliveryPartIndex", "deliveryDirectory", "deliverySummary")],
|
|
"hyperparams": [{"key": key, "value": value} for key, value in params.items()],
|
|
}
|
|
|
|
|
|
def compatibility_inputs(tmp_path, version, metadata):
|
|
tmp_path = tmp_path.resolve()
|
|
inputs = prepare(tmp_path, [{"description": "拿起杯子", "dictionary": dict(ACTION)}])
|
|
root = Path(inputs[0]["path"])
|
|
manifest_path = root / "revision-manifest.json"
|
|
manifest = json.loads(manifest_path.read_text())
|
|
reason = manifest["items"][1]["discardEvidence"]
|
|
reason["discardedByUserId"] = "legacy-discarder"
|
|
if metadata == "canonical":
|
|
reason.update(note="canonical note", discardedAt="2026-10-19T00:00:00Z")
|
|
if metadata == "absent":
|
|
for key in ("reasonNote", "reasonVersion", "dictionary", "at"):
|
|
reason.pop(key, None)
|
|
for name in ("annotation.json", "synchronized-slice.json"):
|
|
path = root / "slices/slice-000001" / name
|
|
doc = json.loads(path.read_text())
|
|
doc.pop("dictionary", None)
|
|
for action in doc.get("actions", []):
|
|
action.pop("dictionary", None)
|
|
path.write_text(json.dumps(doc))
|
|
if version in {"v2", "source-v3"}:
|
|
# Real pinned-source checks, without ffmpeg, external services or media decoding.
|
|
run_root = tmp_path / "system/runs/source-run"
|
|
target = run_root / "human-materializations/item/task/revision-000001"
|
|
target.parent.mkdir(parents=True)
|
|
root.rename(target)
|
|
root = target
|
|
inputs[0]["path"] = str(root)
|
|
manifest_path = root / "revision-manifest.json"
|
|
fingerprint = "a" * 64
|
|
source_dir = run_root / "annotation-sources" / fingerprint
|
|
source_dir.mkdir(parents=True)
|
|
sources = {}
|
|
for kind in ("leftVideo", "rightVideo", "leftPts", "rightPts", "imu"):
|
|
suffix = ".mp4" if kind.endswith("Video") else ".csv"
|
|
path = source_dir / (kind + suffix)
|
|
path.write_bytes(f"synthetic pinned {kind}\n".encode())
|
|
sources[kind] = {"path": str(path), "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "bytes": path.stat().st_size}
|
|
manifest.update(
|
|
schemaVersion="droboticflow.source-slice-index/v1", sourceFingerprint=fingerprint,
|
|
sources=sources, sourceDurations={"left": 2.0, "right": 2.0},
|
|
sourceFrameCounts={"left": 4, "right": 4},
|
|
)
|
|
for index, row in enumerate(manifest["items"]):
|
|
row["sourceFrames"] = {side: [index * 2, index * 2 + 2] for side in ("left", "right")}
|
|
row["files"] = {}
|
|
for kind, name in (("annotation", "annotation.json"), ("synchronization", "synchronized-slice.json")):
|
|
path = root / row["directory"] / name
|
|
row["files"][kind] = {"name": name, "sha256": hashlib.sha256(path.read_bytes()).hexdigest()}
|
|
manifest_path.write_text(json.dumps(manifest))
|
|
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"] = hashlib.sha256(manifest_path.read_bytes()).hexdigest()
|
|
return inputs
|
|
|
|
|
|
def output_bytes(implementation, inputs, output, node):
|
|
artifacts = implementation.run("r", "p", output, node, inputs, helpers())
|
|
path = Path(artifacts[0]["path"])
|
|
if path.is_dir():
|
|
return {p.name: p.read_bytes() for p in path.iterdir()}
|
|
return {"part.json": path.read_bytes()}
|
|
|
|
|
|
@pytest.mark.parametrize("version", ["v1", "v2", "v3", "source-v3"])
|
|
@pytest.mark.parametrize("metadata", ["absent", "aliases", "canonical"])
|
|
def test_legacy_payload_bytes_match_pre_v4_baseline(tmp_path, implementation, version, metadata):
|
|
inputs = compatibility_inputs(tmp_path, version, metadata)
|
|
for explicit_off in (False, True):
|
|
files = output_bytes(implementation, inputs, tmp_path / str(explicit_off), legacy_node(version, explicit_off))
|
|
raw = files["part.json"]
|
|
expected = BASELINE_BYTES[version][metadata == "canonical"]
|
|
assert hashlib.sha256(raw).hexdigest() == expected
|
|
doc = json.loads(raw)
|
|
expected_version = "v3" if version == "source-v3" else version
|
|
assert doc["schemaVersion"] == f"droboticflow.delivery.part-index/{expected_version}"
|
|
assert b'"dictionary"' not in raw and b'"reasonVersion"' not in raw
|
|
reason = doc["discardedSlices"][0]["classification"]["discardEvidence"]
|
|
assert reason["note"] == ("canonical note" if metadata == "canonical" else "")
|
|
assert reason["discardedAt"] == ("2026-10-19T00:00:00Z" if metadata == "canonical" else "")
|
|
if expected_version == "v3":
|
|
assert set(files) == {"part.json", "review-history.json"}
|
|
assert hashlib.sha256(files["review-history.json"]).hexdigest() == BASELINE_HISTORY
|
|
else:
|
|
assert set(files) == {"part.json"}
|
|
|
|
|
|
@pytest.mark.parametrize("source_index", [False, True])
|
|
@pytest.mark.parametrize("metadata", ["aliases", "canonical"])
|
|
def test_v4_keeps_frozen_selections_and_discard_aliases(tmp_path, implementation, source_index, metadata):
|
|
inputs = compatibility_inputs(tmp_path, "v2" if source_index else "v1", metadata)
|
|
node = {**NODE, "hyperparams": [*NODE["hyperparams"]]}
|
|
if source_index:
|
|
node["hyperparams"].append({"key": "SOURCE_INDEX_DELIVERY", "value": "true"})
|
|
artifacts = implementation.run("r", "p", tmp_path / "out", node, inputs, helpers())
|
|
assert len(artifacts) == 2
|
|
directory = Path(artifacts[0]["path"])
|
|
assert {p.name for p in directory.iterdir()} == {"part.json"}
|
|
raw = (directory / "part.json").read_bytes()
|
|
doc = json.loads(raw)
|
|
assert doc["schemaVersion"] == "droboticflow.delivery.part-index/v4"
|
|
assert doc["slices"][0]["classification"]["dictionary"] == BASE
|
|
assert doc["slices"][0]["segments"][0]["dictionary"] == ACTION
|
|
reason = doc["discardedSlices"][0]["classification"]["discardEvidence"]
|
|
assert reason["note"] == ("canonical note" if metadata == "canonical" else "持续遮挡")
|
|
assert reason["reasonVersion"] == reason["dictionary"]["version"] == 4
|
|
assert reason["dictionary"]["tags"] == ["不可用"]
|
|
assert "discardedAt" not in reason and "discardedByUserId" not in reason
|
|
audit_path = Path(artifacts[1]["path"])
|
|
assert audit_path.parent != directory and artifacts[1]["portId"] == "deliveryAudit"
|
|
assert json.loads(audit_path.read_text())["partSha256"] == hashlib.sha256(raw).hexdigest()
|
|
|
|
# The private intermediate mapper retains canonical precedence and the v4
|
|
# timestamp alias before the customer formatter deliberately removes it.
|
|
root = Path(inputs[0]["path"])
|
|
row = json.loads((root / "revision-manifest.json").read_text())["items"][1]
|
|
mapped = implementation._part_index_discarded_slice(row, root, classification_v4=True)
|
|
assert mapped["classification"]["discardEvidence"]["discardedAt"] == (
|
|
"2026-10-19T00:00:00Z" if metadata == "canonical" else "2026-10-20T00:00:00Z"
|
|
)
|