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

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

describe('EditFormPage T03 contract', () => {
  it('prioritizes readonly over dirty and dirty over the supplied state', () => {
    const wrapper = mount(EditFormPage, { global, props: { title: '편집', state: 'READY', dirty: true, readonly: true } })
    expect(wrapper.find('.ks-page__meta').text()).toContain('상태: READONLY')

    return wrapper.setProps({ readonly: false }).then(() => {
      expect(wrapper.find('.ks-page__meta').text()).toContain('상태: DIRTY')
    })
  })

  it('forwards submit and retry events through the shared boundaries', async () => {
    const wrapper = mount(EditFormPage, { global, props: { title: '편집', state: 'READY' }, slots: { default: '<input />' } })
    await wrapper.find('form').trigger('submit')
    await wrapper.setProps({ state: 'ERROR' })
    await wrapper.findComponent({ name: 'QueryStateBoundary' }).vm.$emit('retry')
    expect(wrapper.emitted('submit')).toHaveLength(1)
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
