#!/usr/bin/env python3 """ Simulate exactly what happens when a user clicks download in the browser. This test replicates the frontend download flow step by step. """ import socketio import time import json def simulate_user_download(): """Simulate the exact flow: search -> click download -> watch progress""" print("šŸŽ­ SIMULATING USER DOWNLOAD FLOW") print("=" * 50) print("This simulates exactly what happens when you:") print("1. Search for 'you lloyd'") print("2. Click download on the first result") print("3. Watch the download progress") print() # Create a Socket.IO client like the frontend sio = socketio.Client() # Track what we receive search_results = None download_updates = [] @sio.event def connect(): print("āœ… Connected to backend (like browser connecting)") @sio.event def disconnect(): print("āŒ Disconnected from backend") @sio.event def search_results(data): nonlocal search_results search_results = data print(f"šŸ” Search results received: {len(data.get('results', []))} items") # Show first few results like the UI would results = data.get('results', []) for i, result in enumerate(results[:3]): print(f" {i+1}. '{result.get('name', 'Unknown')}' by '{result.get('artist', 'Unknown')}'") print(f" ID: {result.get('id', 'NO ID!')}") print(f" URL: {result.get('url', 'NO URL!')}") @sio.event def update_status(data): nonlocal download_updates download_updates.append(data) print(f"šŸ“„ Download status update received:") print(f" {json.dumps(data, indent=4)}") if data.get('history'): for item in data['history']: status = item.get('status', 'Unknown') name = item.get('name', 'Unknown') print(f" šŸ“Š {name}: {status}") try: # Step 1: Connect (like browser loading the page) print("šŸ”Œ Step 1: Connecting to backend...") sio.connect('http://localhost:5140') time.sleep(1) # Step 2: Search (like typing in search box) print("\nšŸ” Step 2: Searching for 'you lloyd'...") sio.emit('search', { 'query': 'you lloyd', 'type': 'track' }) time.sleep(3) # Wait for search results if not search_results or not search_results.get('results'): print("āŒ No search results received - cannot proceed with download test") return False # Step 3: Click download on first result (simulate user click) print("\nšŸ”½ Step 3: User clicks download on first result...") first_result = search_results['results'][0] print(f" Downloading: '{first_result.get('name')}' by '{first_result.get('artist')}'") print(f" Spotify URL: {first_result.get('url')}") print(f" Track ID: {first_result.get('id', 'MISSING ID!')}") # Send download request exactly like the frontend does download_request = { 'url': first_result.get('url'), 'searchResult': first_result, 'name': first_result.get('name'), 'artist': first_result.get('artist'), 'type': first_result.get('type'), 'id': first_result.get('id') } print(f"\nšŸ“¤ Sending download request:") print(json.dumps(download_request, indent=2)) sio.emit('download', download_request) # Step 4: Wait and watch for download progress (like user watching UI) print("\nā³ Step 4: Waiting for download progress...") print(" (This is where you'd see the download button change to 'Downloading...')") for i in range(15): # Wait up to 15 seconds time.sleep(1) print(f" Waiting... {i+1}/15 seconds") if download_updates: latest_update = download_updates[-1] if latest_update.get('history'): for item in latest_update['history']: status = item.get('status', 'Unknown') if status in ['Complete', 'Failed', 'Error']: print(f" šŸŽÆ Final status reached: {status}") return True print(" ā° Timeout reached - download may still be processing") return len(download_updates) > 0 except Exception as e: print(f"šŸ’„ Error during simulation: {e}") return False finally: if sio.connected: sio.disconnect() if __name__ == "__main__": print("šŸš€ DOWNLOAD SIMULATION TEST") print("This simulates clicking download in the web browser") print("=" * 60) success = simulate_user_download() print("\n" + "=" * 60) if success: print("āœ… SIMULATION COMPLETED - Download process was triggered!") print(" Check the logs above to see if the download actually worked.") else: print("āŒ SIMULATION FAILED - Download process did not work properly.") print(" This explains why downloads don't work in the browser.")