#!/usr/bin/env python3 """ Test the error handling with a valid response. """ import sys import os import json sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from generate_prompts import JournalPromptGenerator def test_valid_response(): """Test with a valid JSON response.""" # Create a mock generator generator = JournalPromptGenerator(config_path=".env") # Create a valid response with 4 prompts as a list (new format) valid_response = [ "Write about a time when you felt truly at peace.", "Describe your ideal morning routine in detail.", "What are three things you're grateful for today?", "Reflect on a recent challenge and what you learned from it." ] # Convert to JSON string json_response = json.dumps(valid_response) print("\n=== Test: Valid JSON response (list format) ===") result = generator._parse_ai_response(json_response) print(f"Number of prompts extracted: {len(result)}") print(f"Type of result: {type(result)}") for i, prompt_text in enumerate(result): print(f"Prompt {i+1}: {prompt_text[:50]}...") # Test with backticks print("\n=== Test: Valid JSON response with backticks ===") backticks_response = f"```json\n{json_response}\n```" result = generator._parse_ai_response(backticks_response) print(f"Number of prompts extracted: {len(result)}") # Test with "json" prefix print("\n=== Test: Valid JSON response with 'json' prefix ===") json_prefix_response = f"json\n{json_response}" result = generator._parse_ai_response(json_prefix_response) print(f"Number of prompts extracted: {len(result)}") # Test fallback for old dictionary format print("\n=== Test: Fallback for old dictionary format ===") old_format_response = { "newprompt0": "Write about a time when you felt truly at peace.", "newprompt1": "Describe your ideal morning routine in detail.", "newprompt2": "What are three things you're grateful for today?", "newprompt3": "Reflect on a recent challenge and what you learned from it." } json_old_response = json.dumps(old_format_response) result = generator._parse_ai_response(json_old_response) print(f"Number of prompts extracted: {len(result)}") if __name__ == "__main__": test_valid_response()