import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import DetailReadPage from '../v2/DetailReadPage.vue'
import { nativeUiAdapter } from '../../adapter/native'
import { uiAdapterKey } from '../../adapter/contracts'

const global = { provide: { [uiAdapterKey as unknown as string]: nativeUiAdapter } }

describe('DetailReadPage T02 contract', () => {
  it('renders evidence as-of and version metadata with the detail slot', () => {
    const wrapper = mount(DetailReadPage, {
      global,
      props: { title: '모델 상세', state: 'READY', evidence: { asOf: '2026-08-12T00:00:00Z', version: 'model-v2' } },
      slots: { default: '<article data-test="detail">내용</article>', evidence: '<aside data-test="evidence">증거</aside>' }
    })

    expect(wrapper.find('.ks-page__meta').text()).toContain('As-of: 2026-08-12T00:00:00Z')
    expect(wrapper.find('.ks-page__meta').text()).toContain('Version: model-v2')
    expect(wrapper.find('[data-test="detail"]').exists()).toBe(true)
    expect(wrapper.find('[data-test="evidence"]').exists()).toBe(true)
  })

  it('suppresses detail content while forbidden and forwards retry', async () => {
    const wrapper = mount(DetailReadPage, {
      global,
      props: { title: '보호된 상세', state: 'FORBIDDEN' },
      slots: { default: '<article data-test="sensitive-detail">민감 내용</article>' }
    })

    expect(wrapper.find('[data-test="sensitive-detail"]').exists()).toBe(false)
    expect(wrapper.text()).toContain('권한 없음')

    await wrapper.setProps({ state: 'ERROR' })
    await wrapper.findComponent({ name: 'QueryStateBoundary' }).vm.$emit('retry')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
