|
| 1 | +"""Integration test: every robot in robots.json resolves to an existing file. |
| 2 | +
|
| 3 | +Walks the full registry and asserts that every ``asset.dir / asset.model_xml`` |
| 4 | +path is a valid relative path that would resolve under the search directories |
| 5 | +**if** the assets were downloaded. This catches: |
| 6 | + - Typos in ``robots.json`` (e.g. ``asimov_v0.xml`` → ``xmls/asimov.xml``) |
| 7 | + - Upstream layout regressions in robot_descriptions / MuJoCo Menagerie |
| 8 | + - Missing ``dir`` or ``model_xml`` keys in sim-capable robots |
| 9 | + - Path traversal sequences in registry entries |
| 10 | +
|
| 11 | +The test does NOT require downloaded assets or GPU — it only validates the |
| 12 | +registry metadata itself (directory/file names, path safety). Run it in the |
| 13 | +unit or integ hatch env. |
| 14 | +
|
| 15 | +Added as follow-up to PR #84 review (issue #105, task 2). |
| 16 | +""" |
| 17 | + |
| 18 | +import json |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +# ───────────────────────────────────────────────────────────────────── |
| 24 | +# Load registry directly to avoid import side effects |
| 25 | +# ───────────────────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +_REGISTRY_PATH = Path(__file__).resolve().parent.parent / "strands_robots" / "registry" / "robots.json" |
| 28 | + |
| 29 | + |
| 30 | +def _load_registry() -> dict: |
| 31 | + """Load robots.json and return the robots dict.""" |
| 32 | + with open(_REGISTRY_PATH) as f: |
| 33 | + data = json.load(f) |
| 34 | + return data.get("robots", data) |
| 35 | + |
| 36 | + |
| 37 | +_ROBOTS = _load_registry() |
| 38 | + |
| 39 | +# Robots that have simulation assets (asset.dir + asset.model_xml). |
| 40 | +# Hardware-only robots (e.g. lekiwi, reachy2) have no 'asset' key. |
| 41 | +_SIM_ROBOTS = {name: info for name, info in _ROBOTS.items() if "asset" in info} |
| 42 | +_SIM_ROBOT_NAMES = list(_SIM_ROBOTS.keys()) |
| 43 | + |
| 44 | + |
| 45 | +# ───────────────────────────────────────────────────────────────────── |
| 46 | +# Tests for ALL robots (sim + hardware-only) |
| 47 | +# ───────────────────────────────────────────────────────────────────── |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("name", list(_ROBOTS.keys()), ids=list(_ROBOTS.keys())) |
| 51 | +def test_registry_entry_is_well_formed(name: str) -> None: |
| 52 | + """Every robot must have a description and category.""" |
| 53 | + info = _ROBOTS[name] |
| 54 | + assert "description" in info, f"Robot '{name}' missing 'description'" |
| 55 | + assert "category" in info, f"Robot '{name}' missing 'category'" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("name", list(_ROBOTS.keys()), ids=list(_ROBOTS.keys())) |
| 59 | +def test_registry_resolve_via_api(name: str) -> None: |
| 60 | + """Verify the registry API can look up each robot without errors.""" |
| 61 | + from strands_robots.registry import get_robot, resolve_name |
| 62 | + |
| 63 | + canonical = resolve_name(name) |
| 64 | + assert canonical is not None, f"resolve_name({name!r}) returned None" |
| 65 | + |
| 66 | + info = get_robot(name) |
| 67 | + assert info is not None, f"get_robot({name!r}) returned None" |
| 68 | + |
| 69 | + |
| 70 | +# ───────────────────────────────────────────────────────────────────── |
| 71 | +# Tests for sim-capable robots only (have 'asset' key) |
| 72 | +# ───────────────────────────────────────────────────────────────────── |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("name", _SIM_ROBOT_NAMES, ids=_SIM_ROBOT_NAMES) |
| 76 | +def test_sim_robot_has_required_asset_fields(name: str) -> None: |
| 77 | + """Sim robots must have asset.dir and asset.model_xml.""" |
| 78 | + asset = _SIM_ROBOTS[name]["asset"] |
| 79 | + assert "dir" in asset, f"Robot '{name}' missing 'asset.dir'" |
| 80 | + assert "model_xml" in asset, f"Robot '{name}' missing 'asset.model_xml'" |
| 81 | + assert isinstance(asset["dir"], str) and asset["dir"], f"Robot '{name}' has empty 'asset.dir'" |
| 82 | + assert isinstance(asset["model_xml"], str) and asset["model_xml"], f"Robot '{name}' has empty 'asset.model_xml'" |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("name", _SIM_ROBOT_NAMES, ids=_SIM_ROBOT_NAMES) |
| 86 | +def test_sim_robot_paths_are_safe(name: str) -> None: |
| 87 | + """No registry path should contain traversal sequences.""" |
| 88 | + asset = _SIM_ROBOTS[name]["asset"] |
| 89 | + dir_name = asset.get("dir", "") |
| 90 | + model_xml = asset.get("model_xml", "") |
| 91 | + scene_xml = asset.get("scene_xml", "") |
| 92 | + |
| 93 | + for field, value in [("dir", dir_name), ("model_xml", model_xml), ("scene_xml", scene_xml)]: |
| 94 | + if not value: |
| 95 | + continue |
| 96 | + assert ".." not in value, f"Robot '{name}' asset.{field} contains '..': {value!r}" |
| 97 | + assert not value.startswith("/"), f"Robot '{name}' asset.{field} is absolute: {value!r}" |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("name", _SIM_ROBOT_NAMES, ids=_SIM_ROBOT_NAMES) |
| 101 | +def test_sim_robot_xml_has_xml_extension(name: str) -> None: |
| 102 | + """model_xml and scene_xml should end with .xml.""" |
| 103 | + asset = _SIM_ROBOTS[name]["asset"] |
| 104 | + model_xml = asset.get("model_xml", "") |
| 105 | + scene_xml = asset.get("scene_xml", "") |
| 106 | + |
| 107 | + if model_xml: |
| 108 | + assert model_xml.endswith(".xml"), f"Robot '{name}' model_xml doesn't end with .xml: {model_xml!r}" |
| 109 | + if scene_xml: |
| 110 | + assert scene_xml.endswith(".xml"), f"Robot '{name}' scene_xml doesn't end with .xml: {scene_xml!r}" |
0 commit comments