#!/usr/bin/env python3
"""
Test the CumulativeMarkdownManager fix.
"""

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))

from src.cumulative_markdown_manager import CumulativeMarkdownManager
from src.base_scraper import ScraperConfig

def test_cumulative_manager():
    """Test that the update_cumulative_file method works."""
    print("Testing CumulativeMarkdownManager fix...")
    
    # Create test config
    config = ScraperConfig(
        source_name='TestSource',
        brand_name='hkia',
        data_dir=Path('test_data'),
        logs_dir=Path('test_logs'),
        timezone='America/Halifax'
    )
    
    # Create manager
    manager = CumulativeMarkdownManager(config)
    
    # Test data
    test_items = [
        {
            'id': 'test123',
            'title': 'Test Post',
            'type': 'test',
            'link': 'https://example.com/test123',
            'author': 'test_user',
            'publish_date': '2025-08-19',
            'views': 1000,
            'likes': 50,
            'comments': 10,
            'local_images': ['test_data/media/test_image.jpg'],
            'description': 'This is a test post'
        }
    ]
    
    try:
        # This should work now
        output_file = manager.update_cumulative_file(test_items, 'TestSource')
        print(f"✅ Success! Created file: {output_file}")
        
        # Check that the file exists and has content
        if output_file.exists():
            content = output_file.read_text()
            print(f"✅ File has {len(content)} characters")
            print(f"✅ Contains ID section: {'# ID: test123' in content}")
            return True
        else:
            print("❌ File was not created")
            return False
            
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

if __name__ == "__main__":
    success = test_cumulative_manager()
    sys.exit(0 if success else 1)