Initial Commit

This commit is contained in:
2023-09-10 21:48:48 +02:00
commit 227cca7d31
791 changed files with 165200 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
const stdout = execSync(
'LOGS_DIR=tests/__data__/input/logs OUTPUT_DIR=tests/__data__/output/api npm run api:update',
{ encoding: 'utf8' }
)
})
it('can generate guides.json', () => {
expect(content('tests/__data__/output/api/guides.json')).toBe(
content('tests/__data__/expected/api/guides.json')
)
})
function content(filepath) {
const data = fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
return JSON.stringify(data)
}

View File

@@ -0,0 +1,19 @@
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\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`
)
}
})

View File

@@ -0,0 +1,24 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
const stdout = execSync(
'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml',
{ encoding: 'utf8' }
)
})
it('can parse channels', () => {
expect(content('tests/__data__/output/channels.xml')).toEqual(
content('tests/__data__/expected/sites/parse-channels.channels.xml')
)
})
function content(filepath) {
return fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
}

View File

@@ -0,0 +1,49 @@
const { execSync } = require('child_process')
it('will show a message if the file contains a duplicate', () => {
try {
const stdout = execSync(
'DATA_DIR=tests/__data__/input/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\n\ntests/__data__/input/sites/duplicate.channels.xml
┌─────────┬─────────────┬──────┬─────────────────────────────┬─────────┬─────────────────────┐
│ (index) │ type │ lang │ xmltv_id │ site_id │ name │
├─────────┼─────────────┼──────┼─────────────────────────────┼─────────┼─────────────────────┤
│ 0 │ 'duplicate' │ 'en' │ 'CNNInternationalEurope.us' │ '140' │ 'CNN International' │
└─────────┴─────────────┴──────┴─────────────────────────────┴─────────┴─────────────────────┘
\n1 error(s) in 1 file(s)\n`
)
}
})
it('will show a message if the file contains a channel with wrong xmltv_id', () => {
try {
const stdout = execSync(
'DATA_DIR=tests/__data__/input/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\n\ntests/__data__/input/sites/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`
)
}
})

View File

@@ -0,0 +1,37 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
fs.copyFileSync('tests/__data__/input/database/queue.db', 'tests/__data__/output/queue.db')
})
it('can load cluster and will terminate process if programs not found', () => {
try {
execSync(
'DB_DIR=tests/__data__/output LOGS_DIR=tests/__data__/output/logs npm run cluster:load -- --cluster-id=1 --timeout=10000',
{ encoding: 'utf8' }
)
process.exit(1)
} catch (err) {
expect(err.status).toBe(1)
expect(err.stdout.includes('Error: No programs found')).toBe(true)
expect(content('tests/__data__/output/logs/cluster/load/cluster_1.log')).toEqual(
content('tests/__data__/expected/logs/cluster/load/cluster_1.log')
)
}
})
function content(filepath) {
const data = fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
return data
.split('\n')
.filter(l => l)
.map(l => {
return JSON.parse(l)
})
}

View File

@@ -0,0 +1,49 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
const glob = require('glob')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
fs.copyFileSync(
'tests/__data__/input/database/update-guides/programs.db',
'tests/__data__/output/programs.db'
)
fs.copyFileSync(
'tests/__data__/input/database/update-guides/queue.db',
'tests/__data__/output/queue.db'
)
})
it('can generate /guides', () => {
const stdout = execSync(
'DB_DIR=tests/__data__/output LOGS_DIR=tests/__data__/output/logs DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output CURR_DATE=2022-10-20 npm run guides:update',
{ encoding: 'utf8' }
)
const uncompressed = glob
.sync('tests/__data__/expected/guides/**/*.xml')
.map(f => f.replace('tests/__data__/expected/', ''))
uncompressed.forEach(filepath => {
expect(content(`output/${filepath}`), filepath).toBe(content(`expected/${filepath}`))
})
const compressed = glob
.sync('tests/__data__/expected/guides/**/*.xml.gz')
.map(f => f.replace('tests/__data__/expected/', ''))
compressed.forEach(filepath => {
expect(content(`output/${filepath}`), filepath).toBe(content(`expected/${filepath}`))
})
expect(content('output/logs/guides/update.log')).toEqual(
content('expected/logs/guides/update.log')
)
})
function content(filepath) {
return fs.readFileSync(`tests/__data__/${filepath}`, {
encoding: 'utf8'
})
}

View File

@@ -0,0 +1,48 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
fs.copyFileSync('tests/__data__/input/database/queue.db', 'tests/__data__/output/queue.db')
const stdout = execSync(
'DB_DIR=tests/__data__/output LOGS_DIR=tests/__data__/input/logs npm run programs:save',
{ encoding: 'utf8' }
)
})
it('can save programs to database', () => {
let output = content('tests/__data__/output/programs.db')
let expected = content('tests/__data__/expected/database/programs.db')
output = output.map(i => {
i._id = null
return i
})
expected = expected.map(i => {
i._id = null
return i
})
expect(output).toEqual(expected)
})
it('can update queue', () => {
expect(content('tests/__data__/output/queue.db')).toEqual(
content('tests/__data__/expected/database/queue-with-errors.db')
)
})
function content(filepath) {
const data = fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
return data
.split('\n')
.filter(l => l)
.map(l => {
return JSON.parse(l)
})
}

View File

@@ -0,0 +1,48 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
const stdout = execSync(
'DB_DIR=tests/__data__/output/database CHANNELS_PATH=tests/__data__/input/sites/example.com_*.channels.xml DATA_DIR=tests/__data__/input/data CURR_DATE=2022-12-29 npm run queue:create -- --max-clusters=1',
{ encoding: 'utf8' }
)
})
it('can create queue', () => {
let output = content('tests/__data__/output/database/queue.db')
let expected = content('tests/__data__/expected/database/create-queue/queue.db')
output = output.map(i => {
i._id = null
return i
})
expected = expected.map(i => {
i._id = null
return i
})
expect(output).toEqual(
expect.arrayContaining([
expect.objectContaining(expected[0]),
expect.objectContaining(expected[1]),
expect.objectContaining(expected[2]),
expect.objectContaining(expected[3])
])
)
})
function content(filepath) {
const data = fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
return data
.split('\n')
.filter(l => l)
.map(l => {
return JSON.parse(l)
})
}

View File

@@ -0,0 +1,26 @@
const { execSync } = require('child_process')
const fs = require('fs-extra')
const path = require('path')
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
const stdout = execSync(
'LOGS_DIR=tests/__data__/input/logs DATA_DIR=tests/__data__/input/data npm run readme:update -- --config=tests/__data__/input/readme.json',
{ encoding: 'utf8' }
)
})
it('can update readme.md', () => {
expect(content('tests/__data__/output/readme.md')).toBe(
content('tests/__data__/expected/_readme.md')
)
})
function content(filepath) {
const data = fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8'
})
return JSON.stringify(data)
}