"""Unit tests for TaskReassigner: safe abort on low battery, queue recovery, and preemption.""" import pytest from warehouse_twin.domain.robot import Robot, RobotState from warehouse_twin.domain.task import Task, TaskPriority, TaskStatus, TaskType from warehouse_twin.domain.warehouse import Position3D from warehouse_twin.fleet.task_reassigner import TaskReassigner from warehouse_twin.routing.reservations import TimeSpaceReservationGrid @pytest.fixture def robot(): return Robot( robot_id="amr_light_01", robot_type="light_amr", position=Position3D(2.0, 36.0, 0.0), maximum_speed=1.8, payload_capacity=150.0, battery_level=0.70, # ~23% SoC battery_capacity=3.0, energy_consumption={"idle": 0.05, "moving_empty": 0.35, "moving_loaded": 0.65, "handling": 0.15}, ) @pytest.fixture def task(): return Task( task_id="task_test_01", task_type=TaskType.OUTBOUND_PICKING, source="rack_a_01", destination="picking_station_a1", priority=TaskPriority.HIGH.value, release_time=100.0, required_payload=50.0, ) def test_reassign_due_to_low_battery(robot, task): grid = TimeSpaceReservationGrid() # Add dummy reservation for robot grid.reserve_node("node_1", 100.0, 110.0, "amr_light_01") assert "node_1" in grid.node_reservations reassigner = TaskReassigner(grid) # Assign robot to task robot.assign_task(task, sim_time=100.0) task.assign_to_robot(robot.robot_id, sim_time=100.0) robot.transition_to(RobotState.DISPATCHED, sim_time=100.0) assert robot.current_task_id == "task_test_01" assert task.status == TaskStatus.ASSIGNED # Trigger safe reassignment at t=105s res = reassigner.reassign_due_to_low_battery( robot=robot, task=task, sim_time=105.0, reason="Battery critically low before pick", ) assert res.reassigned is True assert res.task_id == "task_test_01" assert res.previous_robot_id == "amr_light_01" # Robot bindings must be cleared assert robot.current_task_id is None # Task must be reset to PENDING status assert task.status == TaskStatus.PENDING assert task.assigned_robot_id is None # Grid reservations must be cleared assert "node_1" not in grid.node_reservations def test_preemption_for_urgent_task(robot, task): grid = TimeSpaceReservationGrid() reassigner = TaskReassigner(grid) # Robot assigned to low priority task robot.assign_task(task, sim_time=50.0) task.assign_to_robot(robot.robot_id, sim_time=50.0) # Urgent production feed arrives urgent_task = Task( task_id="task_urgent_feed", task_type=TaskType.PRODUCTION_FEED, source="rack_c_01", destination="production_dropoff_c1", priority=TaskPriority.CRITICAL.value, release_time=60.0, ) res = reassigner.preempt_task_for_priority( urgent_task=urgent_task, candidate_robot=robot, existing_task=task, sim_time=60.0, ) assert res.reassigned is True # Existing task reverted to PENDING assert task.status == TaskStatus.PENDING assert task.assigned_robot_id is None # Urgent task now assigned to robot assert robot.current_task_id == "task_urgent_feed" assert urgent_task.status == TaskStatus.ASSIGNED assert urgent_task.assigned_robot_id == "amr_light_01"