import { describe, it, expect } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import CameraViewer from '../../app/components/CameraViewer.vue'

const createCamera = (overrides = {}) => ({
  id: 't1',
  name: 'Test Camera',
  streamUrl: 'https://example.com/stream.mjpg',
  sourceType: 'mjpeg',
  ...overrides,
})

describe('CameraViewer (device stream)', () => {
  it('renders device name and close button', async () => {
    const wrapper = await mountSuspended(CameraViewer, {
      props: { camera: createCamera({ name: 'Test Camera' }) },
    })
    expect(wrapper.text()).toContain('Test Camera')
    expect(wrapper.find('button[aria-label="Close panel"]').exists()).toBe(true)
  })

  it.each([
    ['javascript:alert(1)', false],
    ['https://example.com/cam.mjpg', true],
  ])('handles streamUrl: %s -> img exists: %s', async (streamUrl, shouldExist) => {
    const wrapper = await mountSuspended(CameraViewer, {
      props: { camera: createCamera({ streamUrl }) },
    })
    const img = wrapper.find('img')
    expect(img.exists()).toBe(shouldExist)
    if (shouldExist) {
      expect(img.attributes('src')).toBe(streamUrl)
    }
  })

  it('emits close when close button clicked', async () => {
    const wrapper = await mountSuspended(CameraViewer, {
      props: { camera: createCamera() },
    })
    await wrapper.find('button[aria-label="Close panel"]').trigger('click')
    expect(wrapper.emitted('close')).toHaveLength(1)
  })

  it('shows stream unavailable when img errors', async () => {
    const wrapper = await mountSuspended(CameraViewer, {
      props: { camera: createCamera({ streamUrl: 'https://example.com/bad.mjpg' }) },
    })
    await wrapper.find('img').trigger('error')
    await wrapper.vm.$nextTick()
    expect(wrapper.text()).toContain('Stream unavailable')
  })

  it('renders video element for hls sourceType', async () => {
    const wrapper = await mountSuspended(CameraViewer, {
      props: { camera: createCamera({ sourceType: 'hls', streamUrl: 'https://example.com/stream.m3u8' }) },
    })
    expect(wrapper.find('video').exists()).toBe(true)
  })
})
