import os
import pytest
import pytest_asyncio
from playwright.async_api import async_playwright, expect
import asyncio

@pytest_asyncio.fixture(scope="session")
def event_loop():
    policy = asyncio.get_event_loop_policy()
    loop = policy.new_event_loop()
    yield loop
    loop.close()

@pytest.mark.asyncio
async def test_space_loads():
    """Test that we can access the Space and see the heading"""
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=False)
        page = await browser.new_page()
        
        try:
            # Navigate to the Space
            await page.goto("https://huggingface.co/spaces/rawwerks/handwriting-ocr")
            
            if os.getenv('PWDEBUG'):
                await page.pause()
            
            # Now look for the login button from Gradio
            print("Looking for login button...")
            login_button = page.get_by_role("button").filter(has_text="Login")
            await login_button.wait_for(state="visible", timeout=5000)
            print("Found login button, clicking...")
            
            if os.getenv('PWDEBUG'):
                await page.pause()
            
            await login_button.click()
            print("Clicked login button")
            
        finally:
            await browser.close()