import { describe, expect, it } from 'vitest'
import { mount } from '@vue/test-utils'

import QuestionCard from './QuestionCard.vue'
import type { InteractiveQuestion } from '@/utils/interactiveMessage'

function question(overrides: Partial<InteractiveQuestion> = {}): InteractiveQuestion {
  return {
    version: 1,
    id: 'target_user',
    title: '这个需求主要面向谁？',
    description: '选择后我会补全场景。',
    mode: 'single',
    options: [
      { id: 'admin', label: '后台管理员', summary: '负责配置和审核' },
      { id: 'user', label: '普通用户', details: '直接使用业务功能。' },
    ],
    allowCustom: true,
    ...overrides,
  }
}

describe('QuestionCard', () => {
  it('单选提交选中的答案', async () => {
    const wrapper = mount(QuestionCard, { props: { question: question() } })

    await wrapper.get('[data-testid="question-option-admin"]').trigger('click')
    await wrapper.get('[data-testid="question-submit"]').trigger('click')

    expect(wrapper.emitted('answer')?.[0][0]).toMatchObject({
      question: { id: 'target_user' },
      selectedOptionIds: ['admin'],
      customText: '',
    })
  })

  it('多选可以提交多个答案', async () => {
    const wrapper = mount(QuestionCard, {
      props: { question: question({ mode: 'multiple', allowCustom: false }) },
    })

    await wrapper.get('[data-testid="question-option-admin"]').trigger('click')
    await wrapper.get('[data-testid="question-option-user"]').trigger('click')
    await wrapper.get('[data-testid="question-submit"]').trigger('click')

    expect(wrapper.emitted('answer')?.[0][0]).toMatchObject({
      selectedOptionIds: ['admin', 'user'],
    })
  })

  it('禁用时不能选择或提交', async () => {
    const wrapper = mount(QuestionCard, { props: { question: question(), disabled: true } })

    await wrapper.get('[data-testid="question-option-admin"]').trigger('click')
    await wrapper.get('[data-testid="question-submit"]').trigger('click')

    expect(wrapper.emitted('answer')).toBeUndefined()
  })
})
