const { MandatoryRiskManager } = require('./lib/mandatory-risk-manager');

async function testMandatoryRiskManagement() {
  console.log('🧪 TESTING MANDATORY RISK MANAGEMENT SYSTEM');
  console.log('============================================================');
  
  const riskManager = new MandatoryRiskManager();

  // Test 1: Trade with missing SL/TP (should auto-calculate)
  console.log('\n📋 TEST 1: Trade with missing SL/TP');
  try {
    const test1 = {
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 0.5,
      currentPrice: 185,
      leverage: 1
    };
    
    const result = await riskManager.enforceRiskManagement(test1);
    console.log('✅ Test 1 PASSED - Auto-calculated SL/TP');
    console.log(`   SL: $${result.stopLoss}, TP: $${result.takeProfit}`);
  } catch (error) {
    console.log('❌ Test 1 FAILED:', error.message);
  }

  // Test 2: Trade with proper SL/TP (should pass)
  console.log('\n📋 TEST 2: Trade with proper SL/TP');
  try {
    const test2 = {
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 0.5,
      currentPrice: 185,
      stopLoss: 182,    // 1.6% risk
      takeProfit: 192,  // 3.8% reward
      leverage: 2
    };
    
    const result = await riskManager.enforceRiskManagement(test2);
    console.log('✅ Test 2 PASSED - Proper SL/TP accepted');
  } catch (error) {
    console.log('❌ Test 2 FAILED:', error.message);
  }

  // Test 3: Trade with wrong SL direction (should fail)
  console.log('\n📋 TEST 3: Trade with wrong SL direction');
  try {
    const test3 = {
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 1,
      currentPrice: 185,
      stopLoss: 190,  // Wrong direction for BUY
      takeProfit: 180, // Wrong direction for BUY
      leverage: 10
    };
    
    await riskManager.enforceRiskManagement(test3);
    console.log('❌ Test 3 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 3 PASSED - Correctly blocked:', error.message);
  }

  // Test 4: High risk trade (should fail)
  console.log('\n📋 TEST 4: High risk trade with 20x leverage');
  try {
    const test4 = {
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 1,
      currentPrice: 185,
      stopLoss: 180,
      takeProfit: 190,
      leverage: 20
    };
    
    await riskManager.enforceRiskManagement(test4);
    console.log('❌ Test 4 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 4 PASSED - Correctly blocked high risk:', error.message);
  }

  // Test 5: Poor risk/reward ratio (should fail)
  console.log('\n📋 TEST 5: Poor risk/reward ratio');
  try {
    const test5 = {
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 1,
      currentPrice: 185,
      stopLoss: 175,    // Large risk
      takeProfit: 190,  // Small reward
      leverage: 10
    };
    
    await riskManager.enforceRiskManagement(test5);
    console.log('❌ Test 5 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 5 PASSED - Correctly blocked poor ratio:', error.message);
  }

  console.log('\n🎯 MANDATORY RISK MANAGEMENT TESTING COMPLETE');
  console.log('💡 System will now BLOCK trades without proper SL/TP');
}

testMandatoryRiskManagement();
