import sys
import os

# Add src/ to sys.path so Python can find scraper/
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))

# Import modules
from scraper.parser import parse_from_file
from scraper.fetcher import Fetcher

# Initialize Fetcher
fetcher = Fetcher()

def test_parse_saved_files():
    """Test parsing all saved HTML files (debug mode)."""
    html_files = [f for f in os.listdir("tests") if f.endswith(".html")]

    for filename in html_files:
        print(f"\n🚀 Testing parser on: {filename}")
        data = parse_from_file(filename)
        assert data["tokens"], f"❌ No tokens found in {filename}!"
        print(f"✅ Parsed {len(data['tokens'])} tokens from {filename}.")

def test_live_fetch_and_parse():
    """Test fetching & parsing live data (production mode)."""
    test_sites = ["Airdrops.io", "CoinMarketCap Recently Added"]  # Match `sites.yml` names

    for site in test_sites:
        print(f"\n🚀 Testing live fetch & parse for: {site}")
        data = fetcher.fetch_and_parse(site)
        assert data["tokens"], f"❌ No tokens found for {site}!"
        print(f"✅ Live parsed {len(data['tokens'])} tokens from {site}.")

if __name__ == "__main__":
    test_parse_saved_files()  # Debug mode
    test_live_fetch_and_parse()  # Production mode
    print("\n🎉 ALL PARSER TESTS COMPLETED SUCCESSFULLY! 🚀")
