droboticflow/tests/backend/test_capture_part_selection.py
2026-09-11 04:47:13 +08:00

120 lines
5.9 KiB
Python

import pytest
from backend.operators.builtin.directory_scan import operator as directory
from backend.operators.builtin.orbbec_data_normalize import operator as normalize
C = "FYLX_B10_AZEC86400GP_19700101_035516"
PREFIX = "incoming/" + C
def test_part_pairing_shares_only_parent_calibration():
video = f"part0001/{C}_camera_left_part0001.mp4"
paths = [video, f"part0001/{C}_camera_left_part0001_pts.csv",
f"part0001/{C}_imu_part0001.csv", f"{C}_calibration_camera.yaml",
f"{C}_calibration_imu.yaml"]
entries = [{"path": path} for path in paths]
result = normalize._paired_files(video, entries)
assert result["pts"] == paths[1]
assert result["imu"] == paths[2]
assert result["cameraCalibration"] == paths[3]
assert result["imuCalibration"] == paths[4]
assert normalize._capture_id(video) == C
assert normalize._capture_id(paths[3]) == C
entries[2]["path"] = f"part0002/{C}_imu_part0001.csv"
with pytest.raises(RuntimeError, match="不在当前 part"):
normalize._paired_files(video, entries)
def mock_helpers(monkeypatch):
for key in ("BOS_ENDPOINT", "BOS_ACCESS_KEY_ID", "BOS_SECRET_ACCESS_KEY"):
monkeypatch.setenv(key, "isolated-test")
paths = [f"part0001/{C}_camera_left_part0001_pts.csv",
f"part0002/{C}_camera_left_part0002_pts.csv", f"{C}_calibration_camera.yaml"]
reads = []
class Client:
def get_object_as_string(self, bucket, key):
reads.append(key)
assert "part0002" not in key
return b"timestamp_us\n1\n2\n"
helpers = {
"list_bos_entries": lambda *args: [{"path": path, "sizeBytes": 20} for path in paths],
"build_bos_client": lambda *args: (Client(), None),
"canonical_bos_path": lambda bucket, prefix: f"bos://{bucket}/{prefix}",
}
return paths, reads, helpers
def test_directory_and_normalizer_honor_same_frozen_selection(monkeypatch):
paths, reads, helpers = mock_helpers(monkeypatch)
selected = [PREFIX + "/" + paths[i] for i in (0, 2)]
entries = directory._bos_entries("bucket", PREFIX, "", {}, helpers, selected)
assert {entry["path"] for entry in entries} == {paths[0], paths[2]}
assert reads == [selected[0]]
inputs = [{"payload": {"ref": {"kind": "directory", "bucket": "bucket", "prefix": PREFIX, "objectKeys": selected}}}]
entries, source = normalize._directory_entries(inputs, helpers)
assert {entry["path"] for entry in entries} == {paths[0], paths[2]}
assert source == "bos://bucket/" + PREFIX
@pytest.mark.parametrize("keys", [[], ["other/capture.csv"], [PREFIX + "/../other.csv"], [PREFIX + "/missing.csv"]])
def test_missing_or_out_of_scope_selected_files_fail_closed(monkeypatch, keys):
_, _, helpers = mock_helpers(monkeypatch)
with pytest.raises(RuntimeError):
directory._bos_entries("bucket", PREFIX, "", {}, helpers, keys)
inputs = [{"payload": {"ref": {"kind": "directory", "bucket": "bucket", "prefix": PREFIX, "objectKeys": keys}}}]
with pytest.raises(RuntimeError):
normalize._directory_entries(inputs, helpers)
def test_current_transcoded_filename_does_not_bypass_pts_safety():
video = f"part0001/{C}_camera_left_part0001_silent_480p_wm.mp4"
entries = [{"path": f"part0001/{C}_camera_left_part0001_pts.csv"}]
assert normalize._paired_files(video, entries)["pts"] == entries[0]["path"]
def test_transcoded_pts_validation_requires_count_and_order(tmp_path, monkeypatch):
from types import SimpleNamespace
video = tmp_path / "video.mp4"
pts = tmp_path / "pts.csv"
pts.write_text("timestamp_us\n100\n200\n")
monkeypatch.setattr(normalize.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout='{"streams":[{"nb_read_frames":"2"}]}', stderr=""))
assert normalize._validate_transcoded_pts(video, pts)["ptsRows"] == 2
with pytest.raises(RuntimeError, match="meta 与 PTS"):
normalize._validate_transcoded_pts(video, pts, 3)
pts.write_text("timestamp_us\n100\n200\n300\n")
with pytest.raises(RuntimeError, match="实际 2 帧.*3 行"):
normalize._validate_transcoded_pts(video, pts)
for content in ("timestamp_us\n", "timestamp_us\n100\n100\n", "timestamp_us\n100\n50\n", "timestamp_us\nbad\n", "other\n1\n2\n"):
pts.write_text(content)
with pytest.raises(RuntimeError):
normalize._validate_transcoded_pts(video, pts)
def test_arbitrary_capture_names_keep_media_and_calibration_together():
for path in ("part0001/business_camera_left_part0001.mp4", "business_calibration_camera.yaml", "part0001/business_imu_part0001.csv"):
assert normalize._capture_id(path) == "business"
def test_fallback_reads_matching_meta_and_keeps_pts_bytes(tmp_path, monkeypatch):
import json
from types import SimpleNamespace
video = f"part0001/{C}_camera_left_part0001_silent_480p_wm.mp4"
pts = f"part0001/{C}_camera_left_part0001_pts.csv"
meta = f"meta_{C}.json"
contents = {video: "fixture", pts: "timestamp_us\n100\n200\n", meta: json.dumps({"streams":{"color_left":{"segments":[{"index":1,"frame_count":2}]}}})}
entries = []
for name, text in contents.items():
file = tmp_path / name
file.parent.mkdir(parents=True, exist_ok=True)
file.write_text(text)
entries.append({"path":name,"source":{"path":str(file)}})
monkeypatch.setattr(normalize.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout='{"streams":[{"nb_read_frames":"2"}]}', stderr=""))
paired = normalize._paired_files(video, entries)
result = normalize._validate_fallback_pair(video, paired, entries, str(tmp_path), tmp_path, {}, {})
assert result["videoFrames"] == 2
assert (tmp_path / pts).read_text() == contents[pts]
# Old configurations with an explicit false value must no longer block.
assert normalize._validate_fallback_pair(video, paired, entries, str(tmp_path), tmp_path, {"TRANSCODE_PRESERVES_FRAME_ORDER":"false"}, {}) == result