import { describe, it, expect } from 'vitest'
import {
  AgentInitializationError,
  AgentIterationError,
  AgentCancellationError,
  AgentCompletionError,
  AgentOutputValidationError,
  AgentMemoryValidationError,
  AgentMaxIterationsError,
  AgentTimeoutError,
  AgentStalledError
} from '../errors'
import { ExecutionError } from '../../base/errors'

describe('Agent Errors', () => {
  describe('AgentInitializationError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has critical severity', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error.severity).toBe('critical')
    })

    it('has agent category', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error.category).toBe('agent')
    })

    it('is not retryable by default', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentInitializationError('Failed to initialize agent: Invalid LLM config')
      expect(error.message).toBe('Failed to initialize agent: Invalid LLM config')
    })

    it('sets name to AgentInitializationError', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error.name).toBe('AgentInitializationError')
    })

    it('stores context with initialization details', () => {
      const context = {
        agentId: 'agent-123',
        reason: 'missing_api_key',
        llmModel: 'gpt-4'
      }
      const error = new AgentInitializationError('Initialization failed', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentInitializationError('Initialization failed')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentInitializationError')
    })
  })

  describe('AgentIterationError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has warning severity', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.severity).toBe('warning')
    })

    it('has agent category', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.category).toBe('agent')
    })

    it('is not retryable by default', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentIterationError('Agent iteration failed: Tool execution timeout')
      expect(error.message).toBe('Agent iteration failed: Tool execution timeout')
    })

    it('sets name to AgentIterationError', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.name).toBe('AgentIterationError')
    })

    it('returns correct error type', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.name).toBe('AgentIterationError')
    })

    it('stores context with iteration details', () => {
      const context = {
        agentId: 'agent-123',
        iterationNumber: 5,
        toolName: 'web_search',
        attemptCount: 2
      }
      const error = new AgentIterationError('Iteration failed', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentIterationError('Iteration failed')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentIterationError')
    })
  })

  describe('AgentCompletionError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has warning severity', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error.severity).toBe('warning')
    })

    it('has agent category', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error.category).toBe('agent')
    })

    it('is not retryable by default', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentCompletionError('Failed to generate final output: LLM timeout')
      expect(error.message).toBe('Failed to generate final output: LLM timeout')
    })

    it('sets name to AgentCompletionError', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error.name).toBe('AgentCompletionError')
    })

    it('stores context with completion details', () => {
      const context = {
        agentId: 'agent-123',
        attempts: 1,
        reason: 'llm_timeout'
      }
      const error = new AgentCompletionError('Completion failed', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentCompletionError('Completion failed')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentCompletionError')
    })
  })

  describe('AgentOutputValidationError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has info severity', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.severity).toBe('info')
    })

    it('has validation category', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.category).toBe('validation')
    })

    it('is not retryable by default', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentOutputValidationError('Agent output validation failed: Missing field "result"')
      expect(error.message).toBe('Agent output validation failed: Missing field "result"')
    })

    it('sets name to AgentOutputValidationError', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.name).toBe('AgentOutputValidationError')
    })

    it('returns correct error type', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.name).toBe('AgentOutputValidationError')
    })

    it('stores context with validation details', () => {
      const context = {
        agentId: 'agent-123',
        iterationNumber: 3,
        expectedSchema: 'CompletionSchema',
        field: 'result'
      }
      const error = new AgentOutputValidationError('Validation failed', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentOutputValidationError('Validation failed')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentOutputValidationError')
    })
  })

  describe('AgentMemoryValidationError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has info severity', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.severity).toBe('info')
    })

    it('has validation category', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.category).toBe('validation')
    })

    it('is not retryable by default', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentMemoryValidationError(
        'turnNumber required for history entries (use null for session memory)'
      )
      expect(error.message).toBe('turnNumber required for history entries (use null for session memory)')
    })

    it('sets name to AgentMemoryValidationError', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.name).toBe('AgentMemoryValidationError')
    })

    it('returns correct error type', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.name).toBe('AgentMemoryValidationError')
    })

    it('stores context with memory validation details', () => {
      const context = {
        entryType: 'reasoning',
        missingField: 'turnNumber',
        iterationNumber: 1
      }
      const error = new AgentMemoryValidationError('Memory validation failed', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentMemoryValidationError('Memory validation failed')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentMemoryValidationError')
    })
  })

  describe('AgentTimeoutError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has critical severity', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error.severity).toBe('critical')
    })

    it('has agent category', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error.category).toBe('agent')
    })

    it('is not retryable by default', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentTimeoutError('Agent execution exceeded timeout (300000ms)')
      expect(error.message).toBe('Agent execution exceeded timeout (300000ms)')
    })

    it('sets name to AgentTimeoutError', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error.name).toBe('AgentTimeoutError')
    })

    it('stores context with timeout details', () => {
      const context = { timeout: 300000, iteration: 3 }
      const error = new AgentTimeoutError('Timeout exceeded', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentTimeoutError('Timeout exceeded')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentTimeoutError')
    })
  })

  describe('AgentCancellationError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has warning severity', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.severity).toBe('warning')
    })

    it('has agent category', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.category).toBe('agent')
    })

    it('has correct type', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.type).toBe('agent_cancellation_error')
    })

    it('is not retryable by default', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.message).toBe('Execution cancelled by user')
    })

    it('sets name to AgentCancellationError', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.name).toBe('AgentCancellationError')
    })

    it('stores context with iteration details', () => {
      const context = { iteration: 3 }
      const error = new AgentCancellationError('Execution cancelled by user', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentCancellationError('Execution cancelled by user')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentCancellationError')
    })
  })

  describe('AgentStalledError', () => {
    it('extends ExecutionError', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error).toBeInstanceOf(ExecutionError)
      expect(error).toBeInstanceOf(Error)
    })

    it('has critical severity', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.severity).toBe('critical')
    })

    it('has agent category', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.category).toBe('agent')
    })

    it('has correct type', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.type).toBe('agent_stalled_error')
    })

    it('is not retryable by default', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.isRetryable()).toBe(false)
    })

    it('sets message correctly', () => {
      const error = new AgentStalledError('Execution stalled: no heartbeat received within threshold')
      expect(error.message).toBe('Execution stalled: no heartbeat received within threshold')
    })

    it('sets name to AgentStalledError', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.name).toBe('AgentStalledError')
    })

    it('stores context with iteration details', () => {
      const context = { iteration: 5 }
      const error = new AgentStalledError('Execution stalled', context)
      expect(error.context).toEqual(context)
    })

    it('captures stack trace', () => {
      const error = new AgentStalledError('Execution stalled')
      expect(error.stack).toBeDefined()
      expect(error.stack).toContain('AgentStalledError')
    })
  })

  describe('instanceof checks', () => {
    it('AgentInitializationError maintains inheritance chain', () => {
      const error = new AgentInitializationError('test')
      expect(error instanceof AgentInitializationError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentIterationError maintains inheritance chain', () => {
      const error = new AgentIterationError('test')
      expect(error instanceof AgentIterationError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentCompletionError maintains inheritance chain', () => {
      const error = new AgentCompletionError('test')
      expect(error instanceof AgentCompletionError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentOutputValidationError maintains inheritance chain', () => {
      const error = new AgentOutputValidationError('test')
      expect(error instanceof AgentOutputValidationError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentMemoryValidationError maintains inheritance chain', () => {
      const error = new AgentMemoryValidationError('test')
      expect(error instanceof AgentMemoryValidationError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentTimeoutError maintains inheritance chain', () => {
      const error = new AgentTimeoutError('test')
      expect(error instanceof AgentTimeoutError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentCancellationError maintains inheritance chain', () => {
      const error = new AgentCancellationError('test')
      expect(error instanceof AgentCancellationError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('AgentStalledError maintains inheritance chain', () => {
      const error = new AgentStalledError('test')
      expect(error instanceof AgentStalledError).toBe(true)
      expect(error instanceof ExecutionError).toBe(true)
      expect(error instanceof Error).toBe(true)
    })

    it('agent errors are distinct types', () => {
      const initError = new AgentInitializationError('test')
      const iterationError = new AgentIterationError('test')
      const completionError = new AgentCompletionError('test')
      const outputValidationError = new AgentOutputValidationError('test')
      const memoryValidationError = new AgentMemoryValidationError('test')
      const timeoutError = new AgentTimeoutError('test')
      const cancellationError = new AgentCancellationError('test')
      const stalledError = new AgentStalledError('test')

      expect(initError instanceof AgentIterationError).toBe(false)
      expect(initError instanceof AgentCompletionError).toBe(false)
      expect(iterationError instanceof AgentCompletionError).toBe(false)
      expect(iterationError instanceof AgentOutputValidationError).toBe(false)
      expect(completionError instanceof AgentOutputValidationError).toBe(false)
      expect(outputValidationError instanceof AgentIterationError).toBe(false)
      expect(outputValidationError instanceof AgentMemoryValidationError).toBe(false)
      expect(memoryValidationError instanceof AgentOutputValidationError).toBe(false)
      expect(timeoutError instanceof AgentMaxIterationsError).toBe(false)
      expect(timeoutError instanceof AgentIterationError).toBe(false)
      expect(cancellationError instanceof AgentTimeoutError).toBe(false)
      expect(cancellationError instanceof AgentIterationError).toBe(false)
      expect(stalledError instanceof AgentTimeoutError).toBe(false)
      expect(stalledError instanceof AgentCancellationError).toBe(false)
    })
  })
})
