|
| 1 | +"""Tests for image utility functions.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from openhands.tools.utils.image import ( |
| 6 | + BASE64_IMAGE_PREFIXES, |
| 7 | + detect_image_mime_type, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def test_base64_image_prefixes_mapping(): |
| 12 | + """Test that BASE64_IMAGE_PREFIXES contains expected mappings.""" |
| 13 | + assert BASE64_IMAGE_PREFIXES["/9j/"] == "image/jpeg" |
| 14 | + assert BASE64_IMAGE_PREFIXES["iVBORw0KGgo"] == "image/png" |
| 15 | + assert BASE64_IMAGE_PREFIXES["R0lGODlh"] == "image/gif" |
| 16 | + assert BASE64_IMAGE_PREFIXES["UklGR"] == "image/webp" |
| 17 | + |
| 18 | + |
| 19 | +def test_detect_image_mime_type_jpeg(): |
| 20 | + """Test JPEG detection.""" |
| 21 | + jpeg_base64 = "/9j/4AAQSkZJRg..." |
| 22 | + assert detect_image_mime_type(jpeg_base64) == "image/jpeg" |
| 23 | + |
| 24 | + |
| 25 | +def test_detect_image_mime_type_png(): |
| 26 | + """Test PNG detection.""" |
| 27 | + png_base64 = "iVBORw0KGgoAAAANSUhEUg..." |
| 28 | + assert detect_image_mime_type(png_base64) == "image/png" |
| 29 | + |
| 30 | + |
| 31 | +def test_detect_image_mime_type_gif(): |
| 32 | + """Test GIF detection.""" |
| 33 | + gif_base64 = "R0lGODlhAQABAIAAAP..." |
| 34 | + assert detect_image_mime_type(gif_base64) == "image/gif" |
| 35 | + |
| 36 | + |
| 37 | +def test_detect_image_mime_type_webp(): |
| 38 | + """Test WebP detection.""" |
| 39 | + webp_base64 = "UklGRiQAAABXRUJQ..." |
| 40 | + assert detect_image_mime_type(webp_base64) == "image/webp" |
| 41 | + |
| 42 | + |
| 43 | +def test_detect_image_mime_type_unknown(): |
| 44 | + """Test unknown format defaults to PNG.""" |
| 45 | + unknown_base64 = "UNKNOWN_FORMAT_DATA" |
| 46 | + assert detect_image_mime_type(unknown_base64) == "image/png" |
| 47 | + |
| 48 | + |
| 49 | +def test_detect_image_mime_type_empty_string(): |
| 50 | + """Test empty string defaults to PNG.""" |
| 51 | + assert detect_image_mime_type("") == "image/png" |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "base64_data,expected_mime", |
| 56 | + [ |
| 57 | + ( |
| 58 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", # noqa: E501 |
| 59 | + "image/png", |
| 60 | + ), |
| 61 | + ( |
| 62 | + "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAv/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/", # noqa: E501 |
| 63 | + "image/jpeg", |
| 64 | + ), |
| 65 | + ( |
| 66 | + "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7", |
| 67 | + "image/gif", |
| 68 | + ), |
| 69 | + ( |
| 70 | + "UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAQAcJaQAA3AA/v3AgAA=", |
| 71 | + "image/webp", |
| 72 | + ), |
| 73 | + ("AAAABBBBCCCC", "image/png"), # Unknown format |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_detect_image_mime_type_parametrized(base64_data, expected_mime): |
| 77 | + """Test MIME type detection with various real base64 samples.""" |
| 78 | + assert detect_image_mime_type(base64_data) == expected_mime |
0 commit comments