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

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

describe('MasterDetailCrudPage T04 contract', () => {
  it('forwards as-of/version metadata and renders master/detail slots', () => {
    const wrapper = mount(MasterDetailCrudPage, {
      global,
      props: { title: '대사 상세', state: 'READY', evidence: { asOf: '2026-08-12T00:00:00Z', version: 'projection-v3' } },
      slots: { master: '<section data-test="master">목록</section>', detail: '<aside data-test="detail">상세</aside>' }
    })

    expect(wrapper.find('.ks-page__meta').text()).toContain('Version: projection-v3')
    expect(wrapper.find('[data-test="master"]').exists()).toBe(true)
    expect(wrapper.find('[data-test="detail"]').exists()).toBe(true)
  })

  it('does not expose detail content while unauthorized and forwards retry', async () => {
    const wrapper = mount(MasterDetailCrudPage, {
      global,
      props: { title: '보호된 대사', state: 'FORBIDDEN' },
      slots: { master: '<section>목록</section>', detail: '<aside data-test="sensitive-detail">민감 상세</aside>' }
    })

    expect(wrapper.find('[data-test="sensitive-detail"]').exists()).toBe(false)
    await wrapper.setProps({ state: 'ERROR' })
    await wrapper.findComponent({ name: 'QueryStateBoundary' }).vm.$emit('retry')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
