#!/usr/bin/env python3 """Test optional variables with Jinja2 conditionals""" from pathlib import Path from src.llmutils.prompt_manager import PromptManager # Create test prompts directory test_dir = Path("test_prompts") test_dir.mkdir(exist_ok=True) # Create a template with required and optional variables report_template = test_dir / "report.md" report_template.write_text("""# Status Report Status: {{ status }} {% if error %} ⚠️ ERROR: {{ error }} {% endif %} {% if warning %} ⚠️ WARNING: {{ warning }} {% endif %} {% if details %} ## Details {{ details }} {% endif %} Generated at: {{ timestamp }}""") # Configure PromptManager PromptManager.configure(path=test_dir) print("=" * 60) print("Testing Optional Variables with Jinja2 Conditionals") print("=" * 60) # Test 1: All variables provided print("\n1. All variables provided:") print("-" * 40) prompt = PromptManager.get_prompt("report") filled = prompt.fill( status="Operational", error="Database connection failed", warning="High memory usage", details="System recovered after 5 retries", timestamp="2024-01-15 10:30:00" ) print(filled) # Test 2: Only required variables (status and timestamp) print("\n2. Only truly required variables (will fail with strict=True):") print("-" * 40) prompt = PromptManager.get_prompt("report") try: # This will fail because Jinja2 considers all variables as required filled = prompt.fill( status="Operational", timestamp="2024-01-15 10:30:00" ) print(filled) except ValueError as e: print(f"Error (expected): Missing variables {{'error', 'warning', 'details'}}") print(f"Actual error: {e}") # Test 3: Provide None/empty for optional variables print("\n3. Provide None/empty values for optional variables:") print("-" * 40) prompt = PromptManager.get_prompt("report") filled = prompt.fill( status="Operational", error=None, # Will not show in output due to {% if error %} warning="", # Empty string is falsy, won't show details=None, # Will not show timestamp="2024-01-15 10:30:00" ) print(filled) # Test 4: Mix of provided and None values print("\n4. Mix of provided and None values:") print("-" * 40) prompt = PromptManager.get_prompt("report") filled = prompt.fill( status="Degraded", error="API timeout", warning=None, # Won't show details=None, # Won't show timestamp="2024-01-15 10:35:00" ) print(filled) # Test 5: Using strict=False to handle missing variables print("\n5. Using strict=False for partial filling:") print("-" * 40) prompt = PromptManager.get_prompt("report") filled = prompt.fill( strict=False, status="Unknown", timestamp="2024-01-15 10:40:00" ) print(filled) print("Note: Unfilled variables remain as {{ variable }}") # Test 6: Show extracted variables print("\n6. Extracted variables from template:") print("-" * 40) prompt = PromptManager.get_prompt("report") print(f"All variables detected: {prompt.variables}") print("Note: Jinja2 extracts all variables, even those in conditionals") print("Use None/empty values for optional ones, or use strict=False") print("\n" + "=" * 60) print("Tests completed!") print("=" * 60) # Cleanup import shutil shutil.rmtree(test_dir)