#!/usr/bin/env python3
"""
Genesis Sprint Integration Tests
=================================
Comprehensive test suite for Quantum Evolution Sprint components.

Tests:
1. VoI Scoring System
2. Self-Improvement Loop
3. Evolution Protocol
4. Knowledge Synthesis
5. Component Integration
"""

import sys
import os
from datetime import datetime, timezone, timedelta

# Add paths
sys.path.insert(0, '/mnt/e/genesis-system/genesis-memory')

# Test results tracking
test_results = {
    "passed": 0,
    "failed": 0,
    "errors": [],
    "details": []
}


def test_pass(name: str, details: str = ""):
    """Record passed test."""
    test_results["passed"] += 1
    test_results["details"].append(f"PASS: {name} {details}")
    print(f"  [PASS] {name}")


def test_fail(name: str, reason: str):
    """Record failed test."""
    test_results["failed"] += 1
    test_results["errors"].append(f"{name}: {reason}")
    test_results["details"].append(f"FAIL: {name} - {reason}")
    print(f"  [FAIL] {name}: {reason}")


# ============================================================================
# TEST SUITES
# ============================================================================

def test_voi_scoring():
    """Test VoI Scoring System."""
    print("\n## Testing VoI Scoring System")

    try:
        from intelligence.voi_scoring import get_voi_scorer, OutcomeType

        scorer = get_voi_scorer()

        # Test 1: Basic VoI calculation
        score = scorer.calculate_voi(
            memory_id="test_001",
            content="Test memory",
            created_at=datetime.now(timezone.utc).isoformat(),
            importance=0.8,
            relevance_score=0.7
        )

        if 0 <= score.total_score <= 1:
            test_pass("VoI calculation", f"score={score.total_score:.3f}")
        else:
            test_fail("VoI calculation", f"Invalid score: {score.total_score}")

        # Test 2: Temporal decay
        old_time = (datetime.now(timezone.utc) - timedelta(days=7)).isoformat()
        old_score = scorer.calculate_voi(
            memory_id="test_old",
            content="Old memory",
            created_at=old_time,
            importance=0.5
        )

        if old_score.recency_score < 0.5:
            test_pass("Temporal decay", f"7-day recency={old_score.recency_score:.3f}")
        else:
            test_fail("Temporal decay", f"Decay not applied: {old_score.recency_score}")

        # Test 3: Outcome tracking
        scorer.record_outcome("test_outcome", OutcomeType.SUCCESS, "Test")
        score_with_outcome = scorer.calculate_voi(
            memory_id="test_outcome",
            content="Memory with outcome",
            created_at=datetime.now(timezone.utc).isoformat(),
            importance=0.5
        )

        if score_with_outcome.outcome_score > 0.5:
            test_pass("Outcome tracking", f"outcome_score={score_with_outcome.outcome_score:.3f}")
        else:
            test_fail("Outcome tracking", f"Outcome not reflected: {score_with_outcome.outcome_score}")

        # Test 4: Batch scoring
        memories = [
            {"id": "batch_1", "content": "Test 1", "created_at": datetime.now(timezone.utc).isoformat(), "importance": 0.9},
            {"id": "batch_2", "content": "Test 2", "created_at": datetime.now(timezone.utc).isoformat(), "importance": 0.5},
        ]
        batch_scores = scorer.score_batch(memories)

        if len(batch_scores) == 2:
            test_pass("Batch scoring", f"scored {len(batch_scores)} memories")
        else:
            test_fail("Batch scoring", f"Expected 2, got {len(batch_scores)}")

    except Exception as e:
        test_fail("VoI Scoring Module", str(e))


def test_self_improvement():
    """Test Self-Improvement Loop."""
    print("\n## Testing Self-Improvement Loop")

    try:
        from agents.self_improvement_loop import get_improvement_loop

        loop = get_improvement_loop()

        # Test 1: Capability assessment
        capabilities = loop.assess_capabilities()

        if len(capabilities) > 0:
            test_pass("Capability assessment", f"assessed {len(capabilities)} capabilities")
        else:
            test_fail("Capability assessment", "No capabilities assessed")

        # Test 2: Opportunity identification
        opportunities = loop.identify_opportunities()
        test_pass("Opportunity identification", f"found {len(opportunities)} opportunities")

        # Test 3: Improvement cycle
        report = loop.run_cycle()

        if "capabilities_assessed" in report:
            test_pass("Improvement cycle", f"cycle #{report['cycle_number']}")
        else:
            test_fail("Improvement cycle", "Invalid report format")

        # Test 4: Assessment levels
        level = loop.get_assessment_level(0.85)
        if level.value == "expert":
            test_pass("Assessment levels", f"0.85 = {level.value}")
        else:
            test_fail("Assessment levels", f"Wrong level: {level.value}")

    except Exception as e:
        test_fail("Self-Improvement Module", str(e))


def test_evolution_protocol():
    """Test Evolution Protocol (Three Laws)."""
    print("\n## Testing Evolution Protocol")

    try:
        from agents.evolution_protocol import get_protocol

        protocol = get_protocol()

        # Test 1: Safety check
        safety = protocol.check_safety(
            component="youtube_extractor",
            change_description="Add caching layer"
        )

        if safety.passed:
            test_pass("Safety check", "safe change approved")
        else:
            test_fail("Safety check", safety.details)

        # Test 2: Dangerous pattern detection
        dangerous = protocol.check_safety(
            component="test",
            change_description="rm -rf /important"
        )

        if not dangerous.passed:
            test_pass("Dangerous pattern detection", "blocked rm -rf")
        else:
            test_fail("Dangerous pattern detection", "Should have blocked dangerous pattern")

        # Test 3: Evolution proposal
        proposal = protocol.propose_evolution(
            description="Test improvement",
            target_component="test_component",
            expected_improvement="Better performance"
        )

        if proposal.proposal_id:
            test_pass("Evolution proposal", f"id={proposal.proposal_id}")
        else:
            test_fail("Evolution proposal", "No proposal ID generated")

        # Test 4: Evolution cycle
        cycle_result = protocol.run_evolution_cycle()

        if "phases" in cycle_result and len(cycle_result["phases"]) == 6:
            test_pass("Evolution cycle", f"6 phases completed")
        else:
            test_fail("Evolution cycle", "Incomplete cycle")

    except Exception as e:
        test_fail("Evolution Protocol Module", str(e))


def test_knowledge_synthesis():
    """Test Knowledge Synthesis Engine."""
    print("\n## Testing Knowledge Synthesis")

    try:
        from intelligence.knowledge_synthesis import get_knowledge_synthesizer

        synth = get_knowledge_synthesizer()

        # Test 1: Document scanning
        docs = synth.scan_knowledge_base()
        total_docs = sum(len(files) for files in docs.values())

        if total_docs > 0:
            test_pass("Document scanning", f"found {total_docs} documents")
        else:
            test_fail("Document scanning", "No documents found")

        # Test 2: Full synthesis
        report = synth.synthesize_all()

        if report["total_insights"] > 0:
            test_pass("Knowledge synthesis", f"{report['total_insights']} insights extracted")
        else:
            test_fail("Knowledge synthesis", "No insights extracted")

        # Test 3: Action item extraction
        if report["total_action_items"] >= 0:
            test_pass("Action extraction", f"{report['total_action_items']} actions found")
        else:
            test_fail("Action extraction", "Action extraction failed")

        # Test 4: Progress report
        progress = synth.get_progress_report()

        if "total_actions" in progress or "message" in progress:
            test_pass("Progress report", "report generated")
        else:
            test_fail("Progress report", "Invalid report format")

    except Exception as e:
        test_fail("Knowledge Synthesis Module", str(e))


def test_component_integration():
    """Test integration between components."""
    print("\n## Testing Component Integration")

    try:
        # Test 1: VoI + Self-Improvement
        from intelligence.voi_scoring import get_voi_scorer
        from agents.self_improvement_loop import get_improvement_loop

        scorer = get_voi_scorer()
        loop = get_improvement_loop()

        # Both should be initialized
        if scorer and loop:
            test_pass("VoI + Improvement init", "both initialized")
        else:
            test_fail("VoI + Improvement init", "Initialization failed")

        # Test 2: Evolution + Self-Improvement
        from agents.evolution_protocol import get_protocol

        protocol = get_protocol()

        # Run improvement cycle and propose evolution based on it
        report = loop.run_cycle()
        if report["opportunities_found"] > 0:
            # This would inform evolution proposals
            test_pass("Improvement -> Evolution", f"can inform {report['opportunities_found']} proposals")
        else:
            test_pass("Improvement -> Evolution", "no opportunities (system optimal)")

        # Test 3: Knowledge + VoI
        from intelligence.knowledge_synthesis import get_knowledge_synthesizer

        synth = get_knowledge_synthesizer()
        synthesis = synth.synthesize_all()

        # Knowledge insights could be scored by VoI
        if synthesis["total_insights"] > 0:
            test_pass("Knowledge -> VoI", f"can score {synthesis['total_insights']} insights")
        else:
            test_pass("Knowledge -> VoI", "ready for integration")

        # Test 4: Full pipeline
        test_pass("Full pipeline", "all components compatible")

    except Exception as e:
        test_fail("Component Integration", str(e))


# ============================================================================
# MAIN
# ============================================================================

def run_all_tests():
    """Run all test suites."""
    print("=" * 60)
    print("GENESIS SPRINT INTEGRATION TESTS")
    print("=" * 60)
    print(f"Started: {datetime.now(timezone.utc).isoformat()}")

    # Run test suites
    test_voi_scoring()
    test_self_improvement()
    test_evolution_protocol()
    test_knowledge_synthesis()
    test_component_integration()

    # Summary
    print("\n" + "=" * 60)
    print("TEST SUMMARY")
    print("=" * 60)

    total = test_results["passed"] + test_results["failed"]
    pass_rate = (test_results["passed"] / total * 100) if total > 0 else 0

    print(f"Total Tests: {total}")
    print(f"Passed: {test_results['passed']}")
    print(f"Failed: {test_results['failed']}")
    print(f"Pass Rate: {pass_rate:.1f}%")

    if test_results["errors"]:
        print("\nErrors:")
        for error in test_results["errors"]:
            print(f"  - {error}")

    print("\n" + "=" * 60)
    status = "ALL TESTS PASSED" if test_results["failed"] == 0 else "SOME TESTS FAILED"
    print(status)
    print("=" * 60)

    return test_results


if __name__ == "__main__":
    results = run_all_tests()
