"""Unit tests for trajectory playback and spatial interpolation engine.""" from __future__ import annotations import json from pathlib import Path import pytest from warehouse_twin.visualization.trajectory_player import ( RobotPlaybackState, TrajectoryData, TrajectoryPlayer, interpolate_robot_state, interpolate_yaw, ) def test_interpolate_yaw_standard_and_wrap(): """Verify angular interpolation along shortest circular arc.""" # Standard interpolation assert pytest.approx(interpolate_yaw(0.0, 90.0, 0.5)) == 45.0 assert pytest.approx(interpolate_yaw(10.0, 50.0, 0.25)) == 20.0 # Wrap-around across 180 / -180 boundary # e.g. from 170 to -170 is a 20 degree positive rotation through 180 res = interpolate_yaw(170.0, -170.0, 0.5) assert pytest.approx(abs(res)) == 180.0 # From -170 to 170 (negative direction through -180) res2 = interpolate_yaw(-170.0, 170.0, 0.5) assert pytest.approx(abs(res2)) == 180.0 def test_interpolate_robot_state_endpoints_and_interior(): """Verify state interpolation at endpoints and midway between trajectory waypoints.""" points = [ { "t": 0.0, "pos": [10.0, 5.0, 0.2], "yaw_deg": 0.0, "state": "IDLE", "battery_percent": 100.0, "is_loaded": False, }, { "t": 10.0, "pos": [20.0, 15.0, 0.2], "yaw_deg": 90.0, "state": "LOADED_TRANSIT", "battery_percent": 98.0, "is_loaded": True, }, ] # Before start s_before = interpolate_robot_state("robot_1", points, -5.0) assert s_before.x == 10.0 assert s_before.y == 5.0 assert s_before.yaw_degrees == 0.0 assert s_before.state == "IDLE" assert s_before.battery_percent == 100.0 assert not s_before.is_loaded # After end s_after = interpolate_robot_state("robot_1", points, 15.0) assert s_after.x == 20.0 assert s_after.y == 15.0 assert s_after.yaw_degrees == 90.0 assert s_after.state == "LOADED_TRANSIT" assert s_after.battery_percent == 98.0 assert s_after.is_loaded # Midpoint (t = 5.0) s_mid = interpolate_robot_state("robot_1", points, 5.0) assert pytest.approx(s_mid.x) == 15.0 assert pytest.approx(s_mid.y) == 10.0 assert pytest.approx(s_mid.yaw_degrees) == 45.0 assert pytest.approx(s_mid.battery_percent) == 99.0 # At alpha = 0.5, picks p2 discrete state assert s_mid.state == "LOADED_TRANSIT" assert s_mid.is_loaded def test_trajectory_data_loading(tmp_path: Path): """Verify TrajectoryData parsing from dict and json file.""" mock_payload = { "metadata": {"simulation_duration": 100.0, "coordinate_system": "meters_z_up"}, "robots": { "amr_1": [ {"t": 0.0, "pos": [0, 0, 0], "yaw_deg": 0, "state": "IDLE"}, {"t": 50.0, "pos": [10, 0, 0], "yaw_deg": 0, "state": "DISPATCHED"}, ], "amr_2": [ {"t": 10.0, "pos": [5, 5, 0], "yaw_deg": 90, "state": "PICKING"}, {"t": 80.0, "pos": [5, 20, 0], "yaw_deg": 90, "state": "DROPPING"}, ], }, } file_path = tmp_path / "test_traj.json" file_path.write_text(json.dumps(mock_payload), encoding="utf-8") data = TrajectoryData.from_file(file_path) assert data.get_robot_ids() == ["amr_1", "amr_2"] assert data.get_point_count() == 4 assert data.start_time == 0.0 assert data.end_time == 80.0 assert data.duration == 80.0 def test_trajectory_player_playback_flow(): """Verify player play, pause, seek, speed, advance, and callbacks.""" raw_data = { "robots": { "r1": [ {"t": 0.0, "pos": [0.0, 0.0, 0.0], "yaw_deg": 0.0, "state": "IDLE"}, {"t": 100.0, "pos": [100.0, 0.0, 0.0], "yaw_deg": 0.0, "state": "LOADED_TRANSIT"}, ] } } data = TrajectoryData.from_dict(raw_data) player = TrajectoryPlayer(data, playback_speed=2.0) callback_states = [] def on_frame(state): callback_states.append(state) player.register_callback(on_frame) assert not player.is_playing assert player.current_time == 0.0 assert player.progress_fraction == 0.0 # Advance when paused does not change time st = player.advance(1.0) assert player.current_time == 0.0 # Start playback player.play() assert player.is_playing # Advance 5 wall-clock seconds at 2.0x speed = 10 simulation seconds st = player.advance(5.0) assert player.current_time == 10.0 assert pytest.approx(st["r1"].x) == 10.0 assert len(callback_states) == 1 # Seek player.seek(50.0) assert player.current_time == 50.0 assert player.progress_fraction == 0.5 assert len(callback_states) == 2 # Advance past end without loop player.advance(40.0) # 40 * 2 = 80s -> 50 + 80 = 130 >= 100 assert player.current_time == 100.0 assert not player.is_playing # Test loop wrap-around player.loop = True player.reset() player.play() player.advance(60.0) # 60 * 2 = 120s -> wraps to 20s assert player.is_playing assert pytest.approx(player.current_time) == 20.0 # Clean teardown player.cleanup() assert not player.is_playing assert len(player._callbacks) == 0