""" Test podcast download functionality 测试播客下载功能 """ import pytest import asyncio import os from pathlib import Path from typing import Dict, Any from src.tools.download_tools import download_apple_podcast_tool, download_xyz_podcast_tool from src.services.podcast_download_service import PodcastDownloadService from src.interfaces.podcast_downloader import PodcastPlatform class TestPodcastDownload: """Test podcast download integration""" @pytest.mark.asyncio async def test_apple_podcast_info_extraction(self, podcast_download_service: PodcastDownloadService): """Test Apple podcast information extraction""" print("\n🍎 Testing Apple Podcast info extraction...") # Use a known working Apple Podcast URL test_url = "https://podcasts.apple.com/us/podcast/the-tim-ferriss-show/id863897795" try: # Test platform detection can_handle = podcast_download_service.can_handle_url(test_url) assert can_handle, "Should be able to handle Apple Podcast URL" # Test podcast info extraction podcast_info = await podcast_download_service.extract_podcast_info(test_url) assert podcast_info is not None assert podcast_info.platform == PodcastPlatform.APPLE assert podcast_info.title is not None assert len(podcast_info.title) > 0 print(f"✅ Successfully extracted Apple Podcast info:") print(f" Title: {podcast_info.title}") print(f" Platform: {podcast_info.platform}") print(f" Episode ID: {podcast_info.episode_id}") except Exception as e: print(f"❌ Apple Podcast info extraction failed: {str(e)}") pytest.skip(f"Apple Podcast info extraction failed: {str(e)}") @pytest.mark.asyncio async def test_xiaoyuzhou_podcast_info_extraction(self, podcast_download_service: PodcastDownloadService): """Test XiaoYuZhou podcast information extraction""" print("\n🎵 Testing XiaoYuZhou Podcast info extraction...") # Use a test XYZ URL pattern test_url = "https://www.xiaoyuzhoufm.com/episode/example123" try: # Test platform detection can_handle = podcast_download_service.can_handle_url(test_url) assert can_handle, "Should be able to handle XiaoYuZhou Podcast URL" # Test podcast info extraction (might fail due to network/content) try: podcast_info = await podcast_download_service.extract_podcast_info(test_url) assert podcast_info is not None assert podcast_info.platform == PodcastPlatform.XIAOYUZHOU print(f"✅ Successfully extracted XiaoYuZhou Podcast info:") print(f" Title: {podcast_info.title}") print(f" Platform: {podcast_info.platform}") print(f" Episode ID: {podcast_info.episode_id}") except Exception as e: print(f"⚠️ XiaoYuZhou info extraction failed (expected for test URL): {str(e)}") except Exception as e: print(f"❌ XiaoYuZhou platform detection failed: {str(e)}") @pytest.mark.asyncio async def test_apple_podcast_download_simulation(self, temp_dir: str): """Test Apple podcast download simulation (without actual download)""" print("\n🍎 Testing Apple Podcast download simulation...") # Use a known Apple Podcast URL for testing the download flow test_url = "https://podcasts.apple.com/us/podcast/the-tim-ferriss-show/id863897795" try: # Test the download tool interface result = await download_apple_podcast_tool(test_url) print(f"📋 Download tool result:") print(f" Status: {result.get('status', 'unknown')}") print(f" Original URL: {result.get('original_url', 'N/A')}") if result.get("status") == "success": print(f" Audio file path: {result.get('audio_file_path', 'N/A')}") print("✅ Apple Podcast download simulation successful") else: print(f" Error: {result.get('error_message', 'Unknown error')}") print("⚠️ Apple Podcast download simulation failed (might be network-related)") except Exception as e: print(f"❌ Apple Podcast download test failed: {str(e)}") pytest.skip(f"Apple Podcast download test failed: {str(e)}") @pytest.mark.asyncio async def test_xiaoyuzhou_podcast_download_simulation(self, temp_dir: str): """Test XiaoYuZhou podcast download simulation""" print("\n🎵 Testing XiaoYuZhou Podcast download simulation...") # Use a test XYZ URL test_url = "https://www.xiaoyuzhoufm.com/episode/example123" try: # Test the download tool interface result = await download_xyz_podcast_tool(test_url) print(f"📋 Download tool result:") print(f" Status: {result.get('status', 'unknown')}") print(f" Original URL: {result.get('original_url', 'N/A')}") if result.get("status") == "success": print(f" Audio file path: {result.get('audio_file_path', 'N/A')}") print("✅ XiaoYuZhou Podcast download simulation successful") else: print(f" Error: {result.get('error_message', 'Unknown error')}") print("⚠️ XiaoYuZhou Podcast download simulation failed (expected for test URL)") except Exception as e: print(f"❌ XiaoYuZhou Podcast download test failed: {str(e)}") # This is expected for test URLs, so we don't fail the test @pytest.mark.asyncio async def test_supported_platforms(self, podcast_download_service: PodcastDownloadService): """Test supported platforms detection""" print("\n🌐 Testing supported platforms...") platforms = podcast_download_service.get_supported_platforms() assert PodcastPlatform.APPLE in platforms assert PodcastPlatform.XIAOYUZHOU in platforms print(f"✅ Supported platforms: {[p.value for p in platforms]}") @pytest.mark.asyncio async def test_url_validation(self, podcast_download_service: PodcastDownloadService): """Test URL validation""" print("\n🔗 Testing URL validation...") test_cases = [ ("https://podcasts.apple.com/us/podcast/test", True, "Apple Podcast URL"), ("https://www.xiaoyuzhoufm.com/episode/test", True, "XiaoYuZhou URL"), ("https://example.com/podcast", False, "Generic URL"), ("invalid-url", False, "Invalid URL"), ] for url, expected, description in test_cases: result = podcast_download_service.can_handle_url(url) assert result == expected, f"URL validation failed for {description}: {url}" print(f"✅ {description}: {'✓' if result else '✗'}") def test_download_tools_initialization(self): """Test download tools initialization""" print("\n🔧 Testing download tools initialization...") # Test that the tools can be imported assert download_apple_podcast_tool is not None assert download_xyz_podcast_tool is not None print("✅ Download tools initialized successfully") if __name__ == "__main__": # Run tests with verbose output pytest.main([__file__, "-v", "-s"])