#!/usr/bin/env python3 """ Test to demonstrate the fix for the AttributeError when API returns list instead of dict. """ import json from generate_prompts import JournalPromptGenerator def test_original_error_case(): """Test the exact error case: API returns a list instead of a dict.""" print("Testing the original error case: API returns list instead of dict") print("="*60) # Create a mock generator generator = JournalPromptGenerator() # Simulate API returning a list (which could happen with null/malformed data) list_response = json.dumps([]) # Empty list print("\n1. Testing with empty list []:") try: result = generator._parse_ai_response(list_response) print(f" Result: Successfully parsed {len(result)} prompts (no AttributeError)") except AttributeError as e: print(f" ERROR: AttributeError occurred: {e}") except Exception as e: print(f" Other error: {type(e).__name__}: {e}") # Test with list containing dictionaries (another possible malformed response) list_with_dicts = json.dumps([ {"some_key": "some value"}, {"another_key": "another value"} ]) print("\n2. Testing with list of dictionaries:") try: result = generator._parse_ai_response(list_with_dicts) print(f" Result: Successfully parsed {len(result)} prompts (no AttributeError)") except AttributeError as e: print(f" ERROR: AttributeError occurred: {e}") except Exception as e: print(f" Other error: {type(e).__name__}: {e}") # Test with None/null data (worst case) print("\n3. Testing with None/null data (simulated):") # We can't directly test None since json.loads would fail, but our code # handles the case where data might be None after parsing print("\n" + "="*60) print("Test complete! The fix prevents AttributeError for list responses.") if __name__ == "__main__": test_original_error_case()