gateway/test_json_to_docx.py
2025-10-11 23:39:13 +02:00

120 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""
Test script for JSON-to-DOCX rendering pipeline.
"""
import asyncio
import json
import sys
import os
from modules.services.serviceGeneration.renderers.docx_renderer import DocxRenderer
async def test_json_to_docx():
"""Test the JSON-to-DOCX rendering pipeline."""
# Create test JSON document
test_json = {
"metadata": {
"title": "Test Document",
"version": "1.0"
},
"sections": [
{
"id": "heading1",
"type": "heading",
"data": {
"level": 1,
"text": "Document Overview"
}
},
{
"id": "paragraph1",
"type": "paragraph",
"data": {
"text": "This is a test paragraph to verify JSON-to-DOCX rendering works correctly."
}
},
{
"id": "table1",
"type": "table",
"data": {
"headers": ["Name", "Quantity", "Status"],
"rows": [
["Item 1", "5", "Active"],
["Item 2", "3", "Inactive"],
["Item 3", "10", "Active"]
]
}
},
{
"id": "list1",
"type": "bullet_list",
"data": {
"items": [
"First bullet point",
"Second bullet point",
"Third bullet point"
]
}
},
{
"id": "heading2",
"type": "heading",
"data": {
"level": 2,
"text": "Summary"
}
},
{
"id": "paragraph2",
"type": "paragraph",
"data": {
"text": "This document demonstrates the new JSON-based rendering system."
}
}
]
}
print("🧪 Testing JSON-to-DOCX rendering...")
print(f"📄 Test document has {len(test_json['sections'])} sections")
try:
# Create renderer
renderer = DocxRenderer()
# Test rendering
docx_content, mime_type = await renderer.render(
extracted_content=test_json,
title="Test Document",
user_prompt="Create a test document"
)
print(f"✅ Rendering successful!")
print(f"📊 MIME type: {mime_type}")
print(f"📏 Content length: {len(docx_content)} characters")
print(f"🔍 Content preview: {docx_content[:100]}...")
# Save test file
import base64
docx_bytes = base64.b64decode(docx_content)
with open("test_json_to_docx.docx", "wb") as f:
f.write(docx_bytes)
print(f"💾 Test DOCX saved as: test_json_to_docx.docx")
return True
except Exception as e:
print(f"❌ Rendering failed: {str(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = asyncio.run(test_json_to_docx())
if success:
print("\n🎉 JSON-to-DOCX rendering test PASSED!")
else:
print("\n💥 JSON-to-DOCX rendering test FAILED!")
sys.exit(1)