108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
"""Unit tests for SimPy discrete-event simulation engine and metrics."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import pytest
|
|
|
|
from warehouse_twin.config.loader import ConfigLoader
|
|
from warehouse_twin.domain.inventory import InventoryCatalog
|
|
from warehouse_twin.domain.robot import Robot, RobotTypeSpec
|
|
from warehouse_twin.domain.warehouse import WarehouseModel
|
|
from warehouse_twin.simulation.dispatcher import DispatcherSettings
|
|
from warehouse_twin.simulation.order_generator import WorkloadConfig
|
|
from warehouse_twin.simulation.sim_engine import SimulationEnvironment, SimulationMetrics
|
|
|
|
|
|
@pytest.fixture
|
|
def sim_env_builder(project_config_dir: Path, tmp_path: Path):
|
|
def _create_env(seed: int = 42, event_log: bool = False):
|
|
loader = ConfigLoader(project_config_dir)
|
|
bundle = loader.load_all()
|
|
warehouse = WarehouseModel.from_config_dict(bundle.warehouse)
|
|
catalog = InventoryCatalog.from_config_dict(bundle.inventory)
|
|
|
|
type_specs = {
|
|
t_id: RobotTypeSpec.from_dict(t_id, t_data)
|
|
for t_id, t_data in bundle.robots["robot_types"].items()
|
|
}
|
|
fleet = [
|
|
Robot.from_fleet_dict(r_data, type_specs)
|
|
for r_data in bundle.robots["fleet"]
|
|
]
|
|
|
|
workload = WorkloadConfig.from_dict(bundle.simulation)
|
|
dispatch_settings = DispatcherSettings.from_dict(bundle.dispatch_policies)
|
|
log_file = tmp_path / f"events_seed_{seed}.jsonl" if event_log else None
|
|
|
|
return SimulationEnvironment(
|
|
warehouse=warehouse,
|
|
catalog=catalog,
|
|
fleet=fleet,
|
|
workload_config=workload,
|
|
dispatcher_settings=dispatch_settings,
|
|
event_log_path=log_file,
|
|
seed=seed,
|
|
)
|
|
return _create_env
|
|
|
|
|
|
def test_simulation_environment_creation(sim_env_builder):
|
|
env = sim_env_builder(seed=42)
|
|
assert len(env.robots) == 4
|
|
assert env.env.now == 0.0
|
|
assert len(env.all_tasks) == 0
|
|
|
|
|
|
def test_short_simulation_run(sim_env_builder):
|
|
env = sim_env_builder(seed=42)
|
|
metrics: SimulationMetrics = env.run(until=300.0)
|
|
|
|
assert metrics.duration_seconds == 300.0
|
|
assert metrics.tasks_created > 0
|
|
assert metrics.tasks_completed > 0
|
|
assert metrics.throughput_tasks_per_hour > 0.0
|
|
assert metrics.fleet_utilization_percent > 0.0
|
|
assert metrics.average_turnaround_time > 0.0
|
|
assert metrics.on_time_delivery_percent == 100.0
|
|
|
|
|
|
def test_deterministic_simulation_runs(sim_env_builder):
|
|
"""Verify that runs with identical random seed yield identical metrics and events."""
|
|
env1 = sim_env_builder(seed=42)
|
|
metrics1 = env1.run(until=600.0)
|
|
|
|
env2 = sim_env_builder(seed=42)
|
|
metrics2 = env2.run(until=600.0)
|
|
|
|
assert metrics1.tasks_created == metrics2.tasks_created
|
|
assert metrics1.tasks_completed == metrics2.tasks_completed
|
|
assert metrics1.throughput_tasks_per_hour == pytest.approx(metrics2.throughput_tasks_per_hour)
|
|
assert metrics1.average_turnaround_time == pytest.approx(metrics2.average_turnaround_time)
|
|
assert len(env1.events) == len(env2.events)
|
|
|
|
|
|
def test_simulation_event_logging(sim_env_builder, tmp_path: Path):
|
|
env = sim_env_builder(seed=42, event_log=True)
|
|
env.run(until=300.0)
|
|
|
|
log_file = tmp_path / "events_seed_42.jsonl"
|
|
assert log_file.exists()
|
|
assert log_file.stat().st_size > 500
|
|
|
|
lines = log_file.read_text(encoding="utf-8").strip().splitlines()
|
|
assert len(lines) > 10
|
|
|
|
|
|
def test_metrics_serialization(sim_env_builder):
|
|
env = sim_env_builder(seed=42)
|
|
metrics = env.run(until=300.0)
|
|
data = metrics.to_dict()
|
|
|
|
assert "tasks_created" in data
|
|
assert "tasks_completed" in data
|
|
assert "throughput_tasks_per_hour" in data
|
|
assert "fleet_utilization_percent" in data
|
|
assert "robot_metrics" in data
|
|
assert len(data["robot_metrics"]) == 4
|