"""Unit tests for layout variations and facility topology optimization.""" from __future__ import annotations import json from pathlib import Path import pytest from warehouse_twin.domain.warehouse import Position3D, WarehouseModel from warehouse_twin.optimization.layout_optimizer import ( LAYOUT_REGISTRY, LayoutOptimizer, LayoutVariant, ) def test_layout_registry_definitions(): """Verify all 5 layout variants are defined with descriptions and rationales.""" for variant in LayoutVariant: assert variant in LAYOUT_REGISTRY spec = LAYOUT_REGISTRY[variant] assert spec.variant == variant assert len(spec.name) > 0 assert len(spec.description) > 0 assert len(spec.rationale) > 0 def test_decentralized_charging_layout_configuration(project_config_dir: Path): """Verify decentralized charging distributes bays across Zones A, B, and C.""" optimizer = LayoutOptimizer(project_config_dir) base_cfg = optimizer.get_warehouse_config(LayoutVariant.BASELINE) base_bays = base_cfg["charging_stations"] assert len(base_bays) == 8 # Baseline chargers are all grouped in the NW corner (x <= 16.0, y = 36.0) for b in base_bays: assert b["position"][0] <= 16.0 assert b["position"][1] == 36.0 decent_cfg = optimizer.get_warehouse_config(LayoutVariant.DECENTRALIZED_CHARGING) decent_bays = decent_cfg["charging_stations"] assert len(decent_bays) == 8 positions = [b["position"] for b in decent_bays] # Bay 5 should be in Zone B picking area (x=30.0, y=2.0) assert [30.0, 2.0, 0.0] in positions # Bay 7 should be in Zone C production feed area (x=48.0, y=36.0, non-overlapping with dropoff dock) assert [48.0, 36.0, 0.0] in positions def test_velocity_slotting_inventory_configuration(project_config_dir: Path): """Verify velocity slotting reassigns high-frequency SKUs to front racks.""" optimizer = LayoutOptimizer(project_config_dir) inv_cfg = optimizer.get_inventory_config(LayoutVariant.VELOCITY_SLOTTING) items = {} for cat in inv_cfg.get("categories", {}).values(): for item in cat.get("items", []): items[item["item_id"]] = item assert items["filter_hydraulic_hf1"]["storage_location"] == "rack_a_01" assert items["belt_drive_bd10"]["storage_location"] == "rack_a_03" assert items["tractor_hood_panel_th2"]["storage_location"] == "rack_a_05" def test_directional_highways_graph_modification(project_config_dir: Path): """Verify directional highways configure one-way flow on outer arterial corridors.""" optimizer = LayoutOptimizer(project_config_dir) model, graph = optimizer.build_graph_for_variant(LayoutVariant.DIRECTIONAL_HIGHWAYS) assert graph.number_of_nodes() > 100 assert graph.number_of_edges() > 100 # Test South highway (y=2.0): should have Eastbound edges, not Westbound south_nodes = [ n for n, d in graph.nodes(data=True) if round(float(d.get("y", 0.0)), 1) == 2.0 and d.get("node_type") == "waypoint" ] assert len(south_nodes) >= 10 eastbound_found = False for u in south_nodes: ux = round(float(graph.nodes[u]["x"]), 1) for v in graph.successors(u): if round(float(graph.nodes[v]["y"]), 1) == 2.0 and graph.nodes[v].get("node_type") == "waypoint": vx = round(float(graph.nodes[v]["x"]), 1) assert vx >= ux, f"Disallowed Westbound edge found on South highway: {u} -> {v}" if vx > ux: eastbound_found = True assert eastbound_found, "Expected Eastbound edges along South highway" def test_layout_comparison_simulation(project_config_dir: Path, tmp_path: Path): """Verify short layout comparison run across Baseline and Decentralized Charging.""" optimizer = LayoutOptimizer(project_config_dir) res = optimizer.compare_layouts( variants=[LayoutVariant.BASELINE, LayoutVariant.DECENTRALIZED_CHARGING], duration_seconds=120.0, seed=42, output_dir=tmp_path / "layouts", ) assert res.baseline_result is not None assert "decentralized_charging" in res.variant_results assert len(res.comparison_table) == 2 assert res.best_variant in ("baseline", "decentralized_charging") # Export layout configs cfgs = optimizer.export_layout_configs(tmp_path / "configs") assert len(cfgs) == len(LayoutVariant) assert (tmp_path / "configs" / "warehouse_baseline.json").exists() assert (tmp_path / "configs" / "warehouse_hybrid_optimal.json").exists() assert (tmp_path / "configs" / "warehouse_efficiency_optimized.json").exists()