From 7278b672f407d7fa7e508fa39447eeaa9af4c7b7 Mon Sep 17 00:00:00 2001 From: seszele64 Date: Tue, 22 Jul 2025 19:12:49 +0200 Subject: [PATCH] test(video): add tests for video download trimming --- src/__tests__/video.test.ts | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/__tests__/video.test.ts diff --git a/src/__tests__/video.test.ts b/src/__tests__/video.test.ts new file mode 100644 index 0000000..9a5badd --- /dev/null +++ b/src/__tests__/video.test.ts @@ -0,0 +1,68 @@ +// @ts-nocheck +// @jest-environment node +import { describe, test, expect } from '@jest/globals'; +import * as os from 'os'; +import * as path from 'path'; +import { downloadVideo } from '../modules/video.js'; +import { CONFIG } from '../config.js'; +import * as fs from 'fs'; + +// 設置 Python 環境 +process.env.PYTHONPATH = ''; +process.env.PYTHONHOME = ''; + +describe('downloadVideo with trimming', () => { + const testUrl = 'https://www.youtube.com/watch?v=jNQXAC9IVRw'; + const testConfig = { + ...CONFIG, + file: { + ...CONFIG.file, + downloadsDir: path.join(os.tmpdir(), 'yt-dlp-test-downloads'), + tempDirPrefix: 'yt-dlp-test-' + } + }; + + beforeEach(async () => { + await fs.promises.mkdir(testConfig.file.downloadsDir, { recursive: true }); + }); + + afterEach(async () => { + await fs.promises.rm(testConfig.file.downloadsDir, { recursive: true, force: true }); + }); + + test('downloads video with start time trimming', async () => { + const result = await downloadVideo(testUrl, testConfig, '720p', '00:00:10'); + expect(result).toContain('Video successfully downloaded'); + + const files = await fs.promises.readdir(testConfig.file.downloadsDir); + expect(files.length).toBeGreaterThan(0); + expect(files[0]).toMatch(/\.(mp4|webm|mkv)$/); + }, 30000); + + test('downloads video with end time trimming', async () => { + const result = await downloadVideo(testUrl, testConfig, '720p', undefined, '00:00:20'); + expect(result).toContain('Video successfully downloaded'); + + const files = await fs.promises.readdir(testConfig.file.downloadsDir); + expect(files.length).toBeGreaterThan(0); + expect(files[0]).toMatch(/\.(mp4|webm|mkv)$/); + }, 30000); + + test('downloads video with both start and end time trimming', async () => { + const result = await downloadVideo(testUrl, testConfig, '720p', '00:00:10', '00:00:20'); + expect(result).toContain('Video successfully downloaded'); + + const files = await fs.promises.readdir(testConfig.file.downloadsDir); + expect(files.length).toBeGreaterThan(0); + expect(files[0]).toMatch(/\.(mp4|webm|mkv)$/); + }, 30000); + + test('downloads video without trimming when no times provided', async () => { + const result = await downloadVideo(testUrl, testConfig, '720p'); + expect(result).toContain('Video successfully downloaded'); + + const files = await fs.promises.readdir(testConfig.file.downloadsDir); + expect(files.length).toBeGreaterThan(0); + expect(files[0]).toMatch(/\.(mp4|webm|mkv)$/); + }, 30000); +}); \ No newline at end of file