/**
 * Security Benchmark Test Runner
 * 
 * Comprehensive security benchmark suite for the Oculum scanner, covering:
 * - Traditional OWASP-style vulnerabilities
 * - AI-specific security issues (prompt injection, unsafe execution, BYOK, etc.)
 * - False positive scenarios (patterns that should NOT be flagged)
 *
 * Run with: npx tsx src/lib/scanner/__tests__/benchmark/run-benchmark.ts
 */

import {
  allTestGroups,
  falsePositiveFixtures,
  highEntropyTests,
} from './fixtures'
import {
  runTestFixture,
  printTestResult,
  computeSummary,
  printSummary,
  printMetrics,
} from './utils/test-runner'
import type { TestResult } from './types'

async function main() {
  console.log('='.repeat(80))
  console.log('OCULUM SECURITY BENCHMARK TEST SUITE')
  console.log('='.repeat(80))
  console.log('')

  const results: TestResult[] = []

  // ========================================
  // LAYER 1 TESTS
  // ========================================

  console.log('\n' + '─'.repeat(80))
  console.log('LAYER 1: SURFACE SCAN TESTS')
  console.log('─'.repeat(80))

  const layer1Groups = allTestGroups.filter(g => g.layer === 1)
  
  for (const group of layer1Groups) {
    console.log(`\n📌 TIER ${group.tier} (${group.tier === 'A' ? 'Core' : 'AI-Assisted'}): ${group.name}`)
    
    if (group.truePositives.length > 0) {
      console.log('  Testing TRUE POSITIVES...')
      for (const fixture of group.truePositives) {
        const result = await runTestFixture(fixture)
        results.push(result)
        printTestResult(result)
      }
    }

    if (group.falseNegatives.length > 0) {
      console.log('  Testing FALSE NEGATIVES (should NOT flag)...')
      for (const fixture of group.falseNegatives) {
        const result = await runTestFixture(fixture)
        results.push(result)
        printTestResult(result)
      }
    }
  }

  // High Entropy (Tier B)
  console.log(`\n📌 TIER B (AI-Assisted): ${highEntropyTests.name}`)
  console.log('  Testing TRUE POSITIVES...')
  for (const fixture of highEntropyTests.truePositives) {
    const result = await runTestFixture(fixture)
    results.push(result)
    printTestResult(result)
  }

  // ========================================
  // LAYER 2 TESTS
  // ========================================

  console.log('\n' + '─'.repeat(80))
  console.log('LAYER 2: STRUCTURAL SCAN TESTS')
  console.log('─'.repeat(80))

  const layer2Groups = allTestGroups.filter(g => g.layer === 2)
  
  for (const group of layer2Groups) {
    console.log(`\n📌 TIER ${group.tier} (${group.tier === 'A' ? 'Core' : 'AI-Assisted'}): ${group.name}`)
    
    if (group.truePositives.length > 0) {
      console.log('  Testing TRUE POSITIVES...')
      for (const fixture of group.truePositives) {
        const result = await runTestFixture(fixture)
        results.push(result)
        printTestResult(result)
      }
    }

    if (group.falseNegatives.length > 0) {
      console.log('  Testing FALSE NEGATIVES (should NOT flag)...')
      for (const fixture of group.falseNegatives) {
        const result = await runTestFixture(fixture)
        results.push(result)
        printTestResult(result)
      }
    }
  }

  // ========================================
  // FALSE POSITIVE TESTS
  // ========================================

  console.log('\n' + '─'.repeat(80))
  console.log('FALSE POSITIVE TESTS')
  console.log('─'.repeat(80))

  for (const fixture of falsePositiveFixtures) {
    console.log(`\n📌 ${fixture.name}`)
    const result = await runTestFixture(fixture)
    results.push(result)
    printTestResult(result)
  }

  // ========================================
  // RESULTS SUMMARY
  // ========================================

  console.log('\n' + '='.repeat(80))
  console.log('BENCHMARK RESULTS SUMMARY')
  console.log('='.repeat(80))

  // Compute summary with metrics
  const summary = computeSummary(results)

  // Print detailed metrics
  printMetrics(summary.metrics)

  // Print final summary
  printSummary(summary)

  // Exit with appropriate code
  process.exit(summary.passRate === 100 ? 0 : 1)
}

// Run the test suite
main().catch(console.error)
