From 7df6b224d4ba710a11a8bd6aef34b101e8ae7b19 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Mon, 2 Oct 2023 05:52:53 +0300 Subject: [PATCH] Update tests --- tests/commands/channels/editor.test.ts | 27 +++++ tests/commands/channels/lint.test.js | 19 ---- tests/commands/channels/lint.test.ts | 28 ++++++ tests/commands/channels/parse.test.js | 39 -------- tests/commands/channels/parse.test.ts | 41 ++++++++ .../{validate.test.js => validate.test.ts} | 81 ++++++++------- tests/commands/epg/grab.test.js | 39 -------- tests/commands/epg/grab.test.ts | 98 +++++++++++++++++++ 8 files changed, 239 insertions(+), 133 deletions(-) create mode 100644 tests/commands/channels/editor.test.ts delete mode 100644 tests/commands/channels/lint.test.js create mode 100644 tests/commands/channels/lint.test.ts delete mode 100644 tests/commands/channels/parse.test.js create mode 100644 tests/commands/channels/parse.test.ts rename tests/commands/channels/{validate.test.js => validate.test.ts} (56%) delete mode 100644 tests/commands/epg/grab.test.js create mode 100644 tests/commands/epg/grab.test.ts diff --git a/tests/commands/channels/editor.test.ts b/tests/commands/channels/editor.test.ts new file mode 100644 index 00000000..8499eb03 --- /dev/null +++ b/tests/commands/channels/editor.test.ts @@ -0,0 +1,27 @@ +import fs from 'fs-extra' +import { execSync } from 'child_process' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') + fs.copySync( + 'tests/__data__/input/channels-editor/channels-editor.channels.xml', + 'tests/__data__/output/channels.xml' + ) +}) + +describe('channels:editor', () => { + it('shows list of options for a channel', () => { + const stdout = execSync( + 'DATA_DIR=tests/__data__/input/temp/data npm run channels:editor -- tests/__data__/output/channels.xml', + { + encoding: 'utf8' + } + ) + + expect(stdout).toContain('CNN International | CNNInternational.us [new]') + expect(stdout).toContain('CNN International Europe | CNNInternationalEurope.us [api]') + expect(stdout).toContain('Overwrite') + expect(stdout).toContain('Skip') + expect(stdout).toContain("File 'tests/__data__/output/channels.xml' successfully saved") + }) +}) diff --git a/tests/commands/channels/lint.test.js b/tests/commands/channels/lint.test.js deleted file mode 100644 index 2563ccfc..00000000 --- a/tests/commands/channels/lint.test.js +++ /dev/null @@ -1,19 +0,0 @@ -const { execSync } = require('child_process') - -it('will show a message if the file contains a syntax error', () => { - try { - const stdout = execSync( - 'npm run channels:lint -- tests/__data__/input/sites/lint.channels.xml', - { - encoding: 'utf8' - } - ) - console.log(stdout) - process.exit(1) - } catch (err) { - expect(err.status).toBe(1) - expect(err.stdout).toBe( - `\n> channels:lint\n> node scripts/commands/channels/lint.js tests/__data__/input/sites/lint.channels.xml\n\n\ntests/__data__/input/sites/lint.channels.xml\n 4:0 Element 'channel': The attribute 'lang' is required but missing.\n\n1 error(s)\n` - ) - } -}) diff --git a/tests/commands/channels/lint.test.ts b/tests/commands/channels/lint.test.ts new file mode 100644 index 00000000..2f9baed8 --- /dev/null +++ b/tests/commands/channels/lint.test.ts @@ -0,0 +1,28 @@ +import { execSync } from 'child_process' + +type ExecError = { + status: number + stdout: string +} + +describe('channels:lint', () => { + it('will show a message if the file contains a syntax error', () => { + try { + const stdout = execSync( + 'npm run channels:lint -- tests/__data__/input/channels-lint/channels-lint.channels.xml', + { + encoding: 'utf8' + } + ) + console.log(stdout) + process.exit(1) + } catch (error) { + expect((error as ExecError).status).toBe(1) + expect( + (error as ExecError).stdout.includes( + "tests/__data__/input/channels-lint/channels-lint.channels.xml\n\n\ntests/__data__/input/channels-lint/channels-lint.channels.xml\n 3:0 Element 'channel': The attribute 'lang' is required but missing.\n\n1 error(s)\n" + ) + ).toBe(true) + } + }) +}) diff --git a/tests/commands/channels/parse.test.js b/tests/commands/channels/parse.test.js deleted file mode 100644 index a9242a4f..00000000 --- a/tests/commands/channels/parse.test.js +++ /dev/null @@ -1,39 +0,0 @@ -const { execSync } = require('child_process') -const fs = require('fs-extra') -const path = require('path') - -beforeEach(() => { - fs.emptyDirSync('tests/__data__/output') - fs.copySync( - 'tests/__data__/input/sites/parse-channels.channels.xml', - 'tests/__data__/output/channels.xml' - ) -}) - -it('can parse channels', () => { - const stdout = execSync( - 'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml', - { encoding: 'utf8' } - ) - - expect(content('tests/__data__/output/channels.xml')).toEqual( - content('tests/__data__/expected/sites/parse-channels.channels.xml') - ) -}) - -it('can parse channels with clean flag', () => { - const stdout = execSync( - 'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml --clean', - { encoding: 'utf8' } - ) - - expect(content('tests/__data__/output/channels.xml')).toEqual( - content('tests/__data__/expected/sites/parse-channels-clean.channels.xml') - ) -}) - -function content(filepath) { - return fs.readFileSync(path.resolve(filepath), { - encoding: 'utf8' - }) -} diff --git a/tests/commands/channels/parse.test.ts b/tests/commands/channels/parse.test.ts new file mode 100644 index 00000000..3c1aa4e7 --- /dev/null +++ b/tests/commands/channels/parse.test.ts @@ -0,0 +1,41 @@ +import { execSync } from 'child_process' +import fs from 'fs-extra' +import path from 'path' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') + fs.copySync( + 'tests/__data__/input/channels-parse/channels-parse.channels.xml', + 'tests/__data__/output/channels.xml' + ) +}) + +describe('channels:parse', () => { + it('can parse channels', () => { + execSync( + 'npm run channels:parse -- --config=tests/__data__/input/channels-parse/channels-parse.config.js --output=tests/__data__/output/channels.xml', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/channels.xml')).toEqual( + content('tests/__data__/expected/sites/channels-parse/channels-parse.channels.xml') + ) + }) + + it('can parse channels with clean flag', () => { + execSync( + 'npm run channels:parse -- --config=tests/__data__/input/channels-parse/channels-parse.config.js --output=tests/__data__/output/channels.xml --clean', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/channels.xml')).toEqual( + content('tests/__data__/expected/sites/channels-parse/channels-parse-clean.channels.xml') + ) + }) +}) + +function content(filepath: string) { + return fs.readFileSync(path.resolve(filepath), { + encoding: 'utf8' + }) +} diff --git a/tests/commands/channels/validate.test.js b/tests/commands/channels/validate.test.ts similarity index 56% rename from tests/commands/channels/validate.test.js rename to tests/commands/channels/validate.test.ts index 337fff41..5b225499 100644 --- a/tests/commands/channels/validate.test.js +++ b/tests/commands/channels/validate.test.ts @@ -1,49 +1,58 @@ -const { execSync } = require('child_process') +import { execSync } from 'child_process' -it('will show a message if the file contains a duplicate', () => { - try { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/tmp/data npm run channels:validate -- tests/__data__/input/sites/duplicate.channels.xml', - { - encoding: 'utf8' - } - ) - console.log(stdout) - process.exit(1) - } catch (err) { - expect(err.status).toBe(1) - expect(err.stdout).toBe( - `\n> channels:validate\n> node scripts/commands/channels/validate.js tests/__data__/input/sites/duplicate.channels.xml\n\ntests/__data__/input/sites/duplicate.channels.xml +type ExecError = { + status: number + stdout: string +} + +describe('channels:validate', () => { + it('will show a message if the file contains a duplicate', () => { + try { + const stdout = execSync( + 'DATA_DIR=tests/__data__/input/temp/data npm run channels:validate -- tests/__data__/input/channels-validate/duplicate.channels.xml', + { + encoding: 'utf8' + } + ) + console.log(stdout) + process.exit(1) + } catch (error) { + expect((error as ExecError).status).toBe(1) + expect( + (error as ExecError).stdout + .includes(`tests/__data__/input/channels-validate/duplicate.channels.xml ┌─────────┬─────────────┬──────┬────────────────┬─────────┬─────────┐ │ (index) │ type │ lang │ xmltv_id │ site_id │ name │ ├─────────┼─────────────┼──────┼────────────────┼─────────┼─────────┤ │ 0 │ 'duplicate' │ 'en' │ 'BravoEast.us' │ '140' │ 'Bravo' │ └─────────┴─────────────┴──────┴────────────────┴─────────┴─────────┘ -\n1 error(s) in 1 file(s)\n` - ) - } -}) +\n1 error(s) in 1 file(s)\n`) + ).toBe(true) + } + }) -it('will show a message if the file contains a channel with wrong xmltv_id', () => { - try { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/tmp/data npm run channels:validate -- tests/__data__/input/sites/wrong_xmltv_id.channels.xml', - { - encoding: 'utf8' - } - ) - console.log(stdout) - process.exit(1) - } catch (err) { - expect(err.status).toBe(1) - expect(err.stdout).toBe( - `\n> channels:validate\n> node scripts/commands/channels/validate.js tests/__data__/input/sites/wrong_xmltv_id.channels.xml\n\ntests/__data__/input/sites/wrong_xmltv_id.channels.xml + it('will show a message if the file contains a channel with wrong xmltv_id', () => { + try { + const stdout = execSync( + 'DATA_DIR=tests/__data__/input/temp/data npm run channels:validate -- tests/__data__/input/channels-validate/wrong_xmltv_id.channels.xml', + { + encoding: 'utf8' + } + ) + console.log(stdout) + process.exit(1) + } catch (error) { + expect((error as ExecError).status).toBe(1) + expect( + (error as ExecError).stdout + .includes(`tests/__data__/input/channels-validate/wrong_xmltv_id.channels.xml ┌─────────┬──────────────────┬──────┬────────────────────┬─────────┬─────────────────────┐ │ (index) │ type │ lang │ xmltv_id │ site_id │ name │ ├─────────┼──────────────────┼──────┼────────────────────┼─────────┼─────────────────────┤ │ 0 │ 'wrong_xmltv_id' │ 'en' │ 'CNNInternational' │ '140' │ 'CNN International' │ └─────────┴──────────────────┴──────┴────────────────────┴─────────┴─────────────────────┘ -\n1 error(s) in 1 file(s)\n` - ) - } +\n1 error(s) in 1 file(s)\n`) + ).toBe(true) + } + }) }) diff --git a/tests/commands/epg/grab.test.js b/tests/commands/epg/grab.test.js deleted file mode 100644 index 0cb2a066..00000000 --- a/tests/commands/epg/grab.test.js +++ /dev/null @@ -1,39 +0,0 @@ -const { execSync } = require('child_process') -const fs = require('fs-extra') -const path = require('path') - -beforeEach(() => { - fs.emptyDirSync('tests/__data__/output') -}) - -it('can grab epg', () => { - const stdout = execSync( - 'BASE_DIR=tests/__data__/input CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/tmp/data npm run grab -- --site=epg-grab --output=tests/__data__/output/{lang}/{site}.xml', - { encoding: 'utf8' } - ) - - expect(content('tests/__data__/output/en/example.com.xml')).toEqual( - content('tests/__data__/expected/guides/en/example.com.xml') - ) - - expect(content('tests/__data__/output/fr/example.com.xml')).toEqual( - content('tests/__data__/expected/guides/fr/example.com.xml') - ) -}) - -it('can grab epg with language filter enabled', () => { - const stdout = execSync( - 'BASE_DIR=tests/__data__/input CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/tmp/data npm run grab -- --site=epg-grab --lang=fr --output=tests/__data__/output/fr/guide.xml', - { encoding: 'utf8' } - ) - - expect(content('tests/__data__/output/fr/guide.xml')).toEqual( - content('tests/__data__/expected/guides/fr/example.com.xml') - ) -}) - -function content(filepath) { - return fs.readFileSync(path.resolve(filepath), { - encoding: 'utf8' - }) -} diff --git a/tests/commands/epg/grab.test.ts b/tests/commands/epg/grab.test.ts new file mode 100644 index 00000000..70c029bc --- /dev/null +++ b/tests/commands/epg/grab.test.ts @@ -0,0 +1,98 @@ +import { execSync } from 'child_process' +import fs from 'fs-extra' +import path from 'path' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') +}) + +describe('epg:grab', () => { + it('can grab epg by site name', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --site=example.com --output=tests/__data__/output/guide.xml', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guide.xml')).toEqual( + content('tests/__data__/expected/guide2.xml') + ) + }) + + it('can grab epg with multiple channels.xml files', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/sites/**/*.channels.xml --output=tests/__data__/output/guide.xml', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guide.xml')).toEqual( + content('tests/__data__/expected/guide.xml') + ) + }) + + it('can grab epg with gzip option enabled', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/sites/**/*.channels.xml --output=tests/__data__/output/guide.xml --gzip', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guide.xml')).toEqual( + content('tests/__data__/expected/guide.xml') + ) + + expect(content('tests/__data__/output/guide.xml.gz')).toEqual( + content('tests/__data__/expected/guide.xml.gz') + ) + }) + + it('can grab epg with wildcard as output', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/sites/example.com/example.com.channels.xml --output=tests/__data__/output/guides/{lang}/{site}.xml', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guides/en/example.com.xml')).toEqual( + content('tests/__data__/expected/guides/en/example.com.xml') + ) + + expect(content('tests/__data__/output/guides/fr/example.com.xml')).toEqual( + content('tests/__data__/expected/guides/fr/example.com.xml') + ) + }) + + it('can grab epg then language filter enabled', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/sites/example.com/example.com.channels.xml --output=tests/__data__/output/guides/{lang}/{site}.xml --lang=fr', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guides/fr/example.com.xml')).toEqual( + content('tests/__data__/expected/guides/fr/example.com.xml') + ) + }) + + it('can grab epg using custom channels list', () => { + execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/custom.channels.xml --output=tests/__data__/output/guide.xml', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/guide.xml')).toEqual( + content('tests/__data__/expected/guide.xml') + ) + }) + + it('it will raise an error if the timeout is exceeded', () => { + const stdout = execSync( + 'SITES_DIR=tests/__data__/input/epg-grab/sites CURR_DATE=2022-10-20 DATA_DIR=tests/__data__/input/temp/data npm run grab -- --channels=tests/__data__/input/epg-grab/custom.channels.xml --output=tests/__data__/output/guide.xml --timeout=0', + { encoding: 'utf8' } + ) + + expect(stdout).toContain('ERR: Connection timeout') + }) +}) + +function content(filepath: string) { + return fs.readFileSync(path.resolve(filepath), { + encoding: 'utf8' + }) +}