/**
 * Excessive Agency Test Fixtures
 * Tests for detecting unbounded agent autonomy risks
 * AI Detection Roadmap Phase 2
 */

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

export const excessiveAgencyTests: TestGroup = {
  name: 'Excessive Agency Detection',
  tier: 'A',
  layer: 2,
  description: 'Detection of unbounded AI agent execution and missing human oversight',

  truePositives: [
    {
      name: 'Excessive Agency - Unbounded Loops',
      expectFindings: true,
      expectedCategories: ['ai_excessive_agency'],
      description: 'Unbounded agent execution patterns that MUST be detected',
      file: {
        path: 'src/agents/unbounded-agent.ts',
        content: `
import { Agent } from '@langchain/agents'

// Unbounded while loop - HIGH
async function runAgentForever(agent: Agent) {
  while (true) {
    await agent.step()  // No termination condition!
  }
}

// No iteration limit - HIGH
const agentConfig = {
  maxIterations: -1,  // Unbounded!
  tools: [searchTool, calculatorTool],
}

// Timeout disabled - MEDIUM
const runtimeConfig = {
  timeout: 0,  // No timeout!
  maxIterations: 10,
}

// Another timeout disabled pattern - MEDIUM
const config2 = {
  timeout: null,  // Disabled
}

// Auto-approve without oversight - HIGH
const autoAgent = {
  autoApprove: true,  // No human review!
  tools: [deleteFileTool, sendEmailTool],
}

// Human-in-loop disabled - MEDIUM
const unsupervisedAgent = {
  humanInLoop: false,  // Explicitly disabled
  capabilities: ['code_execution', 'file_write'],
}
`,
        language: 'typescript',
        size: 700,
      },
    },
    {
      name: 'Excessive Agency - CrewAI Unsafe',
      expectFindings: true,
      expectedCategories: ['ai_excessive_agency'],
      description: 'CrewAI unsafe code execution patterns that MUST be detected',
      file: {
        path: 'src/agents/crewai-unsafe.py',
        content: `
from crewai import Agent, Task, Crew

# CrewAI unsafe code execution mode - CRITICAL
agent = Agent(
    role="Code Executor",
    goal="Execute user code",
    code_execution_mode="unsafe"  # CRITICAL: No sandboxing!
)

# CrewAI code execution without Docker - HIGH
dangerous_agent = Agent(
    role="Script Runner",
    allow_code_execution=True,  # HIGH: Runs on host without Docker
    code_execution_config={
        "timeout": 60,
    }
)
`,
        language: 'python',
        size: 400,
      },
    },
    {
      name: 'Excessive Agency - AutoGen Unsafe',
      expectFindings: true,
      expectedCategories: ['ai_excessive_agency'],
      description: 'AutoGen unsafe patterns that MUST be detected',
      file: {
        path: 'src/agents/autogen-unsafe.py',
        content: `
from autogen import UserProxyAgent, AssistantAgent
from autogen.code_utils import LocalCommandLineCodeExecutor

# AutoGen use_docker=False - CRITICAL
code_executor = LocalCommandLineCodeExecutor(
    work_dir="workspace",
    use_docker=False  # CRITICAL: Executes directly on host!
)

# Another use_docker=False pattern
config = {
    "code_execution_config": {
        "use_docker": False,  # CRITICAL
        "work_dir": "workspace",
    }
}

# AutoGen NEVER human input - HIGH
proxy = UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",  # HIGH: No human oversight
    code_execution_config=config,
)

# UserProxyAgent without reply limit - MEDIUM
unlimited_proxy = UserProxyAgent(
    name="unlimited_proxy",
    human_input_mode="TERMINATE",
    # Missing max_consecutive_auto_reply!
)
`,
        language: 'python',
        size: 600,
      },
    },
  ],

  falseNegatives: [
    {
      name: 'Excessive Agency - Safe Patterns',
      expectFindings: false,
      description: 'Safe agent patterns with proper limits that should NOT be flagged',
      file: {
        path: 'src/agents/safe-agent.ts',
        content: `
import { Agent } from '@langchain/agents'

// Bounded iteration loop - SAFE
async function runAgentWithLimits(agent: Agent) {
  const maxIterations = 10
  for (let i = 0; i < maxIterations; i++) {
    const result = await agent.step()
    if (result.done) break
  }
}

// Proper configuration - SAFE
const safeConfig = {
  maxIterations: 15,  // Bounded
  timeout: 300000,    // 5 minute timeout
  humanInLoop: true,  // Human oversight
  budgetLimit: 100,   // Cost limit
}

// Human approval required - SAFE
const reviewedAgent = {
  requireApproval: true,
  tools: [searchTool],
}
`,
        language: 'typescript',
        size: 500,
      },
    },
    {
      name: 'Excessive Agency - Safe CrewAI/AutoGen',
      expectFindings: false,
      description: 'Safe CrewAI and AutoGen patterns that should NOT be flagged',
      file: {
        path: 'src/agents/safe-frameworks.py',
        content: `
from crewai import Agent
from autogen import UserProxyAgent
from autogen.code_utils import DockerCommandLineCodeExecutor

# CrewAI with safe mode - SAFE
safe_crewai_agent = Agent(
    role="Safe Executor",
    code_execution_mode="safe"  # SAFE: Sandboxed execution
)

# AutoGen with Docker - SAFE
docker_executor = DockerCommandLineCodeExecutor(
    image="python:3.10-slim",
    timeout=60,
)

# AutoGen with human input - SAFE
supervised_proxy = UserProxyAgent(
    name="supervised",
    human_input_mode="ALWAYS",  # SAFE: Human oversight
    max_consecutive_auto_reply=5,  # SAFE: Limited replies
)

# Config with Docker enabled - SAFE
safe_config = {
    "code_execution_config": {
        "use_docker": True,  # SAFE
        "timeout": 60,
    }
}
`,
        language: 'python',
        size: 600,
      },
    },
  ],
}
