Files
daily-journal-prompt/test_valid_response.py

54 lines
1.8 KiB
Python

#!/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 (default num_prompts from settings)
valid_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."
}
# Convert to JSON string
json_response = json.dumps(valid_response)
print("\n=== Test: Valid JSON response ===")
result = generator._parse_ai_response(json_response)
print(f"Number of prompts extracted: {len(result)}")
for i, prompt_dict in enumerate(result):
prompt_key = list(prompt_dict.keys())[0]
prompt_text = prompt_dict[prompt_key]
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)}")
if __name__ == "__main__":
test_valid_response()