36 """Test configuration management"""
37 logger.info(
"\n" +
"=" * 80)
38 logger.info(
"TEST 1: Configuration Management")
43 config.mode = OperationMode.TEST
44 config.consensus_enabled =
True
45 config.save(
"test_config.json")
46 logger.info(
"✓ Config saved")
49 loaded_config = SystemConfig.load(
"test_config.json")
50 assert loaded_config.mode == OperationMode.TEST
51 assert loaded_config.consensus_enabled ==
True
52 logger.info(
"✓ Config loaded correctly")
55 Path(
"test_config.json").unlink()
56 logger.info(
"✅ Configuration management test PASSED")
60 """Test backup and recovery system"""
61 logger.info(
"\n" +
"=" * 80)
62 logger.info(
"TEST 2: Backup and Recovery")
66 config.backup_directory =
"./test_backups"
71 "population_mean": np.zeros(9).tolist(),
72 "population_covariance": np.diag([100.0] * 9).tolist(),
73 "population_strength": 5.0,
77 0: {
"mean": [0] * 9,
"covariance": np.eye(9).tolist()},
78 1: {
"mean": [0] * 9,
"covariance": np.eye(9).tolist()},
81 metadata = {
"test":
True,
"timestamp":
time.time()}
84 success = backup_manager.backup_calibration_state(
85 population_prior, pt_states, metadata
87 assert success,
"Backup failed"
88 logger.info(
"✓ Backup created")
91 backups = backup_manager.list_backups()
92 assert len(backups) > 0,
"No backups found"
93 logger.info(f
"✓ Found {len(backups)} backup(s)")
96 restored = backup_manager.restore_latest_backup()
97 assert restored
is not None,
"Restore failed"
98 assert "population_prior" in restored
99 assert "pt_states" in restored
100 logger.info(
"✓ Backup restored")
103 assert restored[
"population_prior"][
"population_strength"] == 5.0
104 logger.info(
"✓ Data integrity verified")
109 shutil.rmtree(
"./test_backups")
110 logger.info(
"✅ Backup and recovery test PASSED")
114 """Test validation system"""
115 logger.info(
"\n" +
"=" * 80)
116 logger.info(
"TEST 3: Validation System")
117 logger.info(
"=" * 80)
123 result = validator.validate_voltage(2.5, 0)
124 assert result.valid,
"Valid voltage rejected"
125 logger.info(
"✓ Valid voltage accepted")
127 result = validator.validate_voltage(15.0, 0)
128 assert not result.valid,
"Invalid voltage accepted"
129 logger.info(
"✓ Invalid voltage rejected")
131 result = validator.validate_voltage(np.nan, 0)
132 assert not result.valid,
"NaN voltage accepted"
133 logger.info(
"✓ NaN voltage rejected")
136 result = validator.validate_pressure(500.0, 0)
137 assert result.valid,
"Valid pressure rejected"
138 logger.info(
"✓ Valid pressure accepted")
140 result = validator.validate_pressure(2000.0, 0)
141 assert not result.valid,
"Invalid pressure accepted"
142 logger.info(
"✓ Invalid pressure rejected")
145 coeffs = np.array([0, 200, 0, 0, 0, 0, 0, 0, 0])
146 result = validator.validate_calibration_coefficients(coeffs, 0)
147 assert result.valid,
"Valid coefficients rejected"
148 logger.info(
"✓ Valid coefficients accepted")
150 coeffs_bad = np.array([0, 5000, 0, 0, 0, 0, 0, 0, 0])
151 result = validator.validate_calibration_coefficients(coeffs_bad, 0)
152 assert not result.valid,
"Invalid coefficients accepted"
153 logger.info(
"✓ Invalid coefficients rejected")
156 cov = np.eye(9) * 100
157 result = validator.validate_covariance_matrix(cov, 0)
158 assert result.valid,
"Valid covariance rejected"
159 logger.info(
"✓ Valid covariance accepted")
161 cov_bad = np.diag([1, 1, 1, -1, 1, 1, 1, 1, 1])
162 result = validator.validate_covariance_matrix(cov_bad, 0)
163 assert not result.valid,
"Invalid covariance accepted"
164 logger.info(
"✓ Invalid covariance rejected")
166 logger.info(
"✅ Validation system test PASSED")
170 """Test health monitoring system"""
171 logger.info(
"\n" +
"=" * 80)
172 logger.info(
"TEST 4: Health Monitoring")
173 logger.info(
"=" * 80)
176 config.log_directory =
"./test_logs"
183 mode=OperationMode.TEST,
185 calibrated_sensors=10 + i,
186 consensus_confidence=0.8 + i * 0.02,
187 average_uncertainty=5.0 - i * 0.5,
188 max_uncertainty=10.0 - i,
189 drift_detected_count=0,
192 uptime_seconds=i * 10.0,
194 health_monitor.record_metrics(metrics)
197 logger.info(
"✓ Metrics recorded")
200 status = health_monitor.get_current_status()
201 assert status[
"status"]
in (
"healthy",
"degraded"),
"Invalid status"
202 assert status[
"calibrated_sensors"] > 0,
"No calibrated sensors"
203 logger.info(f
"✓ System status: {status['status']}")
206 report = health_monitor.get_diagnostics_report()
207 assert len(report) > 0,
"Empty diagnostics report"
208 logger.info(
"✓ Diagnostics report generated")
214 if Path(
"./test_logs").exists():
215 shutil.rmtree(
"./test_logs")
216 logger.info(
"✅ Health monitoring test PASSED")
220 """Test anomaly detection system"""
221 logger.info(
"\n" +
"=" * 80)
222 logger.info(
"TEST 5: Anomaly Detection")
223 logger.info(
"=" * 80)
230 voltage = 2.5 + np.random.normal(0, 0.01)
231 anomaly = anomaly_detector.detect_voltage_anomaly(0, voltage)
232 assert not anomaly,
"Normal data flagged as anomaly"
233 logger.info(
"✓ Normal data accepted")
236 anomaly = anomaly_detector.detect_voltage_anomaly(0, 8.0)
237 assert anomaly,
"Anomaly not detected"
238 logger.info(
"✓ Voltage anomaly detected")
242 pressure = 500.0 + np.random.normal(0, 1.0)
243 anomaly_detector.detect_pressure_anomaly(0, pressure)
246 anomaly = anomaly_detector.detect_pressure_anomaly(0, 700.0)
247 assert anomaly,
"Pressure anomaly not detected"
248 logger.info(
"✓ Pressure anomaly detected")
251 report = anomaly_detector.get_anomaly_report()
252 assert 0
in report,
"Anomaly report missing sensor"
253 logger.info(f
"✓ Anomaly counts: {report}")
255 logger.info(
"✅ Anomaly detection test PASSED")
259 """Test mode switching"""
260 logger.info(
"\n" +
"=" * 80)
261 logger.info(
"TEST 6: Mode Switching")
262 logger.info(
"=" * 80)
267 manager.set_mode(OperationMode.TEST)
268 assert manager.config.mode == OperationMode.TEST
269 assert manager.config.consensus_enabled ==
True
270 logger.info(
"✓ TEST mode configured correctly")
273 manager.set_mode(OperationMode.FLIGHT)
274 assert manager.config.mode == OperationMode.FLIGHT
275 assert manager.config.consensus_enabled ==
False
276 logger.info(
"✓ FLIGHT mode configured correctly (consensus DISABLED)")
279 manager.set_mode(OperationMode.CALIBRATION)
280 assert manager.config.mode == OperationMode.CALIBRATION
281 assert manager.config.consensus_enabled ==
True
282 logger.info(
"✓ CALIBRATION mode configured correctly")
285 manager.set_mode(OperationMode.SAFE)
286 assert manager.config.mode == OperationMode.SAFE
287 logger.info(
"✓ SAFE mode configured correctly")
290 Path(
"system_config.json").unlink(missing_ok=
True)
291 logger.info(
"✅ Mode switching test PASSED")
329 print("\n" + "=" * 80)
330 print("ROBUSTNESS SYSTEM COMPREHENSIVE TEST SUITE")
334 ("Configuration Management", test_config_management),
335 ("Backup and Recovery", test_backup_and_recovery),
336 ("Validation System", test_validation),
337 ("Health Monitoring", test_health_monitoring),
338 ("Anomaly Detection", test_anomaly_detection),
339 ("Mode Switching", test_mode_switching),
340 ("Integrated Manager", test_integrated_robustness_manager),
346 for name, test_func in tests:
350 except Exception as e:
351 logger.error(f"❌ {name} test FAILED: {e}")
354 traceback.print_exc()
357 print("\n" + "=" * 80)
358 print("TEST SUMMARY")
360 print(f"Total tests: {len(tests)}")
361 print(f"Passed: {passed} ✅")
362 print(f"Failed: {failed} ❌")
363 print(f"Success rate: {passed/len(tests)*100:.0f}%")
364 print("=" * 80 + "\n")