From 88e62715e81d7318b238203ff95b74753cda7dd0 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Tue, 22 Apr 2025 03:47:28 +0300 Subject: [PATCH] Update tests --- tests/commands/api/generate.test.ts | 25 ++++++--- tests/commands/playlist/format.test.ts | 30 +++++++---- tests/commands/playlist/generate.test.ts | 39 +++++++++------ tests/commands/playlist/test.test.ts | 35 +++++++++---- tests/commands/playlist/update.test.ts | 40 ++++++++------- tests/commands/playlist/validate.test.ts | 64 +++++++++++++----------- tests/commands/readme/update.test.ts | 27 ++++++---- tests/commands/report/create.test.ts | 26 ++++++---- 8 files changed, 175 insertions(+), 111 deletions(-) diff --git a/tests/commands/api/generate.test.ts b/tests/commands/api/generate.test.ts index 7f8f68aa93..3f2178da67 100644 --- a/tests/commands/api/generate.test.ts +++ b/tests/commands/api/generate.test.ts @@ -1,19 +1,28 @@ import { execSync } from 'child_process' import fs from 'fs-extra' +import os from 'os' + +let ENV_VAR = + 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/api_generate API_DIR=tests/__data__/output/.api' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/data" && SET "STREAMS_DIR=tests/__data__/input/api_generate" && SET "API_DIR=tests/__data__/output/.api" &&' +} beforeEach(() => { fs.emptyDirSync('tests/__data__/output') }) -it('can create streams.json', () => { - execSync( - 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/api_generate API_DIR=tests/__data__/output/.api npm run api:generate', - { encoding: 'utf8' } - ) +describe('api:generate', () => { + it('can create streams.json', () => { + const cmd = `${ENV_VAR} npm run api:generate` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) - expect(content('output/.api/streams.json')).toMatchObject( - content('expected/api_generate/.api/streams.json') - ) + expect(content('output/.api/streams.json')).toMatchObject( + content('expected/api_generate/.api/streams.json') + ) + }) }) function content(filepath: string) { diff --git a/tests/commands/playlist/format.test.ts b/tests/commands/playlist/format.test.ts index d513c8b386..58174f93a3 100644 --- a/tests/commands/playlist/format.test.ts +++ b/tests/commands/playlist/format.test.ts @@ -1,25 +1,33 @@ import { execSync } from 'child_process' import * as fs from 'fs-extra' import { glob } from 'glob' +import os from 'os' + +let ENV_VAR = 'STREAMS_DIR=tests/__data__/output/streams' +if (os.platform() === 'win32') { + ENV_VAR = 'SET "STREAMS_DIR=tests/__data__/output/streams" &&' +} beforeEach(() => { fs.emptyDirSync('tests/__data__/output') fs.copySync('tests/__data__/input/playlist_format', 'tests/__data__/output/streams') }) -it('can format playlists', () => { - execSync('STREAMS_DIR=tests/__data__/output/streams npm run playlist:format', { - encoding: 'utf8' - }) +describe('playlist:format', () => { + it('can format playlists', () => { + const cmd = `${ENV_VAR} npm run playlist:format` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) - const files = glob - .sync('tests/__data__/expected/playlist_format/*.m3u') - .map(f => f.replace('tests/__data__/expected/playlist_format/', '')) + const files = glob + .sync('tests/__data__/expected/playlist_format/*.m3u') + .map(f => f.replace('tests/__data__/expected/playlist_format/', '')) - files.forEach(filepath => { - expect(content(`output/streams/${filepath}`), filepath).toBe( - content(`expected/playlist_format/${filepath}`) - ) + files.forEach(filepath => { + expect(content(`output/streams/${filepath}`), filepath).toBe( + content(`expected/playlist_format/${filepath}`) + ) + }) }) }) diff --git a/tests/commands/playlist/generate.test.ts b/tests/commands/playlist/generate.test.ts index 389ee022ee..d51428aca6 100644 --- a/tests/commands/playlist/generate.test.ts +++ b/tests/commands/playlist/generate.test.ts @@ -1,32 +1,39 @@ import { execSync } from 'child_process' import * as fs from 'fs-extra' import * as glob from 'glob' +import os from 'os' + +let ENV_VAR = + 'STREAMS_DIR=tests/__data__/input/playlist_generate DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output/.gh-pages LOGS_DIR=tests/__data__/output/logs' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "STREAMS_DIR=tests/__data__/input/playlist_generate" && SET "DATA_DIR=tests/__data__/input/data" && SET "PUBLIC_DIR=tests/__data__/output/.gh-pages" && SET "LOGS_DIR=tests/__data__/output/logs" &&' +} beforeEach(() => { fs.emptyDirSync('tests/__data__/output') }) -it('can generate playlists and logs', () => { - const stdout = execSync( - 'STREAMS_DIR=tests/__data__/input/playlist_generate DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output/.gh-pages LOGS_DIR=tests/__data__/output/logs npm run playlist:generate', - { encoding: 'utf8' } - ) +describe('playlist:generate', () => { + it('can generate playlists and logs', () => { + const cmd = `${ENV_VAR} npm run playlist:generate` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) - if (process.env.DEBUG === 'true') console.log(stdout) + const playlists = glob + .sync('tests/__data__/expected/playlist_generate/.gh-pages/**/*.m3u') + .map((file: string) => file.replace('tests/__data__/expected/playlist_generate/', '')) - const playlists = glob - .sync('tests/__data__/expected/playlist_generate/.gh-pages/**/*.m3u') - .map((file: string) => file.replace('tests/__data__/expected/playlist_generate/', '')) + playlists.forEach((filepath: string) => { + expect(content(`output/${filepath}`), filepath).toBe( + content(`expected/playlist_generate/${filepath}`) + ) + }) - playlists.forEach((filepath: string) => { - expect(content(`output/${filepath}`), filepath).toBe( - content(`expected/playlist_generate/${filepath}`) + expect(content('output/logs/generators.log').split('\n').sort()).toStrictEqual( + content('expected/playlist_generate/logs/generators.log').split('\n').sort() ) }) - - expect(content('output/logs/generators.log').split('\n').sort()).toStrictEqual( - content('expected/playlist_generate/logs/generators.log').split('\n').sort() - ) }) function content(filepath: string) { diff --git a/tests/commands/playlist/test.test.ts b/tests/commands/playlist/test.test.ts index 0842f0f108..0020106dce 100644 --- a/tests/commands/playlist/test.test.ts +++ b/tests/commands/playlist/test.test.ts @@ -1,19 +1,32 @@ import { execSync } from 'child_process' +import os from 'os' type ExecError = { status: number stdout: string } -it('shows an error if the playlist contains a broken link', () => { - try { - execSync('ROOT_DIR=tests/__data__/input npm run playlist:test playlist_test/ag.m3u', { - encoding: 'utf8' - }) - process.exit(1) - } catch (error) { - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain('playlist_test/ag.m3u') - expect((error as ExecError).stdout).toContain('2 problems (1 errors, 1 warnings)') - } +let ENV_VAR = 'ROOT_DIR=tests/__data__/input' +if (os.platform() === 'win32') { + ENV_VAR = 'SET "ROOT_DIR=tests/__data__/input" &&' +} + +describe('playlist:test', () => { + it('shows an error if the playlist contains a broken link', () => { + const cmd = `${ENV_VAR} npm run playlist:test playlist_test/ag.m3u` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + checkStdout(stdout) + } catch (error) { + // NOTE: for Windows only + if (process.env.DEBUG === 'true') console.log(cmd, error) + checkStdout((error as ExecError).stdout) + } + }) }) + +function checkStdout(stdout: string) { + expect(stdout).toContain('playlist_test/ag.m3u') + expect(stdout).toContain('2 problems (1 errors, 1 warnings)') +} diff --git a/tests/commands/playlist/update.test.ts b/tests/commands/playlist/update.test.ts index cc104d8cce..16dbefaf85 100644 --- a/tests/commands/playlist/update.test.ts +++ b/tests/commands/playlist/update.test.ts @@ -1,33 +1,39 @@ import { execSync } from 'child_process' import * as fs from 'fs-extra' import { glob } from 'glob' +import os from 'os' + +let ENV_VAR = 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/output/streams' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/data" && SET "STREAMS_DIR=tests/__data__/output/streams" &&' +} beforeEach(() => { fs.emptyDirSync('tests/__data__/output') fs.copySync('tests/__data__/input/playlist_update', 'tests/__data__/output/streams') }) -it('can update playlists', () => { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/output/streams npm run playlist:update --silent', - { - encoding: 'utf8' - } - ) +describe('playlist:update', () => { + it('can update playlists', () => { + const cmd = `${ENV_VAR} npm run playlist:update --silent` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) - const files = glob - .sync('tests/__data__/expected/playlist_update/*.m3u') - .map(f => f.replace('tests/__data__/expected/playlist_update/', '')) + const files = glob + .sync('tests/__data__/expected/playlist_update/*.m3u') + .map(f => f.replace('tests/__data__/expected/playlist_update/', '')) - files.forEach(filepath => { - expect(content(`output/streams/${filepath}`), filepath).toBe( - content(`expected/playlist_update/${filepath}`) + files.forEach(filepath => { + expect(content(`output/streams/${filepath}`), filepath).toBe( + content(`expected/playlist_update/${filepath}`) + ) + }) + + expect(stdout).toBe( + 'OUTPUT=closes #14151, closes #14150, closes #14110, closes #14120, closes #14175, closes #14105, closes #14104, closes #14057, closes #14034, closes #13964, closes #13893, closes #13881, closes #13793, closes #13751, closes #13715\n' ) }) - - expect(stdout).toBe( - 'OUTPUT=closes #14151, closes #14150, closes #14110, closes #14120, closes #14175, closes #14105, closes #14104, closes #14057, closes #14034, closes #13964, closes #13893, closes #13881, closes #13793, closes #13751, closes #13715\n' - ) }) function content(filepath: string) { diff --git a/tests/commands/playlist/validate.test.ts b/tests/commands/playlist/validate.test.ts index b2a43324e4..933099bc9c 100644 --- a/tests/commands/playlist/validate.test.ts +++ b/tests/commands/playlist/validate.test.ts @@ -1,41 +1,47 @@ import { execSync } from 'child_process' +import os from 'os' type ExecError = { status: number stdout: string } -it('show an error if channel id in the blocklist', () => { - try { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/playlist_validate npm run playlist:validate -- us_blocked.m3u', - { - encoding: 'utf8' - } +let ENV_VAR = + 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/playlist_validate' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/data" && SET "STREAMS_DIR=tests/__data__/input/playlist_validate" &&' +} + +describe('playlist:validate', () => { + it('show an error if channel id in the blocklist', () => { + const cmd = `${ENV_VAR} npm run playlist:validate -- us_blocked.m3u` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + checkStdout(stdout) + } catch (error) { + // NOTE: for Windows only + if (process.env.DEBUG === 'true') console.log(cmd, error) + checkStdout((error as ExecError).stdout) + } + }) + + it('show a warning if channel has wrong id', () => { + const cmd = `${ENV_VAR} npm run playlist:validate -- wrong_id.m3u` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + + expect(stdout).toContain( + 'wrong_id.m3u\n 2 warning "qib22lAq1L.us" is not in the database\n\n1 problems (0 errors, 1 warnings)\n' ) - if (process.env.DEBUG === 'true') console.log(stdout) - process.exit(1) - } catch (error) { - if (process.env.DEBUG === 'true') console.log((error as ExecError).stdout) - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain(`us_blocked.m3u + }) +}) + +function checkStdout(stdout: string) { + expect(stdout).toContain(`us_blocked.m3u 2 error "FoxSports2.us" is on the blocklist due to claims of copyright holders (https://github.com/iptv-org/iptv/issues/0002) 4 error "TVN.pl" is on the blocklist due to NSFW content (https://github.com/iptv-org/iptv/issues/0003) 2 problems (2 errors, 0 warnings)`) - } -}) - -it('show a warning if channel has wrong id', () => { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/playlist_validate npm run playlist:validate -- wrong_id.m3u', - { - encoding: 'utf8' - } - ) - if (process.env.DEBUG === 'true') console.log(stdout) - - expect(stdout).toContain( - 'wrong_id.m3u\n 2 warning "qib22lAq1L.us" is not in the database\n\n1 problems (0 errors, 1 warnings)\n' - ) -}) +} diff --git a/tests/commands/readme/update.test.ts b/tests/commands/readme/update.test.ts index f15e71f48b..0ba363409d 100644 --- a/tests/commands/readme/update.test.ts +++ b/tests/commands/readme/update.test.ts @@ -1,6 +1,14 @@ import { execSync } from 'child_process' import fs from 'fs-extra' import path from 'path' +import os from 'os' + +let ENV_VAR = + 'DATA_DIR=tests/__data__/input/data LOGS_DIR=tests/__data__/input/readme_update README_DIR=tests/__data__/output/.readme' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/data" && SET "LOGS_DIR=tests/__data__/input/readme_update" && SET "README_DIR=tests/__data__/output/.readme" &&' +} beforeEach(() => { fs.emptyDirSync('tests/__data__/output') @@ -13,17 +21,18 @@ beforeEach(() => { 'tests/__data__/input/readme_update/.readme/template.md', 'tests/__data__/output/.readme/template.md' ) - - execSync( - 'DATA_DIR=tests/__data__/input/data LOGS_DIR=tests/__data__/input/readme_update README_DIR=tests/__data__/output/.readme npm run readme:update', - { encoding: 'utf8' } - ) }) -it('can update readme.md', () => { - expect(content('tests/__data__/output/readme.md')).toEqual( - content('tests/__data__/expected/readme_update/_readme.md') - ) +describe('readme:update', () => { + it('can update readme.md', () => { + const cmd = `${ENV_VAR} npm run readme:update` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + + expect(content('tests/__data__/output/readme.md')).toEqual( + content('tests/__data__/expected/readme_update/_readme.md') + ) + }) }) function content(filepath: string) { diff --git a/tests/commands/report/create.test.ts b/tests/commands/report/create.test.ts index de449347af..18c7994455 100644 --- a/tests/commands/report/create.test.ts +++ b/tests/commands/report/create.test.ts @@ -1,15 +1,20 @@ import { execSync } from 'child_process' +import os from 'os' -it('can create report', () => { - const stdout = execSync( - 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/report_create npm run report:create', - { - encoding: 'utf8' - } - ) +let ENV_VAR = 'DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/input/report_create' +if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/data" && SET "STREAMS_DIR=tests/__data__/input/report_create" &&' +} - expect( - stdout.includes(` +describe('report:create', () => { + it('can create report', () => { + const cmd = `${ENV_VAR} npm run report:create` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + + expect( + stdout.includes(` ┌─────────┬─────────────┬──────────────────┬─────────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────┬───────────────┐ │ (index) │ issueNumber │ type │ streamId │ streamUrl │ status │ ├─────────┼─────────────┼──────────────────┼─────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────────┤ @@ -20,5 +25,6 @@ it('can create report', () => { │ 4 │ 16120 │ 'broken stream' │ undefined │ 'http://190.61.102.67:2000/play/a038/index.m3u8' │ 'wrong_link' │ │ 5 │ 19956 │ 'channel search' │ 'CNBCe.tr' │ undefined │ 'invalid_id' │ └─────────┴─────────────┴──────────────────┴─────────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────────┘`) - ).toBe(true) + ).toBe(true) + }) })