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

const entries = [
  { screenId: 'SCR-1', path: '/ops/data', module: 'Operations', section: 'Operations', title: '데이터 품질', order: 1, favoriteAllowed: true, internalOnly: false, permissions: [] },
  { screenId: 'SCR-2', path: '/portfolio/risk', module: 'Portfolio', section: 'Portfolio', title: '리스크', order: 1, favoriteAllowed: true, internalOnly: false, permissions: [] },
]

const global = { stubs: { KsDialog: { props: ['visible'], template: '<div><slot /></div>' } } }

describe('KsMenuSearch contract', () => {
  it('connects the active result to the search input for assistive technology', async () => {
    const wrapper = mount(KsMenuSearch, { props: { open: true, entries }, global })
    await wrapper.vm.$nextTick()

    const input = wrapper.get('input')
    expect(input.attributes('aria-controls')).toBe('ks-menu-search-results')
    expect(input.attributes('aria-activedescendant')).toBe('ks-menu-search-result-0')
    expect(wrapper.get('#ks-menu-search-result-0').attributes('aria-selected')).toBe('true')

    await input.trigger('keydown', { key: 'ArrowDown' })
    expect(input.attributes('aria-activedescendant')).toBe('ks-menu-search-result-1')
  })

  it('removes the active descendant when filtering returns no results', async () => {
    const wrapper = mount(KsMenuSearch, { props: { open: true, entries }, global })
    const input = wrapper.get('input')
    await input.setValue('없는 메뉴')

    expect(input.attributes('aria-activedescendant')).toBeUndefined()
    expect(wrapper.get('[role="listbox"]').text()).toContain('검색 결과가 없습니다.')
  })
})
