173 lines
6.2 KiB
Python
173 lines
6.2 KiB
Python
"""Unit tests for configuration loader and schema validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import pytest
|
|
|
|
from warehouse_twin.config.loader import ConfigLoader, ConfigurationError
|
|
|
|
|
|
def test_load_real_project_configurations(project_config_dir: Path):
|
|
"""Verify that the real project configurations in config/ load and validate cleanly."""
|
|
loader = ConfigLoader(config_dir=project_config_dir)
|
|
bundle = loader.load_all()
|
|
|
|
# Warehouse checks
|
|
assert bundle.warehouse["dimensions"]["width"] == 60.0
|
|
assert "zone_a" in bundle.warehouse["zones"]
|
|
assert "zone_b" in bundle.warehouse["zones"]
|
|
assert "zone_c" in bundle.warehouse["zones"]
|
|
assert len(bundle.warehouse["stations"]) >= 4
|
|
assert len(bundle.warehouse["charging_stations"]) >= 2
|
|
|
|
# Robot checks
|
|
assert "light_amr" in bundle.robots["robot_types"]
|
|
assert "heavy_amr" in bundle.robots["robot_types"]
|
|
assert len(bundle.robots["fleet"]) >= 4
|
|
|
|
# Inventory checks
|
|
assert len(bundle.inventory["categories"]) >= 3
|
|
# Check specific items from prompt
|
|
all_items = []
|
|
for cat in bundle.inventory["categories"].values():
|
|
all_items.extend([i["item_id"] for i in cat.get("items", [])])
|
|
assert any("filter" in i for i in all_items)
|
|
assert any("transmission" in i for i in all_items)
|
|
assert any("coil" in i for i in all_items)
|
|
|
|
# Simulation checks
|
|
assert bundle.simulation["simulation"]["duration_seconds"] > 0
|
|
assert bundle.simulation["simulation"]["random_seed"] == 42
|
|
|
|
# Dispatch checks
|
|
assert bundle.dispatch_policies["active_policy"] in bundle.dispatch_policies["available_policies"]
|
|
|
|
|
|
def test_missing_config_file_raises_error(tmp_path: Path):
|
|
"""Ensure missing required configuration file raises a clear ConfigurationError."""
|
|
empty_dir = tmp_path / "empty_cfg"
|
|
empty_dir.mkdir()
|
|
|
|
loader = ConfigLoader(config_dir=empty_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "Missing required configuration file" in str(exc_info.value)
|
|
|
|
|
|
def test_invalid_json_syntax_raises_error(temp_config_dir: Path):
|
|
"""Ensure malformed JSON syntax raises ConfigurationError with line details."""
|
|
broken_file = temp_config_dir / "warehouse.json"
|
|
broken_file.write_text("{ unquoted_bad_json: true, }", encoding="utf-8")
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "Invalid JSON syntax" in str(exc_info.value)
|
|
|
|
|
|
def test_missing_warehouse_zone_raises_error(temp_config_dir: Path):
|
|
"""Ensure omitting a mandatory zone (zone_a, zone_b, zone_c) fails validation."""
|
|
wh_file = temp_config_dir / "warehouse.json"
|
|
with open(wh_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
# Delete zone_c
|
|
del data["zones"]["zone_c"]
|
|
with open(wh_file, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "missing required zone specification: 'zone_c'" in str(exc_info.value)
|
|
|
|
|
|
def test_invalid_warehouse_dimensions_raises_error(temp_config_dir: Path):
|
|
"""Ensure non-positive dimensions trigger validation failure."""
|
|
wh_file = temp_config_dir / "warehouse.json"
|
|
with open(wh_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
data["dimensions"]["width"] = -10.0
|
|
with open(wh_file, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "must be a positive number" in str(exc_info.value)
|
|
|
|
|
|
def test_undefined_robot_type_raises_error(temp_config_dir: Path):
|
|
"""Ensure fleet member referencing an unknown robot_type fails validation."""
|
|
robots_file = temp_config_dir / "robots.json"
|
|
with open(robots_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
data["fleet"].append({
|
|
"robot_id": "alien_bot_99",
|
|
"robot_type": "quantum_antigravity_bot", # Not in robot_types
|
|
"initial_position": [0, 0, 0]
|
|
})
|
|
with open(robots_file, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "references undefined robot_type 'quantum_antigravity_bot'" in str(exc_info.value)
|
|
|
|
|
|
def test_duplicate_item_id_in_inventory_raises_error(temp_config_dir: Path):
|
|
"""Ensure duplicate item_id in inventory fails validation."""
|
|
inv_file = temp_config_dir / "inventory.json"
|
|
with open(inv_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
# Add duplicate item_1
|
|
data["categories"]["zone_a_items"]["items"].append({
|
|
"item_id": "item_1",
|
|
"name": "Duplicate Item 1",
|
|
})
|
|
with open(inv_file, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "Duplicate item_id 'item_1' detected" in str(exc_info.value)
|
|
|
|
|
|
def test_invalid_active_dispatch_policy_raises_error(temp_config_dir: Path):
|
|
"""Ensure active policy not present in available_policies raises error."""
|
|
disp_file = temp_config_dir / "dispatch_policies.json"
|
|
with open(disp_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
data["active_policy"] = "non_existent_policy"
|
|
with open(disp_file, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
|
|
loader = ConfigLoader(config_dir=temp_config_dir)
|
|
with pytest.raises(ConfigurationError) as exc_info:
|
|
loader.load_all()
|
|
|
|
assert "is not in available_policies" in str(exc_info.value)
|
|
|
|
|
|
def test_config_dir_env_var_override(temp_config_dir: Path, monkeypatch):
|
|
"""Verify that WAREHOUSE_TWIN_CONFIG_DIR env var is respected."""
|
|
monkeypatch.setenv("WAREHOUSE_TWIN_CONFIG_DIR", str(temp_config_dir))
|
|
loader = ConfigLoader() # No explicit config_dir passed
|
|
assert loader.config_dir == temp_config_dir.resolve()
|
|
bundle = loader.load_all()
|
|
assert bundle is not None
|