"""
Test script for enhanced image support in Deckbuilder presentations.
Demonstrates both valid images and PlaceKitten fallback functionality.
"""
import os
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src")) # noqa: E402
from deckbuilder.core.engine import Deckbuilder # noqa: E402
from deckbuilder.content.frontmatter_to_json_converter import markdown_to_canonical_json # noqa: E402
def run_image_presentation_test():
"""Run the enhanced presentation test with image support."""
print("π¨ Testing Enhanced Deckbuilder with Image Support")
print("=" * 60)
# Set output to tests directory
test_output_dir = Path(__file__).parent / "output"
test_output_dir.mkdir(exist_ok=True)
# Set environment variable for output
original_output = os.environ.get("DECK_OUTPUT_FOLDER")
os.environ["DECK_OUTPUT_FOLDER"] = str(test_output_dir)
try:
# Initialize Deckbuilder
deck = Deckbuilder()
print("β
Deckbuilder initialized successfully")
# Check PlaceKitten availability
if deck.placekitten.is_available():
print("β
PlaceKitten integration available")
fallback_info = deck.placekitten.get_fallback_info((800, 600))
print(f" Available images: {fallback_info.get('image_id', 'N/A')}")
styling = fallback_info.get("styling", {}).get("base_filter", "N/A")
print(f" Professional styling: {styling}")
else:
print("β οΈ PlaceKitten not available - fallbacks disabled")
# Read test markdown file
test_file = Path(__file__).parent / "test_presentation.md"
print(f"π Reading test file: {test_file.name}")
with open(test_file, "r", encoding="utf-8") as f:
markdown_content = f.read()
# Convert markdown to canonical JSON format
print("\nπ Converting markdown to canonical JSON...")
canonical_data = markdown_to_canonical_json(markdown_content)
# Create presentation from canonical JSON
print("ποΈ Creating presentation with image support...")
result = deck.create_presentation(canonical_data, "enhanced_image_test")
print(f"β
{result}")
# List generated files
output_files = list(test_output_dir.glob("*enhanced_image_test*.g.pptx"))
if output_files:
for file in output_files:
file_size = file.stat().st_size / 1024 # KB
print(f"π Generated: {file.name} ({file_size:.1f} KB)")
# Display cache statistics
cache_stats = deck.image_handler.get_cache_stats()
print("\nπ Image Cache Statistics:")
print(f" Files cached: {cache_stats['file_count']}")
print(f" Cache size: {cache_stats['total_size_mb']} MB")
print(f" Cache location: {Path(cache_stats['cache_dir']).name}")
print("\nπ Image presentation test completed successfully!")
return True
except Exception as e:
print(f"β Test failed: {e}")
import traceback
traceback.print_exc()
return False
finally:
# Restore environment
if original_output:
os.environ["DECK_OUTPUT_FOLDER"] = original_output
elif "DECK_OUTPUT_FOLDER" in os.environ:
del os.environ["DECK_OUTPUT_FOLDER"]
def demonstrate_image_features():
"""Demonstrate specific image handling features."""
print("\nπ§ Image Feature Demonstration")
print("-" * 40)
try:
deck = Deckbuilder()
# Test ImageHandler
print("πΌοΈ ImageHandler Features:")
valid_image = "src/placekitten/images/ACuteKitten-1.png"
invalid_image = "assets/non_existent.png"
print(f" Valid image check: {deck.image_handler.validate_image(valid_image)}")
print(f" Invalid image check: {deck.image_handler.validate_image(invalid_image)}")
if Path(valid_image).exists():
dimensions = deck.image_handler.get_image_dimensions(valid_image)
print(f" Image dimensions: {dimensions}")
# Test PlaceKitten fallback info
print("\nπ± PlaceKitten Features:")
if deck.placekitten.is_available():
fallback_info = deck.placekitten.get_fallback_info((800, 600), context={"layout": "Picture with Caption", "slide_index": 1})
print(f" Fallback available: {fallback_info['available']}")
print(f" Selected image ID: {fallback_info['image_id']}")
print(f" Professional config: {fallback_info['styling']['base_filter']}")
print(f" Cached: {fallback_info['cached']}")
return True
except Exception as e:
print(f"β Feature demonstration failed: {e}")
return False