"""Unit tests for configurable multi-row warehouse layout generation, boundaries, and validation.""" import pytest from warehouse_twin.domain.grid import CellType from warehouse_twin.domain.warehouse import WarehouseDimensions from warehouse_twin.layout.layout_generator import ModularWarehouseBuilder, WarehouseLayoutConfig from warehouse_twin.layout.layout_validator import LayoutValidator from warehouse_twin.routing.graph_builder import WarehouseGraphBuilder def test_modular_warehouse_builder_creates_valid_test_layout(): """Verify test warehouse generation with 2 parallel rack rows, cross aisles, and chargers.""" cfg = WarehouseLayoutConfig( warehouse_length=30.0, warehouse_width=20.0, warehouse_height=6.0, grid_cell_size=0.5, rack_length=2.4, rack_depth=1.0, rack_height=3.0, num_shelf_levels=3, num_rows=2, bays_per_row=4, clear_aisle_width=2.0, num_chargers=2, ) builder = ModularWarehouseBuilder(cfg) model, grid, report = builder.build_test_layout() # 1. Structural assertions assert model.dimensions.width == 30.0 assert model.dimensions.depth == 20.0 assert len(model.storage_locations) == 8 # 2 rows * 4 bays assert len(model.picking_points) == 8 assert len(model.charging_stations) == 2 # 2. Layout validation report must pass cleanly without errors assert report.is_valid is True assert len(report.errors) == 0 assert report.num_racks == 8 assert report.num_picking_points == 8 def test_layout_validator_detects_boundary_and_overlap_violations(): """Verify LayoutValidator flags racks outside boundaries and intersecting footprints.""" validator = LayoutValidator() # Test 1: Rack exceeding boundary from warehouse_twin.domain.warehouse import Position3D, StorageLocation, WarehouseModel dims = WarehouseDimensions(width=20.0, depth=15.0, height=5.0) rack_outside = StorageLocation( location_id="rack_out", zone="zone_a", position=Position3D(19.5, 14.5, 0.0), rack_dimensions=(2.4, 1.0, 3.0), # extends to x=20.7, y=15.0 ) model_bad = WarehouseModel( dimensions=dims, zones={}, stations={}, charging_stations={}, storage_locations={"rack_out": rack_outside}, ) rep1 = validator.validate_warehouse_model(model_bad) assert rep1.is_valid is False assert any("exceeds warehouse boundary" in err for err in rep1.errors) # Test 2: Overlapping racks r1 = StorageLocation( location_id="r1", zone="zone_a", position=Position3D(10.0, 8.0, 0.0), rack_dimensions=(2.4, 1.0, 3.0), ) r2 = StorageLocation( location_id="r2", zone="zone_a", position=Position3D(10.5, 8.2, 0.0), # Direct overlap rack_dimensions=(2.4, 1.0, 3.0), ) model_overlap = WarehouseModel( dimensions=dims, zones={}, stations={}, charging_stations={}, storage_locations={"r1": r1, "r2": r2}, ) rep2 = validator.validate_warehouse_model(model_overlap) assert rep2.is_valid is False assert any("footprint collision" in err for err in rep2.errors) def test_generated_layout_graph_connectivity(): """Verify WarehouseGraphBuilder successfully builds a connected graph for generated layout.""" cfg = WarehouseLayoutConfig( warehouse_length=30.0, warehouse_width=20.0, num_rows=2, bays_per_row=3, num_chargers=2, ) builder = ModularWarehouseBuilder(cfg) model, grid, _ = builder.build_test_layout() config_dict = builder.export_to_config_dict(model) graph_builder = WarehouseGraphBuilder.from_config_dict(config_dict) graph = graph_builder.build_graph() # Validate reachability report = builder.validator.validate_warehouse_model(model, graph=graph) assert report.is_valid is True assert len(report.errors) == 0 # Ensure all picking points are in the graph and reachable for loc_id in model.storage_locations: assert graph.has_node(f"pick_{loc_id}")