196 lines
7.0 KiB
Python
196 lines
7.0 KiB
Python
"""Regressions across the verified model, packaged assets and native app runtime."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
mujoco = pytest.importorskip("mujoco")
|
|
|
|
from mjrl.controllers import setup_controller
|
|
from mjrl.onboarding.verify import verify
|
|
from mjrl.platform.bundle import build_bundle, open_bundle
|
|
from mjrl.platform.model import copy_model, validate_contained_model
|
|
|
|
_ARM = """
|
|
<mujoco>
|
|
<compiler angle="radian"/>
|
|
<option timestep=".002" gravity="0 0 0" integrator="implicitfast"/>
|
|
<worldbody>
|
|
<body name="base">
|
|
<geom type="box" size=".05 .05 .05"/>
|
|
<body name="arm" pos=".08 0 0">
|
|
<joint name="elbow" axis="0 0 1" range="-1 1"/>
|
|
<geom type="capsule" fromto="0 0 0 .2 0 0" size=".02" mass=".1"/>
|
|
</body>
|
|
</body>
|
|
</worldbody>
|
|
<actuator><position name="elbow_ctrl" joint="elbow" kp="25" kv="3"/></actuator>
|
|
</mujoco>
|
|
"""
|
|
|
|
|
|
def _heightfield(path: Path, pixels: list[list[int]]) -> None:
|
|
image = pytest.importorskip("PIL.Image")
|
|
image.fromarray(np.asarray(pixels, dtype=np.uint8)).save(path)
|
|
|
|
|
|
def _field_model(meshdir: Path | str) -> str:
|
|
return f"""
|
|
<mujoco>
|
|
<compiler meshdir="{meshdir}"/>
|
|
<asset><hfield name="surface" file="field.png" size="1 1 1 .1"/></asset>
|
|
<worldbody><geom type="hfield" hfield="surface"/></worldbody>
|
|
</mujoco>
|
|
"""
|
|
|
|
|
|
def test_heightfield_dependency_is_relocated_with_its_compiled_values(tmp_path: Path) -> None:
|
|
assets = tmp_path / "assets"
|
|
assets.mkdir()
|
|
_heightfield(assets / "field.png", [[0, 1], [2, 255]])
|
|
source = tmp_path / "surface.xml"
|
|
source.write_text(_field_model("assets"), encoding="utf-8")
|
|
expected = mujoco.MjModel.from_xml_path(str(source)).hfield_data.copy()
|
|
output = tmp_path / "normalized" / "surface.xml"
|
|
copy_model(source, output)
|
|
(assets / "field.png").unlink()
|
|
validate_contained_model(output.parent, output)
|
|
rebuilt = mujoco.MjModel.from_xml_path(str(output))
|
|
np.testing.assert_array_equal(rebuilt.hfield_data, expected)
|
|
|
|
|
|
def test_heightfield_compiler_path_cannot_read_outside_app(tmp_path: Path) -> None:
|
|
root = tmp_path / "app"
|
|
root.mkdir()
|
|
external = tmp_path / "outside"
|
|
external.mkdir()
|
|
# An internal decoy has the same file name as the external PNG. The
|
|
# validation path and the native compiler must not disagree about it.
|
|
_heightfield(root / "field.png", [[0, 1], [2, 255]])
|
|
_heightfield(external / "field.png", [[255, 0], [0, 0]])
|
|
model = root / "robot.xml"
|
|
model.write_text(_field_model(external.as_posix()), encoding="utf-8")
|
|
with pytest.raises(ValueError):
|
|
validate_contained_model(root, model)
|
|
|
|
|
|
def test_nested_model_cannot_load_an_external_include(tmp_path: Path) -> None:
|
|
root = tmp_path / "app"
|
|
root.mkdir()
|
|
outside = tmp_path / "outside.xml"
|
|
outside.write_text(
|
|
'<mujoco><body name="inner"><geom type="box" size=".1 .1 .1"/></body></mujoco>',
|
|
encoding="utf-8",
|
|
)
|
|
(root / "nested.xml").write_text(
|
|
f'<mujoco><worldbody><include file="{outside.as_posix()}"/></worldbody></mujoco>',
|
|
encoding="utf-8",
|
|
)
|
|
model = root / "robot.xml"
|
|
model.write_text(
|
|
"""
|
|
<mujoco>
|
|
<asset><model name="nested" file="nested.xml"/></asset>
|
|
<worldbody><attach model="nested" body="inner" prefix="attached_"/></worldbody>
|
|
</mujoco>
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
assert mujoco.MjModel.from_xml_path(str(model)).ngeom == 1
|
|
with pytest.raises(ValueError):
|
|
validate_contained_model(root, model)
|
|
|
|
|
|
def test_fused_geom_offsets_survive_portable_relocation(tmp_path: Path) -> None:
|
|
source = tmp_path / "source.xml"
|
|
source.write_text(
|
|
"""
|
|
<mujoco>
|
|
<compiler fusestatic="true"/>
|
|
<worldbody>
|
|
<body name="arm">
|
|
<joint name="hinge"/>
|
|
<geom type="sphere" size=".04"/>
|
|
<body name="fixed" pos="0 0 .1"><geom type="sphere" size=".02"/></body>
|
|
</body>
|
|
</worldbody>
|
|
</mujoco>
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
original = mujoco.MjModel.from_xml_path(str(source))
|
|
output = tmp_path / "normalized" / "robot.xml"
|
|
copy_model(source, output)
|
|
relocated = mujoco.MjModel.from_xml_path(str(output))
|
|
assert original.nbody == relocated.nbody == 2
|
|
np.testing.assert_allclose(original.geom_pos, relocated.geom_pos)
|
|
np.testing.assert_allclose(original.body_inertia, relocated.body_inertia)
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["./model/robot.xml", "model//robot.xml", "model/./robot.xml"])
|
|
def test_archive_paths_cannot_alias_one_extracted_file(tmp_path: Path, alias: str) -> None:
|
|
path = tmp_path / "aliased.app"
|
|
with zipfile.ZipFile(path, "w") as archive:
|
|
archive.writestr("model/robot.xml", "first")
|
|
archive.writestr(alias, "second")
|
|
with pytest.raises(ValueError, match="path|duplicate"):
|
|
with open_bundle(path):
|
|
pytest.fail("archive with two spellings for the same file was opened")
|
|
|
|
|
|
def test_active_direct_controls_cannot_ship_as_a_viewer(tmp_path: Path) -> None:
|
|
source = tmp_path / "arm.xml"
|
|
source.write_text(_ARM, encoding="utf-8")
|
|
config = tmp_path / "controller.json"
|
|
config.write_text('{"kind": "direct", "actuators": ["elbow_ctrl"]}', encoding="utf-8")
|
|
output = tmp_path / "viewer"
|
|
with pytest.raises(ValueError, match="viewer|active|control"):
|
|
build_bundle(
|
|
source,
|
|
output,
|
|
robot_id="arm",
|
|
modes={"view": {"control_mode": "model_viewer", "controller": config}},
|
|
)
|
|
assert not output.exists() and not output.with_suffix(".app").exists()
|
|
|
|
|
|
def test_inconsistent_trajectory_is_refused_before_publish(tmp_path: Path) -> None:
|
|
source = tmp_path / "arm.xml"
|
|
source.write_text(_ARM, encoding="utf-8")
|
|
setup = setup_controller(
|
|
source,
|
|
{"kind": "joint_sliders", "joints": ["elbow"], "kp": 25, "kd": 3, "effort_limit": 10},
|
|
tmp_path / "prepared",
|
|
)
|
|
report = verify(
|
|
setup.model,
|
|
{
|
|
"controller": str(setup.config),
|
|
"controls": str(setup.controls),
|
|
"steps": 500,
|
|
"command": {"elbow": 0.3},
|
|
"joint_positions": {"elbow": 0.3},
|
|
},
|
|
)
|
|
assert report["passed"], report["stages"]
|
|
# Fingerprints and pass flags are still intact. Only the measured final
|
|
# position disagrees with the controller that the application will ship.
|
|
report["probes"][0]["samples"][-1]["qpos"][0] += 1
|
|
verification = tmp_path / "report.json"
|
|
verification.write_text(json.dumps(report), encoding="utf-8")
|
|
output = tmp_path / "rejected"
|
|
with pytest.raises(ValueError, match="reproduce|reference"):
|
|
build_bundle(
|
|
setup.model,
|
|
output,
|
|
robot_id="arm",
|
|
modes={"joints": {"controller": setup.config, "controls": setup.controls}},
|
|
verification=verification,
|
|
)
|
|
assert not output.exists() and not output.with_suffix(".app").exists()
|