File size: 3,791 Bytes
cb922bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc8577b
cb922bf
 
 
 
 
 
cc8577b
 
cb922bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc8577b
cb922bf
 
cc8577b
cb922bf
cc8577b
cb922bf
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Unit tests for FastAPI integration.

Tests verify that:
1. /reset endpoint returns {"status": "ok"}
2. Gradio interface is mounted at "/"
3. Existing Gradio functionality still works
"""

import pytest
from fastapi.testclient import TestClient
from fastapi import FastAPI
import gradio as gr


def test_reset_endpoint_returns_ok():
    """Test /reset endpoint returns {"status": "ok"}."""
    # Create a minimal FastAPI app with reset endpoint
    app = FastAPI()
    
    @app.post("/reset")
    async def reset_endpoint():
        """Reset endpoint for validator compliance."""
        return {"status": "ok"}
    
    client = TestClient(app)
    response = client.post("/reset")
    
    assert response.status_code == 200
    assert response.json() == {"status": "ok"}


def test_gradio_mounted_at_root():
    """Test Gradio interface is mounted at root path '/'."""
    # Create a minimal Gradio interface
    with gr.Blocks() as demo:
        gr.Markdown("Test Interface")
    
    # Create FastAPI app and mount Gradio
    app = FastAPI()
    
    @app.post("/reset")
    async def reset_endpoint():
        return {"status": "ok"}
    
    app = gr.mount_gradio_app(app, demo, path="/")
    
    client = TestClient(app)
    
    # Test that root path returns HTML (Gradio interface)
    response = client.get("/")
    assert response.status_code == 200
    assert "text/html" in response.headers.get("content-type", "")


def test_reset_endpoint_with_gradio_mounted():
    """Test /reset endpoint works when Gradio is mounted."""
    # Create a minimal Gradio interface
    with gr.Blocks() as demo:
        gr.Markdown("Test Interface")
    
    # Create FastAPI app with reset endpoint
    app = FastAPI()
    
    @app.post("/reset")
    async def reset_endpoint():
        return {"status": "ok"}
    
    # Mount Gradio at root
    app = gr.mount_gradio_app(app, demo, path="/")
    
    client = TestClient(app)
    
    # Test reset endpoint still works after mounting Gradio
    response = client.post("/reset")
    assert response.status_code == 200
    assert response.json() == {"status": "ok"}


def test_fastapi_app_structure():
    """Test that the main app.py has correct FastAPI structure."""
    # Import the demo from server/app.py
    import sys
    import os
    
    # Add parent directory to path to import app
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    
    # Import the demo object from server/app.py
    from server.app import demo
    
    # Verify demo is a Gradio Blocks instance
    assert isinstance(demo, gr.Blocks)
    
    # Create FastAPI app and mount demo
    app = FastAPI()
    
    @app.post("/reset")
    async def reset_endpoint():
        return {"status": "ok"}
    
    app = gr.mount_gradio_app(app, demo, path="/")
    
    client = TestClient(app)
    
    # Test reset endpoint
    response = client.post("/reset")
    assert response.status_code == 200
    assert response.json() == {"status": "ok"}
    
    # Test Gradio interface at root
    response = client.get("/")
    assert response.status_code == 200


def test_port_7860_configuration():
    """Test that port 7860 is the configured port."""
    # This is a documentation test - we verify the port is set in server/app.py
    # The actual port binding happens at runtime with uvicorn.run()
    
    # Read server/app.py to verify port configuration
    import os
    app_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "server", "app.py")
    
    with open(app_path, 'r') as f:
        content = f.read()
    
    # Verify port 7860 is specified in uvicorn.run()
    assert "port=7860" in content or "port = 7860" in content


if __name__ == "__main__":
    pytest.main([__file__, "-v"])