2025-09-28 17:55:05 +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' )
const date = dayjs . utc ( '2025-07-29' , 'YYYY-MM-DD' ). startOf ( 'd' )
const channel = {
site_id : '9200004683' ,
xmltv_id : 'NationalGeographicWild.us@East'
}
it ( 'can generate valid url' , async () => {
const result = await url ({ date })
expect ( result ). toBe (
2026-04-19 04:55:09 +03:00
'https://backend.tvguide.com/tvschedules/tvguide/9100001138/web?start=1753747200&duration=240'
2025-09-28 17:55:05 +03:00
)
})
it ( 'can parse response' , async () => {
2026-04-19 04:55:09 +03:00
const content = JSON . parse (
fs . readFileSync ( path . join ( __dirname , '__data__' , 'content.json' ), 'utf-8' )
)
2025-09-28 17:55:05 +03:00
axios . get . mockImplementation ( url => {
2026-04-19 04:55:09 +03:00
if ( url === 'https://backend.tvguide.com/tvschedules/tvguide/programdetails/9000058285/web' ) {
2025-09-28 17:55:05 +03:00
return Promise . resolve ({
data : JSON . parse ( fs . readFileSync ( path . join ( __dirname , '__data__' , 'program.json' )))
})
} else {
return Promise . resolve ({ data : '' })
}
})
let results = await parser ({ content , date , channel , fetchSegments : false })
results = results . map ( p => {
p . start = p . start . toJSON ()
p . stop = p . stop . toJSON ()
return p
})
expect ( results [ 0 ]). toMatchObject ({
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' ,
description :
2026-04-19 04:55:09 +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" ,
2025-09-28 17:55:05 +03:00
categories : [ 'Reality' ],
season : 1 ,
2026-04-19 04:55:09 +03:00
episode : 1
2025-09-28 17:55:05 +03:00
})
})
it ( 'can handle empty guide' , async () => {
const results = await parser ({
date ,
channel ,
content : fs . readFileSync ( path . join ( __dirname , '__data__' , 'no-content.json' ))
})
expect ( results ). toMatchObject ([])
})