|
| 1 | +# SPDX-FileCopyrightText: 2025 Sequent Tech Inc <legal@sequentech.io> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +"""Tests for media download and processing utilities.""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +from pathlib import Path |
| 9 | +from unittest.mock import Mock, patch, MagicMock |
| 10 | +from release_tool.media_utils import MediaDownloader |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def media_downloader(tmp_path): |
| 15 | + """Create a MediaDownloader instance for testing.""" |
| 16 | + assets_path = str(tmp_path / "assets" / "v{{ major }}.{{ minor }}") |
| 17 | + return MediaDownloader(assets_path, download_enabled=True) |
| 18 | + |
| 19 | + |
| 20 | +def test_process_markdown_images(media_downloader, tmp_path): |
| 21 | + """Test processing of Markdown image syntax.""" |
| 22 | + output_path = tmp_path / "release-notes.md" |
| 23 | + |
| 24 | + description = """ |
| 25 | +Some text here. |
| 26 | + |
| 27 | +More text. |
| 28 | +""" |
| 29 | + |
| 30 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 31 | + mock_download.return_value = "assets/v1.0/abc123_test-image.png" |
| 32 | + |
| 33 | + result = media_downloader.process_description( |
| 34 | + description, "1.0.0", str(output_path) |
| 35 | + ) |
| 36 | + |
| 37 | + # Check that download was called with correct URL |
| 38 | + mock_download.assert_called_once() |
| 39 | + assert "https://github.com/user-attachments/assets/test-image.png" in mock_download.call_args[0] |
| 40 | + |
| 41 | + # Check that Markdown image was replaced with local path |
| 42 | + assert "" in result |
| 43 | + |
| 44 | + |
| 45 | +def test_process_html_images_conversion(media_downloader, tmp_path): |
| 46 | + """Test processing of HTML img tags with conversion to Markdown.""" |
| 47 | + output_path = tmp_path / "release-notes.md" |
| 48 | + |
| 49 | + description = """ |
| 50 | +Some text here. |
| 51 | +<img width="1014" height="835" alt="Screenshot" src="https://github.com/user-attachments/assets/8184a4b2-25f5-42d9-85c3-296e81ddd4d3" /> |
| 52 | +More text. |
| 53 | +""" |
| 54 | + |
| 55 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 56 | + mock_download.return_value = "assets/v1.0/abc123_screenshot.png" |
| 57 | + |
| 58 | + result = media_downloader.process_description( |
| 59 | + description, "1.0.0", str(output_path), convert_html_to_markdown=True |
| 60 | + ) |
| 61 | + |
| 62 | + # Check that download was called |
| 63 | + mock_download.assert_called_once() |
| 64 | + assert "https://github.com/user-attachments/assets/8184a4b2-25f5-42d9-85c3-296e81ddd4d3" in mock_download.call_args[0] |
| 65 | + |
| 66 | + # Check that HTML img was converted to Markdown with local path |
| 67 | + assert "" in result |
| 68 | + # Original HTML should be gone |
| 69 | + assert "<img" not in result |
| 70 | + |
| 71 | + |
| 72 | +def test_process_html_images_without_conversion(media_downloader, tmp_path): |
| 73 | + """Test that HTML img tags are NOT converted when convert_html_to_markdown=False.""" |
| 74 | + output_path = tmp_path / "release-notes.md" |
| 75 | + |
| 76 | + description = """ |
| 77 | +Some text here. |
| 78 | +<img width="1014" height="835" alt="Screenshot" src="https://github.com/user-attachments/assets/8184a4b2-25f5-42d9-85c3-296e81ddd4d3" /> |
| 79 | +More text. |
| 80 | +""" |
| 81 | + |
| 82 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 83 | + mock_download.return_value = "assets/v1.0/abc123_screenshot.png" |
| 84 | + |
| 85 | + result = media_downloader.process_description( |
| 86 | + description, "1.0.0", str(output_path), convert_html_to_markdown=False |
| 87 | + ) |
| 88 | + |
| 89 | + # Check that download was NOT called (HTML imgs ignored without conversion) |
| 90 | + mock_download.assert_not_called() |
| 91 | + |
| 92 | + # Original HTML should still be there |
| 93 | + assert "<img" in result |
| 94 | + assert "https://github.com/user-attachments/assets/8184a4b2-25f5-42d9-85c3-296e81ddd4d3" in result |
| 95 | + |
| 96 | + |
| 97 | +def test_process_html_images_with_default_alt(media_downloader, tmp_path): |
| 98 | + """Test HTML img tag without alt attribute gets default alt text.""" |
| 99 | + output_path = tmp_path / "release-notes.md" |
| 100 | + |
| 101 | + description = '<img src="https://example.com/image.png" width="100" />' |
| 102 | + |
| 103 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 104 | + mock_download.return_value = "assets/v1.0/abc123_image.png" |
| 105 | + |
| 106 | + result = media_downloader.process_description( |
| 107 | + description, "1.0.0", str(output_path), convert_html_to_markdown=True |
| 108 | + ) |
| 109 | + |
| 110 | + # Check that default alt text "Image" was used |
| 111 | + assert "" in result |
| 112 | + |
| 113 | + |
| 114 | +def test_process_mixed_markdown_and_html_images(media_downloader, tmp_path): |
| 115 | + """Test processing document with both Markdown and HTML images.""" |
| 116 | + output_path = tmp_path / "release-notes.md" |
| 117 | + |
| 118 | + description = """ |
| 119 | +# Title |
| 120 | + |
| 121 | +Here's a Markdown image: |
| 122 | + |
| 123 | + |
| 124 | +And an HTML image: |
| 125 | +<img alt="HTML Image" src="https://example.com/html.png" width="500" /> |
| 126 | + |
| 127 | +End of document. |
| 128 | +""" |
| 129 | + |
| 130 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 131 | + mock_download.side_effect = [ |
| 132 | + "assets/v1.0/md_markdown.png", # First call for HTML img |
| 133 | + "assets/v1.0/md2_markdown.png" # Second call for Markdown img |
| 134 | + ] |
| 135 | + |
| 136 | + result = media_downloader.process_description( |
| 137 | + description, "1.0.0", str(output_path), convert_html_to_markdown=True |
| 138 | + ) |
| 139 | + |
| 140 | + # Both images should be processed |
| 141 | + assert mock_download.call_count == 2 |
| 142 | + |
| 143 | + # Both should be in Markdown format with local paths |
| 144 | + assert "" in result |
| 145 | + assert "" in result |
| 146 | + assert "<img" not in result |
| 147 | + |
| 148 | + |
| 149 | +def test_skip_local_paths(media_downloader, tmp_path): |
| 150 | + """Test that local paths are not downloaded.""" |
| 151 | + output_path = tmp_path / "release-notes.md" |
| 152 | + |
| 153 | + description = """ |
| 154 | + |
| 155 | +<img src="assets/another.png" alt="Another Local" /> |
| 156 | +""" |
| 157 | + |
| 158 | + with patch.object(media_downloader, '_download_media') as mock_download: |
| 159 | + result = media_downloader.process_description( |
| 160 | + description, "1.0.0", str(output_path), convert_html_to_markdown=True |
| 161 | + ) |
| 162 | + |
| 163 | + # No downloads should happen for local paths |
| 164 | + mock_download.assert_not_called() |
| 165 | + |
| 166 | + # Local paths should remain unchanged |
| 167 | + assert "./local/path/image.png" in result |
| 168 | + assert "assets/another.png" in result |
| 169 | + |
| 170 | + |
| 171 | +def test_disabled_media_downloader(tmp_path): |
| 172 | + """Test that disabled MediaDownloader doesn't process anything.""" |
| 173 | + assets_path = str(tmp_path / "assets") |
| 174 | + downloader = MediaDownloader(assets_path, download_enabled=False) |
| 175 | + output_path = tmp_path / "release-notes.md" |
| 176 | + |
| 177 | + description = """ |
| 178 | + |
| 179 | +<img src="https://example.com/html.png" alt="HTML" /> |
| 180 | +""" |
| 181 | + |
| 182 | + result = downloader.process_description( |
| 183 | + description, "1.0.0", str(output_path), convert_html_to_markdown=True |
| 184 | + ) |
| 185 | + |
| 186 | + # Nothing should be changed |
| 187 | + assert result == description |
0 commit comments