"""Unit tests for NetworkX-powered PathPlanner and reachability subsystem.""" from __future__ import annotations from pathlib import Path import pytest from warehouse_twin.config.loader import ConfigLoader from warehouse_twin.domain.warehouse import Position3D, WarehouseModel from warehouse_twin.routing.graph_builder import WarehouseGraphBuilder from warehouse_twin.routing.path_planner import ( NoPathFoundError, PathPlanner, PathPlanningError, ) @pytest.fixture def planner_setup(project_config_dir: Path): """Fixture providing initialized PathPlanner and WarehouseGraphBuilder.""" loader = ConfigLoader(project_config_dir) bundle = loader.load_all() builder = WarehouseGraphBuilder.from_config_dict(bundle.warehouse) graph = builder.build_graph() planner = PathPlanner(graph) return planner, builder, graph def test_path_planner_initialization(planner_setup): """Verify PathPlanner properly caches node positions and initializes.""" planner, _, graph = planner_setup assert len(planner._node_positions) == graph.number_of_nodes() # Query arbitrary node wp_pos = planner.get_node_position("wp_20_20") assert isinstance(wp_pos, Position3D) assert wp_pos.x == 2.0 assert wp_pos.y == 2.0 def test_find_shortest_path_standard(planner_setup): """Verify standard shortest path calculation between dock and picking station.""" planner, _, _ = planner_setup path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", ) assert len(path) > 3 assert path[0] == "station_dock_inbound_1" assert path[-1] == "station_picking_station_a1" dist = planner.compute_path_distance(path) assert dist > 10.0 duration = planner.compute_path_duration(path, robot_speed=1.5) assert duration > 5.0 def test_same_source_and_target_path(planner_setup): """Verify path planning when source equals target returns single-element path.""" planner, _, _ = planner_setup path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_dock_inbound_1", ) assert path == ["station_dock_inbound_1"] assert planner.compute_path_distance(path) == 0.0 def test_light_amr_restricted_from_zone_b(planner_setup): """Verify light AMRs cannot plan paths to Zone B storage locations.""" planner, _, _ = planner_setup # rack_b_01 is situated in Zone B with 4.0m aisles restricted to heavy AMRs with pytest.raises(NoPathFoundError) as exc_info: planner.find_shortest_path( source="station_dock_inbound_1", target="rack_b_01", robot_type="light_amr", ) assert "light_amr" in str(exc_info.value) assert "rack_b_01" in str(exc_info.value) assert not planner.is_destination_reachable( source="station_dock_inbound_1", target="rack_b_01", robot_type="light_amr", ) def test_heavy_amr_allowed_in_zone_b(planner_setup): """Verify heavy AMRs can freely plan paths to Zone B wide aisle racks.""" planner, _, _ = planner_setup assert planner.is_destination_reachable( source="station_dock_inbound_1", target="rack_b_01", robot_type="heavy_amr", ) path = planner.find_shortest_path( source="station_dock_inbound_1", target="rack_b_01", robot_type="heavy_amr", ) assert path[0] == "station_dock_inbound_1" assert path[-1] in ("rack_b_01", planner.resolve_node_id("rack_b_01")) assert len(path) > 5 def test_unreachable_destination_raises_error(planner_setup): """Verify non-existent or severed node triggers NoPathFoundError.""" planner, _, graph = planner_setup # Query with missing target with pytest.raises(KeyError): planner.find_shortest_path("station_dock_inbound_1", "non_existent_node") # Isolate an internal node isolated = "wp_100_100" graph.remove_edges_from(list(graph.in_edges(isolated)) + list(graph.out_edges(isolated))) with pytest.raises(NoPathFoundError): planner.find_shortest_path("station_dock_inbound_1", isolated, robot_type="heavy_amr") def test_path_avoid_nodes_and_edges(planner_setup): """Verify dynamic rerouting avoids specific congested nodes or edges.""" planner, _, _ = planner_setup base_path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", ) # Pick an intermediate node to block intermediate_node = base_path[2] detour_path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", avoid_nodes={intermediate_node}, ) assert intermediate_node not in detour_path assert detour_path[0] == "station_dock_inbound_1" assert detour_path[-1] == "station_picking_station_a1" # Block an edge u, v = base_path[1], base_path[2] edge_detour_path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", avoid_edges={(u, v)}, ) # Verify (u, v) is not consecutive in edge_detour_path pairs = list(zip(edge_detour_path[:-1], edge_detour_path[1:])) assert (u, v) not in pairs def test_dynamic_edge_congestion_weights(planner_setup): """Verify edge with high congestion penalty is steered around if an alternate route exists.""" planner, _, _ = planner_setup base_path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", ) # Heavily penalize first edge penalized_edge = (base_path[1], base_path[2]) penalties = {penalized_edge: 500.0} penalized_path = planner.find_shortest_path( source="station_dock_inbound_1", target="station_picking_station_a1", robot_type="light_amr", dynamic_edge_weights=penalties, ) pairs = list(zip(penalized_path[:-1], penalized_path[1:])) assert penalized_edge not in pairs def test_closest_node_lookup(planner_setup): """Verify closest node lookup with spatial accuracy.""" planner, _, _ = planner_setup query_pos = Position3D(2.1, 2.2, 0.0) closest = planner.get_closest_node(query_pos) assert closest == "wp_20_20" # Zone B filtering for light AMR zone_b_pos = Position3D(35.0, 20.0, 0.0) closest_heavy = planner.get_closest_node(zone_b_pos, robot_type="heavy_amr") closest_light = planner.get_closest_node(zone_b_pos, robot_type="light_amr") # Light AMR node must NOT be inside Zone B internal aisles assert planner.graph.nodes[closest_light].get("zone") != "zone_b"