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

80 lines
3.6 KiB
JavaScript

import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import { existsSync, statSync } from 'node:fs';
import * as path from 'node:path';
import { YtDlp } from '../ytdlp.js';
describe('TikTok Download Tests', () => {
// TikTok URL to test
const tiktokUrl = 'https://www.tiktok.com/@woman.power.quote/video/7476910372121971970';
// Temporary output directory for test downloads
const outputDir = path.join(process.cwd(), 'test-downloads');
// Instance of YtDlp
let ytdlp;
// Path to the downloaded file (will be set during test)
let downloadedFilePath;
beforeAll(() => {
// Initialize YtDlp instance with test options
ytdlp = new YtDlp({
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
});
// Set up spy for console.log to track progress messages
vi.spyOn(console, 'log').mockImplementation(() => { });
});
afterAll(async () => {
// Clean up downloaded files if they exist
if (downloadedFilePath && existsSync(downloadedFilePath)) {
try {
//await unlink(downloadedFilePath);
console.log(`Test cleanup: Deleted ${downloadedFilePath}`);
}
catch (error) {
console.error(`Failed to delete test file: ${error}`);
}
}
// Restore console.log
vi.restoreAllMocks();
});
it('should download a TikTok video successfully', async () => {
// Define download options
const options = {
format: 'best',
outputDir,
// Add a timestamp to ensure unique filenames across test runs
outputTemplate: `tiktok-test-${Date.now()}.%(ext)s`,
};
// Download the video
downloadedFilePath = await ytdlp.downloadVideo(tiktokUrl, options);
// Verify the download was successful
expect(downloadedFilePath).toBeTruthy();
expect(existsSync(downloadedFilePath)).toBe(true);
// Check file has some content (not empty)
const stats = statSync(downloadedFilePath).size;
expect(stats).toBeGreaterThan(0);
console.log(`Downloaded TikTok video to: ${downloadedFilePath}`);
}, 60000); // Increase timeout for download to complete
it('should get video info from TikTok URL', async () => {
// Get video info
const videoInfo = await ytdlp.getVideoInfo(tiktokUrl);
// Verify basic video information
expect(videoInfo).toBeTruthy();
expect(videoInfo.id).toBeTruthy();
expect(videoInfo.title).toBeTruthy();
expect(videoInfo.uploader).toBeTruthy();
console.log(`TikTok Video Title: ${videoInfo.title}`);
console.log(`TikTok Video Uploader: ${videoInfo.uploader}`);
}, 30000); // Increase timeout for API response
it('should list available formats for TikTok video', async () => {
// List available formats
const formats = await ytdlp.listFormats(tiktokUrl);
// Verify formats are returned
expect(formats).toBeInstanceOf(Array);
expect(formats.length).toBeGreaterThan(0);
// At least one format should have a format_id
expect(formats[0].format_id).toBeTruthy();
// Log some useful information for debugging
console.log(`Found ${formats.length} formats for TikTok video`);
if (formats.length > 0) {
console.log('First format:', JSON.stringify(formats[0], null, 2));
}
}, 30000); // Increase timeout for format listing
});
//# sourceMappingURL=tiktok.test.js.map