2023-10-15 14:08:23 +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' )
2025-01-12 23:07:10 +07:00
2023-10-15 14:08:23 +03:00
dayjs . extend ( customParseFormat )
dayjs . extend ( utc )
jest . mock ( 'axios' )
2025-01-12 23:07:10 +07:00
const date = dayjs . utc ( '2025-01-12' , 'YYYY-MM-DD' ) . startOf ( 'd' )
2023-10-15 14:08:23 +03:00
const channel = {
2025-01-12 23:07:10 +07:00
site _id : '9200018514' ,
2023-10-15 14:08:23 +03:00
xmltv _id : 'CBSEast.us'
}
2025-01-12 23:07:10 +07:00
axios . get . mockImplementation ( url => {
const result = { }
const urls = {
'https://www.tvguide.com/listings/' :
'content.html' ,
'https://backend.tvguide.com/tvschedules/tvguide/9100001138/web?start=1736640000&duration=240&apiKey=DI9elXhZ3bU6ujsA2gXEKOANyncXGUGc' :
'content1.json' ,
'https://backend.tvguide.com/tvschedules/tvguide/9100001138/web?start=1736654400&duration=240&apiKey=DI9elXhZ3bU6ujsA2gXEKOANyncXGUGc' :
'content2.json' ,
'https://backend.tvguide.com/tvschedules/tvguide/programdetails/9000351140/web' :
'program1.json' ,
'https://backend.tvguide.com/tvschedules/tvguide/programdetails/9000000408/web' :
'program2.json' ,
}
if ( urls [ url ] !== undefined ) {
result . data = fs . readFileSync ( path . join ( _ _dirname , '__data__' , urls [ url ] ) ) . toString ( )
if ( ! urls [ url ] . startsWith ( 'content1' ) && ! urls [ url ] . endsWith ( '.html' ) ) {
result . data = JSON . parse ( result . data )
}
}
return Promise . resolve ( result )
} )
it ( 'can generate valid url' , async ( ) => {
expect ( await url ( { date } ) ) . toBe (
'https://backend.tvguide.com/tvschedules/tvguide/9100001138/web?start=1736640000&duration=240&apiKey=DI9elXhZ3bU6ujsA2gXEKOANyncXGUGc'
2023-10-15 14:08:23 +03:00
)
} )
it ( 'can parse response' , async ( ) => {
2025-01-12 23:07:10 +07:00
const content = fs . readFileSync ( path . join ( _ _dirname , '__data__' , 'content1.json' ) ) . toString ( )
const results = ( await parser ( { content , channel , date } ) ) . map ( p => {
2023-10-15 14:08:23 +03:00
p . start = p . start . toJSON ( )
p . stop = p . stop . toJSON ( )
return p
} )
2025-01-12 23:07:10 +07:00
expect ( results . length ) . toBe ( 5 )
expect ( results [ 0 ] ) . toMatchObject ( {
start : '2025-01-12T01:00:00.000Z' ,
stop : '2025-01-12T02:00:00.000Z' ,
title : 'FBI: International' ,
sub _title : 'Gift' ,
description :
'The owner of a prominent cyber security company is murdered in Copenhagen just before a massive data leak surfaces online, leading the NSA to ask the team for assistance in catching the killer and leaker before more data is revealed.' ,
categories : [ 'Action & Adventure' , 'Suspense' , 'Drama' ] ,
season : 3 ,
episode : 12 ,
rating : {
system : 'MPA' ,
value : 'L'
}
} )
expect ( results [ 4 ] ) . toMatchObject ( {
start : '2025-01-12T06:00:00.000Z' ,
stop : '2025-01-12T08:00:00.000Z' ,
title : 'Local Programs' ,
2023-10-15 14:08:23 +03:00
description :
2025-01-12 23:07:10 +07:00
'Local programming information.' ,
categories : [ ] ,
2023-10-15 14:08:23 +03:00
rating : {
system : 'MPA' ,
2025-01-12 23:07:10 +07:00
value : 'L'
2023-10-15 14:08:23 +03:00
}
} )
} )
it ( 'can handle empty guide' , async ( ) => {
const results = await parser ( {
date ,
channel ,
2025-01-12 23:07:10 +07:00
content : fs . readFileSync ( path . join ( _ _dirname , '__data__' , 'no-content.json' ) ) . toString ( )
2023-10-15 14:08:23 +03:00
} )
expect ( results ) . toMatchObject ( [ ] )
} )