droboticflow/tests/backend/test_part_index_real_materialization.py
d-robotics b9cdf0f857 🧾 Keep the part index reachable without an object ref
Reading a delivered index through the replica API (object storage disabled,
payload resolved from a workspace file) recognised the document and projected
the full split classification but exposed zero members, because the single
deliverable was only emitted for a durable object ref. The one output of this
delivery mode was therefore not downloadable in that situation. Fall back to the
dataRef payload path when no object ref exists.

Verify against the local replica for the first time: with the API running on the
snapshot Mongo, the members endpoint returns the kept and discarded slices with
their boundaries, action counts and discard reason, and the member content
endpoint returns part.json byte-identical to the delivered file. The delivery
package contents endpoint lists part.json as a final export artifact.

Add a boundary test over real materialization output: the Go acceptance test
builds genuine 60s stereo media (80 frames per slice) and slices it with ffmpeg;
the new test consumes that same revision and asserts the review layer keeps all
seven evidence files per slice while the delivered index contains no media
reference at all. It is opt-in behind FANYU_PART_INDEX_MATERIALIZATION_CONTEXT.

The replica fixture helper lives under the untracked local-data path; it tags and
removes exactly the records it creates and never touches object storage.
2026-10-09 00:45:11 -07:00

115 lines
5.9 KiB
Python

"""Boundary test on REAL materialization output.
The Go acceptance test `TestSliceDiscardMaterialization60Seconds` writes a
`verification-context.json` when `FANYU_DISCARD_ARTIFACT_DIR` is set, after
building genuine stereo media (60 s, 4 fps, 80 frames per slice) and slicing it
with ffmpeg. This test consumes that same revision and pins the boundary the
part-index delivery depends on:
- the review layer keeps every kept slice's media, PTS, IMU and evidence files;
- the delivered index for the same revision references no media at all.
Opt in with, for example:
FANYU_DISCARD_ARTIFACT_DIR=$PWD/local-data/verification/part-index-replica/materialization \\
go test ./internal/api/ -run TestSliceDiscardMaterialization60Seconds
FANYU_PART_INDEX_MATERIALIZATION_CONTEXT=$PWD/local-data/verification/part-index-replica/materialization/verification-context.json \\
.venv/bin/python -m pytest tests/backend/test_part_index_real_materialization.py
"""
import json
import os
from pathlib import Path
import pytest
from backend.operators.builtin.package import operator as package
CONTEXT_ENV = "FANYU_PART_INDEX_MATERIALIZATION_CONTEXT"
# Every member the review workspace and the acceptance workspace read.
REVIEW_EVIDENCE = ("left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "annotation.json", "synchronized-slice.json")
def load_context():
path = os.environ.get(CONTEXT_ENV)
if not path:
pytest.skip(f"opt-in: set {CONTEXT_ENV} to a Go materialization verification-context.json")
return json.loads(Path(path).read_text(encoding="utf-8"))
def test_real_revision_keeps_review_media_while_the_index_ships_no_media(tmp_path, monkeypatch):
context = load_context()
keep = context["keep"]
root = Path(keep["artifact"]["path"])
assert root.is_dir(), f"materialization root is gone: {root}"
manifest = json.loads((root / "revision-manifest.json").read_text(encoding="utf-8"))
assert manifest["schemaVersion"] in {"droboticflow.slice-materialization/v2", "droboticflow.slice-materialization/v3"}
assert manifest["coverage"]["durationSeconds"] == 60
rows = manifest["items"]
assert len(rows) == 3
# 1) The review layer is untouched by the delivery change.
for row in rows:
directory = root / row["directory"]
for name in REVIEW_EVIDENCE:
member = directory / name
assert member.is_file() and member.stat().st_size > 0, f"review evidence lost: {directory}/{name}"
# 2) The delivered index for the same revision references no media bytes.
operator = package
inputs = [
{"id": "accepted", "portId": "acceptedSliceDirectory", "path": str(root)},
{"portId": "acceptanceSummary", "data": {
"decision": "approved", "processingProfile": "raw_human",
"qualityReviews": [],
"capture": {"captureId": "real-materialization"},
"finalAcceptance": {"confirmed": True, "sampled": True, "processingProfile": "raw_human",
"poseDelivery": {"headpose": "exclude", "handpose": "exclude"}},
}},
]
for name in ("calibration_camera.yaml", "calibration_imu.yaml"):
fixture = root.parent / "fixture-calibrations" / name
fixture.parent.mkdir(parents=True, exist_ok=True)
fixture.write_text(f"{name}: synthetic\n", encoding="utf-8")
inputs.append({"portId": "calibrations", "name": name, "path": str(fixture)})
# The real media is already frame-verified by the Go acceptance test; the
# index branch does not re-probe it, so keep this independent of ffprobe.
monkeypatch.setattr(package, "_ffprobe_frame_count", lambda path: 80)
helpers = {
"write_json_artifact": lambda run, node, directory, name, payload, output=None: {
"id": "a", "name": name, "path": str(Path(directory) / name), "data": payload, "portId": output["id"]},
"build_artifact": lambda run, node, path, artifact_type, data=None, metadata=None, output=None: {
"id": "b", "name": Path(path).name, "path": str(path), "type": artifact_type,
"metadata": dict(metadata or {}), "portId": output["id"] if output else ""},
"build_virtual_artifact": lambda run, node, name, path, kind, size, data=None, output=None, **kw: {
"id": "c", "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 ""),
"source_ref_from_artifact": lambda item: {"path": item.get("path")},
}
node = {"outputs": [{"id": "deliveryPartIndex", "name": "deliveryPartIndex"}],
"hyperparams": [{"key": "PART_INDEX_DELIVERY", "value": "true"}]}
artifacts = package.run("r", "p", tmp_path / "part-index-run", node, inputs, helpers)
assert len(artifacts) == 1
index = artifacts[0]
assert index["name"] == "part.json"
assert not list((tmp_path / "part-index-run").rglob("*.mp4")), "delivery produced media"
payload = json.loads(Path(index["path"]).read_text(encoding="utf-8"))
assert payload["delivery"] == {"kind": "part_index", "fileName": "part.json", "artifactName": "delivery.part-index"}
assert len(payload["slices"]) == 3
for entry in payload["slices"]:
assert set(entry) == {"sliceId", "sliceName", "taskSplit", "classification", "segments", "synchronization"}
assert "files" not in entry and "mediaKey" not in entry
body = Path(index["path"]).read_text(encoding="utf-8")
for media in (".mp4", '"files"', "mediaKey"):
assert media not in body, f"index references media: {media}"
# 3) The review layer still exists on disk after the delivery ran.
for row in rows:
assert (root / row["directory"] / "left.mp4").is_file()