/**
 * High-Entropy String Detection Test Fixtures
 * Tests for detecting potential secrets based on Shannon entropy
 */

import type { TestGroup } from '../../types'

export const highEntropyTests: TestGroup = {
  name: 'High-Entropy Strings',
  tier: 'B',
  layer: 1,
  description: 'Detection of high-entropy strings that may be secrets',

  truePositives: [
    {
      name: 'High-Entropy - True Positives',
      expectFindings: true,
      expectedCategories: ['high_entropy_string'],
      description: 'High-entropy strings that MUST be detected',
      file: {
        path: 'src/config/secrets.ts',
        content: `
// Random strings that look like secrets - should be flagged
const randomSecret = 'aB3xK9mPqR5tL7nW2cF4hJ6dG8vS0yU1'
const anotherSecret = 'xYz123AbC456dEf789GhI012JkL345MnO'
const hexSecret = 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8'

// Base64-encoded looking strings
const base64Secret = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo='
const longBase64 = 'SGVsbG9Xb3JsZFRoaXNJc0FMb25nU3RyaW5nRm9yVGVzdGluZw=='

// Mixed alphanumeric high-entropy
const mixedSecret = 'Qw3rTy7U1oP9aS2dF4gH6jK8lZ0xCvB'
const complexSecret = 'n5X2mK8pL3qW7rT9yU4iO1aS6dF0gH'

// Strings that could be API keys
const potentialKey = 'sk_live_51JvXxxxxxxxxxxxxxxxxxxx12345'
const anotherKey = 'rk_test_aBcDeFgHiJkLmNoPqRsTuVwXyZ'

// JWT-like high entropy segments
const jwtPayload = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0'
`,
        language: 'typescript',
        size: 800,
      },
    },
  ],

  falseNegatives: [
    {
      name: 'High-Entropy - False Negatives (Safe Patterns)',
      expectFindings: false,
      description: 'High-entropy but safe patterns that should NOT be flagged',
      allowedInfoFindings: [
        {
          category: 'high_entropy_string',
          maxCount: 5,
          reason: 'Some edge cases may still generate info findings requiring AI validation',
        },
        {
          category: 'hardcoded_secret',
          maxCount: 3,
          reason: 'Hashes and webhook URLs may trigger secret detection',
        },
        {
          category: 'sensitive_url',
          maxCount: 3,
          reason: 'URLs in test data may trigger URL detection',
        },
        {
          category: 'sensitive_variable',
          maxCount: 2,
          reason: 'Variable names may trigger sensitive variable detection',
        },
      ],
      file: {
        path: 'src/components/Dashboard.tsx',
        content: `
import { useState, useEffect } from 'react'

// CSS/Tailwind classes - SAFE (looks high entropy but is CSS)
const styles = 'flex items-center justify-between p-4 bg-gray-100 rounded-lg shadow-md'
const moreStyles = 'text-sm font-medium text-gray-700 hover:text-blue-600 transition-colors duration-200'
const complexTailwind = 'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 px-6 py-8 bg-gradient-to-r'

// UUIDs - SAFE (standard format)
const userId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'

// Short references - SAFE
const commitRef = 'a1b2c3d'

// Natural language content - SAFE
const description = 'This is a longer description that explains what the feature does and how to use it properly'
const message = 'Please enter your email address to continue with the registration process'

// UI strings with model names - SAFE
const modelLabel = 'Claude 3.5 Sonnet - Best for chat and embeddings with high accuracy'
const tooltip = 'Uses GPT-4 Turbo for advanced reasoning and complex analysis tasks'

// URLs - SAFE
const docsUrl = 'https://docs.myapp.com/getting-started'

// File paths - SAFE
const configPath = '/home/user/.config/myapp/settings/production.json'
const assetPath = '/assets/images/icons/dashboard/analytics/chart-line.svg'

// CSS gradients and colors - SAFE
const gradient = 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
const complexColor = 'rgba(102, 126, 234, 0.85)'

// Template literals with code expressions - SAFE
const formattedTime = \`Synced \${lastSync.toLocaleString()} - \${hours}h ago\`
const displayPrice = \`$\${price.toFixed(2).padStart(8, '0')}\`

// Dates/timestamps - SAFE
const timestamp = '2024-01-15T14:30:00.000Z'
const formattedDate = '2024-01-15 14:30:00+00:00'

// Email addresses - SAFE
const supportEmail = 'support+test-user@example-company.com'

// Regex patterns (route matchers) - SAFE
const routePattern = '/api/v1/users/[id]/posts/(?:comments|reactions)'
const pathMatcher = '^\\/api\\/v[0-9]+\\/(?:users|posts)\\/(.*)'
`,
        language: 'typescript',
        size: 2200,
      },
    },
    {
      name: 'High-Entropy - False Negatives (CSS-in-JS)',
      expectFindings: false,
      description: 'CSS-in-JS patterns that should NOT be flagged',
      allowedInfoFindings: [
        {
          category: 'high_entropy_string',
          maxCount: 1,
          reason: 'Complex CSS may occasionally trigger',
        },
      ],
      file: {
        path: 'src/styles/theme.ts',
        content: `
import styled from 'styled-components'

// Styled components - SAFE
const Button = styled.button\`
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #ffffff;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 600;
  transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  }
\`

// Emotion CSS - SAFE
const containerStyles = css\`
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  padding: 32px;
  background: #f8fafc;
  border-radius: 12px;
\`

// Inline styles object - SAFE
const inlineStyle = {
  background: 'linear-gradient(to right, #4facfe 0%, #00f2fe 100%)',
  boxShadow: '0 10px 40px rgba(0, 0, 0, 0.15)',
  transform: 'perspective(1000px) rotateY(5deg)',
}

// CSS custom properties - SAFE
const cssVars = {
  '--primary-gradient': 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
  '--shadow-color': 'rgba(102, 126, 234, 0.25)',
}
`,
        language: 'typescript',
        size: 1000,
      },
    },
    {
      name: 'Test File High-Entropy',
      expectFindings: false,
      description: 'Test files with high-entropy test data',
      allowedInfoFindings: [
        {
          category: 'high_entropy_string',
          maxCount: 3,
          reason: 'Test data may have some high-entropy patterns',
        },
        {
          category: 'hardcoded_secret',
          maxCount: 2,
          reason: 'Test secrets may be detected at low severity',
        },
      ],
      file: {
        path: 'src/__tests__/crypto.test.ts',
        content: `
import { describe, it, expect } from 'vitest'
import { hash, encrypt, decrypt } from '../crypto'

describe('Crypto utils', () => {
  // Test vectors with known outputs - OK in tests
  const testKey = 'aB3xK9mPqR5tL7nW2cF4hJ6dG8vS0yU1'
  const testIV = '0123456789abcdef0123456789abcdef'
  const expectedHash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

  it('should hash correctly', () => {
    const result = hash('test-input')
    expect(result).toMatch(/^[a-f0-9]{64}$/)
  })

  it('should encrypt and decrypt', () => {
    const plaintext = 'Hello, World!'
    const encrypted = encrypt(plaintext, testKey)
    const decrypted = decrypt(encrypted, testKey)
    expect(decrypted).toBe(plaintext)
  })
})
`,
        language: 'typescript',
        size: 700,
      },
    },
  ],
}
