#!/usr/bin/env python3
"""
Test Title Format Functionality
This script tests that snapshot titles are properly formatted using
child forename and author forename/surname instead of post ID.
"""
import sys
import os
import tempfile
from pathlib import Path
# Add the current directory to the path so we can import modules
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from snapshot_downloader import SnapshotDownloader
class TitleFormatTester:
"""Test class for title formatting functionality."""
def __init__(self):
"""Initialize the tester."""
pass
def test_title_formatting(self):
"""Test that titles are formatted correctly with child and author names."""
print("=" * 60)
print("TEST: Title Format - Child by Author")
print("=" * 60)
with tempfile.TemporaryDirectory() as temp_dir:
downloader = SnapshotDownloader(output_dir=temp_dir)
print("1. Testing standard title format...")
# Test case 1: Complete data
mock_snapshot = {
"id": 123456,
"type": "Snapshot",
"child": {
"forename": "Noah",
"surname": "Smith"
},
"author": {
"forename": "Elena",
"surname": "Garcia"
},
"startTime": "2024-01-15T10:30:00",
"notes": "
Test snapshot content
"
}
html_content = downloader.format_snapshot_html(mock_snapshot)
expected_title = "Noah by Elena Garcia"
if f'{expected_title}
' in html_content:
print(f" ✅ Standard format: {expected_title}")
else:
print(f" ❌ Expected: {expected_title}")
print(" Debug: Looking for title in HTML...")
start = html_content.find('snapshot-title')
if start != -1:
sample = html_content[start:start+100]
print(f" Found: {sample}")
return False
print("\n2. Testing edge cases...")
# Test case 2: Missing child surname
mock_snapshot_2 = {
"id": 789012,
"type": "Snapshot",
"child": {
"forename": "Sofia"
# Missing surname
},
"author": {
"forename": "Maria",
"surname": "Rodriguez"
},
"startTime": "2024-01-15T10:30:00",
"notes": "Test content
"
}
html_content_2 = downloader.format_snapshot_html(mock_snapshot_2)
expected_title_2 = "Sofia by Maria Rodriguez"
if f'{expected_title_2}
' in html_content_2:
print(f" ✅ Missing child surname: {expected_title_2}")
else:
print(f" ❌ Expected: {expected_title_2}")
return False
# Test case 3: Missing author surname
mock_snapshot_3 = {
"id": 345678,
"type": "Snapshot",
"child": {
"forename": "Alex",
"surname": "Johnson"
},
"author": {
"forename": "Lisa"
# Missing surname
},
"startTime": "2024-01-15T10:30:00",
"notes": "Test content
"
}
html_content_3 = downloader.format_snapshot_html(mock_snapshot_3)
expected_title_3 = "Alex by Lisa"
if f'{expected_title_3}
' in html_content_3:
print(f" ✅ Missing author surname: {expected_title_3}")
else:
print(f" ❌ Expected: {expected_title_3}")
return False
# Test case 4: Missing child forename (should fallback to ID)
mock_snapshot_4 = {
"id": 999999,
"type": "Snapshot",
"child": {
"surname": "Brown"
# Missing forename
},
"author": {
"forename": "John",
"surname": "Davis"
},
"startTime": "2024-01-15T10:30:00",
"notes": "Test content
"
}
html_content_4 = downloader.format_snapshot_html(mock_snapshot_4)
expected_title_4 = "Snapshot 999999"
if f'{expected_title_4}
' in html_content_4:
print(f" ✅ Missing child forename (fallback): {expected_title_4}")
else:
print(f" ❌ Expected fallback: {expected_title_4}")
return False
# Test case 5: Missing author forename (should fallback to ID)
mock_snapshot_5 = {
"id": 777777,
"type": "Snapshot",
"child": {
"forename": "Emma",
"surname": "Wilson"
},
"author": {
"surname": "Taylor"
# Missing forename
},
"startTime": "2024-01-15T10:30:00",
"notes": "Test content
"
}
html_content_5 = downloader.format_snapshot_html(mock_snapshot_5)
expected_title_5 = "Snapshot 777777"
if f'{expected_title_5}
' in html_content_5:
print(f" ✅ Missing author forename (fallback): {expected_title_5}")
else:
print(f" ❌ Expected fallback: {expected_title_5}")
return False
print("\n3. Testing HTML escaping in titles...")
# Test case 6: Names with special characters
mock_snapshot_6 = {
"id": 555555,
"type": "Snapshot",
"child": {
"forename": "José",
"surname": "García"
},
"author": {
"forename": "María",
"surname": "López