/**
 * Direct-mode fixture E2E test — P7 gate
 * Test: MainAgent direct-mode lifecycle.
 *
 * @module packages/runtime/test/e2e/direct-mode-fixture.test
 */

import { describe, it, expect } from 'bun:test'
import { MainAgent } from '../../src/agents/main/MainAgent.js'

describe('Direct Mode Fixture (P7 gate)', () => {
  const config = {
    session_id: 'test-session',
    project_id: 'test-project'
  }

  describe('MainAgent lifecycle', () => {
    it('should start in IDLE state', () => {
      const agent = new MainAgent(config)
      expect(agent.state).toBe('IDLE')
    })

    it('should classify implementation requests as DELEGATING', async () => {
      const agent = new MainAgent(config)
      const result = await agent.handle_user_message('implement a new feature X')
      expect(result.action).toBe('delegate')
      expect(agent.state).toBe('DELEGATING')
    })

    it('should classify questions as ANSWERING', async () => {
      const agent = new MainAgent(config)
      const result = await agent.handle_user_message('what does this function do?')
      expect(result.action).toBe('answer')
      expect(agent.state).toBe('ANSWERING')
    })

    it('should handle confirmation and transition states', async () => {
      const agent = new MainAgent(config)
      agent.state = 'CONFIRMING'

      await agent.handle_confirmation(true)
      expect(agent.state).toBe('DELEGATING')
    })

    it('should summarise and return to IDLE', () => {
      const agent = new MainAgent(config)
      agent.state = 'DELEGATING'
      agent.summarize()
      expect(agent.state).toBe('IDLE')
    })
  })

  describe('ArchitectureDesigner', () => {
    it('should assess impact and return silent_continue for low-risk changes', async () => {
      const { ArchitectureDesigner } = await import('../../src/agents/architecture/ArchitectureDesigner.js')
      const arch = new ArchitectureDesigner()
      const impact = arch.assess_impact({
        description: 'Add a new helper function',
        files: ['packages/runtime/src/utils/helper.ts']
      })
      expect(impact.result).toBe('silent_continue')
    })

    it('should escalate high-risk contract changes', async () => {
      const { ArchitectureDesigner } = await import('../../src/agents/architecture/ArchitectureDesigner.js')
      const arch = new ArchitectureDesigner()
      const impact = arch.assess_impact({
        description: 'BREAKING: remove the session event type',
        files: ['packages/contracts/src/event.ts', 'packages/runtime/src/events/EventStore.ts', 'packages/runtime/src/events/EventBus.ts']
      })
      expect(impact.result).toBe('reject_or_escalate')
    })
  })
})
