File size: 2,874 Bytes
ffec248
 
 
 
 
37cf6e7
 
 
 
 
 
 
 
 
 
ffec248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37cf6e7
 
ffec248
 
37cf6e7
ffec248
 
 
 
 
37cf6e7
 
 
 
 
 
 
 
ffec248
37cf6e7
 
ffec248
37cf6e7
 
ffec248
 
 
37cf6e7
ffec248
 
37cf6e7
ffec248
37cf6e7
 
 
 
 
 
ffec248
 
 
 
37cf6e7
ffec248
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash

# Exit on error
set -e

# Parse command line arguments
DEBUG=0
while [[ "$#" -gt 0 ]]; do
    case $1 in
        --debug) DEBUG=1 ;;
        *) echo "Unknown parameter: $1"; exit 1 ;;
    esac
    shift
done

# Check if auth_state.json exists
if [ ! -f "auth_state.json" ]; then
    echo "Error: auth_state.json not found. Please run save_hf_storage_state.py first to save your Hugging Face login state."
    exit 1
fi

# Activate virtual environment if it exists, create if it doesn't
if [ ! -d "venv" ]; then
    echo "Creating virtual environment..."
    python -m venv venv
fi

echo "Activating virtual environment..."
source venv/bin/activate

# Install dependencies if needed
echo "Installing dependencies..."
if ! command -v uv &> /dev/null; then
    echo "Installing uv package installer..."
    curl -LsSf https://astral.sh/uv/install.sh | sh
fi
uv pip install -r requirements.txt

# Install Playwright browsers if needed
echo "Installing Playwright browsers..."
playwright install chromium

# Create a new test file specifically for server tests
cat > test_server.py << 'EOL'
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()

EOL

# Run the server tests with or without Playwright Inspector based on debug flag
echo "Running server tests..."
if [ "$DEBUG" -eq 1 ]; then
    echo "Debug mode enabled - launching Playwright Inspector..."
    PWDEBUG=1 python -m pytest test_server.py -v
else
    python -m pytest test_server.py -v
fi

# Cleanup
echo "Cleaning up..."
rm test_server.py
rm -f error.png

# Deactivate virtual environment
deactivate