iptv/tests/commands/database/export.test.js

69 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-02-12 23:40:45 +01:00
const { execSync } = require('child_process')
const fs = require('fs-extra')
2022-08-10 02:41:21 +02:00
const _ = require('lodash')
const dayjs = require('dayjs')
2022-02-12 23:40:45 +01:00
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
2022-08-15 01:22:01 +02:00
fs.mkdirSync('tests/__data__/output/database')
2022-02-12 23:40:45 +01:00
fs.copyFileSync(
2022-02-13 03:42:39 +01:00
'tests/__data__/input/database/db_export.streams.db',
2022-08-15 01:22:01 +02:00
'tests/__data__/output/database/streams.db'
2022-02-12 23:40:45 +01:00
)
const stdout = execSync(
2022-08-15 01:22:01 +02:00
'DB_DIR=tests/__data__/output/database DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output/.api npm run db:export',
2022-02-12 23:40:45 +01:00
{ encoding: 'utf8' }
)
})
it('can create streams.json', () => {
2022-08-10 02:41:21 +02:00
let api = content('input/data/streams.json')
let output = content(`output/.api/streams.json`)
let expected = content(`expected/.api/streams.json`)
2022-08-13 19:43:37 +02:00
const samples = {
unchanged_online: 'https://master.starmena-cloud.com/hls/libyas.m3u8',
unchanged_error: 'https://iptv-all.lanesh4d0w.repl.co/andorra/atv',
updated_error: 'http://46.46.143.222:1935/live/mp4:ldpr.stream/playlist.m3u8',
added_online: 'https://master.starmena-cloud.com/hls/bbc.m3u8'
}
let outputData, savedData
outputData = output.find(i => i.url === samples['unchanged_online'])
savedData = api.find(i => i.url === samples['unchanged_online'])
expect(outputData.added_at).toBe(savedData.added_at)
expect(outputData.updated_at).toBe(savedData.updated_at)
expect(dayjs().diff(outputData.checked_at, 'h')).toBe(0)
outputData = output.find(i => i.url === samples['unchanged_error'])
savedData = api.find(i => i.url === samples['unchanged_error'])
2022-08-10 02:41:21 +02:00
expect(outputData.added_at).toBe(savedData.added_at)
expect(outputData.updated_at).toBe(savedData.updated_at)
expect(dayjs().diff(outputData.checked_at, 'h')).toBe(0)
2022-08-13 19:43:37 +02:00
outputData = output.find(i => i.url === samples['updated_error'])
savedData = api.find(i => i.url === samples['unchanged_error'])
2022-08-10 02:41:21 +02:00
expect(outputData.added_at).toBe(savedData.added_at)
expect(dayjs().diff(outputData.updated_at, 'h')).toBe(0)
expect(dayjs().diff(outputData.checked_at, 'h')).toBe(0)
2022-08-13 19:43:37 +02:00
outputData = output.find(i => i.url === samples['added_online'])
2022-08-10 02:41:21 +02:00
expect(dayjs().diff(outputData.added_at, 'h')).toBe(0)
expect(dayjs().diff(outputData.updated_at, 'h')).toBe(0)
expect(dayjs().diff(outputData.checked_at, 'h')).toBe(0)
expect(output.map(item => _.omit(item, ['added_at', 'updated_at', 'checked_at']))).toMatchObject(
expected.map(item => _.omit(item, ['added_at', 'updated_at', 'checked_at']))
)
2022-02-12 23:40:45 +01:00
})
function content(filepath) {
2022-08-10 02:41:21 +02:00
return JSON.parse(
fs.readFileSync(`tests/__data__/${filepath}`, {
encoding: 'utf8'
})
)
2022-02-12 23:40:45 +01:00
}