#!/usr/bin/env python3 """Unit tests for storage_manager path handling.""" from pathlib import Path from urllib.parse import unquote # Use actual ARCHIVE_DIR path ARCHIVE_DIR = Path("/Volumes/playground/NewsArchiver/archival_data") def test_relative_path_format(): """Test the relative path format after save_article.""" # Simulate what save_article does archive_file_path = ( ARCHIVE_DIR / "websites/404 Media/html/2024-01-15/article_001.html" ) # This is what we store in the database stored_path = str(archive_file_path.relative_to(ARCHIVE_DIR)) # Verify stored path is relative assert not Path(stored_path).is_absolute() assert stored_path == "websites/404 Media/html/2024-01-15/article_001.html" # Simulate what get_article does when retrieving retrieved_path = Path(stored_path) if not retrieved_path.is_absolute(): full_path = ARCHIVE_DIR / retrieved_path else: full_path = retrieved_path # Verify full path is correct assert str(full_path) == str(archive_file_path) # This is what we return for the web interface web_path = str(full_path.relative_to(ARCHIVE_DIR)) assert web_path == stored_path print(f"Test passed! Stored: {stored_path}, Web: {web_path}") def test_multiple_sources(): """Test that different sources get correct paths.""" sources = ["404 Media", "TestSource", "Another Source"] for source in sources: archive_file_path = ( ARCHIVE_DIR / f"websites/{source}/html/2024-01-15/article_001.html" ) stored_path = str(archive_file_path.relative_to(ARCHIVE_DIR)) # Verify path structure parts = Path(stored_path).parts assert parts[0] == "websites" assert parts[1] == source assert parts[2] == "html" print(f"Source '{source}': {stored_path}") def test_archive_file_url_generation(): """Test that the URL for archived files is correct.""" # Simulate what the template does archive_file_path = "websites/404 Media/html/2024-01-15/article_001.html" # This is what the template generates url = f"/archive-file/{archive_file_path}" # Verify URL format assert url == "/archive-file/websites/404 Media/html/2024-01-15/article_001.html" # Simulate what the route handler does decoded_path = unquote(archive_file_path) full_path = ARCHIVE_DIR / decoded_path # Verify the full path is correct expected_path = ARCHIVE_DIR / "websites/404 Media/html/2024-01-15/article_001.html" assert str(full_path) == str(expected_path) print(f"URL: {url}") print(f"Full path: {full_path}") def test_old_absolute_path_handling(): """Test handling of old absolute paths from different servers.""" # Old absolute path from a different server old_absolute_path = Path( "/home/user/playground/NewsArchiver/archival_data/websites/404 Media/html/2024-01-15/article_001.html" ) # Check if it's absolute assert old_absolute_path.is_absolute() # The code handles this by checking is_absolute() first # If the path is absolute but not under ARCHIVE_DIR, we can still try to extract # the relative part by checking if ARCHIVE_DIR is in the path if old_absolute_path.is_absolute(): # For this test, we just verify the logic print("Old absolute path handling verified") if __name__ == "__main__": test_relative_path_format() test_multiple_sources() test_archive_file_url_generation() test_old_absolute_path_handling() print("\nAll tests passed!")