Files
Warehouse-Digital-Twin-and-…/tests/unit/test_heatmap.py
T

88 lines
3.5 KiB
Python

"""Unit tests for CongestionHeatmapGenerator (SVG, ASCII, and bottleneck diagnosis)."""
from pathlib import Path
import networkx as nx
import pytest
from warehouse_twin.analytics.heatmap import CongestionHeatmapGenerator
def _build_simple_test_graph() -> nx.DiGraph:
"""Build a minimal test graph with coordinates and stations."""
g = nx.DiGraph()
g.add_node("n1", pos=(10.0, 10.0, 0.0), station_id="pick_01")
g.add_node("n2", pos=(20.0, 10.0, 0.0))
g.add_node("n3", pos=(20.0, 20.0, 0.0), charger_id="ch_01")
g.add_edge("n1", "n2", distance=10.0)
g.add_edge("n2", "n1", distance=10.0)
g.add_edge("n2", "n3", distance=10.0)
g.add_edge("n3", "n2", distance=10.0)
return g
def test_heatmap_score_computation_and_colors() -> None:
"""Congestion scores should map into appropriate RGB color hex values."""
g = _build_simple_test_graph()
edge_metrics = [
{"source": "n1", "target": "n2", "traversals": 100, "conflicts": 20, "total_wait_time_seconds": 50.0},
{"source": "n2", "target": "n3", "traversals": 5, "conflicts": 0, "total_wait_time_seconds": 0.0},
]
gen = CongestionHeatmapGenerator(g, edge_metrics, width_meters=60.0, length_meters=40.0)
assert ("n1", "n2") in gen.edge_scores
score_hot = gen.edge_scores[("n1", "n2")]
score_cold = gen.edge_scores[("n2", "n3")]
assert score_hot > score_cold
assert gen.get_color_for_score(0.05) == "#22c55e" # Green
assert gen.get_color_for_score(0.85) == "#ef4444" # Red
def test_heatmap_bottleneck_diagnosis() -> None:
"""Diagnoses should flag severe corridors with critical severity and recommendations."""
g = _build_simple_test_graph()
edge_metrics = [
{"source": "n1", "target": "n2", "traversals": 50, "conflicts": 15, "total_wait_time_seconds": 45.0},
{"source": "n2", "target": "n3", "traversals": 20, "conflicts": 2, "total_wait_time_seconds": 5.0},
]
gen = CongestionHeatmapGenerator(g, edge_metrics)
diagnoses = gen.diagnose_bottlenecks(top_k=2)
assert len(diagnoses) == 2
top = diagnoses[0]
assert top["source"] == "n1"
assert top["target"] == "n2"
assert top["severity"] == "CRITICAL"
assert "bidirectional contention" in top["recommendation"].lower()
def test_heatmap_render_ascii() -> None:
"""ASCII render should return a non-empty string grid containing stations and legend."""
g = _build_simple_test_graph()
edge_metrics = [{"source": "n1", "target": "n2", "traversals": 30, "conflicts": 5, "total_wait_time_seconds": 12.0}]
gen = CongestionHeatmapGenerator(g, edge_metrics)
ascii_map = gen.render_ascii_heatmap()
assert "Warehouse Floorplan Congestion Heatmap" in ascii_map
assert "[S] Station" in ascii_map
assert "S" in ascii_map # station
assert "C" in ascii_map # charger
def test_heatmap_render_svg(tmp_path: Path) -> None:
"""SVG render should output a well-formed SVG XML file with graphic prims."""
g = _build_simple_test_graph()
edge_metrics = [{"source": "n1", "target": "n2", "traversals": 40, "conflicts": 8, "total_wait_time_seconds": 18.0}]
gen = CongestionHeatmapGenerator(g, edge_metrics)
svg_file = tmp_path / "test_heatmap.svg"
gen.render_svg_heatmap(svg_file)
assert svg_file.exists()
content = svg_file.read_text(encoding="utf-8")
assert "<svg" in content
assert "</svg>" in content
assert "Warehouse Traffic & Congestion Heatmap" in content
assert "pick_01" in content
assert "ch_01" in content