#!/usr/bin/env python3 """ Simple test script for the watchlist & auto-download system """ import asyncio import sys from pathlib import Path # Add project to path sys.path.insert(0, str(Path(__file__).parent)) from app.watchlist import watchlist_manager from app.episode_checker import episode_checker from app.auto_download_scheduler import auto_download_scheduler from app.models.watchlist import ( WatchlistItemCreate, WatchlistStatus, QualityPreference, WatchlistSettings ) def test_watchlist_basics(): """Test basic watchlist operations""" print("\n" + "="*60) print("๐Ÿงช TEST 1: Watchlist Manager Basics") print("="*60) # Test user ID test_user = "test_user_simple" # Clean up any existing test items first print("\n0. Cleaning up any existing test items...") all_items = watchlist_manager.get_all() for item in all_items: if item.user_id == test_user: watchlist_manager.delete(item.id) print(f" โœ“ Deleted old test item: {item.id}") # Create a test item print("\n1. Creating watchlist item...") item_data = WatchlistItemCreate( anime_title="Test Anime Simple", anime_url="https://anime-sama.si/catalogue/test-simple/vostfr/", provider_id="animesama", lang="vostfr", auto_download=True, quality_preference=QualityPreference.AUTO ) try: item = watchlist_manager.add(test_user, item_data) print(f" โœ… Item created: {item.id}") print(f" Title: {item.anime_title}") print(f" Status: {item.status}") print(f" Auto-download: {item.auto_download}") except Exception as e: print(f" โŒ Create failed: {e}") return False # Get all items for user print("\n2. Getting user's items...") try: items = watchlist_manager.get_all(test_user) print(f" โœ… Found {len(items)} items for user") if len(items) > 0: print(f" First item: {items[0].anime_title}") except Exception as e: print(f" โŒ Get all failed: {e}") return False # Get stats print("\n3. Getting statistics...") try: stats = watchlist_manager.get_stats(test_user) print(f" โœ… Stats: total={stats['total']}, active={stats['active']}, paused={stats['paused']}") except Exception as e: print(f" โŒ Stats failed: {e}") return False # Update item print("\n4. Updating item to paused...") try: updated = watchlist_manager.update(item.id, {"status": WatchlistStatus.PAUSED}) print(f" โœ… Item updated to status: {updated.status}") except Exception as e: print(f" โŒ Update failed: {e}") return False # Update check time print("\n5. Updating check time...") try: updated = watchlist_manager.update_check_time(item.id, 5) print(f" โœ… Check time updated") print(f" Last episode: {updated.last_episode_downloaded}") except Exception as e: print(f" โŒ Update check time failed: {e}") return False # Delete item (cleanup) print("\n6. Cleaning up - deleting test item...") try: result = watchlist_manager.delete(item.id) print(f" โœ… Item deleted: {result}") except Exception as e: print(f" โŒ Delete failed: {e}") return False print("\nโœ… Watchlist Manager tests PASSED") return True def test_settings(): """Test settings management""" print("\n" + "="*60) print("๐Ÿงช TEST 2: Settings Management") print("="*60) print("\n1. Getting current settings...") try: settings = watchlist_manager.get_settings() print(f" โœ… Current settings:") print(f" - Check interval: {settings.check_interval_hours}h") print(f" - Auto-download: {settings.auto_download_enabled}") print(f" - Max concurrent: {settings.max_concurrent_auto_downloads}") print(f" - Notifications: {settings.notify_on_new_episodes}") except Exception as e: print(f" โŒ Get settings failed: {e}") return False print("\n2. Updating settings...") try: new_settings = WatchlistSettings( check_interval_hours=12, auto_download_enabled=True, max_concurrent_auto_downloads=3, notify_on_new_episodes=False ) watchlist_manager.update_settings(new_settings) print(f" โœ… Settings updated") except Exception as e: print(f" โŒ Update settings failed: {e}") import traceback traceback.print_exc() return False print("\n3. Verifying settings...") try: settings = watchlist_manager.get_settings() if settings.check_interval_hours != 12: print(f" โŒ Settings not saved correctly: check_interval is {settings.check_interval_hours}, expected 12") return False print(f" โœ… Settings verified:") print(f" - Check interval: {settings.check_interval_hours}h โœ“") except Exception as e: print(f" โŒ Verify settings failed: {e}") return False # Reset to defaults print("\n4. Resetting to default settings...") try: default_settings = WatchlistSettings() watchlist_manager.update_settings(default_settings) print(f" โœ… Settings reset to defaults") except Exception as e: print(f" โŒ Reset settings failed: {e}") return False print("\nโœ… Settings tests PASSED") return True async def test_scheduler(): """Test scheduler controls""" print("\n" + "="*60) print("๐Ÿงช TEST 3: Auto-Download Scheduler") print("="*60) print("\n1. Testing scheduler start...") try: auto_download_scheduler.start() print(f" โœ… Scheduler started") print(f" Status: running={auto_download_scheduler.is_running()}") except Exception as e: print(f" โŒ Start failed: {e}") import traceback traceback.print_exc() return False if not auto_download_scheduler.is_running(): print(f" โŒ Scheduler not running after start") return False print("\n2. Testing scheduler stop...") try: auto_download_scheduler.stop() print(f" โœ… Scheduler stopped") print(f" Status: running={auto_download_scheduler.is_running()}") except Exception as e: print(f" โŒ Stop failed: {e}") return False if auto_download_scheduler.is_running(): print(f" โŒ Scheduler still running after stop") return False print("\n3. Testing scheduler restart...") try: auto_download_scheduler.start() print(f" โœ… Scheduler restarted") # Get next run time next_run = auto_download_scheduler.get_next_run_time() if next_run: print(f" Next run: {next_run}") auto_download_scheduler.stop() print(f" โœ… Scheduler stopped again") except Exception as e: print(f" โŒ Restart failed: {e}") return False print("\nโœ… Scheduler tests PASSED") return True async def run_all_tests(): """Run all tests""" print("\n" + "="*60) print("๐Ÿš€ WATCHLIST SYSTEM TEST SUITE") print("="*60) tests = [ ("Watchlist Manager", lambda: test_watchlist_basics()), ("Settings", lambda: test_settings()), ("Scheduler", test_scheduler) # This one is async ] results = [] for name, test_func in tests: try: # Check if it's a coroutine function import asyncio if asyncio.iscoroutinefunction(test_func): result = await test_func() else: result = test_func() results.append((name, result)) except Exception as e: print(f"\nโŒ {name} test crashed: {e}") import traceback traceback.print_exc() results.append((name, False)) # Summary print("\n" + "="*60) print("๐Ÿ“Š TEST SUMMARY") print("="*60) passed = sum(1 for _, result in results if result) total = len(results) for name, result in results: status = "โœ… PASSED" if result else "โŒ FAILED" print(f"{status}: {name}") print(f"\nTotal: {passed}/{total} tests passed") if passed == total: print("\n๐ŸŽ‰ ALL TESTS PASSED! The watchlist system is ready to use.") print("\n๐Ÿ“ Next steps:") print(" 1. Start the server: uvicorn main:app --reload") print(" 2. Open http://localhost:3000/watchlist") print(" 3. Add anime to your watchlist") print(" 4. Start the scheduler") return True else: print("\nโš ๏ธ Some tests failed. Please review the errors above.") return False if __name__ == "__main__": success = asyncio.run(run_all_tests()) sys.exit(0 if success else 1)