/**
 * AI Package Hallucination Test Fixtures
 * Tests for detecting AI-hallucinated/fake package names in dependencies
 *
 * Background: USENIX research shows ~20% of AI-generated code references
 * packages that don't exist, creating supply chain attack vectors.
 */

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

export const aiPackageHallucinationTests: TestGroup = {
  name: 'AI Package Hallucination Detection',
  tier: 'A',
  layer: 2,
  description: 'Detection of AI-hallucinated and potentially fake package names in imports and dependencies',

  truePositives: [
    {
      name: 'Package Hallucination - True Positives (Known Fakes)',
      expectFindings: true,
      expectedCategories: ['ai_package_hallucination'],
      description: 'Known hallucinated packages that MUST be detected',
      file: {
        path: 'src/utils/helpers.ts',
        content: `
// Known AI-hallucinated package names (verified fakes from research)
import { Chart } from 'react-charts'  // Fake - react-chartjs-2 or recharts are real
import { MongoClient } from 'mongo-client'  // Fake - mongodb is real
import { PostgresClient } from 'postgres-client'  // Fake - pg is real
import fastJson from 'fast-json'  // Fake - fast-json-stringify or ajv are real
import helpers from 'node-helpers'  // Fake - doesn't exist

// Import with destructuring
const { utils } = require('easy-utils')  // Fake
const tools = require('simple-tools')  // Fake

// Dynamic import
const csvParser = await import('csv-parser-pro')  // Fake - csv-parser or papaparse are real
`,
        language: 'typescript',
        size: 600,
      },
    },
    {
      name: 'Package Hallucination - True Positives (Suspicious Patterns)',
      expectFindings: true,
      expectedCategories: ['ai_package_hallucination'],
      description: 'Suspicious package naming patterns that suggest hallucination',
      file: {
        path: 'src/api/services.ts',
        content: `
// Unscoped generic utility names (frequently hallucinated)
import utils from 'utils'  // Too generic - likely hallucinated
import helpers from 'helpers'  // Too generic - likely hallucinated
import common from 'common'  // Too generic - likely hallucinated
import tools from 'tools'  // Too generic - likely hallucinated

// easy-* pattern (frequently hallucinated)
import easyConfig from 'easy-config'  // Suspicious pattern
import easyDb from 'easy-database'  // Suspicious pattern
import easyAuth from 'easy-auth'  // Suspicious pattern

// simple-* pattern (frequently hallucinated)
import simpleApi from 'simple-api'  // Suspicious pattern
import simpleServer from 'simple-server'  // Suspicious pattern
import simpleCache from 'simple-cache'  // Suspicious pattern

// node-* patterns for non-core modules (frequently hallucinated)
import nodeUtils from 'node-utils'  // Suspicious - not a core module
import nodeHelpers from 'node-helpers'  // Suspicious - not a core module

// fast-* patterns without real packages
import fastParser from 'fast-parser'  // Suspicious pattern
import fastValidator from 'fast-validator'  // Suspicious pattern
`,
        language: 'typescript',
        size: 900,
      },
    },
    {
      name: 'Package Hallucination - True Positives (package.json)',
      expectFindings: true,
      expectedCategories: ['ai_package_hallucination'],
      description: 'Hallucinated packages in package.json dependencies',
      file: {
        path: 'package.json',
        content: `{
  "name": "my-app",
  "dependencies": {
    "react": "^18.0.0",
    "react-charts": "^1.0.0",
    "mongo-client": "^3.0.0",
    "easy-config": "^2.0.0",
    "utils": "^1.0.0",
    "helpers": "^1.0.0",
    "node-helpers": "^1.0.0"
  },
  "devDependencies": {
    "simple-test": "^1.0.0",
    "fast-bundler": "^1.0.0"
  }
}`,
        language: 'json',
        size: 400,
      },
    },
    {
      name: 'Package Hallucination - True Positives (requirements.txt)',
      expectFindings: true,
      expectedCategories: ['ai_package_hallucination'],
      description: 'Hallucinated packages in Python requirements',
      file: {
        path: 'requirements.txt',
        content: `# Real packages
flask==2.0.0
requests==2.28.0

# Likely hallucinated packages
easy-flask==1.0.0
simple-api==1.0.0
fast-db==2.0.0
python-helpers==1.0.0
utils==1.0.0
common==1.0.0
`,
        language: 'text',
        size: 250,
      },
    },
  ],

  falseNegatives: [
    {
      name: 'Package Hallucination - False Negatives (Real Packages)',
      expectFindings: false,
      description: 'Real, well-known packages that should NOT be flagged',
      allowedInfoFindings: [
        {
          category: 'suspicious_package',
          maxCount: 2,
          reason: 'Some real packages may trigger info-level findings from risky-imports detector',
        },
        {
          category: 'ai_package_hallucination',
          maxCount: 1,
          reason: 'Edge cases may generate info-level findings',
        },
      ],
      file: {
        path: 'src/app/real-packages.ts',
        content: `
// Popular, well-known real packages - SHOULD NOT FLAG
import React from 'react'
import { useState } from 'react'
import express from 'express'
import axios from 'axios'
import lodash from 'lodash'
import { z } from 'zod'
import dayjs from 'dayjs'
import { MongoClient } from 'mongodb'  // Real (not 'mongo-client')
import pg from 'pg'  // Real postgres client
import Fastify from 'fastify'  // Real
import csvParser from 'csv-parser'  // Real (not 'csv-parser-pro')

// Scoped packages are generally real
import { Hono } from '@hono/hono'
import { createClient } from '@supabase/supabase-js'
import { OpenAI } from 'openai'
import Anthropic from '@anthropic-ai/sdk'
import { PrismaClient } from '@prisma/client'

// These are real 'simple-' packages that exist
import SimpleSchema from 'simpl-schema'  // Real package
import simpleGit from 'simple-git'  // Real package (has >10M weekly downloads)

// These are real 'fast-' packages that exist
import fastJson from 'fast-json-stringify'  // Real package
import fastify from 'fastify'  // Real package
import fastGlob from 'fast-glob'  // Real package

// Real utility packages with specific names
import _ from 'underscore'
import __ from 'radash'
import clsx from 'clsx'
import nanoid from 'nanoid'
`,
        language: 'typescript',
        size: 1000,
      },
    },
    {
      name: 'Package Hallucination - False Negatives (Internal/Monorepo)',
      expectFindings: false,
      description: 'Internal monorepo packages that should NOT be flagged',
      file: {
        path: 'apps/web/src/index.ts',
        content: `
// Internal monorepo packages - these are workspace packages, not external
import { scanner } from '@oculum/scanner'
import { shared } from '@oculum/shared'
import { Button } from '@mycompany/ui'
import { utils } from '@internal/utils'  // Scoped internal is fine

// Relative imports should never be flagged
import { helper } from './utils'
import { common } from '../shared/common'
import api from '../../api'

// Aliased paths should not be flagged
import { config } from '@/config'
import { utils } from '~/utils'
import { helpers } from '#utils/helpers'
`,
        language: 'typescript',
        size: 500,
      },
    },
    {
      name: 'Package Hallucination - False Negatives (Test Files)',
      expectFindings: false,
      description: 'Test files may use mocks that should be downgraded',
      allowedInfoFindings: [
        {
          category: 'ai_package_hallucination',
          maxCount: 3,
          reason: 'Test file mock packages may generate info-level findings',
        },
      ],
      file: {
        path: 'src/__tests__/mocks/packages.test.ts',
        content: `
// Test mocks - these might look suspicious but are test-only
import mockUtils from 'utils'
import mockHelpers from 'helpers'

jest.mock('utils', () => ({
  format: jest.fn()
}))

jest.mock('helpers', () => ({
  parse: jest.fn()
}))

describe('mock tests', () => {
  it('should work', () => {
    expect(mockUtils).toBeDefined()
  })
})
`,
        language: 'typescript',
        size: 400,
      },
    },
  ],
}
