import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import { createMemoryHistory, createRouter } from 'vue-router'
import { describe, expect, it } from 'vitest'
import KsAppShell from '../KsAppShell.vue'

function buildTestRouter() {
  return createRouter({
    history: createMemoryHistory(),
    routes: [
      { path: '/', redirect: '/home' },
      { path: '/home', component: { template: '<div>home</div>' }, meta: { screenId: 'SCR-000', module: 'Home', title: '홈' } },
      { path: '/research/sell-decision', component: { template: '<div>sell</div>' }, meta: { screenId: 'SCR-002', module: 'Research', section: 'Research', title: '매도 의사결정', order: 1, favoriteAllowed: true } }
    ]
  })
}

describe('KsAppShell contract', () => {
  it('provides skip navigation, header, side navigation, and workspace tab landmarks', async () => {
    const pinia = createPinia()
    setActivePinia(pinia)
    const router = buildTestRouter()
    router.push('/research/sell-decision')
    await router.isReady()

    const wrapper = mount(KsAppShell, { global: { plugins: [pinia, router], stubs: { KsDialog: true } }, slots: { default: '<p>화면 내용</p>' } })
    await wrapper.vm.$nextTick()

    expect(wrapper.get('.ks-skip').attributes('href')).toBe('#ks-main')
    expect(wrapper.find('header.ks-global-header').exists()).toBe(true)
    expect(wrapper.get('aside').attributes('aria-label')).toBe('주요 메뉴')
    expect(wrapper.get('nav.ks-workspace-tabs').attributes('aria-label')).toBe('열린 업무')
    expect(wrapper.get('main').attributes('tabindex')).toBe('-1')
    expect(wrapper.get('.ks-breadcrumb').attributes('aria-label')).toBe('현재 위치')
    expect(wrapper.get('.ks-breadcrumb').text()).toContain('매도 의사결정')
    expect(wrapper.text()).toContain('화면 내용')
    expect(wrapper.text()).toContain('RESEARCH_CANDIDATE_NOT_PRODUCTION')
  })

  it('opens a workspace tab for the active route and records it as a recent screen', async () => {
    const pinia = createPinia()
    setActivePinia(pinia)
    const router = buildTestRouter()
    router.push('/research/sell-decision')
    await router.isReady()

    const wrapper = mount(KsAppShell, { global: { plugins: [pinia, router], stubs: { KsDialog: true } } })
    await wrapper.vm.$nextTick()

    expect(wrapper.text()).toContain('매도 의사결정')
  })
})
