#!/usr/bin/env python3 """ Complete end-to-end download test on the working backup. This will show the full process: search → download → file creation """ import socketio import time import json import os import glob def test_complete_download_flow(): """Test the complete download flow and verify file creation""" print("šŸŽ­ COMPLETE DOWNLOAD FLOW TEST") print("=" * 60) print("Testing the WORKING backup (port 6544) to see:") print("1. Search for a song") print("2. Click download") print("3. Watch download progress") print("4. Verify file appears in /Volumes/Jellyfin Media/Music") print() # Create a Socket.IO client sio = socketio.Client() # Track the process search_results = None download_updates = [] download_completed = False @sio.event def connect(): print("āœ… Connected to working backup") @sio.event def disconnect(): print("āŒ Disconnected from working backup") @sio.event def search_results(data): nonlocal search_results search_results = data print(f"šŸ” Search results received: {len(data.get('results', []))} items") if data.get('results'): first = data['results'][0] print(f" šŸ“€ First result: '{first.get('name')}' by '{first.get('artist')}'") print(f" šŸ†” Spotify ID: {first.get('id')}") print(f" šŸ”— URL: {first.get('url')}") @sio.event def update_status(data): nonlocal download_updates, download_completed download_updates.append(data) print(f"šŸ“„ Download status update:") for item in data.get('history', []): status = item.get('status', 'Unknown') name = item.get('name', 'Unknown') item_id = item.get('id', 'NO-ID') print(f" šŸ“Š {name} (ID: {item_id}): {status}") if status in ['Complete', 'Failed', 'Error']: download_completed = True print(f" šŸŽÆ Download finished with status: {status}") try: # Step 1: Connect print("šŸ”Œ Step 1: Connecting to working backup...") sio.connect('http://localhost:6544') time.sleep(2) # Step 2: Search print("\nšŸ” Step 2: Searching for 'you lloyd'...") sio.emit('search', {'query': 'you lloyd', 'type': 'track'}) time.sleep(4) if not search_results or not search_results.get('results'): print("āŒ No search results - cannot proceed") return False # Step 3: Get download target info target_song = search_results['results'][0] song_name = target_song.get('name', 'Unknown') artist_name = target_song.get('artist', 'Unknown') print(f"\nšŸŽÆ Step 3: Target song selected:") print(f" Song: {song_name}") print(f" Artist: {artist_name}") print(f" ID: {target_song.get('id')}") # Step 4: Check current files before download print(f"\nšŸ“ Step 4: Checking existing files before download...") music_dir = "/Volumes/Jellyfin Media/Music" # Look for existing files with this artist/song existing_files_before = [] if os.path.exists(music_dir): # Search for files containing the artist name search_pattern = f"{music_dir}/**/*{artist_name}*" existing_files_before = glob.glob(search_pattern, recursive=True) print(f" Found {len(existing_files_before)} existing files with '{artist_name}' in path") # Step 5: Trigger download print(f"\nšŸ”½ Step 5: Triggering download...") download_request = { 'url': target_song.get('url'), 'searchResult': target_song, 'name': song_name, 'artist': artist_name, 'type': target_song.get('type'), 'id': target_song.get('id') } print(f"šŸ“¤ Sending download request for: {song_name} by {artist_name}") sio.emit('download', download_request) # Step 6: Wait for download completion print(f"\nā³ Step 6: Waiting for download to complete...") print(" (This may take 30-60 seconds for a real download)") for i in range(60): # Wait up to 60 seconds time.sleep(1) if i % 5 == 0: # Print progress every 5 seconds print(f" ā° Waiting... {i+1}/60 seconds") if download_completed: break # Step 7: Check for new files print(f"\nšŸ“ Step 7: Checking for new files after download...") if os.path.exists(music_dir): # Search for files containing the artist name existing_files_after = glob.glob(search_pattern, recursive=True) new_files = [f for f in existing_files_after if f not in existing_files_before] print(f" Files before: {len(existing_files_before)}") print(f" Files after: {len(existing_files_after)}") print(f" New files: {len(new_files)}") if new_files: print(f" šŸŽ‰ NEW FILES CREATED:") for new_file in new_files: file_size = os.path.getsize(new_file) if os.path.exists(new_file) else 0 print(f" šŸ“„ {new_file}") print(f" Size: {file_size:,} bytes ({file_size/1024/1024:.1f} MB)") return True else: print(f" āŒ No new files found") # Show what files exist for debugging if existing_files_after: print(f" šŸ“‹ Existing files with '{artist_name}':") for existing_file in existing_files_after[:5]: # Show first 5 print(f" šŸ“„ {existing_file}") else: print(f" āŒ Music directory doesn't exist: {music_dir}") return download_completed except Exception as e: print(f"šŸ’„ Error during complete test: {e}") return False finally: if sio.connected: sio.disconnect() if __name__ == "__main__": print("šŸš€ COMPLETE END-TO-END DOWNLOAD TEST") print("Testing the working backup to see the full download process") print("=" * 80) success = test_complete_download_flow() print("\n" + "=" * 80) if success: print("āœ… COMPLETE TEST SUCCESS!") print(" The working backup successfully downloaded and created files.") print(" This shows exactly how the process should work.") else: print("āŒ COMPLETE TEST FAILED") print(" Something went wrong with the download process.") print("\nThis test shows the complete flow that our broken version needs to replicate.")