149 lines
5.5 KiB
Python
149 lines
5.5 KiB
Python
"""Discover calibration files without importing training code or hidden tasks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import tasks
|
|
from tasks import registry
|
|
from tasks.robot_spec import JointSpec, RobotSpec, scaffold_robot_spec
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@pytest.fixture
|
|
def clean_registry(monkeypatch):
|
|
monkeypatch.setattr(registry, "_REGISTRY", {})
|
|
|
|
|
|
@pytest.fixture
|
|
def robot_spec(tmp_path: Path) -> RobotSpec:
|
|
model = tmp_path / "models" / "training.xml"
|
|
model.parent.mkdir()
|
|
model.write_text("<mujoco/>", encoding="utf-8")
|
|
return RobotSpec(
|
|
robot_id="example_robot",
|
|
family="biped",
|
|
model_path=model,
|
|
base_body="base",
|
|
base_height=0.55,
|
|
foot_geoms=("left_foot", "right_foot"),
|
|
position_joints=(JointSpec("left", 12.0, 0.4, 9.0, 0.4), JointSpec("right", 12, 0.4, 9, 0.4)),
|
|
)
|
|
|
|
|
|
def test_import_tasks_does_not_pull_in_simulation_deps() -> None:
|
|
probe = (
|
|
"import sys; import tasks; "
|
|
"leaked = [m for m in ('mjlab', 'torch', 'mujoco', 'warp') if m in sys.modules]; "
|
|
"print(','.join(leaked))"
|
|
)
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", probe], capture_output=True, text=True, check=True,
|
|
)
|
|
assert not result.stdout.strip()
|
|
|
|
|
|
def test_discovery_uses_a_single_spec_file(robot_spec, clean_registry, tmp_path) -> None:
|
|
task_root = tmp_path / "tasks"
|
|
path = scaffold_robot_spec(robot_spec, tasks_root=task_root)
|
|
assert path == task_root / robot_spec.robot_id / "robot_spec.py"
|
|
assert sorted(path.parent.iterdir()) == [path]
|
|
assert str(robot_spec.model_path) not in path.read_text(encoding="utf-8")
|
|
|
|
registry.discover_robots(task_root)
|
|
assert tasks.list_ids() == ["example_robot"]
|
|
assert tasks.list_ids(tag="biped") == ["example_robot"]
|
|
task = tasks.get(robot_spec.robot_id)
|
|
assert task.robot == robot_spec
|
|
assert task.description == "Biped_Template locomotion"
|
|
assert task.resolve_asset(None) == robot_spec.model_path
|
|
assert task.resolve_asset("example_robot") == robot_spec.model_path
|
|
assert task.resolve_asset(str(robot_spec.model_path)) == robot_spec.model_path
|
|
assert tasks.load_cli_args(task.id) is None
|
|
assert tasks.load_export_media(task.id) is None
|
|
|
|
with pytest.raises(KeyError, match="example_robot"):
|
|
task.resolve_asset("unknown")
|
|
with pytest.raises(FileNotFoundError, match="model file does not exist"):
|
|
task.resolve_asset("missing.xml")
|
|
with pytest.raises(FileExistsError):
|
|
scaffold_robot_spec(robot_spec, tasks_root=task_root)
|
|
|
|
|
|
def test_discovery_rejects_executable_config(robot_spec, clean_registry, tmp_path) -> None:
|
|
task_root = tmp_path / "tasks"
|
|
path = scaffold_robot_spec(robot_spec, tasks_root=task_root)
|
|
created = tmp_path / "executed"
|
|
with path.open("a", encoding="utf-8") as file:
|
|
file.write(f"open({str(created)!r}, 'w').close()\n")
|
|
with pytest.raises(ValueError, match="literal ROBOT_SPEC"):
|
|
registry.discover_robots(task_root)
|
|
assert not created.exists()
|
|
assert tasks.list_ids() == []
|
|
|
|
|
|
def test_discovery_rejects_id_directory_mismatch(robot_spec, clean_registry, tmp_path) -> None:
|
|
task_root = tmp_path / "tasks"
|
|
path = scaffold_robot_spec(robot_spec, tasks_root=task_root)
|
|
path.parent.rename(path.parent.with_name("copied_robot"))
|
|
with pytest.raises(ValueError, match="must match its directory name"):
|
|
registry.discover_robots(task_root)
|
|
|
|
|
|
def test_templates_are_not_assets() -> None:
|
|
assert all(spec.robot is not None and spec.id == spec.robot.robot_id for spec in tasks.all_specs())
|
|
assert all("template" not in spec.id.split(".") for spec in tasks.all_specs())
|
|
|
|
|
|
def test_duplicate_id_raises(clean_registry) -> None:
|
|
registry.register(id="dup_task")
|
|
with pytest.raises(ValueError, match="registered twice"):
|
|
registry.register(id="dup_task")
|
|
|
|
|
|
def test_duplicate_asset_name_raises(clean_registry) -> None:
|
|
dup = (
|
|
registry.AssetSpec("same", Path("a.xml")),
|
|
registry.AssetSpec("same", Path("b.xml")),
|
|
)
|
|
with pytest.raises(ValueError, match="duplicate asset names"):
|
|
registry.register(id="dup_asset_task", assets=dup)
|
|
|
|
|
|
def test_unknown_id_lists_available_ones(robot_spec, clean_registry, tmp_path) -> None:
|
|
scaffold_robot_spec(robot_spec, tasks_root=tmp_path)
|
|
registry.discover_robots(tmp_path)
|
|
with pytest.raises(KeyError, match="example_robot"):
|
|
tasks.get("nope")
|
|
|
|
|
|
def test_missing_custom_task_directory_gives_actionable_error(clean_registry) -> None:
|
|
registry.register(id="nosuchfamily.nosuchtask")
|
|
with pytest.raises(ModuleNotFoundError, match="must match a directory under tasks/"):
|
|
registry.load_env_cfg("nosuchfamily.nosuchtask")
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", [".mjcf", ".mjb", ".XML"])
|
|
def test_locked_templates_reject_an_unsupported_model_suffix(robot_spec, tmp_path, suffix):
|
|
from dataclasses import replace
|
|
|
|
wrong = tmp_path / ("wrong" + suffix)
|
|
wrong.touch()
|
|
with pytest.raises(ValueError, match="lowercase .xml suffix"):
|
|
replace(robot_spec, model_path=wrong)
|
|
spec = registry.TaskSpec(id=robot_spec.robot_id, robot=robot_spec)
|
|
with pytest.raises(ValueError, match="lowercase .xml suffix"):
|
|
spec.resolve_asset(str(wrong))
|
|
|
|
|
|
def test_robot_spec_refuses_names_in_the_entity_namespace(robot_spec):
|
|
from dataclasses import replace
|
|
|
|
with pytest.raises(ValueError, match="unqualified model names"):
|
|
replace(robot_spec, foot_geoms=("subassembly/left", "right"))
|