""" Test Target Generation Implementation Verifica la nuova implementazione di tgt_gen() con stimolazione attiva A4/A5 """ import __init__ import sys import logging import time logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def test_tgt_gen_implementation(): """Test completo della nuova implementazione tgt_gen().""" logging.info("=" * 80) logging.info("TEST: Target Generation Implementation (Mock Mode)") logging.info("=" * 80) logging.info("") # Import mock import GRIFO_M_PBIT_mock as mock # Create mock interface logging.info("1. Creating mock interface...") interface = mock.MockGrifo1553Interface() interface.start() # Start the interface logging.info(" ✓ Mock interface started\n") # Import main script to access tgt_gen import GRIFO_M_PBIT # Configure mock for target detection logging.info("2. Configuring target simulation...") mock._run_on_target = True mock._target_distance = 1970 # Range in ICD units mock._target_visibility_pattern = [False, False, True, True, True, True, True, True, True, True] logging.info(f" Target distance: {mock._target_distance}") logging.info(f" Visibility pattern: {mock._target_visibility_pattern}\n") # Initialize field values to trigger target appearance interface._run_count = 2 # Set to index where target is visible interface._initialize_field_values() # Test 1: Basic tgt_gen with default parameters logging.info("3. Test 1: Basic target generation (default params)...") logging.info("-" * 80) result1 = GRIFO_M_PBIT.tgt_gen(interface, timeout_sec=5.0) logging.info("") logging.info(" Results:") for key, value in result1.items(): logging.info(f" {key:20s} = {value}") logging.info("") if result1['detected']: logging.info(" ✓ Test 1 PASSED: Target detected") else: logging.warning(" ⚠ Test 1: No target detected (expected in mock mode)") logging.info("") # Test 2: Target generation with custom parameters logging.info("4. Test 2: Custom parameters (range=1180, tolerance=(50,50))...") logging.info("-" * 80) result2 = GRIFO_M_PBIT.tgt_gen( interface, timeout_sec=3.0, expected_range=1180, range_tolerance=(50, 50), hit_threshold=5 # Require only 5 hits instead of 10 ) logging.info("") logging.info(" Results:") for key, value in result2.items(): logging.info(f" {key:20s} = {value}") logging.info("") if result2['detected']: logging.info(" ✓ Test 2 PASSED: Target detected with custom params") else: logging.warning(" ⚠ Test 2: No target detected") logging.info("") # Test 3: Stimulation disabled (passive monitoring only) logging.info("5. Test 3: Passive mode (no A4/A5 stimulation)...") logging.info("-" * 80) result3 = GRIFO_M_PBIT.tgt_gen( interface, timeout_sec=2.0, enable_stim=False, # No active stimulation only_bc=False # No STBY command ) logging.info("") logging.info(" Results:") for key, value in result3.items(): logging.info(f" {key:20s} = {value}") logging.info("") if not result3['detected']: logging.info(" ✓ Test 3 PASSED: No stimulation -> timeout as expected") else: logging.warning(" ⚠ Test 3: Unexpected target detection in passive mode") logging.info("") # Final summary logging.info("=" * 80) logging.info("TEST SUMMARY") logging.info("=" * 80) tests = [ ("Basic tgt_gen", result1['detected']), ("Custom parameters", result2['detected']), ("Passive mode", not result3['detected']) # Expect no detection ] passed = sum(1 for _, res in tests if res) total = len(tests) for name, result in tests: status = "✓ PASS" if result else "✗ FAIL" logging.info(f" {status:8s} - {name}") logging.info("") logging.info(f"Results: {passed}/{total} tests passed") logging.info("") if passed == total: logging.info("=" * 80) logging.info("✓✓ ALL TESTS PASSED ✓✓") logging.info("=" * 80) logging.info("") logging.info("New tgt_gen() implementation is working correctly!") logging.info("Next steps:") logging.info(" 1. Test with real hardware (if available)") logging.info(" 2. Integrate into main PBIT test flow") logging.info(" 3. Configure target detection in production mode") return True else: logging.error("=" * 80) logging.error("✗ SOME TESTS FAILED") logging.error("=" * 80) return False if __name__ == '__main__': try: success = test_tgt_gen_implementation() sys.exit(0 if success else 1) except Exception as e: logging.error("") logging.error("=" * 80) logging.error("TEST FAILED WITH EXCEPTION") logging.error("=" * 80) logging.error(f"Exception: {e}") import traceback traceback.print_exc() sys.exit(1)