import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { I18nProvider } from '../i18n'
import { AgentTrace } from './AgentTrace'
import type { OrchestrateResult } from '../api/ai'

function make(over: Partial<OrchestrateResult>): OrchestrateResult {
  return {
    decision: 'step_up', reason_codes: [], required_auth: [], agent_state: 'policy_evaluated',
    risk: { band: 'low', recommend_step_up: false, reason_codes: [], confidence: 'ok',
      model_version: 'v1', feature_version: 'v1', shadow: true },
    auth_recommendation: { recommended_method: 'webauthn', meets_pdp_minimum: true },
    rails: { ranked: ['sarie'], eligible_only: true }, explanation: null, payment: null,
    audit: { verified: true, length: 3, protection_level: 'x' }, fallback: null, labels: {},
    ...over,
  }
}

const r = (res: OrchestrateResult) =>
  render(<I18nProvider initial="en"><AgentTrace result={res} /></I18nProvider>)

describe('AgentTrace', () => {
  it('marks deterministic policy authoritative and risk advisory', () => {
    r(make({}))
    expect(screen.getByText('Deterministic policy')).toBeInTheDocument()
    expect(screen.getByText('Authoritative')).toBeInTheDocument()
    expect(screen.getAllByText('Advisory only').length).toBeGreaterThan(0)
  })

  it('shows Payment Core NOT executed unless initiated', () => {
    r(make({ agent_state: 'policy_evaluated' }))
    expect(screen.getAllByText('Not executed').length).toBeGreaterThan(0)
  })

  it('shows Payment Core executed only when initiated', () => {
    r(make({ agent_state: 'initiated', payment: { id: 'pay_1', status: 'processing' } }))
    expect(screen.getByText('pay_1')).toBeInTheDocument()
    expect(screen.getByText('Executed')).toBeInTheDocument()
  })

  it('never labels a reasoning agent as a payment executor', () => {
    const { container } = r(make({}))
    // the executor row is Payment Core; risk/context/auth/rail carry Advisory, never Executed
    const advisorySteps = container.querySelectorAll('.role-advisory')
    expect(advisorySteps.length).toBeGreaterThan(0)
  })
})
