import { describe, expect, it } from 'vitest'
import { createApp } from 'vue'
import { resolveUiProvider } from '../resolveUiProvider'
import { useUiAdapter } from '../../adapter/useUiAdapter'

describe('resolveUiProvider', () => {
  it('uses PrimeVue as the default provider', async () => {
    await expect(resolveUiProvider(undefined)).resolves.toMatchObject({ id: 'primevue-aggrid' })
  })

  it('loads the selected native provider', async () => {
    await expect(resolveUiProvider('native')).resolves.toMatchObject({ id: 'native-accessible' })
  })

  it('fails closed for an unsupported provider before application mount', async () => {
    await expect(resolveUiProvider('unsupported')).rejects.toThrow(
      "Unsupported VITE_UI_ADAPTER 'unsupported'. Allowed values: primevue, native."
    )
  })

  it('installs the validated adapter before the application is mounted', async () => {
    const provider = await resolveUiProvider('native')
    const app = createApp({ template: '<div />' })

    provider.install(app)

    expect(app.runWithContext(() => useUiAdapter()).descriptor.id).toBe('native-accessible')
  })

  it('keeps the native provider available only to the non-production contract harness', async () => {
    const provider = await resolveUiProvider('native')
    expect(provider.id).toBe('native-accessible')
  })
})
