"""Unit tests for EventStore serialization and tabular CSV/JSONL exports.""" import json from pathlib import Path import pytest from warehouse_twin.analytics.kpi_calculator import ComprehensiveKPIReport from warehouse_twin.domain.events import SimulationEvent, SimulationEventType from warehouse_twin.domain.robot import Robot from warehouse_twin.domain.task import Task, TaskPriority, TaskStatus, TaskType from warehouse_twin.domain.warehouse import Position3D from warehouse_twin.storage.event_store import EventStore def test_event_store_exports_jsonl(tmp_path: Path) -> None: """EventStore should stream simulation events to JSONL.""" store = EventStore(tmp_path) events = [ SimulationEvent( event_id="evt_01", simulation_time=12.5, event_type=SimulationEventType.TASK_CREATED, entity_id="task_01", task_id="task_01", metadata={"priority": 1}, ), SimulationEvent( event_id="evt_02", simulation_time=15.0, event_type=SimulationEventType.ROBOT_DISPATCHED, entity_id="amr_01", task_id="task_01", ), ] jsonl_path = store.export_events_jsonl(events) assert jsonl_path.exists() loaded = store.load_events(jsonl_path) assert len(loaded) == 2 assert loaded[0]["simulation_time"] == 12.5 assert loaded[0]["event_type"] == "TASK_RELEASED" assert loaded[1]["entity_id"] == "amr_01" def test_event_store_exports_tasks_csv(tmp_path: Path) -> None: """EventStore should export task records with lifecycle timestamps to CSV.""" store = EventStore(tmp_path) tasks = [ Task( task_id="t100", task_type=TaskType.OUTBOUND_PICKING, source="rack_A", destination="pack_01", priority=1, status=TaskStatus.COMPLETED, release_time=10.0, start_time=14.0, completion_time=54.0, assigned_robot_id="amr_light_01", ) ] csv_path = store.export_tasks_csv(tasks, sim_duration=100.0) assert csv_path.exists() loaded_tasks = store.load_tasks(csv_path) assert len(loaded_tasks) == 1 t = loaded_tasks[0] assert t["task_id"] == "t100" assert t["task_type"] == "ORDER_PICK" assert t["waiting_time_seconds"] == 4.0 assert t["turnaround_time_seconds"] == 44.0 assert t["assigned_robot_id"] == "amr_light_01" def test_event_store_exports_robots_csv(tmp_path: Path) -> None: """EventStore should export robot metrics to CSV.""" store = EventStore(tmp_path) r = Robot( robot_id="amr_01", robot_type="heavy_amr", position=Position3D(0.0, 0.0, 0.0), maximum_speed=1.0, payload_capacity=1500.0, battery_level=12.0, battery_capacity=15.0, energy_consumption={"idle": 0.1, "moving_empty": 0.8, "moving_loaded": 2.0, "handling": 0.5}, accumulated_distance=500.0, loaded_distance=300.0, empty_distance=200.0, busy_time=250.0, charging_time=50.0, total_energy_consumed_kwh=1.25, total_energy_charged_kwh=3.5, ) csv_path = store.export_robots_csv({"amr_01": r}, sim_duration=1000.0) assert csv_path.exists() with open(csv_path, "r", encoding="utf-8") as f: content = f.read() assert "amr_01" in content assert "500.0" in content assert "300.0" in content assert "60.0" in content # loaded_ratio_percent (300/500 * 100) def test_event_store_exports_kpi_summary_json(tmp_path: Path) -> None: """EventStore should serialize ComprehensiveKPIReport to JSON.""" store = EventStore(tmp_path) rep = ComprehensiveKPIReport( duration_seconds=3600.0, tasks_created=100, tasks_completed=95, throughput_tasks_per_hour=95.0, ) json_path = store.export_kpi_summary_json(rep) assert json_path.exists() loaded = store.load_kpis(json_path) assert loaded["tasks_completed"] == 95 assert loaded["throughput_tasks_per_hour"] == 95.0