forked from longtao.wu/droboticflow
99 lines
4.5 KiB
Python
99 lines
4.5 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import hashlib
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
OPERATOR_PATH = ROOT / "custom-operators" / "h2r-rgbd-pipeline" / "operator.py"
|
|
SPEC = importlib.util.spec_from_file_location("h2r_rgbd_operator", OPERATOR_PATH)
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
assert SPEC.loader is not None
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
class H2RRGBDCustomOperatorTest(unittest.TestCase):
|
|
def test_episode_discovery_accepts_raw_and_prepared_inputs(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
raw = root / "raw"
|
|
raw.mkdir()
|
|
(raw / "recording.db3").write_bytes(b"db3")
|
|
(raw / "manifest.final.json").write_text("{}", encoding="utf-8")
|
|
prepared = root / "prepared"
|
|
prepared.mkdir()
|
|
(prepared / "rgb.mp4").write_bytes(b"mp4")
|
|
(prepared / "depth_aligned_z16.npz").write_bytes(b"npz")
|
|
(prepared / "manifest.json").write_text("{}", encoding="utf-8")
|
|
self.assertEqual(MODULE.episode_directories(root), [prepared, raw])
|
|
|
|
def test_zip_extraction_rejects_directory_traversal(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
archive = root / "bad.zip"
|
|
with zipfile.ZipFile(archive, "w") as bundle:
|
|
bundle.writestr("../escape.txt", "no")
|
|
with self.assertRaisesRegex(RuntimeError, "unsafe zip member"):
|
|
MODULE.extract_zip_safe(archive, root / "out")
|
|
|
|
def test_split_zip_reassembly_checks_continuity_and_checksum(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
source = root / "parts"
|
|
source.mkdir()
|
|
payload = b"a valid split archive payload"
|
|
(source / "batch.zip.part-000").write_bytes(payload[:10])
|
|
(source / "batch.zip.part-001").write_bytes(payload[10:])
|
|
digest = hashlib.sha256(payload).hexdigest()
|
|
(source / "batch.zip.sha256").write_text(digest + " batch.zip\n")
|
|
archive = MODULE.reassemble_split_zip(source, root / "joined")
|
|
self.assertIsNotNone(archive)
|
|
self.assertEqual(archive.read_bytes(), payload)
|
|
|
|
(source / "batch.zip.part-001").rename(source / "batch.zip.part-002")
|
|
with self.assertRaisesRegex(RuntimeError, "contiguous"):
|
|
MODULE.reassemble_split_zip(source, root / "bad")
|
|
|
|
def test_split_zip_reassembly_rejects_checksum_mismatch(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / "batch.zip.part-000").write_bytes(b"payload")
|
|
(root / "batch.zip.sha256").write_text("0" * 64 + " batch.zip\n")
|
|
with self.assertRaisesRegex(RuntimeError, "checksum mismatch"):
|
|
MODULE.reassemble_split_zip(root, root / "joined")
|
|
|
|
def test_pipeline_command_exposes_geometry_and_dataset_parameters(self):
|
|
command = MODULE.pipeline_command(Path("/input"), Path("/output"))
|
|
text = " ".join(command)
|
|
self.assertIn("--base-separation-m 0.6", text)
|
|
self.assertIn("--repo-id d-robotics/h2r-rgbd", text)
|
|
self.assertIn("--right-grasp-down", command)
|
|
self.assertIn("--overwrite", command)
|
|
|
|
def test_platform_result_has_category_ports_and_protocol_files(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
output = root / "h2r"
|
|
for category in ("lerobot", "render", "sam", "hand_pose"):
|
|
(output / category).mkdir(parents=True)
|
|
(output / category / "sample.json").write_text("{}", encoding="utf-8")
|
|
(output / "render" / "episode.mp4").write_bytes(b"video")
|
|
(output / "batch_summary.json").write_text(
|
|
json.dumps({"episode_count": 10}), encoding="utf-8"
|
|
)
|
|
platform = root / "platform"
|
|
result_path = MODULE.emit_platform_result(output, platform, 10)
|
|
result = json.loads(result_path.read_text(encoding="utf-8"))
|
|
ports = {artifact["portId"] for artifact in result["artifacts"]}
|
|
self.assertEqual(
|
|
ports, {"summary", "lerobot", "render", "sam", "hand_pose"}
|
|
)
|
|
self.assertTrue((platform / "stage-manifest.json").is_file())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|