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

describe('StandardScreenBoundary contract', () => {
  it('maps screen states into the shared query boundary contract', () => {
    const wrapper = mount(StandardScreenBoundary, {
      props: { state: 'ERROR', error: new Error('failed'), correlationId: 'corr-1', staleAt: '2026-08-13T00:00:00Z' },
      global: {
        stubs: {
          QueryStateBoundary: { name: 'QueryStateBoundary', template: '<div><slot /></div>', props: ['error', 'correlationId', 'staleAt', 'loading', 'processing'] },
        },
      },
      slots: { default: '<article data-test="content">content</article>' },
    })

    const boundary = wrapper.findComponent({ name: 'QueryStateBoundary' })
    expect(boundary.props()).toMatchObject({ loading: false, processing: false, correlationId: 'corr-1', staleAt: '2026-08-13T00:00:00Z' })
    expect(boundary.props('error')).toBeInstanceOf(Error)
    expect((boundary.props('error') as Error).message).toBe('failed')
  })

  it('forwards retry from the shared query boundary', async () => {
    const wrapper = mount(StandardScreenBoundary, {
      props: { state: 'READY' },
      global: { stubs: { QueryStateBoundary: { name: 'QueryStateBoundary', template: '<div><slot /></div>' } } },
    })

    await wrapper.findComponent({ name: 'QueryStateBoundary' }).vm.$emit('retry')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})
