const { parser, url, request } = require('./ontvtonight.com.config.js')
const fs = require('fs')
const path = require('path')
const axios = require('axios')
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const customParseFormat = require('dayjs/plugin/customParseFormat')

dayjs.extend(customParseFormat)
dayjs.extend(utc)

const date = dayjs.utc('2021-11-25', 'YYYY-MM-DD').startOf('d')
const channel = {
  site_id: 'au#1692/7two',
  xmltv_id: '7two.au'
}

jest.mock('axios')

axios.head.mockImplementation(() => Promise.resolve({}))

it('can generate valid url', () => {
  expect(url({ channel, date })).toBe(
    'https://www.ontvtonight.com/au/guide/listings/channel/1692/7two.html?dt=2021-11-25'
  )
})

it('can generate valid request headers', async () => {
  expect(await request.headers({ channel, date })).toMatchObject({
    'user-agent':
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36'
  })
})

it('can parse response', () => {
  const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'), 'utf8')
  const result = parser({ content, channel, date }).map(p => {
    p.start = p.start.toJSON()
    p.stop = p.stop.toJSON()
    return p
  })

  expect(result).toMatchObject([
    {
      start: '2021-11-24T13:10:00.000Z',
      stop: '2021-11-24T13:50:00.000Z',
      title: 'What A Carry On'
    },
    {
      start: '2021-11-24T13:50:00.000Z',
      stop: '2021-11-25T11:50:00.000Z',
      title: 'Bones',
      description: 'The Devil In The Details'
    },
    {
      start: '2021-11-25T11:50:00.000Z',
      stop: '2021-11-25T12:50:00.000Z',
      title: 'Inspector Morse: The Remorseful Day'
    }
  ])
})

it('can handle empty guide', () => {
  const result = parser({
    date,
    channel,
    content: fs.readFileSync(path.resolve(__dirname, '__data__/no_content.html'), 'utf8')
  })
  expect(result).toMatchObject([])
})
