Files
epg/sites/tvguide.com/tvguide.com.test.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-07-31 22:29:01 +03:00
const { parser, url } = require('./tvguide.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)
jest.mock('axios')
2025-08-10 08:00:26 +03:00
const date = dayjs.utc('2025-07-29', 'YYYY-MM-DD').startOf('d')
2025-07-31 22:29:01 +03:00
const channel = {
2025-08-10 08:00:26 +03:00
site_id: '9200004683',
2025-08-13 15:55:45 +02:00
xmltv_id: 'NationalGeographicWild.us@East'
2025-07-31 22:29:01 +03:00
}
2025-08-10 08:00:26 +03:00
it('can generate valid url', async () => {
axios.get.mockImplementation(url => {
if (url === 'https://www.tvguide.com/listings/') {
return Promise.resolve({
2025-08-13 15:55:45 +02:00
data: fs.readFileSync(path.join(__dirname, '__data__', 'content.html'), 'utf8')
2025-08-10 08:00:26 +03:00
})
2025-07-31 22:29:01 +03:00
}
2025-08-10 08:00:26 +03:00
throw new Error(`Unexpected URL: ${url}`)
})
2025-07-31 22:29:01 +03:00
2025-08-10 08:00:26 +03:00
const result = await url({ date })
expect(result).toBe(
'https://backend.tvguide.com/tvschedules/tvguide/9100001138/web?start=1753747200&duration=240&apiKey=DI9elXhZ3bU6ujsA2gXEKOANyncXGUGc'
2025-07-31 22:29:01 +03:00
)
})
it('can parse response', async () => {
2025-08-13 15:55:45 +02:00
const content = JSON.parse(fs.readFileSync(path.join(__dirname, '__data__', 'content.json'), 'utf-8'))
2025-08-10 08:00:26 +03:00
axios.get.mockImplementation(url => {
if (
url ===
'https://backend.tvguide.com/tvschedules/tvguide/programdetails/9000058285/web'
) {
return Promise.resolve({
2025-08-13 15:55:45 +02:00
data: JSON.parse(fs.readFileSync(path.join(__dirname, '__data__', 'program.json')))
2025-08-10 08:00:26 +03:00
})
} else {
return Promise.resolve({ data: '' })
}
})
let results = await parser({ content, date, channel, fetchSegments: false })
results = results.map(p => {
2025-07-31 22:29:01 +03:00
p.start = p.start.toJSON()
p.stop = p.stop.toJSON()
return p
})
expect(results[0]).toMatchObject({
2025-08-10 08:00:26 +03:00
start: '2025-07-29T00:00:00.000Z',
stop: '2025-07-29T01:00:00.000Z',
title: 'Secrets of the Zoo: North Carolina',
sub_title: 'Chimp Off the Old Block',
2025-07-31 22:29:01 +03:00
description:
2025-08-10 08:00:26 +03:00
'Chimps living at the North Carolina Zoo, a zoo located in the center of North Carolina that serves as the world\'s largest natural habitat zoo, as well as one of two state-supported zoos, are cared for',
categories: ['Reality'],
season: 1,
episode: 1,
2025-07-31 22:29:01 +03:00
})
})
it('can handle empty guide', async () => {
const results = await parser({
date,
channel,
2025-08-13 15:55:45 +02:00
content: fs.readFileSync(path.join(__dirname, '__data__', 'no-content.json'))
2025-07-31 22:29:01 +03:00
})
expect(results).toMatchObject([])
})