"""Calibrated actuator and sensor semantics survive metadata review/export.""" import copy import json import mujoco import pytest from mjrl.adapter import build_layout from mjrl.onboarding.upstream import extract_policy_metadata def metadata(): return { "schema": "mjrl-policy-adapter/1", "wire_joint_order": ["joint"], "obs_joint_order": ["joint"], "action_joint_order": ["joint"], "default_joint_pos": {"joint": 0}, "joint_limits": {"joint": [-1, 1]}, "action_scale": 1, "control": {"kp": 1, "kd": 0, "effort_limit": 0.64, "control_hz": 50}, "observation": { "terms": [{"name": "base_ang_vel"}, {"name": "joint_pos"}], "frames": {"base_ang_vel": {"kind": "site", "name": "imu"}}, }, "motor_model": { "kind": "bam", "motor_name": "xl330", "model": "m6", "kp": 100, "vin": 7.0, "max_current": 1.5, "vin_drop_resistance": 0.1, "vin_min": 6, }, } def test_calibrated_metadata_roundtrip(tmp_path): doc = metadata() source = tmp_path / "reviewed.json" source.write_text(json.dumps(doc)) report = extract_policy_metadata([source]) assert report["ready"], report assert report["metadata"] == doc layout = build_layout(report["metadata"]) assert layout["_schema"] == "mjrl-native-layout/1" assert layout["motor_model"] == doc["motor_model"] assert layout["observation"]["frames"] == doc["observation"]["frames"] assert layout["control"]["kp"] == 1 # BAM firmware gain stays separate. assert layout["motor_model"]["kp"] == 100 @pytest.mark.parametrize("frames", [ {"actions": {"kind": "site", "name": "imu"}}, {"projected_gravity": {"kind": "site", "name": "imu"}}, {"base_ang_vel": {"kind": "sensor", "name": "imu"}}, {"base_ang_vel": {"kind": "site", "name": ""}}, {"base_ang_vel": {"kind": "site", "name": "imu", "ignored": 1}}, ]) def test_unknown_or_unobserved_frames_fail_closed(frames): doc = metadata() doc["observation"]["frames"] = frames with pytest.raises(ValueError, match="[Ff]rame"): build_layout(doc) def test_frame_must_exist_in_exact_model(): model = mujoco.MjModel.from_xml_string(''' ''') doc = metadata() with pytest.raises(ValueError, match="no observation frame"): build_layout(doc, model=model) doc["observation"]["frames"]["base_ang_vel"]["name"] = "actual_imu" assert build_layout(doc, model=model)["observation"]["frames"] def test_motor_metadata_rejects_unknown_keys_and_velocity_targets(): doc = metadata() invalid = copy.deepcopy(doc) invalid["motor_model"]["typo"] = 1 with pytest.raises(ValueError, match="BAM fields"): build_layout(invalid) doc["action"] = {"targets": {"joint": {"kind": "velocity", "offset": 0}}} doc["control"]["kp"], doc["control"]["kd"] = 0, 1 with pytest.raises(ValueError, match="requires position targets"): build_layout(doc)