Back to Repositories

Testing Playlist Generation Workflow in IPTV-org Repository

This test suite validates the playlist generation functionality in the IPTV repository, ensuring correct creation of M3U playlists and associated log files. It verifies the output against expected results while managing test data directories and file operations.

Test Coverage Overview

The test suite provides comprehensive coverage of playlist generation workflows.

Key areas tested include:
  • Playlist file generation and content validation
  • Log file generation and content verification
  • Directory structure management
  • File path handling across different environments
Edge cases covered include empty directory handling and UTF-8 encoding requirements.

Implementation Analysis

The testing approach utilizes Jest’s beforeEach hooks for test setup and cleanup, ensuring isolated test environments for each run.

Key patterns include:
  • Child process execution for playlist generation
  • File system operations using fs-extra
  • Glob pattern matching for file discovery
  • Content comparison using Jest matchers

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • fs-extra for enhanced file operations
  • glob for file pattern matching
  • child_process for command execution
  • Environment variables for directory configuration
  • UTF-8 encoding for file operations

Best Practices Demonstrated

The test implementation showcases several testing best practices and patterns.

Notable practices include:
  • Isolated test environments through directory cleanup
  • Deterministic test data management
  • Modular helper functions for file operations
  • Consistent file path handling
  • Comprehensive output validation
  • Sorted log comparison for reliable testing

iptv-org/iptv

tests/commands/playlist/generate.test.ts

            
import { execSync } from 'child_process'
import * as fs from 'fs-extra'
import * as glob from 'glob'

beforeEach(() => {
  fs.emptyDirSync('tests/__data__/output')

  execSync(
    'STREAMS_DIR=tests/__data__/input/streams_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' }
  )
})

it('can generate playlists and logs', () => {
  const playlists = glob
    .sync('tests/__data__/expected/.gh-pages/**/*.m3u')
    .map((file: string) => file.replace('tests/__data__/expected/', ''))

  playlists.forEach((filepath: string) => {
    expect(content(`output/${filepath}`), filepath).toBe(content(`expected/${filepath}`))
  })

  expect(content('output/logs/generators.log').split('\n').sort()).toStrictEqual(
    content('expected/logs/generators.log').split('\n').sort()
  )
})

function content(filepath: string) {
  return fs.readFileSync(`tests/__data__/${filepath}`, {
    encoding: 'utf8'
  })
}