Files

177 lines
7.2 KiB
Python

"""Unit tests for robot domain models, state machine transitions, and compatibility rules."""
from __future__ import annotations
import pytest
from warehouse_twin.domain.robot import (
IncompatibleRobotTaskError,
InvalidStateTransitionError,
Robot,
RobotState,
RobotTypeSpec,
)
from warehouse_twin.domain.task import Task, TaskStatus, TaskType
from warehouse_twin.domain.warehouse import Position3D
def test_robot_initialization_and_properties(sample_light_robot: Robot):
"""Verify AMR initial attributes, battery percentage, and capacity ratings."""
assert sample_light_robot.robot_id == "amr_light_01"
assert sample_light_robot.robot_type == "light_amr"
assert sample_light_robot.current_state == RobotState.IDLE
assert sample_light_robot.battery_percent == pytest.approx(100.0)
assert sample_light_robot.payload_capacity == 150.0
assert sample_light_robot.maximum_speed == 1.8
assert sample_light_robot.accumulated_distance == 0.0
def test_robot_normal_cycle_state_transitions(sample_light_robot: Robot):
"""Verify full operational lifecycle: IDLE -> DISPATCHED -> PICKING -> LOADED_TRANSIT -> DROPPING -> IDLE."""
r = sample_light_robot
sim_time = 0.0
# 1. IDLE -> DISPATCHED
r.transition_to(RobotState.DISPATCHED, sim_time=10.0)
assert r.current_state == RobotState.DISPATCHED
assert r.idle_time == pytest.approx(10.0)
# 2. DISPATCHED -> PICKING
r.transition_to(RobotState.PICKING, sim_time=25.0)
assert r.current_state == RobotState.PICKING
assert r.busy_time == pytest.approx(15.0)
# 3. PICKING -> LOADED_TRANSIT
r.transition_to(RobotState.LOADED_TRANSIT, sim_time=35.0)
assert r.current_state == RobotState.LOADED_TRANSIT
assert r.busy_time == pytest.approx(25.0)
# 4. LOADED_TRANSIT -> DROPPING
r.transition_to(RobotState.DROPPING, sim_time=50.0)
assert r.current_state == RobotState.DROPPING
assert r.busy_time == pytest.approx(40.0)
# 5. DROPPING -> IDLE
r.transition_to(RobotState.IDLE, sim_time=60.0)
assert r.current_state == RobotState.IDLE
assert r.busy_time == pytest.approx(50.0)
assert r.idle_time == pytest.approx(10.0)
def test_robot_charging_state_transitions(sample_light_robot: Robot):
"""Verify charging cycle: IDLE -> TRANSIT_TO_CHARGE -> CHARGING -> IDLE."""
r = sample_light_robot
r.transition_to(RobotState.TRANSIT_TO_CHARGE, sim_time=100.0)
assert r.current_state == RobotState.TRANSIT_TO_CHARGE
assert r.idle_time == pytest.approx(100.0)
r.transition_to(RobotState.CHARGING, sim_time=120.0)
assert r.current_state == RobotState.CHARGING
r.transition_to(RobotState.IDLE, sim_time=300.0)
assert r.current_state == RobotState.IDLE
assert r.charging_time == pytest.approx(180.0)
@pytest.mark.parametrize("invalid_target", [
RobotState.DROPPING,
RobotState.LOADED_TRANSIT,
RobotState.PICKING,
])
def test_illegal_transitions_from_idle_rejected(sample_light_robot: Robot, invalid_target: RobotState):
"""Ensure invalid transitions from IDLE immediately raise InvalidStateTransitionError."""
with pytest.raises(InvalidStateTransitionError) as exc_info:
sample_light_robot.transition_to(invalid_target, sim_time=1.0)
assert f"Cannot transition from IDLE to {invalid_target.value}" in str(exc_info.value)
@pytest.mark.parametrize("from_state,to_state", [
(RobotState.PICKING, RobotState.CHARGING),
(RobotState.LOADED_TRANSIT, RobotState.IDLE),
(RobotState.CHARGING, RobotState.LOADED_TRANSIT),
(RobotState.DROPPING, RobotState.PICKING),
])
def test_various_illegal_transitions_rejected(sample_light_robot: Robot, from_state: RobotState, to_state: RobotState):
"""Verify arbitrary illegal state combinations are rejected."""
sample_light_robot.current_state = from_state
with pytest.raises(InvalidStateTransitionError):
sample_light_robot.transition_to(to_state, sim_time=5.0)
def test_payload_capacity_compatibility(sample_light_robot: Robot, sample_heavy_robot: Robot, sample_tasks: dict):
"""Verify payload rules: Light AMR rejects heavy parts; Heavy AMR accepts them."""
light_bot = sample_light_robot # 150 kg rating
heavy_bot = sample_heavy_robot # 1500 kg rating
light_task = sample_tasks["light_order"] # 12.5 kg
heavy_task = sample_tasks["heavy_order"] # 650.0 kg
feed_task = sample_tasks["production_feed"] # 1100.0 kg
# Light task (12.5 kg) is compatible with both
assert light_bot.can_carry_payload(light_task.required_payload) is True
assert heavy_bot.can_carry_payload(light_task.required_payload) is True
# Heavy task (650 kg) is rejected by light AMR, accepted by heavy AMR
assert light_bot.can_carry_payload(heavy_task.required_payload) is False
assert heavy_bot.can_carry_payload(heavy_task.required_payload) is True
with pytest.raises(IncompatibleRobotTaskError) as exc_info:
light_bot.assign_task(heavy_task)
assert "exceeds robot 'amr_light_01' capacity" in str(exc_info.value)
# Heavy bot can assign heavy task
heavy_bot.assign_task(heavy_task)
assert heavy_bot.current_task_id == heavy_task.task_id
assert heavy_task.assigned_robot_id == heavy_bot.robot_id
def test_zone_accessibility_compatibility(sample_light_robot: Robot, sample_tasks: dict):
"""Verify operational rule: Light AMR is restricted from Zone B tasks."""
light_bot = sample_light_robot
heavy_task_in_zone_b = sample_tasks["heavy_order"] # target_zone='zone_b'
# Even if payload were reduced to 10 kg, zone_b should still block light AMR
heavy_task_in_zone_b.required_payload = 10.0
compatible, reason = light_bot.validate_task_compatibility(heavy_task_in_zone_b)
assert compatible is False
assert "restricted from entering target zone 'zone_b'" in reason
with pytest.raises(IncompatibleRobotTaskError):
light_bot.assign_task(heavy_task_in_zone_b)
def test_busy_robot_assignment_rejected(sample_light_robot: Robot, sample_tasks: dict):
"""Verify that busy robots cannot be assigned new tasks."""
light_bot = sample_light_robot
task = sample_tasks["light_order"]
light_bot.transition_to(RobotState.DISPATCHED, sim_time=10.0)
compatible, reason = light_bot.validate_task_compatibility(task)
assert compatible is False
assert "is currently busy in state DISPATCHED" in reason
with pytest.raises(IncompatibleRobotTaskError):
light_bot.assign_task(task)
def test_energy_consumption_and_charging(sample_light_robot: Robot):
"""Verify energy deductions and charging calculations."""
r = sample_light_robot
initial = r.battery_level
# Consume energy moving empty for 360 seconds at 0.35 kW
consumed = r.consume_energy(360.0, mode="moving_empty")
expected_kwh = (0.35 * 360.0) / 3600.0
assert consumed == pytest.approx(expected_kwh)
assert r.battery_level == pytest.approx(initial - expected_kwh)
# Set battery level to 1.0 kWh to ensure headroom for charging test
r.battery_level = 1.0
added = r.charge_energy(60.0, charging_rate_kw=25.0)
expected_charge = (25.0 * 60.0) / 3600.0 # 0.4167 kWh
assert added == pytest.approx(expected_charge)
assert r.battery_level == pytest.approx(1.0 + expected_charge)