"""Unit tests for AMR motion trajectory recording and JSON export.""" from __future__ import annotations import json from pathlib import Path import pytest from warehouse_twin.config.loader import ConfigLoader from warehouse_twin.domain.inventory import InventoryCatalog from warehouse_twin.domain.robot import Robot, RobotTypeSpec from warehouse_twin.domain.warehouse import WarehouseModel from warehouse_twin.simulation.dispatcher import DispatcherSettings from warehouse_twin.simulation.order_generator import WorkloadConfig from warehouse_twin.simulation.sim_engine import SimulationEnvironment def test_trajectory_recording_and_export(project_config_dir: Path, tmp_path: Path): """Verify trajectory recording during simulation and JSON export schema compliance.""" loader = ConfigLoader(project_config_dir) bundle = loader.load_all() warehouse = WarehouseModel.from_config_dict(bundle.warehouse) catalog = InventoryCatalog.from_config_dict(bundle.inventory) type_specs = { t_id: RobotTypeSpec.from_dict(t_id, t_data) for t_id, t_data in bundle.robots["robot_types"].items() } fleet = [ Robot.from_fleet_dict(r_data, type_specs) for r_data in bundle.robots["fleet"] ] workload = WorkloadConfig.from_dict(bundle.simulation) dispatch_settings = DispatcherSettings.from_dict(bundle.dispatch_policies) env = SimulationEnvironment( warehouse=warehouse, catalog=catalog, fleet=fleet, workload_config=workload, dispatcher_settings=dispatch_settings, seed=42, ) env.run(until=300.0) # Verify trajectory recording in memory for r_id in env.robots: assert r_id in env.trajectories assert len(env.trajectories[r_id]) > 0 point = env.trajectories[r_id][0] assert "t" in point assert "pos" in point assert "yaw_deg" in point assert "state" in point assert len(point["pos"]) == 3 # Export trajectory out_file = tmp_path / "test_trajectory.json" exported_path = env.export_trajectories(out_file) assert exported_path.exists() assert exported_path.stat().st_size > 1000 with open(exported_path, "r", encoding="utf-8") as f: data = json.load(f) assert "metadata" in data assert "robots" in data meta = data["metadata"] assert meta["coordinate_system"] == "right_handed_cartesian_meters_z_up" assert meta["total_robots"] == 10 assert meta["total_trajectory_points"] > 10 assert len(data["robots"]) == 10