import mediaDeviceReducer, { selectCurrentMediaDeviceId, setSelectedMediaDevice } from './mediaDeviceSlice'
import { mockRootState } from '../../testUtils'

test('sets the selected media device for audio inputs', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
    selectedAudioOutputDeviceId: null,
    videoIsLoading: false,
  }

  const newState = mediaDeviceReducer(
    state,
    setSelectedMediaDevice({ kind: 'audioinput', deviceId: 'testAudioDeviceId' })
  )

  expect(newState).toStrictEqual({
    selectedAudioDeviceId: 'testAudioDeviceId',
    selectedAudioOutputDeviceId: null,
    selectedVideoDeviceId: null,
    videoIsLoading: false,
  })
})

test('sets the selected media device for audio outputs', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
    selectedAudioOutputDeviceId: null,
    videoIsLoading: false,
  }

  const newState = mediaDeviceReducer(
    state,
    setSelectedMediaDevice({ kind: 'audiooutput', deviceId: 'testAudioOutputDeviceId' })
  )

  expect(newState).toStrictEqual({
    selectedAudioDeviceId: null,
    selectedAudioOutputDeviceId: 'testAudioOutputDeviceId',
    selectedVideoDeviceId: null,
    videoIsLoading: false,
  })
})

test('sets the selected media device for video inputs', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
    selectedAudioOutputDeviceId: null,
    videoIsLoading: false,
  }

  const newState = mediaDeviceReducer(
    state,
    setSelectedMediaDevice({ kind: 'videoinput', deviceId: 'testVideoDeviceId' })
  )

  expect(newState).toStrictEqual({
    selectedAudioDeviceId: null,
    selectedAudioOutputDeviceId: null,
    selectedVideoDeviceId: 'testVideoDeviceId',
    videoIsLoading: false,
  })
})

test('selectCurrentMediaDeviceId selects the current audio device id', () => {
  const state = {
    selectedAudioDeviceId: 'testAudioDeviceId',
    selectedVideoDeviceId: null,
    selectedAudioOutputDeviceId: null,
    videoIsLoading: false,
  }

  const selectedAudioDeviceId = selectCurrentMediaDeviceId({ ...mockRootState, mediaDevice: state }, 'audioinput')

  expect(selectedAudioDeviceId).toEqual('testAudioDeviceId')
})

test('selectCurrentMediaDeviceId selects the current video device id', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: 'testVideoDeviceId',
    selectedAudioOutputDeviceId: null,
    videoIsLoading: false,
  }

  const selectedVideoDeviceId = selectCurrentMediaDeviceId({ ...mockRootState, mediaDevice: state }, 'videoinput')

  expect(selectedVideoDeviceId).toEqual('testVideoDeviceId')
})

test('selectCurrentMediaDeviceId selects the current audio output device id', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
    selectedAudioOutputDeviceId: 'testAudioOutputDeviceId',
    videoIsLoading: false,
  }

  const selectedVideoDeviceId = selectCurrentMediaDeviceId({ ...mockRootState, mediaDevice: state }, 'audiooutput')

  expect(selectedVideoDeviceId).toEqual('testAudioOutputDeviceId')
})
