#!/usr/bin/env python3 """Test the new strict mode for error handling""" 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 test prompt prompt_file = test_dir / "greeting.md" prompt_file.write_text("Hello {{name}}, you are {{age}} years old!") # Configure PromptManager PromptManager.configure(path=test_dir) print("=" * 60) print("Testing Strict Mode for Error Handling") print("=" * 60) # Test 1: Default behavior with fill() - should raise error print("\n1. Direct fill() with missing variables (default strict=True):") print("-" * 40) try: prompt = PromptManager.get_prompt("greeting") filled = prompt.fill(name="Alice") # Missing 'age' print(f"Result: {filled}") except ValueError as e: print(f"✓ Error raised as expected: {e}") # Test 2: Non-strict mode with fill() print("\n2. Direct fill() with strict=False:") print("-" * 40) try: prompt = PromptManager.get_prompt("greeting") filled = prompt.fill(strict=False, name="Alice") # Missing 'age' print(f"✓ Result (unfilled template): {filled}") except ValueError as e: print(f"Unexpected error: {e}") # Test 3: get_prompt with pre-fill (default non-strict) print("\n3. get_prompt() with pre-fill (default strict=False):") print("-" * 40) try: prompt = PromptManager.get_prompt("greeting", name="Alice") # Missing 'age' print(f"✓ Result (unfilled template): {prompt.prompt}") except ValueError as e: print(f"Unexpected error: {e}") # Test 4: get_prompt with pre-fill in strict mode print("\n4. get_prompt() with pre-fill (strict=True):") print("-" * 40) try: prompt = PromptManager.get_prompt("greeting", strict=True, name="Alice") # Missing 'age' print(f"Result: {prompt.prompt}") except ValueError as e: print(f"✓ Error raised as expected: {e}") # Test 5: Successful fill with all variables print("\n5. Successful fill with all variables:") print("-" * 40) prompt = PromptManager.get_prompt("greeting") filled = prompt.fill(name="Bob", age=25) print(f"✓ Result: {filled}") # Test 6: Successful pre-fill with all variables print("\n6. Successful pre-fill on retrieval:") print("-" * 40) prompt = PromptManager.get_prompt("greeting", name="Charlie", age=30) print(f"✓ Result: {prompt.prompt}") # Test 7: Check that variables are correctly identified print("\n7. Variable extraction:") print("-" * 40) prompt = PromptManager.get_prompt("greeting") print(f"Required variables: {prompt.variables}") print(f"Missing when only 'name' provided: {prompt.get_missing_variables(name='Test')}") print("\n" + "=" * 60) print("All tests completed!") print("=" * 60) # Cleanup import shutil shutil.rmtree(test_dir)