Files

181 lines
5.7 KiB
Python

"""Shared pytest fixtures for Warehouse Digital Twin."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Dict
import pytest
@pytest.fixture
def project_root() -> Path:
"""Return the absolute path to the project root directory."""
return Path(__file__).resolve().parents[1]
@pytest.fixture
def project_config_dir(project_root: Path) -> Path:
"""Return the path to the real project configuration directory."""
return project_root / "config"
@pytest.fixture
def temp_config_dir(tmp_path: Path) -> Path:
"""Create a temporary directory with valid configuration files for testing."""
config_dir = tmp_path / "config"
config_dir.mkdir(parents=True, exist_ok=True)
warehouse_data = {
"coordinate_system": {"type": "right_handed_cartesian", "units": "meters"},
"dimensions": {"width": 60.0, "depth": 40.0, "height": 8.0},
"zones": {
"zone_a": {"name": "Zone A"},
"zone_b": {"name": "Zone B"},
"zone_c": {"name": "Zone C"},
},
"stations": [{"station_id": "st_1", "type": "dock", "position": [1.0, 1.0, 0.0]}],
"charging_stations": [{"station_id": "ch_1", "position": [2.0, 2.0, 0.0], "capacity": 1}],
}
robots_data = {
"robot_types": {
"light_amr": {
"payload_capacity_kg": 150.0,
"max_speed_mps": 1.8,
"battery_capacity_kwh": 3.0,
}
},
"fleet": [
{
"robot_id": "amr_1",
"robot_type": "light_amr",
"initial_position": [0.0, 0.0, 0.0],
}
],
}
inventory_data = {
"categories": {
"zone_a_items": {
"name": "Zone A Items",
"items": [
{
"item_id": "item_1",
"name": "Filter HF-1",
"unit_weight_kg": 2.0,
"initial_stock": 100,
}
],
}
}
}
simulation_data = {
"simulation": {
"duration_seconds": 3600.0,
"random_seed": 42,
}
}
dispatch_data = {
"active_policy": "nearest_idle",
"available_policies": ["nearest_idle", "priority_first"],
}
for name, data in [
("warehouse.json", warehouse_data),
("robots.json", robots_data),
("inventory.json", inventory_data),
("simulation.json", simulation_data),
("dispatch_policies.json", dispatch_data),
]:
with open(config_dir / name, "w", encoding="utf-8") as f:
json.dump(data, f)
return config_dir
@pytest.fixture
def sample_robot_types():
"""Return dictionary of RobotTypeSpec instances."""
from tests.fixtures.sample_models import create_sample_robot_types
return create_sample_robot_types()
@pytest.fixture
def sample_light_robot():
"""Return an initialized Light AMR instance."""
from tests.fixtures.sample_models import create_sample_light_robot
return create_sample_light_robot()
@pytest.fixture
def sample_heavy_robot():
"""Return an initialized Heavy AMR instance."""
from tests.fixtures.sample_models import create_sample_heavy_robot
return create_sample_heavy_robot()
@pytest.fixture
def sample_tasks():
"""Return a dictionary of sample tasks across zones."""
from tests.fixtures.sample_models import create_sample_tasks
return create_sample_tasks()
@pytest.fixture
def sample_inventory_catalog():
"""Return an initialized InventoryCatalog instance."""
from tests.fixtures.sample_models import create_sample_inventory_catalog
return create_sample_inventory_catalog()
@pytest.fixture
def sample_warehouse_config(project_config_dir: Path) -> Dict[str, Any]:
"""Return the raw dictionary from project warehouse.json."""
with open(project_config_dir / "warehouse.json", "r", encoding="utf-8") as f:
return json.load(f)
@pytest.fixture
def sim_env_builder(project_config_dir: Path, tmp_path: Path):
"""Fixture builder providing initialized SimulationEnvironment instances."""
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
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