"""Unit tests for diagnostics system.""" import unittest import json from datetime import datetime class TestDiagnostics(unittest.TestCase): """Test cases for diagnostics node.""" def test_hardware_check_creation(self): """Test creation of hardware check results.""" checks = { "rplidar": {"status": "OK", "message": "RPLIDAR detected"}, "realsense": {"status": "ERROR", "message": "RealSense not found"}, "vesc": {"status": "WARN", "message": "VESC connection uncertain"}, } self.assertEqual(len(checks), 3) self.assertEqual(checks["rplidar"]["status"], "OK") self.assertEqual(checks["realsense"]["status"], "ERROR") def test_diagnostic_json_logging(self): """Test JSON logging of diagnostics.""" log_data = { "timestamp": datetime.now().isoformat(), "check_type": "startup_checks", "hardware_checks": { "rplidar": { "status": "OK", "message": "Device OK", "details": {"port": "/dev/ttyUSB0"}, }, "realsense": { "status": "ERROR", "message": "Device not found", "details": {}, }, }, "runtime_metrics": { "gpu_temp": {"status": "OK", "temperature_c": 65.0}, "network_latency": {"status": "WARN", "latency_ms": 150}, }, } # Should be JSON serializable json_str = json.dumps(log_data) parsed = json.loads(json_str) self.assertIn("timestamp", parsed) self.assertEqual(len(parsed["hardware_checks"]), 2) self.assertEqual(parsed["hardware_checks"]["rplidar"]["status"], "OK") def test_temperature_threshold_detection(self): """Test temperature threshold detection.""" thresholds = { "gpu_temp": {"warn": 80, "error": 85}, "vesc_temp": {"warn": 60, "error": 70}, } test_temps = [ (65, "OK"), (82, "WARN"), (88, "ERROR"), ] for temp, expected_status in test_temps: if temp < thresholds["gpu_temp"]["warn"]: status = "OK" elif temp < thresholds["gpu_temp"]["error"]: status = "WARN" else: status = "ERROR" self.assertEqual(status, expected_status) def test_diagnostic_aggregation(self): """Test aggregation of multiple diagnostics.""" hardware_checks = { "rplidar": "OK", "realsense": "OK", "vesc": "ERROR", "wifi": "OK", "gps": "WARN", } errors = [name for name, status in hardware_checks.items() if status == "ERROR"] warnings = [name for name, status in hardware_checks.items() if status == "WARN"] ok_items = [name for name, status in hardware_checks.items() if status == "OK"] self.assertEqual(len(errors), 1) self.assertEqual(len(warnings), 1) self.assertEqual(len(ok_items), 3) self.assertIn("vesc", errors) self.assertIn("gps", warnings) if __name__ == "__main__": unittest.main()