import mockAxios from 'jest-mock-axios' import utils from '../src/utils' import axios from 'axios' import path from 'path' import fs from 'fs' jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05')) it('can load valid config.js', () => { 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') 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') }) it('can parse valid channels.xml', () => { const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' }) const { channels } = utils.parseChannels(file) expect(channels).toEqual([ { name: '1 TV', xmltv_id: '1TV.com', site_id: '1', site: 'example.com', lang: 'fr', logo: 'https://example.com/logos/1TV.png' }, { name: '2 TV', xmltv_id: '2TV.com', site_id: '2', site: 'example.com', lang: undefined, logo: undefined } ]) }) it('can convert object to xmltv string', () => { const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' }) const { channels } = utils.parseChannels(file) const programs = [ { title: 'Program 1', sub_title: 'Sub-title 1', description: 'Description for Program 1', url: 'http://example.com/title.html', 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', director: { value: 'Director 1', url: { value: 'http://example.com/director1.html', system: 'TestSystem' } }, actor: ['Actor 1', 'Actor 2'], writer: 'Writer 1' } ] const output = utils.convertToXMLTV({ channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Sub-title 1Description for Program 1Testhttp://example.com/title.html8.238.0/1S09E23920220505Director 1http://example.com/director1.htmlActor 1Actor 2Writer 1\r\n' ) }) 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( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Description for Program 1Test0.238.0/1S01E239\r\n' ) }) 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( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Description for Program 1Test\r\n' ) }) 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', start: 1616133600, stop: 1616135400, channel: '1TV.com', lang: 'it' } ] const config = { site: 'example.com' } const output = utils.convertToXMLTV({ config, channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\nProgram 1\r\n' ) }) it('can convert object to xmltv string with multiple categories', () => { 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'], icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777', channel: '1TV.com', lang: 'it' } ] const output = utils.convertToXMLTV({ channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Description for Program 1Test1Test2\r\n' ) }) 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'], url: [ 'https://example.com/noattr.html', { value: 'https://example.com/attr.html', system: 'TestSystem' } ], icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777', channel: '1TV.com', lang: 'it' } ] const output = utils.convertToXMLTV({ channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Description for Program 1Test1Test2https://example.com/noattr.htmlhttps://example.com/attr.html\r\n' ) }) 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'], 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' } ] }, icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777', channel: '1TV.com', lang: 'it' } ] const output = utils.convertToXMLTV({ channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Description for Program 1Test1Test2https://example.com/noattr.htmlhttps://example.com/attr.htmlActor 1https://example.com/image1.jpghttps://example.com/image2.jpg\r\n' ) }) 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', sub_title: 'Sub-title 1', description: 'Description for Program 1', url: 'http://example.com/title.html', 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', 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' } } ] } ] const output = utils.convertToXMLTV({ channels, programs }) expect(output).toBe( '\r\n1 TVhttps://example.com\r\n2 TVhttps://example.com\r\nProgram 1Sub-title 1Description for Program 1Testhttp://example.com/title.html8.238.0/1S09E23920220505Director 1http://example.com/director1.htmlActor 1http://example.com/actor1.htmlWriter 1http://example.com/w1.htmlWriter 2http://example.com/w2.html\r\n' ) }) it('can escape string', () => { const string = 'Música тест dun. &<>"\'\r\n' expect(utils.escapeString(string)).toBe('Música тест dun. &<>"'') }) 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' ) }) it('can fetch data', () => { 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 } 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 }) ) }) it('can build request async', done => { const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js'))) 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() }) .catch(done) }) it('can load logo async', done => { const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js'))) utils .loadLogo({}, config) .then(logo => { expect(logo).toBe('http://example.com/logos/1TV.png?x=шеллы&sid=777') done() }) .catch(done) }) it('can parse programs', done => { const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js'))) utils .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() }) .catch(done) }) it('can parse programs async', done => { const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js'))) utils .parsePrograms({ channel: { xmltv_id: '1tv', lang: 'en' } }, config) .then(programs => { expect(programs.length).toBe(0) done() }) .catch(done) })