mono/packages/media/ref/yt-dlp/dist/__tests__/youtube.test.js

64 lines
2.7 KiB
JavaScript

import { describe, it, expect, afterEach } from 'vitest';
import { YtDlp } from '../ytdlp.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
// Create a test directory for downloads
const TEST_DIR = path.join(process.cwd(), 'test-downloads');
const YOUTUBE_URL = 'https://www.youtube.com/watch?v=_oVI0GW-Xd4';
describe('YouTube Video Download', () => {
// Ensure the test directory exists
if (!fs.existsSync(TEST_DIR)) {
fs.mkdirSync(TEST_DIR, { recursive: true });
}
let downloadedFiles = [];
// Clean up after tests
afterEach(() => {
// Delete any downloaded files
downloadedFiles.forEach(file => {
const fullPath = path.resolve(file);
if (fs.existsSync(fullPath)) {
//fs.unlinkSync(fullPath);
console.log(`Cleaned up test file: ${fullPath}`);
}
});
downloadedFiles = [];
});
it('should successfully download a YouTube video', async () => {
// Create a new YtDlp instance
const ytdlp = new YtDlp();
// Check if yt-dlp is installed
const isInstalled = await ytdlp.isInstalled();
expect(isInstalled).toBe(true);
// Download the video with specific options to keep the test fast
// Use a lower quality format to speed up the test
const downloadOptions = {
outputDir: TEST_DIR,
format: 'worst[ext=mp4]', // Use lowest quality for faster test
outputTemplate: 'youtube-test-%(id)s.%(ext)s'
};
// Execute the download
const filePath = await ytdlp.downloadVideo(YOUTUBE_URL, downloadOptions);
console.log(`Downloaded file: ${filePath}`);
// Add to cleanup list
downloadedFiles.push(filePath);
// Assert that the file exists
expect(fs.existsSync(filePath)).toBe(true);
// Assert that the file has content (not empty)
const stats = fs.statSync(filePath);
expect(stats.size).toBeGreaterThan(0);
}, 60000); // Increase timeout to 60 seconds as downloads may take time
it('should retrieve video information correctly', async () => {
const ytdlp = new YtDlp();
// Get video info
const videoInfo = await ytdlp.getVideoInfo(YOUTUBE_URL);
// Assert video properties
expect(videoInfo).toBeDefined();
expect(videoInfo.id).toBeDefined();
expect(videoInfo.title).toBeDefined();
expect(videoInfo.webpage_url).toBeDefined();
// Verify the video ID matches the expected ID from the URL
const expectedVideoId = '_oVI0GW-Xd4';
expect(videoInfo.id).toBe(expectedVideoId);
});
});
//# sourceMappingURL=youtube.test.js.map