#!/usr/bin/env python
"""Test script for ARC 1 Transition Engine (T001-T020)"""

import sys
sys.path.insert(0, r"C:\ProgramData\Ableton\Live 12 Suite\Resources\MIDI Remote Scripts\AbletonMCP_AI\AbletonMCP_AI\MCP_Server")

from transition_engine import (
    TransitionEngine, CrossfadeShape, FilterType,
    get_transition_engine, TRANSITION_TOOLS
)

def test_all_tools():
    print("=" * 60)
    print("ARC 1: Advanced Transition Engine - Test Suite")
    print("=" * 60)
    
    # Test basic functionality
    engine = get_transition_engine()
    print("\n[SETUP] Transition Engine created")
    
    # Test T001: Crossfade
    print("\n[T001] Testing Crossfade...")
    result = engine.apply_crossfade(0, 1, 16.0, 4.0, CrossfadeShape.EXPONENTIAL)
    assert result["shape"] == "exponential"
    assert result["out_curve_points"] > 0
    print(f"  Shape: {result['shape']}, Points: {result['out_curve_points']}")
    print("  PASSED")
    
    # Test T002: EQ Kill
    print("\n[T002] Testing EQ Kill...")
    result = engine.apply_eq_kill(0, "low", True)
    assert result["kill_type"] == "low"
    assert result["target_gain_db"] < 0
    print(f"  Type: {result['kill_type']}, Freq: {result['frequency']}Hz, Gain: {result['target_gain_db']}dB")
    print("  PASSED")
    
    # Test T003: Low-Kill Swap
    print("\n[T003] Testing Low-Kill Swap...")
    result = engine.automate_low_kill_swap(0, 1, 16.0, 2.0)
    assert len(result["schedule"]) == 3
    print(f"  Swap at bar {result['swap_bar']}, {len(result['schedule'])} schedule points")
    print("  PASSED")
    
    # Test T004: Filter Sweep
    print("\n[T004] Testing Filter Sweep...")
    result = engine.apply_filter_sweep(0, FilterType.HIGH_PASS, 16.0, 24.0, 200, 8000)
    assert result["filter_type"] == "high_pass"
    assert len(result["points"]) > 0
    print(f"  Type: {result['filter_type']}, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T005: Echo-Out
    print("\n[T005] Testing Echo-Out...")
    result = engine.apply_echo_out(0, 48.0, 4.0, 0.7, 0.375)
    assert result["effect"] == "echo_out"
    assert len(result["points"]) > 0
    print(f"  Duration: {result['duration_bars']} bars, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T006: Tempo Ramp
    print("\n[T006] Testing Tempo Ramp...")
    result = engine.apply_tempo_ramp(120.0, 130.0, 32.0, 8.0, "linear")
    assert result["start_bpm"] == 120.0
    assert result["end_bpm"] == 130.0
    assert len(result["points"]) > 0
    print(f"  {result['start_bpm']} -> {result['end_bpm']} BPM, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T007: Volume Fader
    print("\n[T007] Testing Volume Fader...")
    result = engine.apply_volume_fader(0, 16.0, 20.0, 0.85, 0.0)
    assert len(result["points"]) > 0
    print(f"  {result['start_volume']} -> {result['end_volume']}, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T008: Loop-to-Fade
    print("\n[T008] Testing Loop-to-Fade...")
    result = engine.apply_loop_to_fade(0, 16.0, 1.0, 4.0)
    assert len(result["actions"]) == 3
    print(f"  Loop: {result['loop_duration_bars']} bars, Actions: {len(result['actions'])}")
    print("  PASSED")
    
    # Test T009: Vinyl Stop
    print("\n[T009] Testing Vinyl Stop...")
    result = engine.apply_vinyl_stop(0, 60.0, 2.0, True)
    assert len(result["actions"]) > 0
    print(f"  Duration: {result['stop_duration_beats']} beats, Actions: {len(result['actions'])}")
    print("  PASSED")
    
    # Test T010: Gap Detection
    print("\n[T010] Testing Gap Detection...")
    result = engine.detect_transition_gaps([0, 1, 2], 16.0, 32.0, 0.25)
    assert "region" in result
    print(f"  Tracks: {result['tracks_analyzed']}, Duration: {result['region']['duration_bars']} bars")
    print("  PASSED")
    
    # Test T011: The Drop
    print("\n[T011] Testing Drop Transition...")
    result = engine.apply_drop_transition(0, 64.0, 1.0, 4.0)
    assert len(result["actions"]) == 3
    print(f"  Drop at bar {result['drop_bar']}, Actions: {len(result['actions'])}")
    print("  PASSED")
    
    # Test T012: Noise Riser
    print("\n[T012] Testing Noise Riser...")
    result = engine.generate_noise_riser(32.0, 8.0, "noise", 200, 8000, "medium")
    assert result["riser_type"] == "noise"
    assert len(result["points"]) > 0
    print(f"  Type: {result['riser_type']}, Points: {len(result['points'])}, Intensity: {result['intensity']}")
    print("  PASSED")
    
    # Test T013: Acapella Overlay
    print("\n[T013] Testing Acapella Overlay...")
    result = engine.apply_acapella_overlay(5, [1, 2, 3], 80.0, 16.0, True)
    assert len(result["actions"]) > 0
    print(f"  Vocal track: {result['vocal_track']}, Actions: {len(result['actions'])}")
    print("  PASSED")
    
    # Test T014: Stutter Edit
    print("\n[T014] Testing Stutter Edit...")
    result = engine.apply_stutter_edit(0, 40.0, 2.0, "1/8", True)
    assert result["stutter_division"] == "1/8"
    assert len(result["stutters"]) > 0
    print(f"  Division: {result['stutter_division']}, Stutters: {len(result['stutters'])}")
    print("  PASSED")
    
    # Test T015: Reverb Wash
    print("\n[T015] Testing Reverb Wash...")
    result = engine.apply_reverb_wash(0, 56.0, 4.0, 1.0, 8.0)
    assert len(result["points"]) > 0
    print(f"  Max wet: {result['max_wet']}, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T016: Impact/Crash
    print("\n[T016] Testing Impact/Crash Injection...")
    result = engine.inject_impact_crash(10, 64.0, "crash", "heavy", 0.0)
    assert result["impact_type"] == "crash"
    assert result["intensity"] == "heavy"
    print(f"  Type: {result['impact_type']}, Intensity: {result['intensity']}, Velocity: {result['velocity']}")
    print("  PASSED")
    
    # Test T017: Backspin
    print("\n[T017] Testing Backspin...")
    result = engine.apply_backspin(0, 96.0, 2.0, "exponential")
    assert len(result["points"]) > 0
    print(f"  Duration: {result['duration_beats']} beats, Points: {len(result['points'])}")
    print("  PASSED")
    
    # Test T018: Crossfade Shapes
    print("\n[T018] Testing Crossfade Shapes Reference...")
    result = engine.get_crossfade_shapes()
    assert len(result["available_shapes"]) == 6
    print(f"  Shapes: {len(result['available_shapes'])} available")
    print("  PASSED")
    
    # Test T019: Sub-Bass Ducking
    print("\n[T019] Testing Sub-Bass Ducking...")
    result = engine.apply_sub_bass_ducking(2, 0, -6.0, 5.0, 100.0)
    assert result["target_track"] == 2
    assert result["trigger_track"] == 0
    print(f"  Target: {result['target_track']}, Trigger: {result['trigger_track']}, Reduction: {result['reduction_db']}dB")
    print("  PASSED")
    
    # Test T020: Automated Mix
    print("\n[T020] Testing Automated Mix...")
    result = engine.create_automated_mix(10.0, 3, (120, 130), 32.0)
    assert result["duration_minutes"] == 10.0
    assert result["num_tracks"] == 3
    assert len(result["transitions"]) > 0
    print(f"  Duration: {result['duration_minutes']}min, Tracks: {result['num_tracks']}, Transitions: {len(result['transitions'])}")
    print("  PASSED")
    
    # Summary
    print("\n" + "=" * 60)
    print("ARC 1 TRANSITION ENGINE TEST SUMMARY")
    print("=" * 60)
    print(f"Total Tools: 20 (T001-T020)")
    print(f"Tools Implemented: 20")
    print(f"Tools Passed: 20")
    print(f"Status: ALL TESTS PASSED")
    print("=" * 60)
    print("\nARC 1 Implementation Complete!")
    print("Transition tools are ready for use in Ableton Live.")
    
    return True

if __name__ == "__main__":
    try:
        success = test_all_tools()
        sys.exit(0 if success else 1)
    except Exception as e:
        print(f"\n[ERROR] Test failed: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
