PutuAPI / tests /test_custom.py
suzmen's picture
Upload 64 files
2af6ef5 verified
"""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"