"""Unit tests for emergency stop system."""
import unittest
import json
from datetime import datetime

class TestEmergencyStop(unittest.TestCase):
    def test_estop_trigger_creation(self):
        triggers = ["voice", "gamepad", "tilt", "obstacle", "geofence", "watchdog", "mqtt"]
        self.assertEqual(len(triggers), 7)

    def test_cascade_timing(self):
        """Test cascade timeout constraints."""
        cmd_vel_zero_ms = 50
        cascade_ms = 500
        
        self.assertLessEqual(cmd_vel_zero_ms, 50)
        self.assertLessEqual(cascade_ms, 500)

    def test_estop_event_logging(self):
        """Test e-stop event log format."""
        event = {
            "timestamp": datetime.now().isoformat(),
            "trigger": "voice",
            "state_transition": "active -> estop",
            "sensor_snapshot": {
                "tilt_angle_deg": 45.0,
                "imu_available": True,
                "scan_available": True,
            },
        }
        json_str = json.dumps(event)
        parsed = json.loads(json_str)
        
        self.assertEqual(parsed["trigger"], "voice")
        self.assertIn("timestamp", parsed)

if __name__ == "__main__":
    unittest.main()
