mirror of
https://github.com/iptv-org/iptv.git
synced 2026-11-27 19:16:32 +02:00
decodeURI decodes %25, so normalizeURL turned an encoded percent sign into a bare "%": in the path (normalize-url runs decodeURI on the pathname) a%2520b became a%20b, a different file, and in the query token=50%25off became token=50%off, a malformed escape. %25 is now swapped for a placeholder before normalize-url and put back after decoding.
95 lines
3.8 KiB
TypeScript
95 lines
3.8 KiB
TypeScript
import { describe, beforeEach, it, expect } from 'vitest'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { execSync } from 'child_process'
|
|
import * as fs from 'fs-extra'
|
|
import { glob } from 'glob'
|
|
|
|
const ENV_VAR =
|
|
'cross-env STREAMS_DIR=tests/__data__/output/streams DATA_DIR=tests/__data__/input/data'
|
|
|
|
beforeEach(() => {
|
|
fs.emptyDirSync('tests/__data__/output')
|
|
fs.copySync('tests/__data__/input/playlist_format', 'tests/__data__/output/streams')
|
|
})
|
|
|
|
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(filepath => {
|
|
const fileUrl = pathToFileURL(filepath).toString()
|
|
const pathToRemove = pathToFileURL('tests/__data__/expected/playlist_format/').toString()
|
|
|
|
return fileUrl.replace(pathToRemove, '')
|
|
})
|
|
|
|
files.forEach(filepath => {
|
|
expect(content(`tests/__data__/output/streams/${filepath}`)).toBe(
|
|
content(`tests/__data__/expected/playlist_format/${filepath}`)
|
|
)
|
|
})
|
|
})
|
|
|
|
it('formats playlists in nested directories', () => {
|
|
const cmd = `${ENV_VAR} npm run playlist:format`
|
|
execSync(cmd, { encoding: 'utf8' })
|
|
|
|
const output = content('tests/__data__/output/streams/nested/nested.m3u')
|
|
expect(output).toContain(',Alpha (720p)')
|
|
expect(output).toContain(',Zulu (720p)')
|
|
expect(output.indexOf(',Alpha (720p)')).toBeLessThan(output.indexOf(',Zulu (720p)'))
|
|
})
|
|
|
|
it('keeps links with malformed percent escapes', () => {
|
|
fs.emptyDirSync('tests/__data__/output')
|
|
fs.copySync('tests/__data__/input/playlist_normalize', 'tests/__data__/output/streams')
|
|
|
|
const cmd =
|
|
'cross-env STREAMS_DIR=tests/__data__/output/streams DATA_DIR=tests/__data__/input/data npm run playlist:format'
|
|
execSync(cmd, { encoding: 'utf8' })
|
|
|
|
expect(content('tests/__data__/output/streams/bad_encoding.m3u')).toContain(
|
|
'http://example.com/a%ZZ'
|
|
)
|
|
})
|
|
|
|
it('keeps percent escapes whose decoding would change the link', () => {
|
|
fs.emptyDirSync('tests/__data__/output')
|
|
fs.copySync('tests/__data__/input/playlist_normalize', 'tests/__data__/output/streams')
|
|
|
|
const cmd =
|
|
'cross-env STREAMS_DIR=tests/__data__/output/streams DATA_DIR=tests/__data__/input/data npm run playlist:format'
|
|
execSync(cmd, { encoding: 'utf8' })
|
|
|
|
const output = content('tests/__data__/output/streams/reserved_escapes.m3u')
|
|
expect(output).toContain('http://example.com/TVP%20Wilno%20HD/index.m3u8')
|
|
expect(output).toContain('http://example.com/a%2Fb/index.m3u8')
|
|
expect(output).toContain('http://example.com/index.m3u8?id=1%232')
|
|
expect(output).toContain(
|
|
'http://example.com/index.m3u8?name=TV%2B&url=http%3A%2F%2Fexample.org%2Fa.m3u8%3Fk%3D1%26exp%3D2'
|
|
)
|
|
expect(output).toContain('http://example.com/a%2520b/index.m3u8')
|
|
expect(output).toContain('http://example.com/index.m3u8?token=50%25off')
|
|
expect(output).toContain('http://example.com/a%ZZ/index.m3u8?token=50%25off')
|
|
})
|
|
|
|
it('removes a duplicate that was the only stream in its file', () => {
|
|
fs.emptyDirSync('tests/__data__/output')
|
|
fs.copySync('tests/__data__/input/playlist_format_duplicates', 'tests/__data__/output/streams')
|
|
|
|
const cmd = `${ENV_VAR} npm run playlist:format`
|
|
execSync(cmd, { encoding: 'utf8' })
|
|
|
|
expect(content('tests/__data__/output/streams/a.m3u')).toContain(
|
|
'https://example.com/alpha.m3u8?a=1&b=2'
|
|
)
|
|
expect(content('tests/__data__/output/streams/b.m3u')).toBe('#EXTM3U\r\n')
|
|
})
|
|
})
|
|
|
|
function content(filepath: string) {
|
|
return fs.readFileSync(pathToFileURL(filepath), { encoding: 'utf8' })
|
|
}
|