2021-04-02 19:12:27 +02:00
|
|
|
|
import mockAxios from 'jest-mock-axios'
|
|
|
|
|
import utils from '../src/utils'
|
2021-03-19 20:17:07 +01:00
|
|
|
|
|
|
|
|
|
it('can load valid config.js', () => {
|
2021-04-02 19:12:27 +02:00
|
|
|
|
const config = utils.loadConfig('./tests/input/example.com.config.js')
|
|
|
|
|
expect(config).toMatchObject({
|
|
|
|
|
channels: 'tests/input/example.com.channels.xml',
|
|
|
|
|
days: 1,
|
|
|
|
|
delay: 3000,
|
|
|
|
|
lang: 'en',
|
|
|
|
|
output: 'guide.xml',
|
|
|
|
|
site: 'example.com'
|
|
|
|
|
})
|
|
|
|
|
expect(config.request).toMatchObject({
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'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',
|
|
|
|
|
Cookie: 'abc=123'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
expect(typeof config.request.data).toEqual('function')
|
|
|
|
|
expect(typeof config.url).toEqual('function')
|
|
|
|
|
expect(typeof config.logo).toEqual('function')
|
2021-03-19 20:17:07 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can parse valid channels.xml', () => {
|
|
|
|
|
expect(utils.parseChannels('./tests/input/example.com.channels.xml')).toEqual([
|
|
|
|
|
{
|
|
|
|
|
name: '1 TV',
|
|
|
|
|
xmltv_id: '1TV.com',
|
|
|
|
|
site_id: '1',
|
2021-04-01 03:48:30 +02:00
|
|
|
|
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',
|
2021-04-01 03:48:30 +02:00
|
|
|
|
site: 'example.com',
|
|
|
|
|
lang: undefined,
|
|
|
|
|
logo: undefined
|
2021-03-19 20:17:07 +01:00
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can convert object to xmltv string', () => {
|
2021-04-01 03:48:30 +02:00
|
|
|
|
const channels = utils.parseChannels('./tests/input/example.com.channels.xml')
|
2021-03-19 20:17:07 +01:00
|
|
|
|
const programs = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Program 1',
|
|
|
|
|
description: 'Description for Program 1',
|
|
|
|
|
start: '2021-03-19 06:00:00 +0000',
|
|
|
|
|
stop: '2021-03-19 06:30:00 +0000',
|
|
|
|
|
category: 'Test',
|
2021-04-13 22:28:09 +02:00
|
|
|
|
icon: 'https://example.com/images/Program1.png',
|
2021-04-01 03:48:30 +02:00
|
|
|
|
channel: '1TV.com',
|
|
|
|
|
lang: 'it'
|
2021-03-19 20:17:07 +01:00
|
|
|
|
}
|
|
|
|
|
]
|
2021-04-01 03:48:30 +02:00
|
|
|
|
const output = utils.convertToXMLTV({ channels, programs })
|
2021-03-19 20:17:07 +01:00
|
|
|
|
expect(output).toBe(
|
2021-04-13 22:28:09 +02:00
|
|
|
|
'<?xml version="1.0" encoding="UTF-8" ?><tv>\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name></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"/></programme>\r\n</tv>'
|
2021-03-19 20:17:07 +01: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. &<>"'')
|
|
|
|
|
})
|
2021-03-28 03:00:28 +02:00
|
|
|
|
|
|
|
|
|
it('can escape url', () => {
|
|
|
|
|
const string = 'http://example.com/logos/1TV.png?param1=val¶m2=val'
|
|
|
|
|
expect(utils.escapeString(string)).toBe(
|
|
|
|
|
'http://example.com/logos/1TV.png?param1=val&param2=val'
|
|
|
|
|
)
|
|
|
|
|
})
|
2021-04-02 19:12:27 +02:00
|
|
|
|
|
|
|
|
|
it('can fetch data', () => {
|
|
|
|
|
const config = utils.loadConfig('./tests/input/example.com.config.js')
|
|
|
|
|
utils.fetchData({}, config).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
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|