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

const actions = [
  { id: 'save', label: 'Save' },
  { id: 'review', label: 'Review', disabled: true },
  { id: 'publish', label: 'Publish', busy: true }
] as const

describe('KsCommandBar', () => {
  it('preserves supplied action order and executes only enabled, idle actions', async () => {
    const wrapper = mount(KsCommandBar, {
      props: { actions, ariaLabel: 'Decision actions' },
      global: { stubs: { KsButton: { name: 'KsButton', props: ['label'], template: '<button>{{ label }}</button>' } } }
    })

    expect(wrapper.findAll('button').map(button => button.text())).toEqual(['Save', 'Review', 'Publish'])
    const buttons = wrapper.findAllComponents({ name: 'KsButton' })
    buttons[0].vm.$emit('click')
    buttons[1].vm.$emit('click')
    buttons[2].vm.$emit('click')
    await wrapper.vm.$nextTick()

    expect(wrapper.emitted('execute')).toEqual([['save']])
    expect(wrapper.get('nav').attributes()).toMatchObject({ 'aria-label': 'Decision actions', 'aria-busy': 'true' })
  })
})
