presenton/servers/fastapi/tests/test_slide_to_html.py
sudipnext 3d06644914 feat: update placeholder image references and improve asset handling
- Replaced all instances of the placeholder image path from "/static/images/placeholder.jpg" to "/static/images/replaceable_template_image.png".
- Added a new Nginx location block for serving app data with a long cache expiration.
- Enhanced the image generation service to return the new template image when generation fails.
- Updated various services and endpoints to ensure consistent handling of asset paths, including resolving backend asset URLs.
- Removed Electron-specific checks from several components to streamline API calls and improve compatibility with web deployments.
- Improved error handling and logging in the PDF export process.
- Adjusted Next.js configuration for API routing to ensure proper asset serving in Docker environments.
2026-04-18 20:56:37 +05:45

115 lines
No EOL
3.5 KiB
Python

import pytest
import os
from fastapi.testclient import TestClient
# Import the main app
from server import app
client = TestClient(app)
def test_slide_to_html_endpoint():
"""Test the slide-to-html endpoint with streaming API support."""
# Sample XML data (simplified version of OXML)
test_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
<p:cSld>
<p:bg>
<p:bgPr>
<a:solidFill xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:srgbClr val="FFFFFF"/>
</a:solidFill>
</p:bgPr>
</p:bg>
<p:spTree>
<p:sp>
<p:txBody>
<a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:r>
<a:t>Test Slide</a:t>
</a:r>
</a:p>
</p:txBody>
</p:sp>
</p:spTree>
</p:cSld>
</p:sld>'''
# Skip this test if ANTHROPIC_API_KEY is not set
if not os.getenv('ANTHROPIC_API_KEY'):
pytest.skip("ANTHROPIC_API_KEY not set - skipping API test")
# Use a placeholder image path (since we can't easily test with real files)
test_data = {
"image": "/static/images/replaceable_template_image.png",
"xml": test_xml
}
# Make the request with JSON
response = client.post(
"/api/v1/ppt/slide-to-html/",
json=test_data
)
# Check response (may take several minutes due to streaming)
print("Note: This test may take several minutes due to Claude's streaming processing...")
if response.status_code == 200:
data = response.json()
assert data["success"] is True
assert "html" in data
assert len(data["html"]) > 0
print(f"Generated HTML preview: {data['html'][:200]}...")
print("✅ Streaming API test completed successfully")
else:
print(f"Request failed with status {response.status_code}: {response.text}")
# Don't fail the test if API key is missing or invalid
if "ANTHROPIC_API_KEY" in response.text:
pytest.skip("Invalid API key - skipping test")
elif "Streaming is required" in response.text:
print("✅ Streaming error handled correctly by endpoint")
def test_slide_to_html_invalid_path():
"""Test the endpoint with an invalid image path."""
test_data = {
"image": "/app_data/images/nonexistent/image.png",
"xml": "<simple>xml</simple>"
}
response = client.post(
"/api/v1/ppt/slide-to-html/",
json=test_data
)
assert response.status_code == 404
assert "Image file not found" in response.json()["detail"]
def test_slide_to_html_missing_xml():
"""Test the endpoint with missing XML data."""
test_data = {
"image": "/static/images/replaceable_template_image.png"
# No XML data provided
}
response = client.post(
"/api/v1/ppt/slide-to-html/",
json=test_data
)
assert response.status_code == 422 # Validation error
if __name__ == "__main__":
# Run a simple test
test_slide_to_html_invalid_path()
print("✅ Invalid path test passed")
test_slide_to_html_missing_xml()
print("✅ Missing XML test passed")
print("🧪 Run full tests with: pytest test_slide_to_html.py")