import { describe, it, expect } from 'vitest'
import { registerEndpoint } from '@nuxt/test-utils/runtime'
import { getWebRTCFailureReason } from '../../app/composables/useWebRTCFailureReason.js'

describe('useWebRTCFailureReason', () => {
  it('returns wrongHost null when server and client hostname match', async () => {
    registerEndpoint('/api/live/debug-request-host', () => ({ hostname: 'localhost' }))
    const result = await getWebRTCFailureReason()
    expect(result).toEqual({ wrongHost: null })
  })

  it('returns wrongHost when server and client hostname differ', async () => {
    registerEndpoint('/api/live/debug-request-host', () => ({ hostname: 'server.example.com' }))
    const result = await getWebRTCFailureReason()
    expect(result.wrongHost).toBeDefined()
    expect(result.wrongHost?.serverHostname).toBe('server.example.com')
  })

  it('returns wrongHost null when fetch fails', async () => {
    registerEndpoint('/api/live/debug-request-host', () => {
      throw new Error('network')
    })
    const result = await getWebRTCFailureReason()
    expect(result).toEqual({ wrongHost: null })
  })
})
