#!/usr/bin/env python3 """ Test script to verify that images are being embedded in .ppz files """ import os import sys import zipfile import tempfile import shutil # Add project to path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from pyPhotoAlbum.project import Project, Page from pyPhotoAlbum.models import ImageData from pyPhotoAlbum.project_serializer import save_to_zip, load_from_zip def test_zip_embedding(): """Test that images are properly embedded in the zip file""" # Create a test project with an image project = Project("Test Embedding") page = Page() project.add_page(page) # Use an existing test image test_image = "./projects/project_with_image/assets/test_image.jpg" if not os.path.exists(test_image): print(f"ERROR: Test image not found: {test_image}") return False print(f"Using test image: {test_image}") print(f"Test image size: {os.path.getsize(test_image)} bytes") # Import the image through the asset manager print("\n1. Importing image through asset manager...") asset_path = project.asset_manager.import_asset(test_image) print(f" Asset path: {asset_path}") # Check that the asset was copied full_asset_path = project.asset_manager.get_absolute_path(asset_path) if os.path.exists(full_asset_path): print(f" ✓ Asset exists at: {full_asset_path}") print(f" Asset size: {os.path.getsize(full_asset_path)} bytes") else: print(f" ✗ ERROR: Asset not found at {full_asset_path}") return False # Add image to page img = ImageData() img.image_path = asset_path img.position = (10, 10) img.size = (50, 50) page.layout.add_element(img) # Save to a temporary zip file print("\n2. Saving project to .ppz file...") with tempfile.NamedTemporaryFile(suffix='.ppz', delete=False) as tmp: zip_path = tmp.name success, error = save_to_zip(project, zip_path) if not success: print(f" ✗ ERROR: Failed to save: {error}") return False print(f" ✓ Saved to: {zip_path}") zip_size = os.path.getsize(zip_path) print(f" Zip file size: {zip_size:,} bytes") # Inspect the zip file contents print("\n3. Inspecting zip file contents...") with zipfile.ZipFile(zip_path, 'r') as zf: files = zf.namelist() print(f" Files in zip: {len(files)}") for fname in files: info = zf.getinfo(fname) print(f" - {fname} ({info.file_size:,} bytes)") # Check if assets are included asset_files = [f for f in files if f.startswith('assets/')] print(f"\n4. Checking for embedded assets...") print(f" Assets found: {len(asset_files)}") if len(asset_files) == 0: print(" ✗ ERROR: No assets embedded in zip file!") print(f"\n DEBUG INFO:") print(f" Project folder: {project.folder_path}") print(f" Assets folder: {project.asset_manager.assets_folder}") print(f" Assets folder exists: {os.path.exists(project.asset_manager.assets_folder)}") if os.path.exists(project.asset_manager.assets_folder): assets = os.listdir(project.asset_manager.assets_folder) print(f" Files in assets folder: {assets}") # Cleanup os.unlink(zip_path) return False print(f" ✓ Found {len(asset_files)} asset file(s) in zip") # Load the project back print("\n5. Loading project from zip...") try: loaded_project = load_from_zip(zip_path) print(f" ✓ Loaded project: {loaded_project.name}") except Exception as e: print(f" ✗ ERROR: Failed to load: {e}") os.unlink(zip_path) return False # Check that the image is accessible print("\n6. Verifying loaded image...") if loaded_project.pages and loaded_project.pages[0].layout.elements: img_elem = loaded_project.pages[0].layout.elements[0] if isinstance(img_elem, ImageData): loaded_img_path = loaded_project.asset_manager.get_absolute_path(img_elem.image_path) if os.path.exists(loaded_img_path): print(f" ✓ Image accessible at: {loaded_img_path}") print(f" Image size: {os.path.getsize(loaded_img_path)} bytes") else: print(f" ✗ ERROR: Image not found at {loaded_img_path}") os.unlink(zip_path) return False # Cleanup os.unlink(zip_path) loaded_project.cleanup() print("\n✅ ALL TESTS PASSED - Images are being embedded correctly!") return True if __name__ == "__main__": print("=" * 70) print("Testing .ppz file image embedding") print("=" * 70) success = test_zip_embedding() sys.exit(0 if success else 1)