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

const sections = [{ module: 'ModelOps', entries: [{ screenId: 'models', path: '/model-ops/models', module: 'ModelOps', section: 'ModelOps', title: 'Models', order: 1, favoriteAllowed: true, internalOnly: false, permissions: [] }] }]
const routerLinkStub = { props: ['to'], template: '<a :href="to"><slot /></a>' }
const global = { stubs: { RouterLink: routerLinkStub } }

describe('KsSideNavigation contract', () => {
  it('marks a parent menu active for a nested detail route', () => {
    const wrapper = mount(KsSideNavigation, { props: { sections, activePath: '/model-ops/models/model-1' }, global })
    const link = wrapper.get('a')
    expect(link.classes()).toContain('active')
    expect(link.attributes('aria-current')).toBe('page')
  })

  it('does not mark an unrelated route active', () => {
    const wrapper = mount(KsSideNavigation, { props: { sections, activePath: '/portfolio/risk' }, global })
    expect(wrapper.get('a').classes()).not.toContain('active')
    expect(wrapper.get('a').attributes('aria-current')).toBeUndefined()
  })

  it('collapses a module section without changing its navigation entries', async () => {
    const wrapper = mount(KsSideNavigation, { props: { sections, activePath: '' }, global })
    const toggle = wrapper.get('.ks-side-nav__section-toggle')
    expect(toggle.attributes('aria-expanded')).toBe('true')
    await toggle.trigger('click')
    expect(wrapper.emitted('toggleSection')).toEqual([['ModelOps']])
    await wrapper.setProps({ collapsedSections: ['ModelOps'] })
    expect(wrapper.get('.ks-side-nav__section-toggle').attributes('aria-expanded')).toBe('false')
    expect(wrapper.find('a').exists()).toBe(false)
  })
})
