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

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

describe('ScorecardDashboardPage T07 contract', () => {
  it('renders dashboard slots and evidence version for a partial snapshot', () => {
    const wrapper = mount(ScorecardDashboardPage, {
      global,
      props: { title: '리스크 Scorecard', state: 'PARTIAL', evidence: { asOf: '2026-08-12T00:00:00Z', version: 'scorecard-v2' } },
      slots: { kpis: '<div data-test="kpis">KPI</div>', primary: '<div data-test="primary">Primary</div>', secondary: '<div data-test="secondary">Secondary</div>', alerts: '<div data-test="alerts">Alerts</div>', metricDefinitions: '<div data-test="metrics">Definitions</div>' }
    })

    expect(wrapper.find('.ks-page__meta').text()).toContain('Version: scorecard-v2')
    expect(wrapper.find('[data-test="kpis"]').exists()).toBe(true)
    expect(wrapper.find('[data-test="metrics"]').exists()).toBe(true)
    expect(wrapper.text()).toContain('일부 데이터만 표시')
  })

  it('suppresses dashboard content on forbidden and forwards retry after an error', async () => {
    const wrapper = mount(ScorecardDashboardPage, { global, props: { title: '보호된 대시보드', state: 'FORBIDDEN' }, slots: { primary: '<div data-test="sensitive-kpi">민감 KPI</div>' } })
    expect(wrapper.find('[data-test="sensitive-kpi"]').exists()).toBe(false)
    await wrapper.setProps({ state: 'ERROR' })
    await wrapper.findComponent({ name: 'QueryStateBoundary' }).vm.$emit('retry')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
