epg-grabber/tests/utils.test.js

402 lines
17 KiB
JavaScript
Raw Normal View History

import mockAxios from 'jest-mock-axios'
import utils from '../src/utils'
2022-03-29 15:00:02 +02:00
import axios from 'axios'
2021-10-06 02:02:45 +02:00
import path from 'path'
2021-10-14 23:09:18 +02:00
import fs from 'fs'
2021-03-19 20:17:07 +01:00
2022-05-06 12:41:00 +02:00
jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05'))
2021-03-19 20:17:07 +01:00
it('can load valid config.js', () => {
2021-10-06 02:02:45 +02:00
const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
expect(config).toMatchObject({
days: 1,
delay: 3000,
lang: 'en',
site: 'example.com'
})
expect(config.request).toMatchObject({
timeout: 5000,
headers: {
'Content-Type': 'application/json',
Cookie: 'abc=123'
}
})
expect(typeof config.request.data).toEqual('function')
expect(typeof config.url).toEqual('function')
expect(typeof config.logo).toEqual('function')
2021-04-26 14:47:12 +02:00
expect(config.request.data()).toEqual({ accountID: '123' })
expect(config.url()).toEqual('http://example.com/20210319/1tv.json')
expect(config.logo()).toEqual('http://example.com/logos/1TV.png?x=шеллы&sid=777')
2021-03-19 20:17:07 +01:00
})
it('can parse valid channels.xml', () => {
2021-10-14 23:09:18 +02:00
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
2022-01-25 15:51:26 +01:00
const { channels } = utils.parseChannels(file)
2021-11-18 15:31:57 +01:00
expect(channels).toEqual([
2021-03-19 20:17:07 +01:00
{
name: '1 TV',
xmltv_id: '1TV.com',
site_id: '1',
site: 'example.com',
lang: 'fr',
logo: 'https://example.com/logos/1TV.png'
2021-03-19 20:17:07 +01:00
},
{
name: '2 TV',
xmltv_id: '2TV.com',
site_id: '2',
site: 'example.com',
lang: undefined,
logo: undefined
2021-03-19 20:17:07 +01:00
}
])
})
it('can convert object to xmltv string', () => {
2021-10-14 23:09:18 +02:00
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
2022-01-25 15:51:26 +01:00
const { channels } = utils.parseChannels(file)
2021-03-19 20:17:07 +01:00
const programs = [
{
title: 'Program 1',
2022-05-06 12:41:00 +02:00
sub_title: 'Sub-title 1',
2021-03-19 20:17:07 +01:00
description: 'Description for Program 1',
2022-05-06 12:41:00 +02:00
url: 'http://example.com/title.html',
2022-01-10 11:50:06 +01:00
start: 1616133600,
stop: 1616135400,
2021-03-19 20:17:07 +01:00
category: 'Test',
2022-01-19 13:54:56 +01:00
season: 9,
episode: 239,
2021-04-26 14:47:12 +02:00
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
2022-05-05 13:07:06 +02:00
lang: 'it',
date: '20220505',
2022-05-06 12:41:00 +02:00
director: {
value: 'Director 1',
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
},
2022-05-05 13:07:06 +02:00
actor: ['Actor 1', 'Actor 2'],
writer: 'Writer 1'
2021-03-19 20:17:07 +01:00
}
]
2021-11-18 15:31:57 +01:00
const output = utils.convertToXMLTV({ channels, programs })
2021-03-19 20:17:07 +01:00
expect(output).toBe(
2022-05-05 13:07:06 +02:00
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor>Actor 1</actor><actor>Actor 2</actor><writer>Writer 1</writer></credits></programme>\r\n</tv>'
2021-03-19 20:17:07 +01:00
)
})
2021-03-20 15:03:21 +01:00
2022-04-09 17:43:10 +02:00
it('can convert object to xmltv string without season number', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: 'Test',
episode: 239,
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
2022-05-05 13:07:06 +02:00
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">0.238.0/1</episode-num><episode-num system="onscreen">S01E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
2022-04-09 17:43:10 +02:00
)
})
2022-04-20 15:15:58 +02:00
it('can convert object to xmltv string without episode number', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: 'Test',
season: 1,
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
2022-05-05 13:07:06 +02:00
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
2022-04-20 15:15:58 +02:00
)
})
2021-11-05 12:44:56 +01:00
it('can convert object to xmltv string without categories', () => {
const channels = [
{
name: '1 TV',
xmltv_id: '1TV.com',
site_id: '1',
site: 'example.com',
lang: 'fr',
logo: 'https://example.com/logos/1TV.png'
}
]
const programs = [
{
title: 'Program 1',
2022-01-10 11:50:06 +01:00
start: 1616133600,
stop: 1616135400,
2021-11-05 12:44:56 +01:00
channel: '1TV.com',
lang: 'it'
}
]
const config = { site: 'example.com' }
const output = utils.convertToXMLTV({ config, channels, programs })
expect(output).toBe(
2022-05-05 13:07:06 +02:00
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title></programme>\r\n</tv>'
2021-11-05 12:44:56 +01:00
)
})
2021-10-30 23:59:57 +02:00
it('can convert object to xmltv string with multiple categories', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
2022-01-25 15:51:26 +01:00
const { channels } = utils.parseChannels(file)
2021-10-30 23:59:57 +02:00
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
2022-01-10 11:50:06 +01:00
start: 1616133600,
stop: 1616135400,
2021-10-30 23:59:57 +02:00
category: ['Test1', 'Test2'],
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
2021-11-18 15:31:57 +01:00
const output = utils.convertToXMLTV({ channels, programs })
2021-10-30 23:59:57 +02:00
expect(output).toBe(
2022-05-05 13:07:06 +02:00
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
2022-05-05 16:59:42 +02:00
it('can convert object to xmltv string with multiple urls', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: ['Test1', 'Test2'],
2022-05-06 12:41:00 +02:00
url: [
'https://example.com/noattr.html',
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
],
2022-05-05 16:59:42 +02:00
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
it('can convert object to xmltv string with multiple images', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: ['Test1', 'Test2'],
2022-05-06 12:41:00 +02:00
url: [
'https://example.com/noattr.html',
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
],
actor: {
value: 'Actor 1',
image: [
'https://example.com/image1.jpg',
{
value: 'https://example.com/image2.jpg',
type: 'person',
size: '2',
system: 'TestSystem',
orient: 'P'
}
]
},
2022-05-05 16:59:42 +02:00
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/><credits><actor>Actor 1<image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></actor></credits></programme>\r\n</tv>'
)
})
2022-05-05 13:07:06 +02:00
it('can convert object to xmltv string with multiple credits member', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
2022-05-06 12:41:00 +02:00
sub_title: 'Sub-title 1',
2022-05-05 13:07:06 +02:00
description: 'Description for Program 1',
2022-05-06 12:41:00 +02:00
url: 'http://example.com/title.html',
2022-05-05 13:07:06 +02:00
start: 1616133600,
stop: 1616135400,
category: 'Test',
season: 9,
episode: 239,
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it',
date: '20220505',
2022-05-06 12:41:00 +02:00
director: {
value: 'Director 1',
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
},
actor: {
value: 'Actor 1',
role: 'Manny',
guest: 'yes',
url: { value: 'http://example.com/actor1.html', system: 'TestSystem' }
},
writer: [
{ value: 'Writer 1', url: { value: 'http://example.com/w1.html', system: 'TestSystem' } },
{ value: 'Writer 2', url: { value: 'http://example.com/w2.html', system: 'TestSystem' } }
]
2022-05-05 13:07:06 +02:00
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor role="Manny" guest="yes">Actor 1<url system="TestSystem">http://example.com/actor1.html</url></actor><writer>Writer 1<url system="TestSystem">http://example.com/w1.html</url></writer><writer>Writer 2<url system="TestSystem">http://example.com/w2.html</url></writer></credits></programme>\r\n</tv>'
2021-10-30 23:59:57 +02:00
)
})
2021-03-20 15:03:21 +01:00
it('can escape string', () => {
const string = 'Música тест dun. &<>"\'\r\n'
expect(utils.escapeString(string)).toBe('Música тест dun. &amp;&lt;&gt;&quot;&apos;')
})
2021-03-28 03:00:28 +02:00
it('can escape url', () => {
const string = 'http://example.com/logos/1TV.png?param1=val&param2=val'
expect(utils.escapeString(string)).toBe(
'http://example.com/logos/1TV.png?param1=val&amp;param2=val'
)
})
2021-10-13 21:52:41 +02:00
it('can fetch data', () => {
2021-08-21 18:11:06 +02:00
const request = {
data: { accountID: '123' },
headers: {
'Content-Type': 'application/json',
Cookie: 'abc=123',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
},
maxContentLength: 5242880,
method: 'POST',
responseType: 'arraybuffer',
timeout: 5000,
url: 'http://example.com/20210319/1tv.json',
withCredentials: true
}
2022-04-20 15:15:58 +02:00
utils.fetchData(mockAxios, request).then(jest.fn).catch(jest.fn)
expect(mockAxios).toHaveBeenCalledWith(
expect.objectContaining({
data: { accountID: '123' },
headers: {
'Content-Type': 'application/json',
Cookie: 'abc=123',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
},
method: 'POST',
responseType: 'arraybuffer',
timeout: 5000,
url: 'http://example.com/20210319/1tv.json',
withCredentials: true
})
)
})
2021-08-21 18:11:06 +02:00
2021-10-13 21:52:41 +02:00
it('can build request async', done => {
2021-10-06 02:02:45 +02:00
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
2022-04-20 15:15:58 +02:00
utils
.buildRequest({}, config)
.then(request => {
expect(request).toMatchObject({
data: { accountID: '123' },
headers: {
'Content-Type': 'application/json',
Cookie: 'abc=123',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
},
maxContentLength: 5242880,
method: 'POST',
responseType: 'arraybuffer',
timeout: 5000,
url: 'http://example.com/20210319/1tv.json',
withCredentials: true
})
done()
2021-08-21 18:11:06 +02:00
})
2022-04-20 15:15:58 +02:00
.catch(done)
2021-08-21 18:11:06 +02:00
})
2021-08-23 12:45:57 +02:00
2021-10-13 21:52:41 +02:00
it('can load logo async', done => {
2021-10-06 02:02:45 +02:00
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
2022-04-20 15:15:58 +02:00
utils
.loadLogo({}, config)
.then(logo => {
expect(logo).toBe('http://example.com/logos/1TV.png?x=шеллы&sid=777')
done()
})
.catch(done)
2021-08-23 12:45:57 +02:00
})
2022-01-19 15:00:41 +01:00
it('can parse programs', done => {
const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
2022-04-20 15:15:58 +02:00
utils
2022-01-19 15:00:41 +01:00
.parsePrograms({ channel: { xmltv_id: '1tv', lang: 'en' } }, config)
.then(programs => {
expect(programs).toMatchObject([
{
title: 'Title',
description: 'Description',
lang: 'en',
category: ['Category1', 'Category2'],
icon: 'https://example.com/image.jpg',
season: 9,
episode: 238,
start: 1640995200,
stop: 1640998800
}
])
done()
})
2022-04-20 15:15:58 +02:00
.catch(done)
2022-01-19 15:00:41 +01:00
})
2021-10-13 21:52:41 +02:00
it('can parse programs async', done => {
2021-10-06 02:02:45 +02:00
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
2022-04-20 15:15:58 +02:00
utils
2021-08-23 12:45:57 +02:00
.parsePrograms({ channel: { xmltv_id: '1tv', lang: 'en' } }, config)
.then(programs => {
expect(programs.length).toBe(0)
2021-10-13 21:52:41 +02:00
done()
2021-08-23 12:45:57 +02:00
})
2022-04-20 15:15:58 +02:00
.catch(done)
2021-08-23 12:45:57 +02:00
})