File size: 1,499 Bytes
2af6ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""PhantomAPI — Test custom API endpoint."""

import pytest
from httpx import ASGITransport, AsyncClient
from app.main import app
from unittest.mock import patch

@pytest.fixture
async def client():
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac

@pytest.mark.anyio
async def test_custom_api_endpoint(client):
    """POST /api should return the specific JSON format."""
    # Mock the browser engine to avoid running Playwright
    with patch("app.api.custom.engine.chat") as mock_chat:
        mock_chat.return_value = "Mocked Response"
        
        response = await client.post(
            "/api",
            json={"prompt": "hello"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "success"
        assert data["text"] == "Mocked Response"

@pytest.mark.anyio
async def test_custom_api_error_handling(client):
    """POST /api should return error status on failure."""
    with patch("app.api.custom.engine.chat") as mock_chat:
        mock_chat.side_effect = Exception("Browser error")
        
        response = await client.post(
            "/api",
            json={"prompt": "hello"}
        )
        
        assert response.status_code == 200 # We return 200 with status="error" in the schema
        data = response.json()
        assert data["status"] == "error"
        assert data["text"] == "Browser error"