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

describe('DataFreshnessBadge', () => {
  const base = { asOf: '2026-08-12T00:00:00Z', staleAfterMinutes: 30, now: '2026-08-12T00:29:00Z' }

  it('uses the injected instant instead of the machine clock', () => {
    const wrapper = mount(DataFreshnessBadge, { props: base })
    expect(wrapper.attributes('data-status')).toBe('fresh')
  })

  it('marks data stale only after the configured boundary', () => {
    const wrapper = mount(DataFreshnessBadge, { props: { ...base, now: '2026-08-12T00:30:01Z' } })
    expect(wrapper.attributes('data-status')).toBe('stale')
    expect(wrapper.attributes('aria-label')).toBe('데이터 지연')
  })

  it('exposes source/revision and emits an explicit refresh command only when enabled', async () => {
    const wrapper = mount(DataFreshnessBadge, { props: { ...base, source: 'approved-read-model', revision: 3, refreshable: true } })
    expect(wrapper.text()).toContain('rev 3')
    expect(wrapper.attributes('title')).toContain('approved-read-model')
    await wrapper.trigger('click')
    expect(wrapper.emitted('refresh')).toHaveLength(1)
  })
})
