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

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

describe('SearchListCrudPage', () => {
  it('keeps the T01 list body and optional detail in the shared responsive workspace', () => {
    const wrapper = mount(SearchListCrudPage, {
      props: {
        title: '고객 목록',
        state: 'READY',
        evidence: { asOf: '2026-08-09T00:00:00Z', version: 'v1' }
      },
      slots: {
        default: '<section data-test="list">목록</section>',
        detail: '<section data-test="detail">상세</section>'
      }
    })

    expect(wrapper.get('.ks-crud-workspace__body').classes()).toContain('has-aside')
    expect(wrapper.get('main [data-test="list"]').text()).toBe('목록')
    expect(wrapper.get('aside [data-test="detail"]').text()).toBe('상세')
  })

  it('does not render an empty detail region when the T01 caller has no selection', () => {
    const wrapper = mount(SearchListCrudPage, {
      props: { title: '고객 목록', state: 'READY' },
      slots: { default: '<section>목록</section>' }
    })

    expect(wrapper.find('.ks-crud-workspace__body').classes()).not.toContain('has-aside')
    expect(wrapper.find('.ks-crud-workspace aside').exists()).toBe(false)
  })

  it('does not expose list content while the screen is forbidden', () => {
    const wrapper = mount(SearchListCrudPage, {
      props: { title: '권한 목록', state: 'FORBIDDEN' },
      global: adapterGlobal,
      slots: { default: '<section data-test="sensitive-list">민감 목록</section>' }
    })

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

  it('forwards retry from the shared state boundary', async () => {
    const wrapper = mount(SearchListCrudPage, { props: { title: '목록', state: 'ERROR' }, global: adapterGlobal })
    const boundary = wrapper.findComponent({ name: 'QueryStateBoundary' })
    await boundary.vm.$emit('retry')

    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
