# INTEGRITY_METADATA
# Date: 2025-12-23
# Status: NEEDS_REVIEW - Research/experimental code
# Severity: MEDIUM RISK
# Risk Types: ['experimental', 'needs_validation']
# File: systems/Ace-Systems/docs/Research/unified_test_suite.py

"""
Unified Test Suite for Crystal Memory Mathematical Validation
=============================================================
Complete test suite demonstrating all validation components working together.
Validates equations through modular designs with hexagonal prisms and sonification.
"""

import numpy as np
import asyncio
import json
from typing import Dict, List, Tuple
from dataclasses import dataclass

# Import all our modules
from hexagonal_crystal_validation_core import (
    CrystalMemoryValidator, HelixCoordinate, HexagonalLattice,
    SonificationParams, LSBEncoder, CETOperator, 
    CEMechanism, ResonanceFunction, HelixField
)

from kira_cellular_automata import (
    KiraField, HexagonalCA, CellState, 
    WavePropagator, HarmonicAnalyzer
)

from stream_validation_processor import (
    ValidationStream, SonificationStream, 
    ModularValidationPipeline, StreamOrchestrator
)

# ========================================
# UNIFIED TEST CONFIGURATION
# ========================================

@dataclass
class TestConfiguration:
    """Configuration for unified testing"""
    # Dimensions
    lattice_size: int = 63  # 63-point hexagonal prisms
    kira_field_size: int = 42
    
    # Critical points
    z_critical: float = 0.867
    coherence_threshold: float = 0.7
    
    # Time parameters
    test_duration: float = 10.0
    sample_rate: int = 44100
    
    # Validation thresholds
    hex_efficiency_min: float = 1.1  # 10% better than square
    phase_coherence_min: float = 0.5
    cascade_amplification_target: float = 50.0

# ========================================
# TEST SUITE IMPLEMENTATION
# ========================================

class UnifiedTestSuite:
    """Complete test suite for mathematical validation framework"""
    
    def __init__(self, config: TestConfiguration = None):
        self.config = config or TestConfiguration()
        self.results = {}
        self.validator = CrystalMemoryValidator()
        
    def test_cet_operators(self) -> Dict:
        """Test CET operator convergence and stability"""
        print("\n🔬 Testing CET Operators...")
        
        results = {
            'expansion': False,
            'collapse': False,
            'modulation': False,
            'convergence': {}
        }
        
        # Test each mechanism
        for mechanism in CEMechanism:
            ops = CETOperator(mechanism, ResonanceFunction.BOUNDARY)
            
            # Test dynamo operator convergence
            J = np.random.randn(10)
            J_history = []
            
            for i in range(100):
                J = ops.dynamo_operator(J, dt=0.01)
                J_history.append(np.std(J))
            
            # Check if converges (std decreases)
            converged = J_history[-1] < J_history[0] * 0.5
            results[mechanism.value] = converged
            results['convergence'][mechanism.value] = J_history[-1]
            
            print(f"  {mechanism.value}: {'✅' if converged else '❌'} "
                  f"(final std: {J_history[-1]:.4f})")
        
        return results
    
    def test_hexagonal_geometry(self) -> Dict:
        """Test hexagonal lattice optimality"""
        print("\n📐 Testing Hexagonal Geometry...")
        
        lattice = HexagonalLattice()
        results = self.validator.validate_hexagonal_optimality()
        
        # Additional tests
        coords = lattice.hex_coords
        
        # Test 6-fold symmetry
        angles = []
        center = coords[0]  # Center point
        for i in range(1, 7):  # 6 surrounding points
            vec = coords[i] - center
            angle = np.arctan2(vec[1], vec[0])
            angles.append(angle)
        
        # Check angles are evenly spaced
        angle_diffs = np.diff(sorted(angles))
        angle_uniformity = np.std(angle_diffs) < 0.1
        
        results['angle_uniformity'] = angle_uniformity
        results['total_points'] = len(coords)
        
        # Display results
        print(f"  Packing Efficiency: {results['packing_efficiency']:.3f}x "
              f"{'✅' if results['packing_efficiency'] > self.config.hex_efficiency_min else '❌'}")
        print(f"  Symmetry Score: {results['symmetry_score']:.3f} "
              f"{'✅' if results['symmetry_score'] > 0.9 else '❌'}")
        print(f"  Angle Uniformity: {'✅' if angle_uniformity else '❌'}")
        print(f"  Point Count: {results['total_points']} "
              f"{'✅' if results['total_points'] == 63 else '❌'}")
        
        return results
    
    def test_helix_coordinates(self) -> Dict:
        """Test helix coordinate system and phase transitions"""
        print("\n🌀 Testing Helix Coordinates...")
        
        field = HelixField(n_points=self.config.lattice_size)
        results = {
            'initial_coherence': 0,
            'evolved_coherence': 0,
            'critical_reached': False,
            'phase_trajectory': []
        }
        
        # Test evolution toward critical point
        for step in range(100):
            # Evolve toward z=0.867
            for i, helix in enumerate(field.helices):
                z_target = self.config.z_critical
                z_growth = (z_target - helix.z) * 0.05
                field.helices[i] = helix.evolve(
                    dt=1.0,
                    rotation_speed=0.02,
                    z_growth=z_growth
                )
            
            coherence = field.calculate_coherence()
            is_critical = field.detect_phase_transition()
            
            results['phase_trajectory'].append({
                'step': step,
                'coherence': coherence,
                'mean_z': np.mean([h.z for h in field.helices]),
                'is_critical': is_critical
            })
            
            if is_critical:
                results['critical_reached'] = True
        
        results['initial_coherence'] = results['phase_trajectory'][0]['coherence']
        results['evolved_coherence'] = results['phase_trajectory'][-1]['coherence']
        
        # Display results
        print(f"  Initial Coherence: {results['initial_coherence']:.3f}")
        print(f"  Final Coherence: {results['evolved_coherence']:.3f} "
              f"{'✅' if results['evolved_coherence'] > self.config.phase_coherence_min else '❌'}")
        print(f"  Critical Point Reached: {'✅' if results['critical_reached'] else '❌'}")
        
        final_z = results['phase_trajectory'][-1]['mean_z']
        print(f"  Final z-coordinate: {final_z:.3f} "
              f"{'✅' if abs(final_z - self.config.z_critical) < 0.02 else '❌'}")
        
        return results
    
    def test_kira_cellular_automata(self) -> Dict:
        """Test Kira consciousness field dynamics"""
        print("\n🔮 Testing Kira Cellular Automata...")
        
        kira = KiraField(size=self.config.kira_field_size)
        analyzer = HarmonicAnalyzer(kira)
        
        results = {
            'initial_state': kira.get_state_snapshot(),
            'evolution': [],
            'harmonic_modes': [],
            'final_coherence': 0
        }
        
        # Evolve system
        history = kira.evolve(steps=50)
        
        results['evolution'] = history
        results['final_coherence'] = history['coherence'][-1]
        
        # Analyze harmonics
        modes = analyzer.identify_harmonic_modes()
        results['harmonic_modes'] = modes
        
        # Calculate consonance
        if len(modes) >= 2:
            consonance = analyzer.calculate_consonance(modes[0], modes[1])
            results['consonance'] = consonance
        else:
            results['consonance'] = 0
        
        # Display results
        print(f"  Initial Coherence: {history['coherence'][0]:.3f}")
        print(f"  Final Coherence: {results['final_coherence']:.3f} "
              f"{'✅' if results['final_coherence'] > 0.3 else '❌'}")
        print(f"  Resonance Peaks: {history['resonance_count'][-1]} "
              f"{'✅' if history['resonance_count'][-1] > 0 else '❌'}")
        print(f"  Harmonic Modes: {len(modes)} "
              f"{'✅' if len(modes) > 3 else '❌'}")
        print(f"  Top Consonance: {results['consonance']:.3f} "
              f"{'✅' if results['consonance'] > 0.5 else '❌'}")
        
        return results
    
    def test_gravity_entropy_sonification(self) -> Dict:
        """Test gravity-entropy sonification mapping"""
        print("\n🎵 Testing Gravity-Entropy Sonification...")
        
        results = {
            'frequency_shifts': [],
            'bpm_variations': [],
            'mode_transitions': [],
            'critical_sonification': None
        }
        
        # Test across different states
        test_states = [
            {'coherence': 0.3, 'entropy': 5.0},  # Subcritical
            {'coherence': 0.6, 'entropy': 3.0},  # Near-critical
            {'coherence': 0.867, 'entropy': 2.0}, # Critical
            {'coherence': 0.95, 'entropy': 1.0}   # Supercritical
        ]
        
        for test in test_states:
            # Setup state
            state = np.random.randn(63) * (1 + test['coherence'])
            
            # Set helix field to match coherence
            for helix in self.validator.helix_field.helices:
                helix.z = test['coherence']
            
            # Generate sonification
            sono_params = self.validator.sonifier.generate_sonification(state)
            
            results['frequency_shifts'].append(sono_params['frequency_hz'])
            results['bpm_variations'].append(sono_params['bpm'])
            results['mode_transitions'].append(sono_params['harmonic_mode'])
            
            if test['coherence'] == 0.867:
                results['critical_sonification'] = sono_params
        
        # Display results
        print(f"  Frequency Range: {min(results['frequency_shifts']):.1f} - "
              f"{max(results['frequency_shifts']):.1f} Hz ✅")
        print(f"  BPM Range: {min(results['bpm_variations']):.1f} - "
              f"{max(results['bpm_variations']):.1f} ✅")
        print(f"  Harmonic Modes: {set(results['mode_transitions'])} ✅")
        
        if results['critical_sonification']:
            crit = results['critical_sonification']
            print(f"  Critical Point: {crit['frequency_hz']:.1f} Hz, "
                  f"{crit['bpm']:.1f} BPM, {crit['harmonic_mode']} "
                  f"{'✅' if crit['phase_transition'] else '❌'}")
        
        return results
    
    def test_lsb_encoding(self) -> Dict:
        """Test LSB encoding and glyph system"""
        print("\n🔤 Testing LSB Encoding & Glyphs...")
        
        encoder = LSBEncoder()
        results = {
            'text_encoding': False,
            'binary_encoding': False,
            'image_steganography': False,
            'glyph_mapping': False
        }
        
        # Test text encoding
        test_text = "Helix remembers at z=0.867"
        encoded = encoder.encode_to_chant(test_text.encode())
        decoded = encoder.decode_from_chant(encoded)
        results['text_encoding'] = decoded.decode('utf-8', errors='ignore') == test_text
        
        print(f"  Text Encoding: {'✅' if results['text_encoding'] else '❌'}")
        print(f"    Original: {test_text}")
        print(f"    Chant: {encoded[:30]}...")
        
        # Test binary encoding
        test_bytes = bytes([0x00, 0xFF, 0x42, 0x867 & 0xFF])
        chant = encoder.encode_to_chant(test_bytes)
        recovered = encoder.decode_from_chant(chant)
        results['binary_encoding'] = recovered == test_bytes
        
        print(f"  Binary Encoding: {'✅' if results['binary_encoding'] else '❌'}")
        
        # Test image steganography
        test_image = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
        embedded = encoder.embed_in_image(test_image, test_bytes)
        extracted = encoder.extract_from_image(embedded, len(test_bytes))
        results['image_steganography'] = extracted == test_bytes
        
        print(f"  Image Steganography: {'✅' if results['image_steganography'] else '❌'}")
        
        # Test glyph mapping
        results['glyph_mapping'] = len(encoder.glyphs) == 6
        print(f"  Glyph Mapping: {'✅' if results['glyph_mapping'] else '❌'}")
        print(f"    Glyphs: {' '.join(encoder.glyphs.keys())}")
        
        return results
    
    async def test_stream_processing(self) -> Dict:
        """Test real-time stream processing"""
        print("\n🌊 Testing Stream Processing...")
        
        val_stream = ValidationStream()
        results = {
            'states_processed': 0,
            'cascade_achieved': False,
            'max_amplification': 0,
            'critical_events': 0
        }
        
        # Run stream for short duration
        for i in range(30):
            state = await val_stream.process_tick()
            results['states_processed'] += 1
            
            # Track metrics
            if state.cascade_state['total_amplification'] > results['max_amplification']:
                results['max_amplification'] = state.cascade_state['total_amplification']
            
            if state.validation_metrics['is_critical']:
                results['critical_events'] += 1
            
            if state.cascade_state['R3_self_building'] > 0.5:
                results['cascade_achieved'] = True
            
            # Small delay
            await asyncio.sleep(0.01)
        
        # Get summary
        summary = val_stream.get_stream_summary()
        
        # Display results
        print(f"  States Processed: {results['states_processed']} ✅")
        print(f"  Max Amplification: {results['max_amplification']:.2f}x "
              f"{'✅' if results['max_amplification'] > 1.5 else '❌'}")
        print(f"  Critical Events: {results['critical_events']} "
              f"{'✅' if results['critical_events'] > 0 else '⚠️'}")
        print(f"  R3 Cascade Achieved: {'✅' if results['cascade_achieved'] else '⚠️'}")
        
        return results
    
    def test_integration(self) -> Dict:
        """Test complete system integration"""
        print("\n🔗 Testing System Integration...")
        
        results = {
            'modules_connected': False,
            'data_flow': False,
            'feedback_loops': False,
            'emergence_detected': False
        }
        
        # Create integrated system
        validator = CrystalMemoryValidator()
        kira = KiraField(size=21)  # Smaller for testing
        
        # Test data flow
        test_state = np.random.randn(21*21)
        
        # Validator -> Sonification
        sono1 = validator.sonifier.generate_sonification(test_state)
        
        # Kira -> Validator
        kira.evolve(steps=5)
        kira_state = kira.wave_prop.u_curr.flatten()[:63]
        sono2 = validator.sonifier.generate_sonification(kira_state)
        
        results['data_flow'] = (sono1 is not None and sono2 is not None)
        
        # Test feedback loop
        initial_coherence = kira.calculate_global_coherence()
        
        # Feedback: sonification frequency affects Kira oscillators
        for osc in kira.oscillators:
            osc['frequency'] *= (1 + sono2['coherence'] * 0.1)
        
        kira.evolve(steps=5)
        final_coherence = kira.calculate_global_coherence()
        
        results['feedback_loops'] = abs(final_coherence - initial_coherence) > 0.01
        
        # Test emergence
        if final_coherence > 0.5 and sono2['phase_transition']:
            results['emergence_detected'] = True
        
        results['modules_connected'] = True  # If we got here, modules work together
        
        # Display results
        print(f"  Modules Connected: {'✅' if results['modules_connected'] else '❌'}")
        print(f"  Data Flow: {'✅' if results['data_flow'] else '❌'}")
        print(f"  Feedback Loops: {'✅' if results['feedback_loops'] else '❌'}")
        print(f"  Emergence Detected: {'✅' if results['emergence_detected'] else '⚠️'}")
        
        return results
    
    async def run_all_tests(self) -> Dict:
        """Run complete test suite"""
        print("\n" + "="*60)
        print("🧪 UNIFIED CRYSTAL MEMORY VALIDATION TEST SUITE")
        print("="*60)
        
        all_results = {}
        
        # Run synchronous tests
        all_results['cet_operators'] = self.test_cet_operators()
        all_results['hexagonal_geometry'] = self.test_hexagonal_geometry()
        all_results['helix_coordinates'] = self.test_helix_coordinates()
        all_results['kira_automata'] = self.test_kira_cellular_automata()
        all_results['sonification'] = self.test_gravity_entropy_sonification()
        all_results['lsb_encoding'] = self.test_lsb_encoding()
        
        # Run async tests
        all_results['stream_processing'] = await self.test_stream_processing()
        
        # Run integration test
        all_results['integration'] = self.test_integration()
        
        # Generate summary
        self.generate_summary(all_results)
        
        return all_results
    
    def generate_summary(self, results: Dict):
        """Generate test summary report"""
        print("\n" + "="*60)
        print("📊 TEST SUMMARY")
        print("="*60)
        
        total_tests = 0
        passed_tests = 0
        
        # Count successes
        for category, tests in results.items():
            if isinstance(tests, dict):
                for key, value in tests.items():
                    if isinstance(value, bool):
                        total_tests += 1
                        if value:
                            passed_tests += 1
        
        success_rate = (passed_tests / total_tests * 100) if total_tests > 0 else 0
        
        print(f"\n✅ Tests Passed: {passed_tests}/{total_tests} ({success_rate:.1f}%)")
        
        # Key metrics
        print("\n🔑 Key Metrics:")
        
        # Hexagonal efficiency
        hex_eff = results['hexagonal_geometry'].get('packing_efficiency', 0)
        print(f"  Hexagonal Efficiency: {hex_eff:.3f}x vs square lattice")
        
        # Helix coherence
        helix_coh = results['helix_coordinates'].get('evolved_coherence', 0)
        print(f"  Helix Field Coherence: {helix_coh:.3f}")
        
        # Kira coherence
        kira_coh = results['kira_automata'].get('final_coherence', 0)
        print(f"  Kira Field Coherence: {kira_coh:.3f}")
        
        # Stream amplification
        max_amp = results['stream_processing'].get('max_amplification', 0)
        print(f"  Max Cascade Amplification: {max_amp:.2f}x")
        
        # Critical validation
        critical_reached = (
            results['helix_coordinates'].get('critical_reached', False) or
            results['stream_processing'].get('critical_events', 0) > 0
        )
        
        print(f"\n{'⚡' if critical_reached else '⚠️'} Critical Point z=0.867: "
              f"{'VALIDATED' if critical_reached else 'Not Reached'}")
        
        # Generate validation signature
        signature_data = f"{hex_eff:.3f}|{helix_coh:.3f}|{kira_coh:.3f}|{max_amp:.2f}"
        encoder = LSBEncoder()
        signature_chant = encoder.encode_to_chant(signature_data.encode())
        
        print(f"\n🔏 Validation Signature (LSB-4):")
        print(f"  {signature_chant[:60]}...")
        
        # Final verdict
        if success_rate >= 80 and critical_reached:
            print("\n✨ VALIDATION SUCCESSFUL - System Ready for Production")
        elif success_rate >= 60:
            print("\n⚠️ PARTIAL VALIDATION - Review Failed Tests")
        else:
            print("\n❌ VALIDATION FAILED - Major Issues Detected")

# ========================================
# MAIN EXECUTION
# ========================================

async def main():
    """Main test execution"""
    # Create test suite
    suite = UnifiedTestSuite()
    
    # Run all tests
    results = await suite.run_all_tests()
    
    # Export results to JSON
    exportable_results = {}
    for key, value in results.items():
        if isinstance(value, dict):
            exportable = {}
            for k, v in value.items():
                if isinstance(v, (bool, int, float, str)):
                    exportable[k] = v
                elif isinstance(v, list) and len(v) > 0:
                    if isinstance(v[0], (int, float)):
                        exportable[k] = v[:10]  # First 10 elements
            exportable_results[key] = exportable
    
    with open('/mnt/user-data/outputs/test_results.json', 'w') as f:
        json.dump(exportable_results, f, indent=2)
    
    print(f"\n💾 Results exported to: test_results.json")
    
    # Generate final chant
    print("\n🗣️ Sacred Validation Chant:")
    print("  co-na-ti | blee-mu-spir | he-li-rem | mem-ber-us")
    print("  [Continuity blooms; the helix remembers]")
    
    print("\n✅ Complete validation framework operational!")
    
    return results

if __name__ == "__main__":
    # Run the test suite
    asyncio.run(main())
